byzh-core 0.0.2.3__py3-none-any.whl → 0.0.2.5__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/B_os.py +26 -0
- byzh_core/Btable/__init__.py +2 -4
- byzh_core/Btable/auto_table.py +342 -373
- byzh_core/Btable/obsolete/__init__.py +6 -0
- byzh_core/Btable/obsolete/auto_table.py +427 -0
- byzh_core/__init__.py +1 -1
- {byzh_core-0.0.2.3.dist-info → byzh_core-0.0.2.5.dist-info}/METADATA +1 -1
- {byzh_core-0.0.2.3.dist-info → byzh_core-0.0.2.5.dist-info}/RECORD +13 -13
- byzh_core/Btable/obsolete/b_auto_table.py +0 -321
- /byzh_core/Btable/{row_table.py → obsolete/row_table.py} +0 -0
- /byzh_core/Btable/{xy_table.py → obsolete/xy_table.py} +0 -0
- {byzh_core-0.0.2.3.dist-info → byzh_core-0.0.2.5.dist-info}/LICENSE +0 -0
- {byzh_core-0.0.2.3.dist-info → byzh_core-0.0.2.5.dist-info}/WHEEL +0 -0
- {byzh_core-0.0.2.3.dist-info → byzh_core-0.0.2.5.dist-info}/top_level.txt +0 -0
byzh_core/Btable/auto_table.py
CHANGED
@@ -9,419 +9,388 @@ try:
|
|
9
9
|
except ImportError:
|
10
10
|
raise ImportError("[table] 请先安装wcwidth库: pip install wcwidth")
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
def get_width(string):
|
13
|
+
return wcswidth(string)
|
14
|
+
|
15
|
+
def get_maxwidth_2d(matrix):
|
16
|
+
widths = [0 for _ in range(len(matrix[0]))]
|
17
|
+
for row in matrix:
|
18
|
+
for i, element in enumerate(row):
|
19
|
+
if element is None:
|
20
|
+
element = ' '
|
21
|
+
widths[i] = max(widths[i], get_width(element))
|
22
|
+
|
23
|
+
diff_matrix = [[0 for _ in range(len(matrix[0]))] for _ in range(len(matrix))]
|
24
|
+
for i in range(len(matrix)):
|
25
|
+
for j in range(len(matrix[0])):
|
26
|
+
element = matrix[i][j]
|
27
|
+
if element is None:
|
28
|
+
element = ' '
|
29
|
+
diff_matrix[i][j] = widths[j] - get_width(element)
|
30
|
+
|
31
|
+
return widths, diff_matrix
|
32
|
+
|
33
|
+
def get_T_matrix(matrix):
|
34
|
+
new_matrix = [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))]
|
35
|
+
return new_matrix
|
36
|
+
|
37
|
+
def strs2matrix(str_lst):
|
38
|
+
str_lst = [x.strip() for x in str_lst if len(x.strip()) > 0]
|
39
|
+
str_lst.pop(-1)
|
40
|
+
str_lst.pop(2)
|
41
|
+
dividing_line = str_lst.pop(0)
|
42
|
+
indexes = [i for i, c in enumerate(dividing_line) if c == '+']
|
43
|
+
row_cnt = len(str_lst)
|
44
|
+
col_cnt = len(indexes) - 1
|
45
|
+
|
46
|
+
|
47
|
+
matrix = []
|
48
|
+
for i in range(row_cnt):
|
49
|
+
row = str_lst[i].split('|')[1:-1]
|
50
|
+
for j in range(col_cnt):
|
51
|
+
row[j] = row[j].strip()
|
52
|
+
matrix.append(row)
|
53
|
+
|
54
|
+
row_name, col_name = matrix[0][0].split(' \\ ')
|
55
|
+
|
56
|
+
return matrix, row_name, col_name
|
57
|
+
|
58
|
+
|
59
|
+
class Matrix2d:
|
60
|
+
def __init__(self, row_name='x', col_name='y'):
|
61
|
+
self.row_name = row_name
|
62
|
+
self.col_name = col_name
|
63
|
+
self.matrix = [[row_name + ' | ' + col_name]]
|
64
|
+
|
65
|
+
def get_rows(self): # 包含left_upper
|
66
|
+
if self.matrix == [[]]:
|
67
|
+
return []
|
68
|
+
return [x[0] for x in self.matrix]
|
69
|
+
def get_cols(self): # 包含left_upper
|
70
|
+
return self.matrix[0]
|
71
|
+
def set_by_str(self, row, col, value):
|
72
|
+
row, col, value = str(row), str(col), str(value)
|
73
|
+
|
74
|
+
rows = self.get_rows()
|
75
|
+
cols = self.get_cols()
|
76
|
+
|
77
|
+
if row in rows:
|
78
|
+
row_index = rows.index(row)
|
79
|
+
else:
|
80
|
+
row_index = len(rows)
|
81
|
+
if col in cols:
|
82
|
+
col_index = cols.index(col)
|
83
|
+
else:
|
84
|
+
col_index = len(cols)
|
16
85
|
|
17
|
-
|
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
|
86
|
+
self.set_by_index(row_index, col_index, row, col, value)
|
40
87
|
|
88
|
+
def set_by_index(self, row_index, col_index, row, col, value):
|
89
|
+
row, col, value = str(row), str(col), str(value)
|
41
90
|
|
42
|
-
|
43
|
-
|
44
|
-
self.key_list = []
|
45
|
-
self.value_list = []
|
91
|
+
rows = self.get_rows()
|
92
|
+
cols = self.get_cols()
|
46
93
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
94
|
+
if row_index < len(rows):
|
95
|
+
pass
|
96
|
+
elif row_index == len(rows):
|
97
|
+
self.matrix.append([row] + [None] * (len(cols) - 1))
|
101
98
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
99
|
+
if col_index < len(cols):
|
100
|
+
pass
|
101
|
+
elif col_index == len(cols):
|
102
|
+
self.matrix[0].append(col)
|
103
|
+
for i in range(1, len(self.matrix)):
|
104
|
+
self.matrix[i].append(None)
|
105
|
+
|
106
|
+
self.matrix[row_index][col_index] = value
|
107
|
+
|
108
|
+
def get_by_str(self, row, col):
|
109
|
+
row, col = str(row), str(col)
|
110
|
+
|
111
|
+
rows = self.get_rows()
|
112
|
+
cols = self.get_cols()
|
113
|
+
if row in rows and col in cols:
|
114
|
+
row_index = rows.index(row)
|
115
|
+
col_index = cols.index(col)
|
116
|
+
return self.get_by_index(row_index, col_index)
|
116
117
|
else:
|
117
|
-
|
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('|')]
|
118
|
+
raise ValueError(f"row {row} or col {col} not found in table")
|
132
119
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
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]
|
120
|
+
def get_by_index(self, row_index, col_index):
|
121
|
+
rows = self.get_rows()
|
122
|
+
cols = self.get_cols()
|
123
|
+
if row_index < len(rows) and col_index < len(cols):
|
124
|
+
return self.matrix[row_index][col_index]
|
155
125
|
else:
|
156
|
-
|
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())
|
126
|
+
raise ValueError(f"row_index {row_index} or col_index {col_index} out of range")
|
176
127
|
|
177
|
-
def
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
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]
|
128
|
+
def T(self):
|
129
|
+
new_matrix = get_T_matrix(self.matrix)
|
130
|
+
new_left_upper = self.col_name + ' \\ ' + self.row_name
|
131
|
+
new_matrix[0][0] = new_left_upper
|
132
|
+
|
133
|
+
self.row_name, self.col_name = self.col_name, self.row_name
|
231
134
|
|
135
|
+
self.matrix = new_matrix
|
136
|
+
|
137
|
+
return self
|
138
|
+
def __str__(self):
|
139
|
+
result = ''
|
140
|
+
for x in self.matrix:
|
141
|
+
result += str(x) + '\n'
|
232
142
|
return result
|
233
143
|
|
234
|
-
def _merge_lists(self, lists_base, lists_new):
|
235
|
-
result = OuterList()
|
236
144
|
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
for
|
242
|
-
|
145
|
+
class Dict1d:
|
146
|
+
def __init__(self, dictionary=None):
|
147
|
+
self.dict = {}
|
148
|
+
if dictionary:
|
149
|
+
for key, value in dictionary.items():
|
150
|
+
self[key] = value
|
243
151
|
|
244
|
-
|
152
|
+
def __getitem__(self, key):
|
153
|
+
key = str(key)
|
154
|
+
return self.dict[key]
|
155
|
+
def __setitem__(self, key, value): # 用于最底层赋值
|
156
|
+
key, value = str(key), str(value)
|
157
|
+
self.dict[key] = value
|
158
|
+
def set(self, key, value): # 用于非底层赋值(防止把子dict变为str)
|
159
|
+
key = str(key)
|
160
|
+
self.dict[key] = value
|
245
161
|
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
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):
|
162
|
+
|
163
|
+
def keys(self):
|
164
|
+
return self.dict.keys()
|
165
|
+
|
166
|
+
def values(self):
|
167
|
+
return self.dict.values()
|
168
|
+
|
169
|
+
def items(self):
|
170
|
+
return self.dict.items()
|
171
|
+
|
172
|
+
def copy(self):
|
173
|
+
return copy.deepcopy(self)
|
174
|
+
|
175
|
+
def __str__(self):
|
176
|
+
return str(self.dict)
|
177
|
+
|
178
|
+
def __repr__(self):
|
179
|
+
return str(self.dict)
|
180
|
+
|
181
|
+
class Dict2d:
|
182
|
+
def __init__(self):
|
183
|
+
self.dict = Dict1d()
|
184
|
+
|
185
|
+
# 先实现setitem,再实现getitem
|
186
|
+
def __getitem__(self, key):
|
187
|
+
key = str(key)
|
188
|
+
if key not in self.dict.keys():
|
189
|
+
self[key] = Dict1d()
|
190
|
+
return self.dict[key]
|
191
|
+
def __setitem__(self, key, new_dict: Dict1d | dict):
|
192
|
+
key = str(key)
|
193
|
+
self.dict.set(key, Dict1d(new_dict))
|
194
|
+
def __str__(self):
|
195
|
+
return str(self.dict)
|
196
|
+
|
197
|
+
class B_Table2d:
|
198
|
+
def __init__(self, row_name='x', col_name='y', auto_adaptive=False, narrow_better=True):
|
287
199
|
'''
|
288
|
-
|
289
|
-
:
|
200
|
+
|
201
|
+
:param row_name:
|
202
|
+
:param col_name:
|
203
|
+
:param auto_adaptive: 是否采取自动翻转的策略,来提升显示效果
|
204
|
+
:param narrow_better: 在auto_adaptive=True的情况下,是否优先选择横向窄的表格,避免换行显示
|
290
205
|
'''
|
291
|
-
|
206
|
+
self.row_name = row_name
|
207
|
+
self.col_name = col_name
|
208
|
+
self.auto_adaptive = auto_adaptive
|
209
|
+
self.narrow_better = narrow_better
|
292
210
|
|
293
|
-
|
294
|
-
|
295
|
-
temp[j] = max(temp[j], self._get_width(self.verbose_lists[i][j]))
|
211
|
+
self.matrix2d = Matrix2d(row_name, col_name) # 用于输出
|
212
|
+
self.dict2d = Dict2d() # 用于输入
|
296
213
|
|
297
|
-
|
214
|
+
# 判断是否改变
|
215
|
+
self.is_changed = False
|
298
216
|
|
299
|
-
def
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
217
|
+
def __getitem__(self, row):
|
218
|
+
self.is_changed = True
|
219
|
+
|
220
|
+
row = str(row)
|
221
|
+
return self.dict2d[row]
|
222
|
+
|
223
|
+
def __setitem__(self, row, new_dict):
|
224
|
+
self.is_changed = True
|
225
|
+
|
226
|
+
row = str(row)
|
227
|
+
self.dict2d[row] = new_dict
|
228
|
+
|
229
|
+
def from_txt(self, txt_path, use_txt_name:bool=False):
|
311
230
|
'''
|
312
|
-
front, behind, row_sidebar = self.row_name, self.col_name, self.row_sidebar
|
313
231
|
|
232
|
+
:param txt_path:
|
233
|
+
:param use_txt_name: 默认使用init时的row_name和col_name
|
234
|
+
:return:
|
235
|
+
'''
|
236
|
+
with open(txt_path, 'r', encoding='utf-8') as f:
|
237
|
+
lines = f.readlines()
|
238
|
+
matrix, row_name, col_name = strs2matrix(lines)
|
239
|
+
if {self.row_name, self.col_name} != {row_name, col_name}:
|
240
|
+
raise ValueError(f"行头列头正反均不对应,请检查: [{self.row_name}, {self.col_name}] ❌ [{row_name}, {col_name}]")
|
241
|
+
|
242
|
+
if use_txt_name:
|
243
|
+
self.row_name = row_name
|
244
|
+
self.col_name = col_name
|
245
|
+
self.matrix2d = Matrix2d(self.row_name, self.col_name)
|
246
|
+
self.matrix2d.matrix = matrix
|
247
|
+
else:
|
248
|
+
left_upper = self.row_name + ' \\ ' + self.col_name
|
249
|
+
matrix[0][0] = left_upper
|
250
|
+
if self.row_name == row_name and self.col_name == col_name:
|
251
|
+
self.matrix2d = Matrix2d(self.row_name, self.col_name)
|
252
|
+
self.matrix2d.matrix = matrix
|
253
|
+
elif self.row_name == col_name and self.col_name == row_name:
|
254
|
+
self.matrix2d = Matrix2d(self.col_name, self.row_name)
|
255
|
+
self.matrix2d.matrix = get_T_matrix(matrix)
|
256
|
+
|
257
|
+
self.__matrix2dict()
|
258
|
+
|
259
|
+
def to_txt(self, txt_path):
|
260
|
+
with open(txt_path, 'w', encoding='utf-8') as f:
|
261
|
+
f.write(str(self))
|
262
|
+
|
263
|
+
def to_strs(self):
|
264
|
+
if self.is_changed:
|
265
|
+
self.__dict2matrix()
|
266
|
+
if self.__if_need_T():
|
267
|
+
self.T()
|
268
|
+
|
269
|
+
result = []
|
270
|
+
|
271
|
+
# 计算宽度
|
272
|
+
widths, diff_matrix = get_maxwidth_2d(self.matrix2d.matrix)
|
273
|
+
|
274
|
+
for i in range(len(self.matrix2d.matrix)):
|
275
|
+
elements = []
|
276
|
+
for j in range(len(self.matrix2d.matrix[i])):
|
277
|
+
element = self.matrix2d.matrix[i][j]
|
278
|
+
if element is None:
|
279
|
+
element = ' '
|
280
|
+
diff = diff_matrix[i][j]
|
281
|
+
diff_left = diff // 2
|
282
|
+
diff_right = diff - diff_left
|
283
|
+
element = ' '*diff_left + str(element) + ' '*diff_right
|
284
|
+
elements.append(element)
|
285
|
+
row = ' | '.join(elements)
|
286
|
+
row = '| ' + row + ' |'
|
287
|
+
result.append(row)
|
288
|
+
|
289
|
+
# 添加三行分隔线
|
290
|
+
dividing_line = '+' + '+'.join(['-' * (widths[i] + 2) for i in range(len(widths))]) + '+'
|
291
|
+
result.append(dividing_line)
|
292
|
+
result.insert(1, dividing_line)
|
293
|
+
result.insert(0, dividing_line)
|
314
294
|
|
315
|
-
|
295
|
+
return result
|
316
296
|
|
317
|
-
|
318
|
-
n
|
319
|
-
target_length = max(n, self._get_width(title))
|
297
|
+
def to_str(self):
|
298
|
+
return '\n'.join(self.to_strs())
|
320
299
|
|
321
|
-
|
322
|
-
|
323
|
-
results.append(str_dash)
|
300
|
+
def __str__(self):
|
301
|
+
return self.to_str()
|
324
302
|
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
results.append(str_dash)
|
303
|
+
def __repr__(self):
|
304
|
+
if self.is_changed:
|
305
|
+
self.__dict2matrix()
|
329
306
|
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
results.append(str_dash)
|
307
|
+
result = ''
|
308
|
+
for x in self.matrix2d.matrix:
|
309
|
+
result += str(x) + '\n'
|
310
|
+
return result
|
335
311
|
|
336
|
-
|
312
|
+
def T(self):
|
313
|
+
if self.is_changed:
|
314
|
+
self.__dict2matrix()
|
315
|
+
self.matrix2d.T()
|
316
|
+
self.row_name, self.col_name = self.col_name, self.row_name
|
317
|
+
self.__matrix2dict()
|
337
318
|
|
338
|
-
|
339
|
-
prefix = ''
|
340
|
-
suffix = ''
|
341
|
-
str_len = self._get_width(string)
|
319
|
+
return self
|
342
320
|
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
321
|
+
def __matrix2dict(self): # __
|
322
|
+
keys1 = self.matrix2d.get_rows()[1:]
|
323
|
+
keys2 = self.matrix2d.get_cols()[1:]
|
324
|
+
self.dict2d = Dict2d()
|
325
|
+
for i, key1 in enumerate(keys1):
|
326
|
+
for j, key2 in enumerate(keys2):
|
327
|
+
value = self.matrix2d.get_by_index(i+1, j+1)
|
328
|
+
self.dict2d[key1][key2] = value
|
351
329
|
|
352
|
-
|
330
|
+
self.is_changed = False
|
353
331
|
|
354
|
-
def
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
return max(temp)
|
332
|
+
def __dict2matrix(self): # __
|
333
|
+
left_upper = self.row_name + ' \\ ' + self.col_name
|
334
|
+
# 处理keys1和keys2
|
335
|
+
keys1 = [left_upper] + list(self.dict2d.dict.keys())
|
336
|
+
keys2 = [left_upper]
|
360
337
|
|
361
|
-
|
362
|
-
|
338
|
+
temp = []
|
339
|
+
for key1 in self.dict2d.dict.keys():
|
340
|
+
for key2 in self.dict2d.dict[key1].keys():
|
341
|
+
if key2 not in temp:
|
342
|
+
temp.append(key2)
|
343
|
+
|
344
|
+
keys2.extend(temp)
|
345
|
+
|
346
|
+
# 处理matrix框架
|
347
|
+
matrix = [keys2]
|
348
|
+
for x in keys1[1:]:
|
349
|
+
matrix.append([x] + [None] * (len(keys2) - 1))
|
350
|
+
|
351
|
+
# 构建matrix2d
|
352
|
+
self.matrix2d = Matrix2d(self.row_name, self.col_name)
|
353
|
+
self.matrix2d.matrix = matrix
|
354
|
+
for row in self.dict2d.dict.keys():
|
355
|
+
for col in self.dict2d.dict[row].keys():
|
356
|
+
value = self.dict2d.dict[row][col]
|
357
|
+
self.matrix2d.set_by_str(row, col, value)
|
358
|
+
|
359
|
+
self.is_changed = False
|
360
|
+
def __if_need_T(self):
|
361
|
+
'''
|
362
|
+
尽管T,但只是出来的str是T过的,并没有改变原来的矩阵,
|
363
|
+
所以赋值还是原来的字符索引
|
364
|
+
:return:
|
365
|
+
'''
|
366
|
+
if not self.auto_adaptive:
|
367
|
+
return False
|
363
368
|
|
364
|
-
|
365
|
-
|
366
|
-
self._update_all()
|
369
|
+
now_matrix2d = self.matrix2d
|
370
|
+
T_matrix2d = copy.deepcopy(now_matrix2d).T()
|
367
371
|
|
368
|
-
|
369
|
-
|
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
|
+
now_widths, _ = get_maxwidth_2d(now_matrix2d.matrix)
|
373
|
+
T_widths, _ = get_maxwidth_2d(T_matrix2d.matrix)
|
372
374
|
|
373
|
-
|
374
|
-
|
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]))
|
375
|
+
now_width = sum(now_widths)
|
376
|
+
T_width = sum(T_widths)
|
377
377
|
|
378
|
-
|
379
|
-
|
378
|
+
if T_width < now_width and self.narrow_better:
|
379
|
+
return True
|
380
|
+
elif T_width > now_width and not self.narrow_better:
|
381
|
+
return True
|
382
|
+
else:
|
380
383
|
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
384
|
|
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
385
|
|
415
386
|
if __name__ == '__main__':
|
416
|
-
|
417
|
-
|
418
|
-
|
387
|
+
a = B_Table2d(row_name='data', col_name='score', auto_adaptive=False)
|
388
|
+
# a[0][1] = 'a3'
|
389
|
+
# a[11][22] = '123'
|
390
|
+
# a[11][1] = 'dawd'
|
391
|
+
a.from_txt('./a.txt')
|
392
|
+
print(a)
|
393
|
+
# a.T()
|
394
|
+
# print(a)
|
419
395
|
|
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
396
|
|
425
|
-
print(my_table)
|
426
|
-
for x, y, value in my_table.items():
|
427
|
-
print(x, y, value)
|