-
Notifications
You must be signed in to change notification settings - Fork 13
/
hist
executable file
·66 lines (55 loc) · 1.24 KB
/
hist
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python2
import sys
import os
import locale
locale.setlocale(locale.LC_ALL, '')
# Read params
if len(sys.argv) > 1:
step = locale.atof(sys.argv[1])
else:
step = 1
complete = False
# Read values
freq = dict()
if 'COLUMNS' in os.environ:
width = int(os.environ['COLUMNS'])
else:
width = 80
total = 0.0
for line in sys.stdin:
if len(line.strip()) > 0:
try:
items = line.strip().split()
val = int(locale.atof(items[0]) / step)
if len(items) > 1:
count = locale.atof(items[1])
else:
count = 1
if count == 0:
continue
if val in freq:
freq[val] += count
else:
freq[val] = count
total += count
except:
continue
# Print histogram
if freq.values() != []:
max = max(freq.values())
else:
sys.exit(1)
if complete:
for c in range(min(freq.keys()), max(freq.keys()), step):
if c in freq:
f = freq[c]
else:
f = 0
N = int( f * width / max )
print "%10.4g | %10.4g | %10.4g | %s\n" % (c * step, f/total, f, ''.join('*' for X in range(N))),
else:
for c in sorted(freq.keys()):
f = freq[c]
N = int( f * width / max )
print "%10.4g | %10.4g | %10.4g | %s\n" % (c * step, f/total, f, ''.join('*' for X in range(N))),
print "TOTAL | %10d | %10d |\n" % (1, total)