byzh-core 0.0.2.3__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 +26 -0
- byzh_core/Btable/__init__.py +2 -4
- byzh_core/Btable/auto_table.py +344 -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.4.dist-info}/METADATA +1 -1
- {byzh_core-0.0.2.3.dist-info → byzh_core-0.0.2.4.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.4.dist-info}/LICENSE +0 -0
- {byzh_core-0.0.2.3.dist-info → byzh_core-0.0.2.4.dist-info}/WHEEL +0 -0
- {byzh_core-0.0.2.3.dist-info → byzh_core-0.0.2.4.dist-info}/top_level.txt +0 -0
@@ -1,321 +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 MyDict:
|
13
|
-
def __init__(self, init_dict = None):
|
14
|
-
self.dict = dict()
|
15
|
-
if type(init_dict) is dict:
|
16
|
-
self.dict = init_dict.copy()
|
17
|
-
if type(init_dict) is MyDict:
|
18
|
-
self.dict = init_dict.dict.copy()
|
19
|
-
|
20
|
-
|
21
|
-
def update(self, other_dict):
|
22
|
-
self.dict.update(other_dict)
|
23
|
-
|
24
|
-
def items(self):
|
25
|
-
return self.dict.items()
|
26
|
-
def keys(self):
|
27
|
-
return self.dict.keys()
|
28
|
-
def values(self):
|
29
|
-
return self.dict.values()
|
30
|
-
def copy(self):
|
31
|
-
return MyDict(self.dict.copy())
|
32
|
-
def __getitem__(self, item):
|
33
|
-
return self.dict[str(item)]
|
34
|
-
|
35
|
-
def __setitem__(self, key, value):
|
36
|
-
key, value = str(key), str(value)
|
37
|
-
|
38
|
-
if key not in self.dict.keys():
|
39
|
-
self.dict.update({key: ''})
|
40
|
-
|
41
|
-
self.dict[key] = value
|
42
|
-
|
43
|
-
class BAutoTable:
|
44
|
-
def __init__(self, x_name='x', y_name='y'):
|
45
|
-
self.x_sidebars = []
|
46
|
-
self.y_sidebars = []
|
47
|
-
self.x_name = x_name
|
48
|
-
self.y_name = y_name
|
49
|
-
self.dict = dict()
|
50
|
-
self._widths = dict()
|
51
|
-
|
52
|
-
def set(self, x_sidebar, y_sidebar, content):
|
53
|
-
x_sidebar, y_sidebar, content = str(x_sidebar), str(y_sidebar), str(content)
|
54
|
-
self[x_sidebar][y_sidebar] = str(content)
|
55
|
-
|
56
|
-
def get_str(self, x_sidebar, y_sidebar):
|
57
|
-
return self[x_sidebar][y_sidebar]
|
58
|
-
def get_int(self, x_sidebar, y_sidebar):
|
59
|
-
return int(self[x_sidebar][y_sidebar])
|
60
|
-
def get_float(self, x_sidebar, y_sidebar):
|
61
|
-
return float(self[x_sidebar][y_sidebar])
|
62
|
-
def get_bool(self, x_sidebar, y_sidebar):
|
63
|
-
temp = self[x_sidebar][y_sidebar]
|
64
|
-
if temp == "True" or temp == "1":
|
65
|
-
return True
|
66
|
-
else:
|
67
|
-
return False
|
68
|
-
def items(self):
|
69
|
-
'''
|
70
|
-
(key1, key2, value)
|
71
|
-
'''
|
72
|
-
result = [(x, y, self.dict[x][y]) for x in self.x_sidebars for y in self.y_sidebars]
|
73
|
-
return result
|
74
|
-
def copy_row(self, old_row:str, new_row:str):
|
75
|
-
old_row, new_row = str(old_row), str(new_row)
|
76
|
-
self[new_row] = self[old_row]
|
77
|
-
def read_txt(self, path):
|
78
|
-
with open(path, 'r') as f:
|
79
|
-
lines = f.readlines()
|
80
|
-
lines = [x.strip() for x in lines if x.startswith('|')]
|
81
|
-
|
82
|
-
temp = []
|
83
|
-
for string in lines:
|
84
|
-
elements = string.split('|')[1:-1]
|
85
|
-
elements = [x.strip() for x in elements]
|
86
|
-
temp.append(elements)
|
87
|
-
|
88
|
-
x_name, y_name = temp[0][0].split(' \\ ') if ('\\' in temp[0][0]) else ("x", "y")
|
89
|
-
x_keys = [var[0] for var in temp[1:]]
|
90
|
-
y_keys = temp[0][1:]
|
91
|
-
|
92
|
-
self.x_sidebars = []
|
93
|
-
self.y_sidebars = []
|
94
|
-
self.x_name = x_name
|
95
|
-
self.y_name = y_name
|
96
|
-
self.dict = dict()
|
97
|
-
self._widths = dict()
|
98
|
-
|
99
|
-
for i, x_element in enumerate(x_keys):
|
100
|
-
y_dict = MyDict()
|
101
|
-
for j, y_element in enumerate(y_keys):
|
102
|
-
y_dict.update({y_element: temp[i+1][j+1]})
|
103
|
-
self.dict.update({x_element: y_dict})
|
104
|
-
self._update_sidebars()
|
105
|
-
|
106
|
-
def to_txt(self, path):
|
107
|
-
'''
|
108
|
-
将表格内容写入文件
|
109
|
-
:param path:
|
110
|
-
:return:
|
111
|
-
'''
|
112
|
-
dir = Path(path).resolve().parent
|
113
|
-
os.makedirs(dir, exist_ok=True)
|
114
|
-
with open(path, 'w') as f:
|
115
|
-
f.write(self.get_table_by_str())
|
116
|
-
|
117
|
-
def update_txt(self, path):
|
118
|
-
'''
|
119
|
-
更新表格内容\n
|
120
|
-
如果文件不存在,则创建文件
|
121
|
-
:param path:
|
122
|
-
:return:
|
123
|
-
'''
|
124
|
-
# 是否存在该文件
|
125
|
-
if not os.path.exists(path):
|
126
|
-
self.to_txt(path)
|
127
|
-
else:
|
128
|
-
new_dict = self.dict
|
129
|
-
self.read_txt(path)
|
130
|
-
origin_dict = self.dict
|
131
|
-
self.dict = self._merge_2d_dicts(origin_dict, new_dict)
|
132
|
-
self._update_sidebars()
|
133
|
-
|
134
|
-
self.to_txt(path)
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
def get_table_by_strs(self) -> List[str]:
|
139
|
-
results = self._create_prefix()
|
140
|
-
|
141
|
-
self._update_widths()
|
142
|
-
|
143
|
-
str_dash = ''
|
144
|
-
str_head = ''
|
145
|
-
for y in self.y_sidebars:
|
146
|
-
pre_space, suf_space = self._get_prefix_suffix(y, self._widths[y], ' ')
|
147
|
-
pre_dash, suf_dash = self._get_prefix_suffix('-', self._widths[y], '-')
|
148
|
-
str_head += ' ' + pre_space + y + suf_space + ' |'
|
149
|
-
str_dash += '-' + pre_dash + '-' + suf_dash + '-+'
|
150
|
-
results[0] += str_dash
|
151
|
-
results[1] += str_head
|
152
|
-
results[2] += str_dash
|
153
|
-
|
154
|
-
offset = 3
|
155
|
-
for index, y_dicts in enumerate(self.dict.values()):
|
156
|
-
for key in self.y_sidebars:
|
157
|
-
value = y_dicts[key] if key in y_dicts.keys() else ''
|
158
|
-
pre_space, suf_space = self._get_prefix_suffix(value, self._widths[key], ' ')
|
159
|
-
str_content = ' ' + pre_space + value + suf_space + ' |'
|
160
|
-
results[index+offset] += str_content
|
161
|
-
|
162
|
-
results[-1] += str_dash
|
163
|
-
|
164
|
-
return results
|
165
|
-
|
166
|
-
def get_table_by_str(self) -> str:
|
167
|
-
result = ""
|
168
|
-
strs = self.get_table_by_strs()
|
169
|
-
for x in strs[:-1]:
|
170
|
-
result += x + '\n'
|
171
|
-
result += strs[-1]
|
172
|
-
|
173
|
-
return result
|
174
|
-
def print_table(self):
|
175
|
-
print(self.get_table_by_str())
|
176
|
-
|
177
|
-
def _merge_2d_dicts(self, origin_dict, new_dict):
|
178
|
-
"""
|
179
|
-
合并两个二维字典。
|
180
|
-
如果两个字典的相同键存在重叠的子键,则 new_dict 的值覆盖 origin_dict。
|
181
|
-
"""
|
182
|
-
merged_dict = {key: value.copy() for key, value in origin_dict.items()} # 复制 origin_dict 避免修改原数据
|
183
|
-
for key, sub_dict in new_dict.items():
|
184
|
-
if key in merged_dict.keys():
|
185
|
-
merged_dict[key].update(sub_dict) # 合并子字典
|
186
|
-
else:
|
187
|
-
merged_dict[key] = sub_dict # 直接添加新键
|
188
|
-
|
189
|
-
return merged_dict
|
190
|
-
def _update_sidebars(self):
|
191
|
-
self.x_sidebars = list(self.dict.keys())
|
192
|
-
|
193
|
-
temp = []
|
194
|
-
for dict_y in self.dict.values():
|
195
|
-
for key in dict_y.keys():
|
196
|
-
if key not in temp:
|
197
|
-
temp.append(key)
|
198
|
-
self.y_sidebars = temp
|
199
|
-
|
200
|
-
def _create_prefix(self):
|
201
|
-
'''
|
202
|
-
得到
|
203
|
-
+-------+
|
204
|
-
| x \ y |
|
205
|
-
+-------+
|
206
|
-
| 1 |
|
207
|
-
| 2 |
|
208
|
-
| 3 |
|
209
|
-
+-------+
|
210
|
-
'''
|
211
|
-
results = []
|
212
|
-
|
213
|
-
title = self.x_name + " \ " + self.y_name
|
214
|
-
self._update_sidebars()
|
215
|
-
n = self._get_maxlength_from_list(self.x_sidebars)
|
216
|
-
length = max(n, self._get_width(title))
|
217
|
-
|
218
|
-
pre_dash, suf_dash = self._get_prefix_suffix("-", length, '-')
|
219
|
-
str_dash = "+-" + pre_dash + "-" + suf_dash + "-+"
|
220
|
-
results.append(str_dash)
|
221
|
-
|
222
|
-
pre_space, suf_space = self._get_prefix_suffix(title, length, ' ')
|
223
|
-
str_index = "| " + pre_space + title + suf_space + " |"
|
224
|
-
results.append(str_index)
|
225
|
-
results.append(str_dash)
|
226
|
-
|
227
|
-
for x in self.x_sidebars:
|
228
|
-
pre_space, suf_space = self._get_prefix_suffix(x, length, ' ')
|
229
|
-
str_number = "| " + pre_space + x + suf_space + " |"
|
230
|
-
results.append(str_number)
|
231
|
-
results.append(str_dash)
|
232
|
-
|
233
|
-
return results
|
234
|
-
|
235
|
-
def _get_prefix_suffix(self, string, length, charactor=' '):
|
236
|
-
prefix = ''
|
237
|
-
suffix = ''
|
238
|
-
str_len = self._get_width(string)
|
239
|
-
|
240
|
-
delta = length - str_len
|
241
|
-
if delta < 0:
|
242
|
-
assert "string的宽度比length宽"
|
243
|
-
elif delta == 0:
|
244
|
-
pass
|
245
|
-
else:
|
246
|
-
prefix = charactor * math.floor(delta / 2)
|
247
|
-
suffix = charactor * math.ceil(delta / 2)
|
248
|
-
|
249
|
-
return prefix, suffix
|
250
|
-
|
251
|
-
def _get_maxlength_from_list(self, lst: List[str]) -> int:
|
252
|
-
temp = [self._get_width(x) for x in lst]
|
253
|
-
if len(temp) == 0:
|
254
|
-
return 0
|
255
|
-
else:
|
256
|
-
return max(temp)
|
257
|
-
|
258
|
-
def _update_widths(self):
|
259
|
-
temp = {key: self._get_width(key) for key in self.y_sidebars}
|
260
|
-
|
261
|
-
# for dict_y in self.dict.values():
|
262
|
-
# for key, value in dict_y.items():
|
263
|
-
# width = self._get_width(value)
|
264
|
-
# if width > temp[key]:
|
265
|
-
# temp[key] = width
|
266
|
-
|
267
|
-
matrix = self._get_width_matrix()
|
268
|
-
for i in range(len(matrix)):
|
269
|
-
for j in range(len(matrix[0])):
|
270
|
-
if matrix[i][j] > temp[self.y_sidebars[j]]:
|
271
|
-
temp[self.y_sidebars[j]] = matrix[i][j]
|
272
|
-
|
273
|
-
self._widths = temp
|
274
|
-
def _get_width_matrix(self):
|
275
|
-
results = [[0 for _ in self.y_sidebars] for _ in self.x_sidebars]
|
276
|
-
for x, dict_y in self.dict.items():
|
277
|
-
for y, value in dict_y.items():
|
278
|
-
results[self.x_sidebars.index(x)][self.y_sidebars.index(y)] = self._get_width(value)
|
279
|
-
return results
|
280
|
-
|
281
|
-
def __repr__(self):
|
282
|
-
print(self._widths)
|
283
|
-
def _get_width(self, string):
|
284
|
-
return wcswidth(string)
|
285
|
-
|
286
|
-
def __len__(self):
|
287
|
-
return len(self.x_sidebars) * len(self.y_sidebars)
|
288
|
-
def __contains__(self, item):
|
289
|
-
for x in self.x_sidebars:
|
290
|
-
for y in self.y_sidebars:
|
291
|
-
if str(item) == self.dict[x][y]:
|
292
|
-
return True
|
293
|
-
return False
|
294
|
-
def __str__(self):
|
295
|
-
return self.get_table_by_str()
|
296
|
-
|
297
|
-
def __getitem__(self, index):
|
298
|
-
index = str(index)
|
299
|
-
|
300
|
-
if index not in self.dict.keys():
|
301
|
-
self.dict.update({index: MyDict()})
|
302
|
-
self._update_sidebars()
|
303
|
-
return self.dict[index]
|
304
|
-
|
305
|
-
return self.dict[index]
|
306
|
-
def __setitem__(self, index, value:MyDict|dict):
|
307
|
-
index, value = str(index), MyDict(value)
|
308
|
-
self.dict.update({index: value})
|
309
|
-
|
310
|
-
if __name__ == '__main__':
|
311
|
-
my_table = BAutoTable("x", "y")
|
312
|
-
|
313
|
-
my_table[1][3] = 123
|
314
|
-
my_table[2][2] = 123
|
315
|
-
my_table["awa"]["12133"] = 123
|
316
|
-
my_table.copy_row('awa', 'qwq')
|
317
|
-
my_table['qwq'][12133] = 333
|
318
|
-
|
319
|
-
# my_table.read_txt(r'E:\byzh_workingplace\byzh-rc-to-pypi\awa.txt')
|
320
|
-
print(my_table)
|
321
|
-
repr(my_table)
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|