Permalink
Browse files

Add aggregators that ignore 0 values

  • Loading branch information...
1 parent 59212cd commit 8f5698fe5ee592d1590ed00a77eff6a86f134a7b @schmichael committed Nov 2, 2012
Showing with 7 additions and 0 deletions.
  1. +7 −0 mmstats/mmash.py
View
@@ -62,12 +62,19 @@ def graph():
numeric_stats=sorted(numeric_stats, key=lambda x: x['label']))
+def _nonzero_avg(values):
+ """Return the average of ``values`` ignoring 0 values"""
+ nonzero_values = [v for v in values if v]
+ return float(sum(nonzero_values)) / len(nonzero_values)
+
aggregators = {
'avg': lambda v: float(sum(v)) / len(v),
'one': operator.itemgetter(0),
'max': max,
'min': min,
'sum': sum,
+ 'nonzero-min': lambda vals: min([v for v in vals if v]),
+ 'nonzero-avg': _nonzero_avg,
}

0 comments on commit 8f5698f

Please sign in to comment.