Skip to content

Commit 9df8227

Browse files
committed
PEP-8 and Python3 compatable python2 code
* My Sublime Text 2 plugin auto-adjusted some small portions to be PEP-8 compliant. * Imported 'print_function' and refactored all print statements
1 parent 0386d56 commit 9df8227

File tree

1 file changed

+53
-44
lines changed

1 file changed

+53
-44
lines changed

bashplotlib/histogram.py

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Plotting terminal based histograms
66
"""
77

8+
from __future__ import print_function
89
import os
910
import sys
1011
import math
@@ -55,32 +56,32 @@ def run_demo():
5556
sys.stderr.write("run the downloaddata.sh script in the example first\n")
5657
sys.exit(1)
5758

58-
#plotting a histogram
59-
print "plotting a basic histogram"
60-
print "plot_hist('%s')" % demo_file
61-
print "hist -f %s" % demo_file
62-
print "cat %s | hist" % demo_file
59+
# plotting a histogram
60+
print("plotting a basic histogram")
61+
print("plot_hist('%s')" % demo_file)
62+
print("hist -f %s" % demo_file)
63+
print("cat %s | hist" % demo_file)
6364
plot_hist(demo_file)
64-
print "*" * 80
65+
print("*" * 80)
6566

66-
#with colours
67-
print "histogram with colours"
68-
print "plot_hist('%s', colour='blue')" % demo_file
69-
print "hist -f %s -c blue" % demo_file
67+
# with colours
68+
print("histogram with colours")
69+
print("plot_hist('%s', colour='blue')" % demo_file)
70+
print("hist -f %s -c blue" % demo_file)
7071
plot_hist(demo_file, colour='blue')
71-
print "*" * 80
72+
print("*" * 80)
7273

73-
#changing the shape of the point
74-
print "changing the shape of the bars"
75-
print "plot_hist('%s', pch='.')" % demo_file
76-
print "hist -f %s -p ." % demo_file
74+
# changing the shape of the point
75+
print("changing the shape of the bars")
76+
print("plot_hist('%s', pch='.')" % demo_file)
77+
print("hist -f %s -p ." % demo_file)
7778
plot_hist(demo_file, pch='.')
78-
print "*" * 80
79+
print("*" * 80)
7980

80-
#changing the size of the plot
81-
print "changing the size of the plot"
82-
print "plot_hist('%s', height=35.0, bincount=40)" % demo_file
83-
print "hist -f %s -s 35.0 -b 40" % demo_file
81+
# changing the size of the plot
82+
print("changing the size of the plot")
83+
print("plot_hist('%s', height=35.0, bincount=40)" % demo_file)
84+
print("hist -f %s -s 35.0 -b 40" % demo_file)
8485
plot_hist(demo_file, height=35.0, bincount=40)
8586

8687

@@ -139,13 +140,13 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
139140
if height > 20:
140141
height = 20
141142

142-
ys = list(drange(start, stop, float(stop - start)/height))
143+
ys = list(drange(start, stop, float(stop - start) / height))
143144
ys.reverse()
144145

145146
nlen = max(len(str(min_y)), len(str(max_y))) + 1
146147

147148
if title:
148-
print box_text(title, max(len(hist)*2, len(title)), nlen)
149+
print(box_text(title, max(len(hist) * 2, len(title)), nlen))
149150
print
150151

151152
used_labs = set()
@@ -155,64 +156,71 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
155156
ylab = ""
156157
else:
157158
used_labs.add(ylab)
158-
ylab = " "*(nlen - len(ylab)) + ylab + "|"
159+
ylab = " " * (nlen - len(ylab)) + ylab + "|"
159160

160-
print ylab,
161+
print(ylab, end=' ')
161162

162163
for i in range(len(hist)):
163164
if int(y) <= hist[i]:
164165
printcolour(pch, True, colour)
165166
else:
166167
printcolour(" ", True, colour)
167-
print
168+
print()
168169
xs = hist.keys()
169170

170-
print " " * (nlen+1) + "-" * len(xs)
171+
print(" " * (nlen + 1) + "-" * len(xs))
171172

172173
if xlab:
173-
xlen = len(str(float((max_y)/height) + max_y))
174+
xlen = len(str(float((max_y) / height) + max_y))
174175
for i in range(0, xlen):
175-
printcolour(" " * (nlen+1), True, colour)
176+
printcolour(" " * (nlen + 1), True, colour)
176177
for x in range(0, len(hist)):
177178
num = str(bins[x])
178179
if x % 2 != 0:
179180
pass
180181
elif i < len(num):
181-
print num[i],
182+
print(num[i], end=' ')
182183
else:
183-
print " ",
184+
print(" ", end=' ')
184185
print
185186

186187
center = max(map(len, map(str, [n, min_val, mean, max_val])))
187188
center += 15
188189

189190
if showSummary:
190-
print
191-
print "-" * (2 + center)
192-
print "|" + "Summary".center(center) + "|"
193-
print "-" * (2 + center)
191+
print()
192+
print("-" * (2 + center))
193+
print("|" + "Summary".center(center) + "|")
194+
print("-" * (2 + center))
194195
summary = "|" + ("observations: %d" % n).center(center) + "|\n"
195196
summary += "|" + ("min value: %f" % min_val).center(center) + "|\n"
196197
summary += "|" + ("mean : %f" % mean).center(center) + "|\n"
197198
summary += "|" + ("max value: %f" % max_val).center(center) + "|\n"
198199
summary += "-" * (2 + center)
199-
print summary
200+
print(summary)
200201

201202

202203
def main():
203204

204205
parser = optparse.OptionParser(usage=hist['usage'])
205206

206-
parser.add_option('-f', '--file', help='a file containing a column of numbers', default=None, dest='f')
207+
parser.add_option(
208+
'-f', '--file', help='a file containing a column of numbers', default=None, dest='f')
207209
parser.add_option('-t', '--title', help='title for the chart', default="", dest='t')
208-
parser.add_option('-b', '--bins', help='number of bins in the histogram', type='int', default=None, dest='b')
209-
parser.add_option('-w', '--binwidth', help='width of bins in the histogram', type='float', default=None, dest='binwidth')
210-
parser.add_option('-s', '--height', help='height of the histogram (in lines)', type='int', default=None, dest='h')
210+
parser.add_option(
211+
'-b', '--bins', help='number of bins in the histogram', type='int', default=None, dest='b')
212+
parser.add_option('-w', '--binwidth', help='width of bins in the histogram',
213+
type='float', default=None, dest='binwidth')
214+
parser.add_option('-s', '--height', help='height of the histogram (in lines)',
215+
type='int', default=None, dest='h')
211216
parser.add_option('-p', '--pch', help='shape of each bar', default='o', dest='p')
212-
parser.add_option('-x', '--xlab', help='label bins on x-axis', default=None, action="store_true", dest='x')
213-
parser.add_option('-c', '--colour', help='colour of the plot (%s)' % colour_help, default='default', dest='colour')
217+
parser.add_option('-x', '--xlab', help='label bins on x-axis',
218+
default=None, action="store_true", dest='x')
219+
parser.add_option('-c', '--colour', help='colour of the plot (%s)' %
220+
colour_help, default='default', dest='colour')
214221
parser.add_option('-d', '--demo', help='run demos', action='store_true', dest='demo')
215-
parser.add_option('-n', '--nosummary', help='hide summary', action='store_false', dest='showSummary', default=True)
222+
parser.add_option('-n', '--nosummary', help='hide summary',
223+
action='store_false', dest='showSummary', default=True)
216224
parser.add_option('-r', '--regular',
217225
help='use regular y-scale (0 - maximum y value), instead of truncated y-scale (minimum y-value - maximum y-value)',
218226
default=False, action="store_true", dest='regular')
@@ -228,9 +236,10 @@ def main():
228236
if opts.demo:
229237
run_demo()
230238
elif opts.f:
231-
plot_hist(opts.f, opts.h, opts.b, opts.binwidth, opts.p, opts.colour, opts.t, opts.x, opts.showSummary, opts.regular)
239+
plot_hist(opts.f, opts.h, opts.b, opts.binwidth, opts.p, opts.colour,
240+
opts.t, opts.x, opts.showSummary, opts.regular)
232241
else:
233-
print "nothing to plot!"
242+
print("nothing to plot!")
234243

235244

236245
if __name__ == "__main__":

0 commit comments

Comments
 (0)