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