plotext-plus 1.0.9__py3-none-any.whl → 1.0.10__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.
plotext_plus/_matrix.py CHANGED
@@ -1,6 +1,7 @@
1
1
  import plotext_plus._utility as ut
2
2
 
3
- class matrix_class():
3
+
4
+ class MatrixClass:
4
5
  def __init__(self):
5
6
  self.set_size(0, 0)
6
7
  self.set_axes_color()
@@ -8,25 +9,28 @@ class matrix_class():
8
9
  self.set_canvas_color()
9
10
  self.set_matrices()
10
11
  self.set_canvas()
11
-
12
+
12
13
  def set_size(self, cols, rows):
13
- self.rows = rows; self.Rows = range(self.rows)
14
- self.cols = cols; self.Cols = range(self.cols)
14
+ self.rows = rows
15
+ self.Rows = range(self.rows)
16
+ self.cols = cols
17
+ self.Cols = range(self.cols)
15
18
  self.size = [self.rows, self.cols]
16
19
 
17
20
  def update_size(self):
18
21
  self.cols, self.rows = ut.matrix_size(self.marker)
19
- self.Rows = range(self.rows); self.Cols = range(self.cols)
22
+ self.Rows = range(self.rows)
23
+ self.Cols = range(self.cols)
20
24
  self.size = [self.rows, self.cols]
21
25
 
22
- def set_axes_color(self, color = ut.no_color):
26
+ def set_axes_color(self, color=ut.no_color):
23
27
  self.axes_color = color
24
28
 
25
29
  def set_canvas_area(self, col1, col2, row1, row2):
26
30
  self.Cols_canvas = range(col1, col2)
27
31
  self.Rows_canvas = range(row1, row2)
28
32
 
29
- def set_canvas_color(self, color = ut.no_color):
33
+ def set_canvas_color(self, color=ut.no_color):
30
34
  self.canvas_color = color
31
35
 
32
36
  def in_canvas(self, col, row):
@@ -35,68 +39,207 @@ class matrix_class():
35
39
  def set_matrices(self):
36
40
  self.marker = [[ut.space] * self.cols for row in self.Rows]
37
41
  self.fullground = [[ut.no_color] * self.cols for row in self.Rows]
38
- get_background = lambda col, row: self.canvas_color if self.in_canvas(col, row) else self.axes_color
39
- self.background = [[get_background(col, row) for col in self.Cols] for row in self.Rows]
42
+ def get_background(col, row):
43
+ return (
44
+ self.canvas_color if self.in_canvas(col, row) else self.axes_color
45
+ )
46
+ self.background = [
47
+ [get_background(col, row) for col in self.Cols] for row in self.Rows
48
+ ]
40
49
  self.style = [[ut.no_color] * self.cols for row in self.Rows]
41
50
 
42
51
  def legal(self, col, row):
43
52
  return col in self.Cols and row in self.Rows
44
53
 
45
54
  def get_marker(self, col, row):
46
- return self.marker[row][col] #if self.legal(col, row) else "OUT"
55
+ return self.marker[row][col] # if self.legal(col, row) else "OUT"
47
56
 
48
57
  def get_marker_row(self, row):
49
- return self.marker[row] #if self.legal(0, row) else "OUT"
58
+ return self.marker[row] # if self.legal(0, row) else "OUT"
50
59
 
51
60
  def get_marker_col(self, col):
52
- return [self.marker[r][col] for r in self.Rows]#if self.legal(0, row) else "OUT"
61
+ return [
62
+ self.marker[r][col] for r in self.Rows
63
+ ] # if self.legal(0, row) else "OUT"
53
64
 
54
65
  def set_marker(self, col, row, marker):
55
66
  self.marker[row][col] = marker
56
-
57
- def set_fullground(self, col, row, fullground = None):
67
+
68
+ def set_fullground(self, col, row, fullground=None):
58
69
  self.fullground[row][col] = fullground
59
-
60
- def set_background(self, col, row, background = None):
70
+
71
+ def set_background(self, col, row, background=None):
61
72
  self.background[row][col] = background
62
-
63
- def set_style(self, col, row, style = None):
73
+
74
+ def set_style(self, col, row, style=None):
64
75
  self.style[row][col] = style
65
76
 
66
- def insert_element(self, col, row, marker, fullground = None, style = None, background = None, check_canvas = False):
67
- test_canvas = True if check_canvas is False else col in self.Cols_canvas and row in self.Rows_canvas
77
+ def insert_element(
78
+ self,
79
+ col,
80
+ row,
81
+ marker,
82
+ fullground=None,
83
+ style=None,
84
+ background=None,
85
+ check_canvas=False,
86
+ ):
87
+ test_canvas = (
88
+ True
89
+ if check_canvas is False
90
+ else col in self.Cols_canvas and row in self.Rows_canvas
91
+ )
68
92
  if self.legal(col, row) and test_canvas:
69
93
  self.set_marker(col, row, marker)
70
- self.set_fullground(col, row, fullground) if fullground is not None else None
71
- self.set_background(col, row, background) if background is not None else None
94
+ (
95
+ self.set_fullground(col, row, fullground)
96
+ if fullground is not None
97
+ else None
98
+ )
99
+ (
100
+ self.set_background(col, row, background)
101
+ if background is not None
102
+ else None
103
+ )
72
104
  self.set_style(col, row, style) if style is not None else None
73
105
 
74
- def add_horizontal_string(self, col, row, string, fullground = None, style = None, background = None, alignment = "left", check_space = False, check_canvas = False):
75
- l = len(string); L = range(l)
76
- col = col if alignment == "left" else col - l // 2 if alignment == "center" else col - l + 1 if alignment == "right" else ut.correct_coord(self.get_marker_row(row), string, col) # if dynamic
77
- b, e = max(col - 1, 0), min(col + l + 1, self.cols)
78
- test_space = all([self.get_marker(c, row) == ut.space for c in range(b, e)]) and col >= 0 and col + l <= self.cols if check_space else True
79
- [self.insert_element(col + i, row, string[i], fullground, style, background, check_canvas) for i in L] if test_space else None
106
+ def add_horizontal_string(
107
+ self,
108
+ col,
109
+ row,
110
+ string,
111
+ fullground=None,
112
+ style=None,
113
+ background=None,
114
+ alignment="left",
115
+ check_space=False,
116
+ check_canvas=False,
117
+ ):
118
+ str_length = len(string)
119
+ char_indices = range(str_length)
120
+ col = (
121
+ col
122
+ if alignment == "left"
123
+ else (
124
+ col - str_length // 2
125
+ if alignment == "center"
126
+ else (
127
+ col - str_length + 1
128
+ if alignment == "right"
129
+ else ut.correct_coord(self.get_marker_row(row), string, col)
130
+ )
131
+ )
132
+ ) # if dynamic
133
+ b, e = max(col - 1, 0), min(col + str_length + 1, self.cols)
134
+ test_space = (
135
+ all(self.get_marker(c, row) == ut.space for c in range(b, e))
136
+ and col >= 0
137
+ and col + str_length <= self.cols
138
+ if check_space
139
+ else True
140
+ )
141
+ (
142
+ [
143
+ self.insert_element(
144
+ col + i, row, string[i], fullground, style, background, check_canvas
145
+ )
146
+ for i in char_indices
147
+ ]
148
+ if test_space
149
+ else None
150
+ )
80
151
  return test_space
81
152
 
82
- def add_vertical_string(self, col, row, string, fullground = None, style = None, background = None, alignment = "bottom", check_canvas = False):
83
- l = len(string); L = range(l)
84
- row = row if alignment == "bottom" else row - l // 2 if alignment == "center" else row - l + 1 #if alignment == "top"
85
- [self.insert_element(col, row + i, string[i], fullground, style, background, check_canvas) for i in L]
153
+ def add_vertical_string(
154
+ self,
155
+ col,
156
+ row,
157
+ string,
158
+ fullground=None,
159
+ style=None,
160
+ background=None,
161
+ alignment="bottom",
162
+ check_canvas=False,
163
+ ):
164
+ str_length = len(string)
165
+ char_indices = range(str_length)
166
+ row = (
167
+ row
168
+ if alignment == "bottom"
169
+ else row - str_length // 2 if alignment == "center" else row - str_length + 1
170
+ ) # if alignment == "top"
171
+ [
172
+ self.insert_element(
173
+ col, row + i, string[i], fullground, style, background, check_canvas
174
+ )
175
+ for i in char_indices
176
+ ]
86
177
 
87
- def add_multiple_horizontal_strings(self, col, row, string, fullground = None, style = None, background = None, alignment = "left", check_space = False, check_canvas = False):
88
- strings = ''.join(string).split('\n'); S = len(strings)
89
- [self.add_horizontal_string(col, row - s, strings[s], fullground, style, background, alignment, check_space, check_canvas) for s in range(S)]
178
+ def add_multiple_horizontal_strings(
179
+ self,
180
+ col,
181
+ row,
182
+ string,
183
+ fullground=None,
184
+ style=None,
185
+ background=None,
186
+ alignment="left",
187
+ check_space=False,
188
+ check_canvas=False,
189
+ ):
190
+ strings = "".join(string).split("\n")
191
+ string_count = len(strings)
192
+ [
193
+ self.add_horizontal_string(
194
+ col,
195
+ row - s,
196
+ strings[s],
197
+ fullground,
198
+ style,
199
+ background,
200
+ alignment,
201
+ check_space,
202
+ check_canvas,
203
+ )
204
+ for s in range(string_count)
205
+ ]
90
206
 
91
- def add_multiple_vertical_strings(self, col, row, string, fullground = None, style = None, background = None, alignment = "left", check_canvas = False):
92
- strings = ''.join(string).split('\n'); S = len(strings)
93
- [self.add_vertical_string(col + s, row, strings[s], fullground, style, background, alignment, check_canvas) for s in range(S)]
207
+ def add_multiple_vertical_strings(
208
+ self,
209
+ col,
210
+ row,
211
+ string,
212
+ fullground=None,
213
+ style=None,
214
+ background=None,
215
+ alignment="left",
216
+ check_canvas=False,
217
+ ):
218
+ strings = "".join(string).split("\n")
219
+ string_count = len(strings)
220
+ [
221
+ self.add_vertical_string(
222
+ col + s,
223
+ row,
224
+ strings[s],
225
+ fullground,
226
+ style,
227
+ background,
228
+ alignment,
229
+ check_canvas,
230
+ )
231
+ for s in range(string_count)
232
+ ]
94
233
 
95
234
  def get_colors(self, col, row):
96
- return [self.fullground[row][col], self.style[row][col], self.background[row][col]] #if self.legal(col, row) else ["OUT"] * 3
235
+ return [
236
+ self.fullground[row][col],
237
+ self.style[row][col],
238
+ self.background[row][col],
239
+ ] # if self.legal(col, row) else ["OUT"] * 3
97
240
 
98
241
  def set_canvas(self):
99
- canvas = ''
242
+ canvas = ""
100
243
  for row in self.Rows[::-1]:
101
244
  for col in self.Cols:
102
245
  marker = self.marker[row][col]
@@ -106,13 +249,13 @@ class matrix_class():
106
249
  canvas += ansi
107
250
  canvas += marker
108
251
  if col == self.cols - 1 or colors != self.get_colors(col + 1, row):
109
- #ansi_next = colors_to_ansi(*colors_next)
110
- canvas += ut.ansi_end #+ ansi_next
111
- canvas += '\n'
252
+ # ansi_next = colors_to_ansi(*colors_next)
253
+ canvas += ut.ansi_end # + ansi_next
254
+ canvas += "\n"
112
255
 
113
256
  self.canvas = canvas
114
- #print(repr(canvas))
115
- #self.canvas = '\n'.join([''.join(self.marker[row]) for row in self.Rows])
257
+ # print(repr(canvas))
258
+ # self.canvas = '\n'.join([''.join(self.marker[row]) for row in self.Rows])
116
259
  return self.canvas
117
260
 
118
261
  def get_canvas(self):
@@ -124,48 +267,59 @@ class matrix_class():
124
267
  self.background = ut.hstack(self.background, extra.background)
125
268
  self.style = ut.hstack(self.style, extra.style)
126
269
  self.update_size()
127
- self.canvas = '';
128
-
270
+ self.canvas = ""
271
+
129
272
  def vstack(self, extra):
130
273
  self.marker = ut.vstack(self.marker, extra.marker)
131
274
  self.fullground = ut.vstack(self.fullground, extra.fullground)
132
275
  self.background = ut.vstack(self.background, extra.background)
133
276
  self.style = ut.vstack(self.style, extra.style)
134
277
  self.update_size()
135
- self.canvas = ''
278
+ self.canvas = ""
136
279
 
137
- def to_html(self): # turns a matrix of character in html form
138
- code = lambda color: "rgb" + str(color).replace(" ", "")
139
- #html = "<body>\n<p style=\"font-family:courier; font-size:11pt;\">\n\n"
140
- html = "<body> \n <code style = style=\"font-family:courier; \"font-size : 10pt;\"> \n\n"
280
+ def to_html(self): # turns a matrix of character in html form
281
+ def code(color):
282
+ return "rgb" + str(color).replace(" ", "")
283
+ # html = "<body>\n<p style=\"font-family:courier; font-size:11pt;\">\n\n"
284
+ html = '<body> \n <code style = style="font-family:courier; "font-size : 10pt;"> \n\n'
141
285
  for r in range(self.rows - 1, -1, -1):
142
286
  for c in range(self.cols):
143
287
  marker = self.get_marker(c, r)
144
288
  color, style, background = self.get_colors(c, r)
145
289
  marker = "&nbsp;" if marker == ut.space else marker
146
- marker = '<b>' + marker + '</b>' if style == 'bold' else marker
147
- marker = '<i>' + marker + '</i>' if style == 'italic' else marker
148
- color = 'black' if color == ut.no_color else color
149
- background = 'white' if background == ut.no_color else background
290
+ marker = "<b>" + marker + "</b>" if style == "bold" else marker
291
+ marker = "<i>" + marker + "</i>" if style == "italic" else marker
292
+ color = "black" if color == ut.no_color else color
293
+ background = "white" if background == ut.no_color else background
150
294
  color = ut.to_rgb(color)
151
295
  background = ut.to_rgb(background)
152
296
  color = code(color)
153
297
  background = code(background)
154
- html += "<span style = \"color:" + color + "; background-color: " + background + "\">" + marker + "</span>"
298
+ html += (
299
+ '<span style = "color:'
300
+ + color
301
+ + "; background-color: "
302
+ + background
303
+ + '">'
304
+ + marker
305
+ + "</span>"
306
+ )
155
307
  html += " <br>\n\n"
156
308
  html += "<code> \n </body>"
157
309
  return html
158
310
 
159
- def join_matrices(matrices): # from a matrix of matrix_class() objects to a single matrix
311
+
312
+ def join_matrices(
313
+ matrices,
314
+ ): # from a matrix of MatrixClass() objects to a single matrix
160
315
  cols, rows = ut.matrix_size(matrices)
161
- M = matrix_class()
316
+ main_matrix = MatrixClass()
162
317
  for r in range(rows):
163
318
  m_rows = matrices[r][0].rows
164
- m = matrix_class()
319
+ m = MatrixClass()
165
320
  m.set_size(0, m_rows)
166
321
  m.set_matrices()
167
322
  for c in range(cols):
168
323
  m.hstack(matrices[r][c])
169
- M.vstack(m)
170
- return M
171
-
324
+ main_matrix.vstack(m)
325
+ return main_matrix