Skip to content

Commit 80d6c7a

Browse files
author
TrungNT
committed
Support custom print function take str as input
1 parent 4525778 commit 80d6c7a

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

bashplotlib/histogram.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ def run_demo():
8686
plot_hist(demo_file, height=35.0, bincount=40)
8787

8888

89-
def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="default", title="", xlab=None, showSummary=False, regular=False):
89+
def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o",
90+
colour="default", title="", xlab=None, showSummary=False,
91+
regular=False, print_func=print):
9092
''' Plot histogram.
9193
1801| oo
9294
1681| oo
@@ -121,7 +123,8 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
121123
whether or not to display a summary
122124
regular : boolean
123125
whether or not to start y-labels at 0
124-
126+
print_func : callable
127+
custom print function
125128
'''
126129
if pch is None:
127130
pch = "o"
@@ -178,8 +181,8 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
178181
nlen = max(len(str(min_y)), len(str(max_y))) + 1
179182

180183
if title:
181-
print(box_text(title, max(len(hist) * 2, len(title)), nlen))
182-
print()
184+
print_func(box_text(title, max(len(hist) * 2, len(title)), nlen))
185+
print_func()
183186

184187
used_labs = set()
185188
for y in ys:
@@ -190,47 +193,47 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
190193
used_labs.add(ylab)
191194
ylab = " " * (nlen - len(ylab)) + ylab + "|"
192195

193-
print(ylab, end=' ')
196+
print_func(ylab, end=' ')
194197

195198
for i in range(len(hist)):
196199
if int(y) <= hist[i]:
197-
printcolour(pch, True, colour)
200+
printcolour(pch, True, colour, print_func)
198201
else:
199-
printcolour(" ", True, colour)
200-
print('')
202+
printcolour(" ", True, colour, print_func)
203+
print_func('')
201204
xs = hist.keys()
202205

203-
print(" " * (nlen + 1) + "-" * len(xs))
206+
print_func(" " * (nlen + 1) + "-" * len(xs))
204207

205208
if xlab:
206209
xlen = len(str(float((max_y) / height) + max_y))
207210
for i in range(0, xlen):
208-
printcolour(" " * (nlen + 1), True, colour)
211+
printcolour(" " * (nlen + 1), True, colour, print_func)
209212
for x in range(0, len(hist)):
210213
num = str(bins[x])
211214
if x % 2 != 0:
212215
pass
213216
elif i < len(num):
214-
print(num[i], end=' ')
217+
print_func(num[i], end=' ')
215218
else:
216-
print(" ", end=' ')
217-
print('')
219+
print_func(" ", end=' ')
220+
print_func('')
218221

219222
center = max(map(len, map(str, [n, min_val, mean, max_val])))
220223
center += 15
221224

222225
if showSummary:
223-
print()
224-
print("-" * (2 + center))
225-
print("|" + "Summary".center(center) + "|")
226-
print("-" * (2 + center))
226+
print_func()
227+
print_func("-" * (2 + center))
228+
print_func("|" + "Summary".center(center) + "|")
229+
print_func("-" * (2 + center))
227230
summary = "|" + ("observations: %d" % n).center(center) + "|\n"
228231
summary += "|" + ("min value: %f" % min_val).center(center) + "|\n"
229232
summary += "|" + ("mean : %f" % mean).center(center) + "|\n"
230233
summary += "|" + ("sd : %f" % sd).center(center) + "|\n"
231234
summary += "|" + ("max value: %f" % max_val).center(center) + "|\n"
232235
summary += "-" * (2 + center)
233-
print(summary)
236+
print_func(summary)
234237

235238

236239
def main():

bashplotlib/scatterplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def plot_scatter(xs, ys, size=None, pch='o', colour='red', title=None, print_fun
7575
plotted.add((xp, yp))
7676
if isinstance(cs, list):
7777
colour = cs[i]
78-
printcolour(point, True, colour)
78+
printcolour(point, True, colour, print_func)
7979
print_func(" |")
8080
print_func("-" * (2 * len(get_scale(xs, False, size)) + 2))
8181

bashplotlib/utils/helpers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
Various helpful function for bashplotlib
55
"""
6-
6+
from __future__ import print_function
77
import sys
88

99
isiterable = lambda x: hasattr(x, '__iter__') or hasattr(x, '__getitem__')
@@ -32,15 +32,18 @@ def get_colour(colour):
3232
return bcolours.get(colour, bcolours['ENDC'])
3333

3434

35-
def printcolour(text, sameline=False, colour=get_colour("ENDC")):
35+
def printcolour(text, sameline=False, colour=get_colour("ENDC"), print_func=print):
3636
"""
3737
Print color text using escape codes
3838
"""
3939
if sameline:
4040
sep = ''
4141
else:
4242
sep = '\n'
43-
sys.stdout.write(get_colour(colour) + text + bcolours["ENDC"] + sep)
43+
if print_function == print:
44+
sys.stdout.write(get_colour(colour) + text + bcolours["ENDC"] + sep)
45+
else:
46+
print_func(get_colour(colour) + text + bcolours["ENDC"] + sep)
4447

4548

4649
def drange(start, stop, step=1.0, include_stop=False):

0 commit comments

Comments
 (0)