From 687679c6e403baabdc73ca2c6a84a238bcbb7285 Mon Sep 17 00:00:00 2001 From: Jeremy Carbaugh Date: Thu, 19 Aug 2010 15:18:39 -0400 Subject: [PATCH] stats --- saucebrush/stats.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 saucebrush/stats.py diff --git a/saucebrush/stats.py b/saucebrush/stats.py new file mode 100644 index 0000000..6f115d1 --- /dev/null +++ b/saucebrush/stats.py @@ -0,0 +1,46 @@ +from saucebrush.filters import Filter + +class StatsFilter(Filter): + + def __init__(self, field, test=None): + self._field = field + self._test = test if test else lambda x: True + + def process_record(self, record): + if self._test(record): + self.process_field(record[self._field]) + return record + + def process_field(self, record): + raise NotImplementedError('process_field not defined in ' + + self.__class__.__name__) + + def value(self): + raise NotImplementedError('value not defined in ' + + self.__class__.__name__) + +class Sum(StatsFilter): + + def __init__(self, field, initial=0, **kwargs): + super(Sum, self).__init__(field, **kwargs) + self._value = initial + + def process_field(self, item): + self._value += item or 0 + + def value(self): + return self._value + +class Average(StatsFilter): + + def __init__(self, field, initial=0, **kwargs): + super(Average, self).__init__(field, **kwargs) + self._value = initial + self._count = 0 + + def process_field(self, item): + self._value += item or 0 + self._count += 1 + + def value(self): + return self._value / self._count \ No newline at end of file