Created
October 20, 2011 00:41
-
-
Save schmichael/1300114 to your computer and use it in GitHub Desktop.
Handy script for calculating averages streamed via stdin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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