1+ #!/usr/bin/evn python
2+ # -*- coding: utf-8 -*-
13
4+ """
5+ Various helpful function for bashplotlib
6+ """
7+
8+ import sys
29
310bcolours = {
4- "white" : '\033 [97m' ,
5- "aqua" : '\033 [96m' ,
6- "pink" : '\033 [95m' ,
7- "blue" : '\033 [94m' ,
11+ "white" : '\033 [97m' ,
12+ "aqua" : '\033 [96m' ,
13+ "pink" : '\033 [95m' ,
14+ "blue" : '\033 [94m' ,
815 "yellow" : '\033 [93m' ,
9- "green" : '\033 [92m' ,
10- "red" : '\033 [91m' ,
11- "grey" : '\033 [90m' ,
12- "ENDC" : '\033 [0m'
16+ "green" : '\033 [92m' ,
17+ "red" : '\033 [91m' ,
18+ "grey" : '\033 [90m' ,
19+ "black" : '\033 [30m' ,
20+ "ENDC" : '\033 [39m' ,
1321}
1422
23+
1524def get_colour (colour ):
16- return bcolours .get (colour , bcolours ['white' ])
25+ """
26+ Get the escape code sequence for a colour
27+ """
28+ return bcolours .get (colour , bcolours ['ENDC' ])
1729
18- def printcolor (txt , sameline = False , color = get_colour ("white" )):
30+
31+ def printcolour (text , sameline = False , colour = get_colour ("ENDC" )):
32+ """
33+ Print color text using escape codes
34+ """
1935 if sameline :
20- if color == '\033 [97m' :
21- print txt ,
22- else :
23- print color + txt + bcolours ["ENDC" ],
36+ sep = ''
2437 else :
25- if color == '\033 [97m' :
26- print txt
27- else :
28- print color + txt + bcolours ["ENDC" ]
38+ sep = '\n '
39+ sys .stdout .write (get_colour (colour ) + text + bcolours ["ENDC" ] + sep )
40+
2941
3042def drange (start , stop , step = 1.0 , include_stop = False ):
31- "generate between 2 numbers w/ optional step, optionally include upper bound"
32- if step == 0 :
43+ """
44+ Generate between 2 numbers w/ optional step, optionally include upper bound
45+ """
46+ if step == 0 :
3347 step = 0.01
3448 r = start
49+
3550 if include_stop :
3651 while r <= stop :
3752 yield r
@@ -43,9 +58,12 @@ def drange(start, stop, step=1.0, include_stop=False):
4358 r += step
4459 r = round (r , 10 )
4560
61+
4662def box_text (text , width , offset = 0 ):
47- box = " " * offset + "-" * (width + 2 ) + "\n "
48- box += " " * offset + "|" + text .center (width ) + "|" + "\n "
49- box += " " * offset + "-" * (width + 2 )
63+ """
64+ Return text inside an ascii textbox
65+ """
66+ box = " " * offset + "-" * (width + 2 ) + "\n "
67+ box += " " * offset + "|" + text .center (width ) + "|" + "\n "
68+ box += " " * offset + "-" * (width + 2 )
5069 return box
51-
0 commit comments