@@ -38,13 +38,17 @@ def load_stream(input_stream):
38
38
yield clean_line
39
39
40
40
def run (input_stream , options ):
41
- data = defaultdict (lambda :0 )
41
+ data = defaultdict (int )
42
+ total = 0
42
43
for row in input_stream :
43
44
if options .agg_values :
44
45
kv = row .replace ('\t ' , ' ' ).split (' ' ,2 );
45
- data [kv [0 ]]+= int (kv [1 ])
46
+ value = int (kv [1 ])
47
+ data [kv [0 ]] += value
48
+ total += value
46
49
else :
47
- data [row ]+= 1
50
+ data [row ] += 1
51
+ total += 1
48
52
49
53
if not data :
50
54
print "Error: no data"
@@ -57,7 +61,7 @@ def run(input_stream, options):
57
61
scale = int (math .ceil (float (max_value ) / value_characters ))
58
62
scale = max (1 , scale )
59
63
60
- print "# each ∎ represents a count of %d" % scale
64
+ print "# each ∎ represents a count of %d. total %d " % ( scale , total )
61
65
62
66
if options .sort_values :
63
67
data = [[value , key ] for key , value in data .items ()]
@@ -71,9 +75,12 @@ def run(input_stream, options):
71
75
else :
72
76
data .sort (key = lambda x : x [1 ], reverse = options .reverse_sort )
73
77
74
- format = "%" + str (max_length ) + "s [%6d] %s"
75
- for value ,key in data :
76
- print format % (key [:max_length ], value , (value / scale ) * "∎" )
78
+ str_format = "%" + str (max_length ) + "s [%6d] %s%s"
79
+ percentage = ""
80
+ for value , key in data :
81
+ if options .percentage :
82
+ percentage = " (%0.2f%%)" % (100 * Decimal (value ) / Decimal (total ))
83
+ print str_format % (key [:max_length ], value , (value / scale ) * "∎" , percentage )
77
84
78
85
if __name__ == "__main__" :
79
86
parser = OptionParser ()
@@ -88,6 +95,8 @@ def run(input_stream, options):
88
95
help = "reverse the sort" )
89
96
parser .add_option ("-n" , "--numeric-sort" , dest = "numeric_sort" , default = False , action = "store_true" ,
90
97
help = "sort keys by numeric sequencing" )
98
+ parser .add_option ("-p" , "--percentage" , dest = "percentage" , default = False , action = "store_true" ,
99
+ help = "List percentage for each bar" )
91
100
92
101
(options , args ) = parser .parse_args ()
93
102
0 commit comments