Skip to content

Instantly share code, notes, and snippets.

@schmichael
Created October 20, 2011 00:41
Show Gist options
  • Save schmichael/1300114 to your computer and use it in GitHub Desktop.
Save schmichael/1300114 to your computer and use it in GitHub Desktop.
Handy script for calculating averages streamed via stdin
#!/usr/bin/env python
import sys
CHUNK = 100
if len(sys.argv) > 1:
CHUNK = int(sys.argv[1])
vals = []
i = 1
while 1:
l = sys.stdin.readline()
if not l:
break
vals.append(float(l.strip()))
if i % CHUNK == 0:
s = sum(vals)
print "Count: %10d - Total: %10.4f - Average: %10.4f" % (
len(vals), s, s / len(vals))
i += 1
s = sum(vals)
print "Count: %10d - Total: %10.4f - Average: %10.4f" % (i+1, s, s / i+1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment