From 8aaabf5e0034c7a4b8f735c6a9f0a66a4b2c9aa1 Mon Sep 17 00:00:00 2001 From: Matt McClure Date: Thu, 13 Sep 2012 10:30:03 -0400 Subject: [PATCH 1/2] Added options for key width, chart width, and key justification. --- data_hacks/bar_chart.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/data_hacks/bar_chart.py b/data_hacks/bar_chart.py index f06c3ef..2d2893a 100644 --- a/data_hacks/bar_chart.py +++ b/data_hacks/bar_chart.py @@ -45,8 +45,8 @@ def run(input_stream, options): sys.exit(1) max_length = max([len(key) for key in data.keys()]) - max_length = min(max_length, 50) - value_characters = 80 - max_length + max_length = min(max_length, options.key_length) + value_characters = options.width - max_length - 10 # ' [%6d] ' max_value = max(data.values()) scale = int(math.ceil(float(max_value) / value_characters)) scale = max(1, scale) @@ -64,7 +64,8 @@ def run(input_stream, options): data = [[key,value] for key,value in data.items()] data.sort(reverse=options.reverse_sort) data = [[value, key] for key,value in data] - format = "%" + str(max_length) + "s [%6d] %s" + justification = "-" if options.justification == "left" else "" + format = "%" + justification + str(max_length) + "s [%6d] %s" for value,key in data: print format % (key[:max_length], value, (value / scale) * "*") @@ -77,6 +78,12 @@ def run(input_stream, options): help="sort by the frequence") parser.add_option("-r", "--reverse-sort", dest="reverse_sort", default=False, action="store_true", help="reverse the sort") + parser.add_option("-j", "--justification", dest="justification", default="right", action="store", + help="output justification of keys [right]") + parser.add_option("-l", "--key-length", dest="key_length", type="int", default=50, action="store", + help="output length of keys [50]") + parser.add_option("-w", "--width", dest="width", type="int", default=80, action="store", + help="output width of chart [80]") (options, args) = parser.parse_args() From 5cef97aa5ae57aef16acf871733ea2bd0143949a Mon Sep 17 00:00:00 2001 From: Matt McClure Date: Thu, 13 Sep 2012 12:38:30 -0400 Subject: [PATCH 2/2] Computes value width instead of hard coding 6. --- data_hacks/bar_chart.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/data_hacks/bar_chart.py b/data_hacks/bar_chart.py index 2d2893a..417a1fb 100644 --- a/data_hacks/bar_chart.py +++ b/data_hacks/bar_chart.py @@ -46,8 +46,9 @@ def run(input_stream, options): max_length = max([len(key) for key in data.keys()]) max_length = min(max_length, options.key_length) - value_characters = options.width - max_length - 10 # ' [%6d] ' max_value = max(data.values()) + max_value_length = len(str(max_value)) + value_characters = options.width - max_length - max_value_length scale = int(math.ceil(float(max_value) / value_characters)) scale = max(1, scale) @@ -65,7 +66,8 @@ def run(input_stream, options): data.sort(reverse=options.reverse_sort) data = [[value, key] for key,value in data] justification = "-" if options.justification == "left" else "" - format = "%" + justification + str(max_length) + "s [%6d] %s" + format = ("%" + justification + str(max_length) + "s [%" + + str(max_value_length) + "d] %s") for value,key in data: print format % (key[:max_length], value, (value / scale) * "*")