Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bar_chart]: fix py3 compatibility #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 50 additions & 29 deletions data_hacks/bar_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from optparse import OptionParser
from decimal import Decimal


def load_stream(input_stream):
for line in input_stream:
clean_line = line.strip()
Expand All @@ -37,9 +38,11 @@ def load_stream(input_stream):
if clean_line:
yield clean_line


def run(input_stream, options):
data = defaultdict(int)
total = 0

for row in input_stream:
if options.agg_key_value:
kv = row.rstrip().rsplit(None, 1)
Expand All @@ -54,63 +57,81 @@ def run(input_stream, options):
else:
data[row] += 1
total += 1

if not data:
print "Error: no data"
print("Error: no data")
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_value = max(data.values())
scale = int(math.ceil(float(max_value) / value_characters))
scale = max(1, scale)

print "# each " + options.dot + " represents a count of %d. total %d" % (scale, total)


print("# each " +
options.dot +
" represents a count of %d. total %d" %
(scale, total))

if options.sort_values:
data = [[value, key] for key, value in data.items()]
data = [[val, key] for key, val in data.items()]
data.sort(key=lambda x: x[0], reverse=options.reverse_sort)
else:
# sort by keys
data = [[value, key] for key, value in data.items()]
data = [[val, key] for key, val in data.items()]
if options.numeric_sort:
# keys could be numeric too
data.sort(key=lambda x: (Decimal(x[1])), reverse=options.reverse_sort)
data.sort(key=lambda x: (Decimal(x[1])),
reverse=options.reverse_sort)
else:
data.sort(key=lambda x: x[1], reverse=options.reverse_sort)

str_format = "%" + str(max_length) + "s [%6d] %s%s"
percentage = ""

for value, key in data:
if options.percentage:
percentage = " (%0.2f%%)" % (100 * Decimal(value) / Decimal(total))
print str_format % (key[:max_length], value, (value / scale) * options.dot, percentage)
print(str_format % (key[:max_length], value, int(value / scale) * options.dot, percentage)) # noqa

if __name__ == "__main__":
parser = OptionParser()
parser.usage = "cat data | %prog [options]"
parser.add_option("-a", "--agg", dest="agg_value_key", default=False, action="store_true",
help="Two column input format, space seperated with value<space>key")
parser.add_option("-A", "--agg-key-value", dest="agg_key_value", default=False, action="store_true",
help="Two column input format, space seperated with key<space>value")
parser.add_option("-k", "--sort-keys", dest="sort_keys", default=True, action="store_true",
help="sort by the key [default]")
parser.add_option("-v", "--sort-values", dest="sort_values", default=False, action="store_true",
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("-n", "--numeric-sort", dest="numeric_sort", default=False, action="store_true",
help="sort keys by numeric sequencing")
parser.add_option("-p", "--percentage", dest="percentage", default=False, action="store_true",
help="List percentage for each bar")
parser.add_option("--dot", dest="dot", default='∎', help="Dot representation")
parser.add_option(
"-a", "--agg",
dest="agg_value_key",
default=False, action="store_true",
help="Two column input format, space seperated with value<space>key")
parser.add_option(
"-A",
"--agg-key-value",
dest="agg_key_value",
default=False,
action="store_true",
help="Two column input format, space seperated with key<space>value")
parser.add_option("-k", "--sort-keys", dest="sort_keys",
default=True, action="store_true",
help="sort by the key [default]")
parser.add_option("-v", "--sort-values", dest="sort_values",
default=False, action="store_true",
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("-n", "--numeric-sort", dest="numeric_sort",
default=False, action="store_true",
help="sort keys by numeric sequencing")
parser.add_option("-p", "--percentage", dest="percentage",
default=False, action="store_true",
help="List percentage for each bar")
parser.add_option("--dot", dest="dot", default='∎',
help="Dot representation")

(options, args) = parser.parse_args()

if sys.stdin.isatty():
parser.print_usage()
print "for more help use --help"
print("for more help use --help")
sys.exit(1)
run(load_stream(sys.stdin), options)