From 0595f4c49dac6497be162270839c6271dee3068d Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Sun, 14 Nov 2010 18:46:12 -0300 Subject: [PATCH] stats.py: Allow specifying input expression and output format Now the script can take an arbitrary (optional) expression as the first argument to parse the input file, using a separator (taken as the third argument) and field specification for the expression, using $1 for the first field, $2 for the second, etc. (similar to AWK). As second argument the script takes the output format, in Python format specification, with keys: min, max, mean, and std. The defaults are '$1' for the first argument (expression to parse as input), '%(min)s,%(mean)s,%(max)s,%(std)s' as output format and ',' as input separator. This defaults match the old behaviour. --- stats.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/stats.py b/stats.py index fc08dd3..0008fe9 100755 --- a/stats.py +++ b/stats.py @@ -1,12 +1,34 @@ #!/usr/bin/env python +import re import sys import numpy.numarray.mlab as m +exp = '$1' +fmt = '%(min)s,%(mean)s,%(max)s,%(std)s' +sep = ',' + +try: + exp = sys.argv[1] + fmt = sys.argv[2] + sep = sys.argv[3] +except: + pass + vals = [] -for l in sys.stdin: +for n, l in enumerate(sys.stdin): l = l.strip() - if l: - vals.append(float(l)) -print '%s,%s,%s,%s' % (min(vals), m.mean(vals), max(vals), m.std(vals)) + if not l: + continue + try: + fields = dict([('$'+str(int(k)+1), float(v.strip())) + for k, v in enumerate(l.split(sep))]) + v = float(eval(re.sub(r'(\$\d+)', r'%(\1)f', exp) % fields)) + except: + if n == 0: + continue + raise + vals.append(v) +vars = dict(min=min(vals), mean=m.mean(vals), max=max(vals), std=m.std(vals)) +print fmt % vars -- 2.43.0