From 5b8752b34b14c3f75490f5a07860919594890b40 Mon Sep 17 00:00:00 2001 From: dv Date: Tue, 10 Feb 2015 15:30:55 +0800 Subject: [PATCH 1/2] translate to Python 3 by 2to3 --- bashplotlib/histogram.py | 64 +++++++++++++++++++------------------- bashplotlib/scatterplot.py | 12 +++---- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/bashplotlib/histogram.py b/bashplotlib/histogram.py index bfff5b1..793819b 100644 --- a/bashplotlib/histogram.py +++ b/bashplotlib/histogram.py @@ -56,31 +56,31 @@ def run_demo(): sys.exit(1) #plotting a histogram - print "plotting a basic histogram" - print "plot_hist('%s')" % demo_file - print "hist -f %s" % demo_file - print "cat %s | hist" % demo_file + print("plotting a basic histogram") + print("plot_hist('%s')" % demo_file) + print("hist -f %s" % demo_file) + print("cat %s | hist" % demo_file) plot_hist(demo_file) - print "*" * 80 + print("*" * 80) #with colours - print "histogram with colours" - print "plot_hist('%s', colour='blue')" % demo_file - print "hist -f %s -c blue" % demo_file + print("histogram with colours") + print("plot_hist('%s', colour='blue')" % demo_file) + print("hist -f %s -c blue" % demo_file) plot_hist(demo_file, colour='blue') - print "*" * 80 + print("*" * 80) #changing the shape of the point - print "changing the shape of the bars" - print "plot_hist('%s', pch='.')" % demo_file - print "hist -f %s -p ." % demo_file + print("changing the shape of the bars") + print("plot_hist('%s', pch='.')" % demo_file) + print("hist -f %s -p ." % demo_file) plot_hist(demo_file, pch='.') - print "*" * 80 + print("*" * 80) #changing the size of the plot - print "changing the size of the plot" - print "plot_hist('%s', height=35.0, bincount=40)" % demo_file - print "hist -f %s -s 35.0 -b 40" % demo_file + print("changing the size of the plot") + print("plot_hist('%s', height=35.0, bincount=40)" % demo_file) + print("hist -f %s -s 35.0 -b 40" % demo_file) plot_hist(demo_file, height=35.0, bincount=40) @@ -145,8 +145,8 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def nlen = max(len(str(min_y)), len(str(max_y))) + 1 if title: - print box_text(title, max(len(hist)*2, len(title)), nlen) - print + print(box_text(title, max(len(hist)*2, len(title)), nlen)) + print() used_labs = set() for y in ys: @@ -157,17 +157,17 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def used_labs.add(ylab) ylab = " "*(nlen - len(ylab)) + ylab + "|" - print ylab, + print(ylab, end=' ') for i in range(len(hist)): if int(y) <= hist[i]: printcolour(pch, True, colour) else: printcolour(" ", True, colour) - print - xs = hist.keys() + print() + xs = list(hist.keys()) - print " " * (nlen+1) + "-" * len(xs) + print(" " * (nlen+1) + "-" * len(xs)) if xlab: xlen = len(str(float((max_y)/height) + max_y)) @@ -178,25 +178,25 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def if x % 2 != 0: pass elif i < len(num): - print num[i], + print(num[i], end=' ') else: - print " ", - print + print(" ", end=' ') + print() - center = max(map(len, map(str, [n, min_val, mean, max_val]))) + center = max(list(map(len, list(map(str, [n, min_val, mean, max_val]))))) center += 15 if showSummary: - print - print "-" * (2 + center) - print "|" + "Summary".center(center) + "|" - print "-" * (2 + center) + print() + print("-" * (2 + center)) + print("|" + "Summary".center(center) + "|") + print("-" * (2 + center)) summary = "|" + ("observations: %d" % n).center(center) + "|\n" summary += "|" + ("min value: %f" % min_val).center(center) + "|\n" summary += "|" + ("mean : %f" % mean).center(center) + "|\n" summary += "|" + ("max value: %f" % max_val).center(center) + "|\n" summary += "-" * (2 + center) - print summary + print(summary) def main(): @@ -230,7 +230,7 @@ def main(): elif opts.f: plot_hist(opts.f, opts.h, opts.b, opts.binwidth, opts.p, opts.colour, opts.t, opts.x, opts.showSummary, opts.regular) else: - print "nothing to plot!" + print("nothing to plot!") if __name__ == "__main__": diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 23a49fd..0a57266 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -54,11 +54,11 @@ def plot_scatter(f, xs, ys, size, pch, colour, title): plotted = set() if title: - print box_text(title, 2*len(get_scale(xs, False, size))+1) + print(box_text(title, 2*len(get_scale(xs, False, size))+1)) - print "-" * (2*len(get_scale(xs, False, size))+2) + print("-" * (2*len(get_scale(xs, False, size))+2)) for y in get_scale(ys, True, size): - print "|", + print("|", end=' ') for x in get_scale(xs, False, size): point = " " for (i, (xp, yp)) in enumerate(zip(xs, ys)): @@ -73,8 +73,8 @@ def plot_scatter(f, xs, ys, size, pch, colour, title): elif y==0: point = "-" printcolour(point, True, colour) - print "|" - print "-"*(2*len(get_scale(xs, False, size))+2) + print("|") + print("-"*(2*len(get_scale(xs, False, size))+2)) def main(): @@ -97,7 +97,7 @@ def main(): if opts.f or (opts.x and opts.y): plot_scatter(opts.f, opts.x, opts.y, opts.size, opts.pch, opts.colour, opts.t) else: - print "nothing to plot!" + print("nothing to plot!") if __name__=="__main__": From dffe6cbdbfd364e0a3790203684dba234acc9a9c Mon Sep 17 00:00:00 2001 From: dv Date: Tue, 10 Feb 2015 15:35:00 +0800 Subject: [PATCH 2/2] fix the file open --- bashplotlib/histogram.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/bashplotlib/histogram.py b/bashplotlib/histogram.py index 793819b..fad7c42 100644 --- a/bashplotlib/histogram.py +++ b/bashplotlib/histogram.py @@ -105,26 +105,28 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def min_val, max_val = None, None n, mean = 0.0, 0.0 - for number in read_numbers(f): - n += 1 - if min_val is None or number < min_val: - min_val = number - if max_val is None or number > max_val: - max_val = number - mean += number + with open(f) as fobj: + for number in read_numbers(fobj): + n += 1 + if min_val is None or number < min_val: + min_val = number + if max_val is None or number > max_val: + max_val = number + mean += number mean /= n bins = list(calc_bins(n, min_val, max_val, bincount, binwidth)) hist = {i: 0 for i in range(len(bins))} - for number in read_numbers(f): - for i, b in enumerate(bins): - if number <= b: - hist[i] += 1 - break - if number == max_val and max_val > bins[len(bins) - 1]: - hist[len(hist) - 1] += 1 + with open(f) as fobj: + for number in read_numbers(fobj): + for i, b in enumerate(bins): + if number <= b: + hist[i] += 1 + break + if number == max_val and max_val > bins[len(bins) - 1]: + hist[len(hist) - 1] += 1 min_y, max_y = min(hist.values()), max(hist.values())