forked from mjpost/bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mean
executable file
·28 lines (22 loc) · 831 Bytes
/
mean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python
import os
import sys
import argparse
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-p', dest='precision', type=int, default=5, help='number of decimal points')
arg_parser.add_argument('-i', dest='index', type=int, default=0, help='column index to use')
arg_parser.add_argument('-v', action="store_true", default=False, help='be verbose')
args = arg_parser.parse_args()
total = 0.0
i = 0
for i, line in enumerate(sys.stdin):
total += float(line.rstrip().split()[args.index])
divisor = 1.0
if os.path.basename(sys.argv[0]) == 'mean':
divisor = i + 1
if args.v:
formatstr = "%%.%df / %d = %%.%df" % (args.precision, i + 1, args.precision)
print(formatstr % (total, total / (i + 1)))
else:
formatstr = "%%.%df" % args.precision
print(formatstr % (total / (i + 1)))