outputformat 0.1.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
outputformat/plot.py ADDED
@@ -0,0 +1,277 @@
1
+ from outputformat import emoji
2
+
3
+
4
+ def bar(
5
+ value,
6
+ maxvalue,
7
+ style="block",
8
+ length=32,
9
+ title=False,
10
+ title_pad=0,
11
+ show_values=True,
12
+ values_pad=0,
13
+ values_precision=2,
14
+ show_percentage=True,
15
+ return_str=False,
16
+ ):
17
+ """Generate a single bar using ASCII.
18
+
19
+ Parameters
20
+ ----------
21
+ value : int, float
22
+ The value, or height, of this bar.
23
+
24
+ maxvalue : int, float
25
+ The maxium value this variable shoud have.
26
+
27
+ style : string
28
+ Style of bar. Options are: 'block', 'battery', 'bar', 'circle', 'star'
29
+ 'block'.....: ▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░
30
+ 'battery'...: ┫████████████ ┣
31
+ 'bar'.......: [■■■■■■■■■■■■ ]
32
+ 'circle'....: ●●●●●●●●●●●●○○○○○○
33
+ 'star': uses the Unicode star emoji '\U00002B50'
34
+
35
+ In case of using 'star', it is recommended a low value for 'length',
36
+ usually something as 3 or 5 (interesting for ratings)
37
+
38
+ An list of strings can also be passed, as:
39
+ >>> bar(35, 50, style=["(", "X", "-", ")"], title="Custom style")
40
+ Custom style: (XXXXXXXXXXXXXXXXXXXXXX----------) 35/50 ( 70.00%)
41
+
42
+ The characters in the list are used to build the bar,
43
+ and only the first 4 values in the list are used.
44
+
45
+ If a single string character is passed, it is used for a basic bar:
46
+ >>> bar(35, 50, style="$", title="Custom style")
47
+ Custom style: [$$$$$$$$$$$$$$$$$$$$$$ ] 35/50 ( 70.00%)
48
+
49
+ length : int
50
+ Total size of the bar (in characters) to be displayed.
51
+
52
+ title : string, optional
53
+ Text for a title displayed before the bar.
54
+
55
+ title_pad : int, optional
56
+ Padding to the right of title. Usefull to aling several bars.
57
+
58
+ show_values : Bool
59
+ If True, shows the values as 'value/maxvalue' in front of the bar
60
+
61
+ show_percentage : Bool
62
+ If True, shows the percentage in front of the bar
63
+
64
+ return_str : Bool, default: False
65
+ If True, returns a string instead of printing.
66
+
67
+ Returns
68
+ -------
69
+ string
70
+ Only returns in case 'return_str = True', otherwise None
71
+
72
+
73
+ """
74
+
75
+ # Check if value < maxvalue
76
+ if value and value > maxvalue:
77
+ raise ValueError(f"'value' cannot be bigger than 'maxvalue' {emoji.crazy}")
78
+
79
+ if style in ["block"]:
80
+ start = ""
81
+ fill = "▓"
82
+ empty = "░"
83
+ end = ""
84
+
85
+ elif style in ["battery"]:
86
+ start = "┫"
87
+ fill = "█"
88
+ empty = " "
89
+ end = "┣"
90
+
91
+ elif style in ["bar"]:
92
+ start = "["
93
+ fill = "■"
94
+ empty = " "
95
+ end = "]"
96
+
97
+ elif style in ["circle"]:
98
+ start = ""
99
+ fill = "●"
100
+ empty = "○"
101
+ end = ""
102
+
103
+ elif style in ["star"]:
104
+ start = ""
105
+ fill = f"{emoji.star}"
106
+ empty = ""
107
+ end = ""
108
+
109
+ else:
110
+ # Use values given as a list
111
+ if isinstance(style, list):
112
+ start = str(style[0])
113
+ fill = str(style[1])
114
+ empty = str(style[2])
115
+ end = str(style[3])
116
+ # Just use the char given
117
+ else:
118
+ start = "["
119
+ fill = str(style)
120
+ empty = " " * len(str(style))
121
+ end = "]"
122
+
123
+ # Start outputstring
124
+ outputstring = ""
125
+ if title:
126
+ title = str(title)
127
+
128
+ outputstring += f"{title:.<{title_pad}}: "
129
+
130
+ # Check if it's not a NaN or None
131
+ if value == value and value:
132
+
133
+ ratio = value / maxvalue
134
+ nfill = int(ratio * length)
135
+ outputstring += start
136
+ outputstring += f"{fill*nfill}{empty*(length-nfill)}"
137
+ outputstring += end
138
+
139
+ if show_values:
140
+ values_pad += values_precision + 1
141
+ outputstring += f" {value:>{values_pad}.{values_precision}f}/{maxvalue:.{values_precision}f}"
142
+
143
+ if show_percentage:
144
+ outputstring += f" ({value/maxvalue:>7.2%})"
145
+
146
+ if return_str:
147
+ return outputstring
148
+ else:
149
+ print(outputstring)
150
+
151
+
152
+ def barlist(
153
+ values,
154
+ titles=False,
155
+ maxvalue=False,
156
+ style="bar",
157
+ length=32,
158
+ show_values=True,
159
+ values_precision=2,
160
+ show_percentage=True,
161
+ return_str=False,
162
+ ):
163
+ """Short summary.
164
+
165
+ Parameters
166
+ ----------
167
+ values : list
168
+ list of values to be displayed (paired with 'titles').
169
+
170
+ titles : list
171
+ list of titles to be displayed (paired with 'values').
172
+
173
+ maxvalue : int, float, optional
174
+ The max value that any of the 'values' could have.
175
+
176
+ In case None (or False) is given, uses the max value from all the values
177
+
178
+ style : string
179
+ Style passed to outputformat.bar
180
+ Options are: 'block', 'battery', 'bar', 'circle', 'star'
181
+ 'block'.....: ▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░
182
+ 'battery'...: ┫████████████ ┣
183
+ 'bar'.......: [■■■■■■■■■■■■ ]
184
+ 'circle'....: ●●●●●●●●●●●●○○○○○○
185
+ 'star': uses the Unicode star emoji '\U00002B50'
186
+ In case of using 'star', it is recommended a low value for 'length',
187
+ usually something as 3 or 5 (interesting for ratings)
188
+
189
+ An list of strings can also be passed, as:
190
+ >>> bar(35, 50, style=["(", "X", "-", ")"], title="Custom style")
191
+ Custom style: (XXXXXXXXXXXXXXXXXXXXXX----------) 35/50 ( 70.00%)
192
+
193
+ The characters in the list are used to build the bar,
194
+ and only the first 4 values in the list are used.
195
+
196
+ If a single string character is passed, it is used for a basic bar:
197
+ >>> bar(35, 50, style="$", title="Custom style")
198
+ Custom style: [$$$$$$$$$$$$$$$$$$$$$$ ] 35/50 ( 70.00%)
199
+
200
+
201
+
202
+ length : int
203
+ Total size of the bars, in characters.
204
+
205
+ show_values : Bool
206
+ If True, shows the values as 'value/maxvalue' in front of the bar
207
+
208
+ show_percentage : Bool
209
+ If True, shows the percentage in front of the bar
210
+
211
+ return_str : Bool, default: False
212
+ If True, returns a string instead of printing.
213
+
214
+ Returns
215
+ -------
216
+ string
217
+ Only returns in case 'return_str = True', otherwise None
218
+
219
+
220
+ """
221
+
222
+ # If titles are not provided, make an empty list
223
+ if not titles:
224
+ titles = [""] * len(values)
225
+
226
+ # Negative values are not suported
227
+ for value in values:
228
+ try:
229
+ if value < 0:
230
+ raise ValueError("Negative values are not supported")
231
+ except:
232
+ pass
233
+
234
+ # Check if titles match values
235
+ if len(values) != len(titles):
236
+ errormsg = f"'values' and 'titles' must have the same length {emoji.sad}"
237
+ errormsg += f"\ntotal values: {len(values)}"
238
+ errormsg += f"\ntotal titles: {len(titles)}"
239
+ raise ValueError(errormsg)
240
+
241
+ # Initialize string
242
+ outputstring = ""
243
+
244
+ # In case maxvalue is no given,
245
+ # uses the max from all the values
246
+ if not maxvalue:
247
+ # Get the max without breaking in case we find a "None"
248
+ maxvalue = max([v for v in values if v])
249
+
250
+ # Convert all titles to strings
251
+ titles = [str(t) for t in titles]
252
+ # Get the longest title to use the proper padding
253
+ longest_title = len(max(titles, key=len))
254
+
255
+ longest_value = len(str(int(maxvalue)))
256
+
257
+ # Create each row
258
+ for idx in range(len(values)):
259
+ outputstring += bar(
260
+ values[idx],
261
+ maxvalue,
262
+ style=style,
263
+ title=titles[idx],
264
+ title_pad=longest_title,
265
+ length=length,
266
+ show_values=show_values,
267
+ values_pad=longest_value,
268
+ values_precision=values_precision,
269
+ show_percentage=show_percentage,
270
+ return_str=True,
271
+ )
272
+ outputstring += "\n"
273
+
274
+ if return_str:
275
+ return outputstring
276
+ else:
277
+ print(outputstring)
outputformat/title.py ADDED
@@ -0,0 +1,235 @@
1
+ from outputformat import fonts
2
+ from outputformat import emoji
3
+ import outputformat as ouf
4
+
5
+ # lh ➔ line horizontal
6
+ # lv ➔ line vertical
7
+ # tl ➔ top left
8
+ # tr ➔ top right
9
+ # bl ➔ bottom left
10
+ # br ➔ bottom right
11
+
12
+
13
+ def linetitle(txt, style="-", color=False, cmap="cool", bold=False, return_str=False):
14
+ """Simple line under text.
15
+
16
+ Parameters
17
+ ----------
18
+ txt : string
19
+ Text to be displayed.
20
+
21
+ style : string
22
+ Style for the title. Options are: 'line', 'double', 'line_hang'
23
+ 'line' or '-': single line under text
24
+ 'double' or '=': double line under text
25
+ 'line_hang': simple line with a hanger
26
+
27
+ If any other value is passed, it will be converted to string and
28
+ used as character for the line. For example:
29
+
30
+ >>> linetitle("Title with custom style", style="%")
31
+ Title with custom style
32
+ %%%%%%%%%%%%%%%%%%%%%%%
33
+
34
+ return_str : Bool, default: False
35
+ If True, returns a string instead of printing.
36
+
37
+ Returns
38
+ -------
39
+ string
40
+ Only returns in case 'return_str = True', otherwise None
41
+
42
+
43
+ """
44
+ # Start outputstring
45
+ outputstring = ""
46
+
47
+ txt = str(txt)
48
+ txt_size = ouf.tools.real_string_length(txt)
49
+ style = str(style)
50
+
51
+ # bl = bottom left
52
+ # lh = line horizontal
53
+ if style in ["line", "-"]:
54
+ bl = "─"
55
+ lh = "─"
56
+ space = ""
57
+
58
+ elif style in ["=", "double"]:
59
+ bl = "═"
60
+ lh = "═"
61
+ space = ""
62
+
63
+ elif style in ["line_hang", "hanging", "hang"]:
64
+ bl = "╭"
65
+ lh = "─"
66
+ space = " "
67
+ else:
68
+ bl = str(style)[0]
69
+ lh = str(style)[0]
70
+ space = ""
71
+
72
+ # Build string
73
+ outputstring += f"{space}{txt}\n"
74
+ outputstring += f"{bl}{lh*(txt_size-1)}{lh*len(space)}"
75
+
76
+ if color:
77
+ outputstring = ouf.c(outputstring, color, cmap=cmap, bold=bold, return_str=True)
78
+
79
+ if bold:
80
+ outputstring = ouf.b(outputstring, return_str=True)
81
+
82
+ if return_str:
83
+ return outputstring
84
+ else:
85
+ print(outputstring)
86
+
87
+
88
+ def boxtitle(txt, style="-", return_str=False, color=False, cmap="cool", bold=False):
89
+ """Creates a text with a box decoration.
90
+
91
+ Parameters
92
+ ----------
93
+ txt : string
94
+ Text to be displayed.
95
+
96
+ style : string
97
+ Style of the box. Options are 'line', 'line_hang', 'double', 'dashes'
98
+ 'line' or '-': Simple box with rounded corners
99
+ 'line_hang': Box with hanger in the bottom-left corner
100
+ 'double': Double line box
101
+ 'dashes' or '--': Box with dashed line
102
+
103
+ If any other value is passed, it will be converted to
104
+ string and used as character for the box
105
+
106
+ return_str : Bool, default: False
107
+ If True, returns a string instead of printing.
108
+
109
+ Returns
110
+ -------
111
+ string
112
+ Only returns in case 'return_str = True', otherwise None
113
+
114
+ """
115
+
116
+ txt = str(txt)
117
+ txt_size = ouf.tools.real_string_length(txt)
118
+ style = str(style)
119
+
120
+ if style in ["line", "-"]:
121
+ lh = "─"
122
+ lv = "│"
123
+ tl = "╭"
124
+ tr = "╮"
125
+ bl = "╰"
126
+ br = "╯"
127
+ elif style in ["line_hang", "hanging", "hang"]:
128
+ lh = "─"
129
+ lv = "│"
130
+ tl = "╭"
131
+ tr = "╮"
132
+ bl = "├"
133
+ br = "╯"
134
+
135
+ elif style in ["double", "="]:
136
+ lh = "═"
137
+ lv = "║"
138
+ tl = "╔"
139
+ tr = "╗"
140
+ bl = "╚"
141
+ br = "╝"
142
+
143
+ elif style in ["dashes", "--", ".", "..", ":", "dots"]:
144
+ lh = "╌"
145
+ lv = "┊"
146
+ tl = "╭"
147
+ tr = "╮"
148
+ bl = "╰"
149
+ br = "╯"
150
+ else:
151
+ # Just use the first value of the provided string
152
+ lh = lv = tl = tr = bl = br = style[0]
153
+
154
+ # Build string
155
+ outputstring = (
156
+ (tl + (lh * (txt_size + 2) + tr))
157
+ + "\n"
158
+ + (lv + " " + txt + " " + lv)
159
+ + "\n"
160
+ + (bl + (lh * (txt_size + 2) + br))
161
+ )
162
+
163
+ if color:
164
+ outputstring = ouf.c(outputstring, color, cmap=cmap, bold=bold, return_str=True)
165
+
166
+ if bold:
167
+ outputstring = ouf.b(outputstring, return_str=True)
168
+
169
+ if return_str:
170
+ return outputstring
171
+ else:
172
+ print(outputstring)
173
+
174
+
175
+ def bigtitle(txt, style="small", return_str=False, color=False, cmap="cool"):
176
+ """uses ASCII art to generate a big title.
177
+
178
+ Parameters
179
+ ----------
180
+ txt : string
181
+ Text to be displayed.
182
+ style : string
183
+ Style used for the ASCII art, for the moment, only 'small' is supported
184
+
185
+ Fonts are defined in outputformat.fonts.py
186
+
187
+ Supported chars are: 0123456789abcdefghijklmnopqrstuvwxyz_-!.'
188
+
189
+ return_str : Bool, default: False
190
+ If True, returns a string instead of printing.
191
+
192
+ Returns
193
+ -------
194
+ string
195
+ Only returns in case 'return_str = True', otherwise None
196
+
197
+ """
198
+
199
+ # Get the font_dict ot be used
200
+ if style not in fonts.font_styles:
201
+ error_message = (
202
+ f"'{style}' is not supported by 'bigtitle' {emoji.sad}"
203
+ + f"\nSupported styles: {fonts.font_styles}"
204
+ )
205
+ raise ValueError(error_message)
206
+
207
+ font_dict = getattr(fonts, style)
208
+
209
+ # Make sure it's a string an lowercase it
210
+ txt = str(txt).lower()
211
+
212
+ # Check if we can print the string
213
+ for char in txt:
214
+ if char not in [supported for supported in fonts.suported_chars]:
215
+ error_message = (
216
+ f"'{char}' is not supported by 'bigtitle' {emoji.sad}"
217
+ + f"\nSupported chars: {fonts.suported_chars}"
218
+ )
219
+ raise ValueError(error_message)
220
+
221
+ # Make the title
222
+ nrows = len(font_dict[fonts.suported_chars[0]])
223
+ outputstring = ""
224
+ for row in range(nrows):
225
+ for letter in txt:
226
+ outputstring += font_dict[letter][row]
227
+ outputstring += " "
228
+ outputstring += "\n"
229
+
230
+ if color:
231
+ outputstring = ouf.c(outputstring, color, cmap=cmap, return_str=True)
232
+ if return_str:
233
+ return outputstring
234
+ else:
235
+ print(outputstring)