From 7100d10f382cb5176b0b994f4dd1c5337706a933 Mon Sep 17 00:00:00 2001 From: Sasha Laundy Date: Fri, 15 Jul 2016 12:09:25 -0400 Subject: [PATCH 1/2] Use Python3 print syntax --- data_hacks/histogram.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_hacks/histogram.py b/data_hacks/histogram.py index 3d16cc8..273decd 100755 --- a/data_hacks/histogram.py +++ b/data_hacks/histogram.py @@ -97,7 +97,7 @@ def load_stream(input_stream, agg_value_key, agg_key_value): yield DataPoint(Decimal(clean_line), 1) except: logging.exception('failed %r', line) - print >>sys.stderr, "invalid line %r" % line + print(sys.stderr, "invalid line %r" % line) def median(values, key=None): @@ -241,7 +241,7 @@ def log_steps(k, n): print("# Mean = %f; Variance = %f; SD = %f; Median %f" % (mvsd.mean(), mvsd.var(), mvsd.sd(), median(accepted_data, key=lambda x: x.value))) - print "# each " + options.dot + " represents a count of %d" % bucket_scale + print("# each " + options.dot + " represents a count of %d" % bucket_scale) bucket_min = min_v bucket_max = min_v percentage = "" @@ -256,8 +256,8 @@ def log_steps(k, n): if options.percentage: percentage = " (%0.2f%%)" % (100 * Decimal(bucket_count) / Decimal(samples)) - print format_string % (bucket_min, bucket_max, bucket_count, options.dot * - star_count, percentage) + print(format_string % (bucket_min, bucket_max, bucket_count, options.dot * + star_count, percentage)) if __name__ == "__main__": @@ -294,7 +294,7 @@ def log_steps(k, n): if sys.stdin.isatty(): # if isatty() that means it's run without anything piped into it parser.print_usage() - print "for more help use --help" + print("for more help use --help") sys.exit(1) histogram(load_stream(sys.stdin, options.agg_value_key, options.agg_key_value), options) From d0e8fc18280a6b7855bea980471c226362ed95d6 Mon Sep 17 00:00:00 2001 From: Sasha Laundy Date: Fri, 15 Jul 2016 12:10:05 -0400 Subject: [PATCH 2/2] Fix two bugs 1) Need integers later, not floats, so force integer division. 2) In the condition, you're implicitly using 'is', which checks to make sure that True and length%2 are the exact same object, which they're not. They are, however, equivalent, so let's use '==' --- data_hacks/histogram.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_hacks/histogram.py b/data_hacks/histogram.py index 273decd..efdb73a 100755 --- a/data_hacks/histogram.py +++ b/data_hacks/histogram.py @@ -104,10 +104,10 @@ def median(values, key=None): if not key: key = None # map and sort accept None as identity length = len(values) - if length % 2: - median_indeces = [length/2] + if length % 2 == 0: + median_indeces = [length//2] else: - median_indeces = [length/2-1, length/2] + median_indeces = [length//2-1, length//2] values = sorted(values, key=key) return sum(map(key, @@ -252,7 +252,7 @@ def log_steps(k, n): bucket_count = bucket_counts[bucket] star_count = 0 if bucket_count: - star_count = bucket_count / bucket_scale + star_count = bucket_count // bucket_scale if options.percentage: percentage = " (%0.2f%%)" % (100 * Decimal(bucket_count) / Decimal(samples))