byzh-core 0.0.6.0__py3-none-any.whl → 0.0.8.0__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.
- {byzh_core → byzh/core}/B_os.py +1 -1
- byzh/core/Brecorder/__init__.py +4 -0
- byzh/core/Brecorder/record1d.py +71 -0
- byzh/core/Brecorder/record2d.py +86 -0
- {byzh_core → byzh/core}/Bterminal/cmd.py +7 -6
- byzh/core/__init__.py +0 -0
- {byzh_core-0.0.6.0.dist-info → byzh_core-0.0.8.0.dist-info}/METADATA +2 -2
- byzh_core-0.0.8.0.dist-info/RECORD +26 -0
- byzh_core-0.0.8.0.dist-info/top_level.txt +1 -0
- byzh_core/__init__.py +0 -5
- byzh_core/__main__.py +0 -9
- byzh_core/obsolete/Bconfig/__init__.py +0 -3
- byzh_core/obsolete/Bconfig/config.py +0 -328
- byzh_core/obsolete/__init__.py +0 -6
- byzh_core/obsolete/auto_table.py +0 -427
- byzh_core/obsolete/row_table.py +0 -175
- byzh_core/obsolete/xy_table.py +0 -211
- byzh_core-0.0.6.0.dist-info/RECORD +0 -30
- byzh_core-0.0.6.0.dist-info/top_level.txt +0 -1
- {byzh_core → byzh/core}/Barchive/__init__.py +0 -0
- {byzh_core → byzh/core}/Barchive/archive.py +0 -0
- {byzh_core → byzh/core}/Btable/__init__.py +0 -0
- {byzh_core → byzh/core}/Btable/auto_table.py +0 -0
- {byzh_core → byzh/core}/Bterminal/__init__.py +0 -0
- {byzh_core → byzh/core}/Bterminal/run_func.py +0 -0
- {byzh_core → byzh/core}/Btqdm/__init__.py +0 -0
- {byzh_core → byzh/core}/Btqdm/my_tqdm.py +0 -0
- {byzh_core → byzh/core}/Butils/__init__.py +0 -0
- {byzh_core → byzh/core}/Butils/decorator.py +0 -0
- {byzh_core → byzh/core}/Butils/text_style.py +0 -0
- {byzh_core → byzh/core}/Butils/timer.py +0 -0
- {byzh_core → byzh/core}/Bwriter/__init__.py +0 -0
- {byzh_core → byzh/core}/Bwriter/writer.py +0 -0
- {byzh_core-0.0.6.0.dist-info → byzh_core-0.0.8.0.dist-info}/LICENSE +0 -0
- {byzh_core-0.0.6.0.dist-info → byzh_core-0.0.8.0.dist-info}/WHEEL +0 -0
- {byzh_core-0.0.6.0.dist-info → byzh_core-0.0.8.0.dist-info}/entry_points.txt +0 -0
byzh_core/obsolete/auto_table.py
DELETED
@@ -1,427 +0,0 @@
|
|
1
|
-
import copy
|
2
|
-
import math
|
3
|
-
import os
|
4
|
-
from pathlib import Path
|
5
|
-
from typing import List, Tuple, Union
|
6
|
-
Seq = Union[List, Tuple]
|
7
|
-
try:
|
8
|
-
from wcwidth import wcswidth
|
9
|
-
except ImportError:
|
10
|
-
raise ImportError("[table] 请先安装wcwidth库: pip install wcwidth")
|
11
|
-
|
12
|
-
class InnerList:
|
13
|
-
def __init__(self):
|
14
|
-
self.key_list = []
|
15
|
-
self.value_list = []
|
16
|
-
|
17
|
-
def items(self):
|
18
|
-
return zip(self.key_list, self.value_list)
|
19
|
-
def keys(self):
|
20
|
-
return self.key_list
|
21
|
-
def values(self):
|
22
|
-
return self.value_list
|
23
|
-
def copy(self):
|
24
|
-
return copy.deepcopy(self)
|
25
|
-
def __getitem__(self, item):
|
26
|
-
item = str(item)
|
27
|
-
if item not in self.key_list:
|
28
|
-
self.key_list.append(item)
|
29
|
-
self.value_list.append('')
|
30
|
-
index = self.key_list.index(item)
|
31
|
-
return self.value_list[index]
|
32
|
-
|
33
|
-
def __setitem__(self, key, value):
|
34
|
-
key, value = str(key), str(value)
|
35
|
-
if key not in self.key_list:
|
36
|
-
self.key_list.append(key)
|
37
|
-
self.value_list.append(value)
|
38
|
-
index = self.key_list.index(key)
|
39
|
-
self.value_list[index] = value
|
40
|
-
|
41
|
-
|
42
|
-
class OuterList:
|
43
|
-
def __init__(self):
|
44
|
-
self.key_list = []
|
45
|
-
self.value_list = []
|
46
|
-
|
47
|
-
def items(self):
|
48
|
-
return zip(self.key_list, self.value_list)
|
49
|
-
def keys(self):
|
50
|
-
return self.key_list
|
51
|
-
def values(self):
|
52
|
-
return self.value_list
|
53
|
-
def __getitem__(self, item: str):
|
54
|
-
item = str(item)
|
55
|
-
if item not in self.key_list:
|
56
|
-
self.key_list.append(item)
|
57
|
-
self.value_list.append(InnerList())
|
58
|
-
index = self.key_list.index(item)
|
59
|
-
return self.value_list[index]
|
60
|
-
|
61
|
-
def __setitem__(self, key, value):
|
62
|
-
key, value = str(key), value
|
63
|
-
if key not in self.key_list:
|
64
|
-
self.key_list.append(key)
|
65
|
-
self.value_list.append(value)
|
66
|
-
self.value_list[self.key_list.index(key)] = value
|
67
|
-
|
68
|
-
class MyMatrix:
|
69
|
-
def __init__(self):
|
70
|
-
self.matrix = []
|
71
|
-
self.fill = ' '
|
72
|
-
|
73
|
-
|
74
|
-
def add(self, i, j, value):
|
75
|
-
if i > len(self.matrix)-1:
|
76
|
-
self.matrix.extend([[] for _ in range(i-len(self.matrix)+1)])
|
77
|
-
if j > len(self.matrix[i])-1:
|
78
|
-
self.matrix[i].extend([self.fill for _ in range(j-len(self.matrix[i])+1)])
|
79
|
-
self.matrix[i][j] = value
|
80
|
-
|
81
|
-
def update_matrix(self):
|
82
|
-
max_length = max([len(x) for x in self.matrix])
|
83
|
-
for i in range(len(self.matrix)):
|
84
|
-
self.matrix[i].extend([self.fill for _ in range(max_length-len(self.matrix[i]))])
|
85
|
-
|
86
|
-
def get_matrix(self):
|
87
|
-
self.update_matrix()
|
88
|
-
return self.matrix
|
89
|
-
|
90
|
-
class B_AutoTable:
|
91
|
-
def __init__(self, row_name='x', col_name='y', auto_adaptive=False):
|
92
|
-
self.row_sidebar = []
|
93
|
-
self.col_sidebar = []
|
94
|
-
self.row_name = row_name
|
95
|
-
self.col_name = col_name
|
96
|
-
self.lists = OuterList()
|
97
|
-
self.verbose_lists = []
|
98
|
-
self._widths = []
|
99
|
-
|
100
|
-
self.auto_adaptive = auto_adaptive
|
101
|
-
|
102
|
-
def set(self, row, col, content):
|
103
|
-
row, col, content = str(row), str(col), str(content)
|
104
|
-
self.lists[row][col] = content
|
105
|
-
|
106
|
-
def get_str(self, row, col):
|
107
|
-
return self[row][col]
|
108
|
-
def get_int(self, row, col):
|
109
|
-
return int(self[row][col])
|
110
|
-
def get_float(self, row, col):
|
111
|
-
return float(self[row][col])
|
112
|
-
def get_bool(self, row, col):
|
113
|
-
temp = self[row][col]
|
114
|
-
if temp == "True" or temp == "1":
|
115
|
-
return True
|
116
|
-
else:
|
117
|
-
return False
|
118
|
-
def items(self):
|
119
|
-
'''
|
120
|
-
(key1, key2, value)
|
121
|
-
'''
|
122
|
-
self._update_all()
|
123
|
-
result = [(row, col, self[row][col]) for row in self.row_sidebar for col in self.col_sidebar]
|
124
|
-
return result
|
125
|
-
def copy_row(self, new_row, old_row):
|
126
|
-
new_row, old_row = str(new_row), str(old_row)
|
127
|
-
self[new_row] = self[old_row]
|
128
|
-
def read_txt(self, path:Path, row_name:None|str=None, col_name:None|str=None):
|
129
|
-
with open(path, 'r') as f:
|
130
|
-
lines = f.readlines()
|
131
|
-
lines = [x.strip() for x in lines if x.startswith('|')]
|
132
|
-
|
133
|
-
# 所有内容
|
134
|
-
temp = []
|
135
|
-
for string in lines:
|
136
|
-
elements = string.split('|')[1:-1]
|
137
|
-
elements = [x.strip() for x in elements]
|
138
|
-
temp.append(elements)
|
139
|
-
|
140
|
-
# 边角内容
|
141
|
-
x_name, y_name = temp[0][0].split(' \\ ') if ('\\' in temp[0][0]) else ("x", "y")
|
142
|
-
x_sidebar = [var[0] for var in temp[1:]]
|
143
|
-
y_sidebar = temp[0][1:]
|
144
|
-
|
145
|
-
if row_name == y_name and col_name == x_name: # 不按txt的样子读, 而是翻折后读
|
146
|
-
self.row_sidebar = y_sidebar
|
147
|
-
self.col_sidebar = x_sidebar
|
148
|
-
self.row_name = y_name
|
149
|
-
self.col_name = x_name
|
150
|
-
self.lists = OuterList()
|
151
|
-
self._widths = []
|
152
|
-
for i in range(len(y_sidebar)):
|
153
|
-
for j in range(len(x_sidebar)):
|
154
|
-
self[y_sidebar[i]][x_sidebar[j]] = temp[j + 1][i + 1]
|
155
|
-
else:
|
156
|
-
self.row_sidebar = x_sidebar
|
157
|
-
self.col_sidebar = y_sidebar
|
158
|
-
self.row_name = x_name
|
159
|
-
self.col_name = y_name
|
160
|
-
self.lists = OuterList()
|
161
|
-
self._widths = []
|
162
|
-
for i in range(len(x_sidebar)):
|
163
|
-
for j in range(len(y_sidebar)):
|
164
|
-
self[x_sidebar[i]][y_sidebar[j]] = temp[i+1][j+1]
|
165
|
-
|
166
|
-
def to_txt(self, path):
|
167
|
-
'''
|
168
|
-
将表格内容写入文件
|
169
|
-
:param path:
|
170
|
-
:return:
|
171
|
-
'''
|
172
|
-
dir = Path(path).resolve().parent
|
173
|
-
os.makedirs(dir, exist_ok=True)
|
174
|
-
with open(path, 'w') as f:
|
175
|
-
f.write(self.to_str())
|
176
|
-
|
177
|
-
def update_txt(self, path):
|
178
|
-
'''
|
179
|
-
更新表格内容\n
|
180
|
-
如果文件不存在,则创建文件
|
181
|
-
:param path:
|
182
|
-
:return:
|
183
|
-
'''
|
184
|
-
# 是否存在该文件
|
185
|
-
if not os.path.exists(path):
|
186
|
-
self.to_txt(path)
|
187
|
-
else:
|
188
|
-
current_lists = self.lists
|
189
|
-
self.read_txt(path, self.row_name, self.col_name)
|
190
|
-
new_lists = self.lists
|
191
|
-
self.lists = self._merge_lists(new_lists, current_lists)
|
192
|
-
|
193
|
-
self.to_txt(path)
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
def to_strs(self) -> List[str]:
|
198
|
-
transpose = self._need_transpose()
|
199
|
-
self._update_all(transpose)
|
200
|
-
self._update_widths()
|
201
|
-
results = self._create_prefix()
|
202
|
-
|
203
|
-
str_dash = ''
|
204
|
-
str_head = ''
|
205
|
-
for index, y in enumerate(self.col_sidebar):
|
206
|
-
pre_space, suf_space = self._get_prefix_suffix(y, self._widths[index], ' ')
|
207
|
-
pre_dash, suf_dash = self._get_prefix_suffix('-', self._widths[index], '-')
|
208
|
-
str_head += ' ' + pre_space + y + suf_space + ' |'
|
209
|
-
str_dash += '-' + pre_dash + '-' + suf_dash + '-+'
|
210
|
-
results[0] += str_dash
|
211
|
-
results[1] += str_head
|
212
|
-
results[2] += str_dash
|
213
|
-
|
214
|
-
offset = 3
|
215
|
-
for i, y_list in enumerate(self.verbose_lists):
|
216
|
-
for j, value in enumerate(y_list):
|
217
|
-
pre_space, suf_space = self._get_prefix_suffix(value, self._widths[j], ' ')
|
218
|
-
str_content = ' ' + pre_space + value + suf_space + ' |'
|
219
|
-
results[i+offset] += str_content
|
220
|
-
|
221
|
-
results[-1] += str_dash
|
222
|
-
|
223
|
-
return results
|
224
|
-
|
225
|
-
def to_str(self) -> str:
|
226
|
-
result = ""
|
227
|
-
strs = self.to_strs()
|
228
|
-
for x in strs[:-1]:
|
229
|
-
result += x + '\n'
|
230
|
-
result += strs[-1]
|
231
|
-
|
232
|
-
return result
|
233
|
-
|
234
|
-
def _merge_lists(self, lists_base, lists_new):
|
235
|
-
result = OuterList()
|
236
|
-
|
237
|
-
for row_key, inner_list in lists_base.items():
|
238
|
-
for col_key, value in inner_list.items():
|
239
|
-
result[row_key][col_key] = value
|
240
|
-
for row_key, inner_list in lists_new.items():
|
241
|
-
for col_key, value in inner_list.items():
|
242
|
-
result[row_key][col_key] = value
|
243
|
-
|
244
|
-
return result
|
245
|
-
|
246
|
-
def _update_all(self, transpose=False):
|
247
|
-
if not transpose:
|
248
|
-
row_sidebar = []
|
249
|
-
col_sidebar = []
|
250
|
-
my_matrix = MyMatrix()
|
251
|
-
for x_key, x_list in self.lists.items():
|
252
|
-
row_sidebar.append(x_key)
|
253
|
-
i = row_sidebar.index(x_key)
|
254
|
-
|
255
|
-
for y_key, value in x_list.items():
|
256
|
-
if y_key not in col_sidebar:
|
257
|
-
col_sidebar.append(y_key)
|
258
|
-
j = col_sidebar.index(y_key)
|
259
|
-
|
260
|
-
my_matrix.add(i, j, value)
|
261
|
-
|
262
|
-
self.row_sidebar = row_sidebar
|
263
|
-
self.col_sidebar = col_sidebar
|
264
|
-
self.verbose_lists = my_matrix.get_matrix()
|
265
|
-
self.row_name, self.col_name = self.row_name, self.col_name
|
266
|
-
else:
|
267
|
-
row_sidebar = []
|
268
|
-
col_sidebar = []
|
269
|
-
my_matrix = MyMatrix()
|
270
|
-
for x_key, x_list in self.lists.items():
|
271
|
-
col_sidebar.append(x_key)
|
272
|
-
i = col_sidebar.index(x_key)
|
273
|
-
|
274
|
-
for y_key, value in x_list.items():
|
275
|
-
if y_key not in row_sidebar:
|
276
|
-
row_sidebar.append(y_key)
|
277
|
-
j = row_sidebar.index(y_key)
|
278
|
-
|
279
|
-
my_matrix.add(j, i, value)
|
280
|
-
|
281
|
-
self.row_sidebar = row_sidebar
|
282
|
-
self.col_sidebar = col_sidebar
|
283
|
-
self.verbose_lists = my_matrix.get_matrix()
|
284
|
-
self.row_name, self.col_name = self.col_name, self.row_name
|
285
|
-
|
286
|
-
def _update_widths(self):
|
287
|
-
'''
|
288
|
-
请先调用_update_sidebar_and_vlist
|
289
|
-
:return:
|
290
|
-
'''
|
291
|
-
temp = [self._get_width(x) for x in self.col_sidebar]
|
292
|
-
|
293
|
-
for i in range(len(self.verbose_lists)):
|
294
|
-
for j in range(len(self.verbose_lists[i])):
|
295
|
-
temp[j] = max(temp[j], self._get_width(self.verbose_lists[i][j]))
|
296
|
-
|
297
|
-
self._widths = temp
|
298
|
-
|
299
|
-
def _create_prefix(self):
|
300
|
-
'''
|
301
|
-
先调用_update_sidebar_and_vlist
|
302
|
-
|
303
|
-
得到
|
304
|
-
+-------+
|
305
|
-
| x \ y |
|
306
|
-
+-------+
|
307
|
-
| 1 |
|
308
|
-
| 2 |
|
309
|
-
| 3 |
|
310
|
-
+-------+
|
311
|
-
'''
|
312
|
-
front, behind, row_sidebar = self.row_name, self.col_name, self.row_sidebar
|
313
|
-
|
314
|
-
|
315
|
-
results = []
|
316
|
-
|
317
|
-
title = front + " \ " + behind
|
318
|
-
n = self._get_maxlength_from_list(row_sidebar)
|
319
|
-
target_length = max(n, self._get_width(title))
|
320
|
-
|
321
|
-
pre_dash, suf_dash = self._get_prefix_suffix("-", target_length, '-')
|
322
|
-
str_dash = "+-" + pre_dash + "-" + suf_dash + "-+"
|
323
|
-
results.append(str_dash)
|
324
|
-
|
325
|
-
pre_space, suf_space = self._get_prefix_suffix(title, target_length, ' ')
|
326
|
-
str_index = "| " + pre_space + title + suf_space + " |"
|
327
|
-
results.append(str_index)
|
328
|
-
results.append(str_dash)
|
329
|
-
|
330
|
-
for x in row_sidebar:
|
331
|
-
pre_space, suf_space = self._get_prefix_suffix(x, target_length, ' ')
|
332
|
-
str_number = "| " + pre_space + x + suf_space + " |"
|
333
|
-
results.append(str_number)
|
334
|
-
results.append(str_dash)
|
335
|
-
|
336
|
-
return results
|
337
|
-
|
338
|
-
def _get_prefix_suffix(self, string, length, charactor=' '):
|
339
|
-
prefix = ''
|
340
|
-
suffix = ''
|
341
|
-
str_len = self._get_width(string)
|
342
|
-
|
343
|
-
delta = length - str_len
|
344
|
-
if delta < 0:
|
345
|
-
assert "string的宽度比length宽"
|
346
|
-
elif delta == 0:
|
347
|
-
pass
|
348
|
-
else:
|
349
|
-
prefix = charactor * math.floor(delta / 2)
|
350
|
-
suffix = charactor * math.ceil(delta / 2)
|
351
|
-
|
352
|
-
return prefix, suffix
|
353
|
-
|
354
|
-
def _get_maxlength_from_list(self, lst: List[str]) -> int:
|
355
|
-
temp = [self._get_width(x) for x in lst]
|
356
|
-
if len(temp) == 0:
|
357
|
-
return 0
|
358
|
-
else:
|
359
|
-
return max(temp)
|
360
|
-
|
361
|
-
def _get_width(self, string):
|
362
|
-
return wcswidth(string)
|
363
|
-
|
364
|
-
def _need_transpose(self):
|
365
|
-
if self.auto_adaptive:
|
366
|
-
self._update_all()
|
367
|
-
|
368
|
-
normal_widths = [self._get_width(x) for x in self.col_sidebar]
|
369
|
-
for i in range(len(self.verbose_lists)):
|
370
|
-
for j in range(len(self.verbose_lists[i])):
|
371
|
-
normal_widths[j] = max(normal_widths[j], self._get_width(self.verbose_lists[i][j]))
|
372
|
-
|
373
|
-
transpose_widths = [self._get_width(x) for x in self.row_sidebar]
|
374
|
-
for i in range(len(self.verbose_lists)):
|
375
|
-
for j in range(len(self.verbose_lists[i])):
|
376
|
-
transpose_widths[i] = max(transpose_widths[i], self._get_width(self.verbose_lists[i][j]))
|
377
|
-
|
378
|
-
if sum(normal_widths) > sum(transpose_widths):
|
379
|
-
return True
|
380
|
-
return False
|
381
|
-
return False
|
382
|
-
|
383
|
-
|
384
|
-
def __len__(self):
|
385
|
-
return len(self.row_sidebar) * len(self.col_sidebar)
|
386
|
-
def __contains__(self, item):
|
387
|
-
item = str(item)
|
388
|
-
for x in self.row_sidebar:
|
389
|
-
for y in self.col_sidebar:
|
390
|
-
if item == self[x][y]:
|
391
|
-
return True
|
392
|
-
return False
|
393
|
-
def __str__(self):
|
394
|
-
return self.to_str()
|
395
|
-
|
396
|
-
def __getitem__(self, index):
|
397
|
-
index = str(index)
|
398
|
-
return self.lists[index]
|
399
|
-
def __setitem__(self, index, value:InnerList):
|
400
|
-
index = str(index)
|
401
|
-
self.lists[index] = value.copy()
|
402
|
-
|
403
|
-
|
404
|
-
# if __name__ == '__main__':
|
405
|
-
# my_table = BAutoTable("x", "y", auto_adaptive=True)
|
406
|
-
#
|
407
|
-
# my_table[1][3] = "w"
|
408
|
-
#
|
409
|
-
# my_table[2][2] = "b"
|
410
|
-
# my_table[1][5] = "a"
|
411
|
-
# my_table[3][5] = "b"
|
412
|
-
#
|
413
|
-
# print(my_table)
|
414
|
-
|
415
|
-
if __name__ == '__main__':
|
416
|
-
my_table = B_AutoTable("x", "y", auto_adaptive=False)
|
417
|
-
|
418
|
-
my_table[1][3] = "w"
|
419
|
-
|
420
|
-
my_table[2][2] = "b"
|
421
|
-
my_table[1][5] = "a"
|
422
|
-
my_table["wq"]["dw"] = "adawd"
|
423
|
-
my_table["wqdq"]["ddasfsaddw"] = "adawd"
|
424
|
-
|
425
|
-
print(my_table)
|
426
|
-
for x, y, value in my_table.items():
|
427
|
-
print(x, y, value)
|
byzh_core/obsolete/row_table.py
DELETED
@@ -1,175 +0,0 @@
|
|
1
|
-
import copy
|
2
|
-
import math
|
3
|
-
from typing import List, Tuple, Union
|
4
|
-
Seq = Union[List, Tuple]
|
5
|
-
try:
|
6
|
-
from wcwidth import wcswidth
|
7
|
-
except ImportError:
|
8
|
-
print("[text table] 请先安装wcwidth库: pip install wcwidth")
|
9
|
-
|
10
|
-
class B_RowTable:
|
11
|
-
def __init__(self, head: Seq):
|
12
|
-
self.head = self._seq2strlist(head)
|
13
|
-
self.rows = []
|
14
|
-
self._widths = []
|
15
|
-
self._update_widths(self.head)
|
16
|
-
|
17
|
-
def append(self, sequence: Seq):
|
18
|
-
assert len(sequence) == len(self.head), "添加的 序列元素个数 与 head元素个数 不一致"
|
19
|
-
self.rows.append(self._seq2strlist(sequence))
|
20
|
-
def insert(self, index, sequence: Seq):
|
21
|
-
assert len(sequence) == len(self.head), "插入的 序列元素个数 与 head元素个数 不一致"
|
22
|
-
self.rows.insert(index, self._seq2strlist(sequence))
|
23
|
-
def remove(self, sequence: Seq):
|
24
|
-
self.rows.remove(self._seq2strlist(sequence))
|
25
|
-
def pop(self, index=-1):
|
26
|
-
return self.rows.pop(index)
|
27
|
-
|
28
|
-
def get_table_by_strs(self) -> List[str]:
|
29
|
-
results = self._create_prefix()
|
30
|
-
|
31
|
-
for x in self.rows:
|
32
|
-
self._update_widths(x)
|
33
|
-
|
34
|
-
str_dash = ''
|
35
|
-
str_head = ''
|
36
|
-
for i, x in enumerate(self.head):
|
37
|
-
pre_space, suf_space = self._get_prefix_suffix(x, self._widths[i], ' ')
|
38
|
-
pre_dash, suf_dash = self._get_prefix_suffix('-', self._widths[i], '-')
|
39
|
-
str_head += ' ' + pre_space + x + suf_space + ' |'
|
40
|
-
str_dash += '-' + pre_dash + '-' + suf_dash + '-+'
|
41
|
-
results[0] += str_dash
|
42
|
-
results[1] += str_head
|
43
|
-
results[2] += str_dash
|
44
|
-
|
45
|
-
offset = 3
|
46
|
-
for i, row in enumerate(self.rows):
|
47
|
-
for j, x in enumerate(row):
|
48
|
-
pre_space, suf_space = self._get_prefix_suffix(x, self._widths[j], ' ')
|
49
|
-
str_content = ' ' + pre_space + x + suf_space + ' |'
|
50
|
-
results[i+offset] += str_content
|
51
|
-
|
52
|
-
results[-1] += str_dash
|
53
|
-
|
54
|
-
return results
|
55
|
-
|
56
|
-
def get_table_by_str(self) -> str:
|
57
|
-
result = ""
|
58
|
-
strs = self.get_table_by_strs()
|
59
|
-
for x in strs[:-1]:
|
60
|
-
result += x + '\n'
|
61
|
-
result += strs[-1]
|
62
|
-
|
63
|
-
return result
|
64
|
-
|
65
|
-
def print_table(self):
|
66
|
-
print(self.get_table_by_str())
|
67
|
-
|
68
|
-
def _create_prefix(self):
|
69
|
-
'''
|
70
|
-
得到
|
71
|
-
+-----+
|
72
|
-
| num |
|
73
|
-
+-----+
|
74
|
-
| 1 |
|
75
|
-
| 2 |
|
76
|
-
| 3 |
|
77
|
-
+-----+
|
78
|
-
'''
|
79
|
-
results = []
|
80
|
-
# 编号的位数
|
81
|
-
n = self._get_width(str(len(self.rows)))
|
82
|
-
length = max(n, self._get_width("index"))
|
83
|
-
|
84
|
-
pre_dash, suf_dash = self._get_prefix_suffix("-", length, '-')
|
85
|
-
str_dash = "+-" + pre_dash + "-" + suf_dash + "-+"
|
86
|
-
results.append(str_dash)
|
87
|
-
|
88
|
-
pre_space, suf_space = self._get_prefix_suffix("index", length, ' ')
|
89
|
-
str_index = "| " + pre_space + "index" + suf_space + " |"
|
90
|
-
results.append(str_index)
|
91
|
-
results.append(str_dash)
|
92
|
-
|
93
|
-
for i in range(len(self.rows)):
|
94
|
-
number = str(i)
|
95
|
-
pre_space, suf_space = self._get_prefix_suffix(number, length, ' ')
|
96
|
-
str_number = "| " + pre_space + number + suf_space + " |"
|
97
|
-
results.append(str_number)
|
98
|
-
results.append(str_dash)
|
99
|
-
|
100
|
-
return results
|
101
|
-
|
102
|
-
def _get_prefix_suffix(self, string, length, charactor=' '):
|
103
|
-
prefix = ''
|
104
|
-
suffix = ''
|
105
|
-
str_len = self._get_width(string)
|
106
|
-
|
107
|
-
delta = length - str_len
|
108
|
-
if delta < 0:
|
109
|
-
assert "string的宽度比length宽"
|
110
|
-
elif delta == 0:
|
111
|
-
pass
|
112
|
-
else:
|
113
|
-
prefix = charactor * math.floor(delta / 2)
|
114
|
-
suffix = charactor * math.ceil(delta / 2)
|
115
|
-
|
116
|
-
return prefix, suffix
|
117
|
-
|
118
|
-
def _update_widths(self, seq):
|
119
|
-
temps = []
|
120
|
-
for x in seq:
|
121
|
-
temps.append(self._get_width(x))
|
122
|
-
|
123
|
-
if len(self._widths) == 0:
|
124
|
-
self._widths = temps
|
125
|
-
else:
|
126
|
-
for i, x in enumerate(temps):
|
127
|
-
if x > self._widths[i]:
|
128
|
-
self._widths[i] = x
|
129
|
-
|
130
|
-
def _seq2strlist(self, seq):
|
131
|
-
seq = copy.deepcopy(list(seq))
|
132
|
-
for i, x in enumerate(seq):
|
133
|
-
seq[i] = str(x)
|
134
|
-
|
135
|
-
return seq
|
136
|
-
|
137
|
-
def _get_width(self, string):
|
138
|
-
return wcswidth(string)
|
139
|
-
|
140
|
-
def __len__(self):
|
141
|
-
return len(self.rows)
|
142
|
-
def __str__(self):
|
143
|
-
return self.get_table_by_str()
|
144
|
-
def __getitem__(self, index):
|
145
|
-
return self.rows[index]
|
146
|
-
def __setitem__(self, index, value):
|
147
|
-
self.rows[index] = self._seq2strlist(value)
|
148
|
-
def __contains__(self, item):
|
149
|
-
for row in self.rows:
|
150
|
-
for x in row:
|
151
|
-
if str(item) == x:
|
152
|
-
return True
|
153
|
-
return False
|
154
|
-
|
155
|
-
if __name__ == '__main__':
|
156
|
-
my_table = B_RowTable(['云编号', '名称', 'IP地址', 'aaaabbbbcccc'])
|
157
|
-
my_table.append(["server01", "服务器01", "172.16.0.1", 'aacc'])
|
158
|
-
my_table.append(["server02", "服务器02", "172.16.0.2", 'aacc'])
|
159
|
-
my_table.append(["server03", "服务器03", "172.16.0.3", 'aacc'])
|
160
|
-
my_table.append(["server04", "服务器04", "172.16.0.4", 'aacc'])
|
161
|
-
my_table.append(["server05", "服务器05", "172.16.0.5", 'aacc'])
|
162
|
-
my_table.append(["server06", "服务器06", "172.16.0.6", 'aacc'])
|
163
|
-
my_table.append(["server07", "服务器07", "172.16.0.7", 'aacc'])
|
164
|
-
my_table.append(["server08", "服务器08", "172.16.0.8", 'aacc'])
|
165
|
-
my_table.append(["server09", "服务器09", "172.16.0.9", 'aacc'])
|
166
|
-
my_table.append(["server10", "服务器10", "172.16.0.0", 'aacc'])
|
167
|
-
my_table.append(["server11", "服务器11", "172.16.0.1", 'aacc'])
|
168
|
-
|
169
|
-
my_table.remove(["server08", "服务器08", "172.16.0.8", 'aacc'])
|
170
|
-
|
171
|
-
my_table[4] = ["server99", "服务器99", "172.16.0.9", '99999']
|
172
|
-
print(my_table)
|
173
|
-
print(len(my_table))
|
174
|
-
|
175
|
-
print("服务器07" in my_table)
|