kevin-toolbox-dev 1.3.3__py3-none-any.whl → 1.3.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.
- kevin_toolbox/__init__.py +2 -2
- kevin_toolbox/nested_dict_list/serializer/__init__.py +1 -0
- kevin_toolbox/nested_dict_list/serializer/read.py +11 -4
- kevin_toolbox/nested_dict_list/serializer/saved_node_name_builder.py +31 -0
- kevin_toolbox/nested_dict_list/serializer/write.py +51 -26
- kevin_toolbox/nested_dict_list/value_parser/__init__.py +1 -0
- kevin_toolbox/nested_dict_list/value_parser/replace_identical_with_reference.py +127 -0
- kevin_toolbox_dev-1.3.4.dist-info/METADATA +67 -0
- {kevin_toolbox_dev-1.3.3.dist-info → kevin_toolbox_dev-1.3.4.dist-info}/RECORD +11 -9
- kevin_toolbox_dev-1.3.3.dist-info/METADATA +0 -75
- {kevin_toolbox_dev-1.3.3.dist-info → kevin_toolbox_dev-1.3.4.dist-info}/WHEEL +0 -0
- {kevin_toolbox_dev-1.3.3.dist-info → kevin_toolbox_dev-1.3.4.dist-info}/top_level.txt +0 -0
kevin_toolbox/__init__.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
__version__ = "1.3.
|
1
|
+
__version__ = "1.3.4"
|
2
2
|
|
3
3
|
|
4
4
|
import os
|
@@ -12,5 +12,5 @@ os.system(
|
|
12
12
|
os.system(
|
13
13
|
f'python {os.path.split(__file__)[0]}/env_info/check_validity_and_uninstall.py '
|
14
14
|
f'--package_name kevin-toolbox-dev '
|
15
|
-
f'--expiration_timestamp
|
15
|
+
f'--expiration_timestamp 1727961379 --verbose 0'
|
16
16
|
)
|
@@ -29,13 +29,15 @@ def read(input_path, **kwargs):
|
|
29
29
|
|
30
30
|
# 读取 var
|
31
31
|
var = json_.read(file_path=os.path.join(input_path, "var.json"), b_use_suggested_converter=True)
|
32
|
+
# 读取 record
|
33
|
+
record_s = dict()
|
34
|
+
if os.path.isfile(os.path.join(input_path, "record.json")):
|
35
|
+
record_s = json_.read(file_path=os.path.join(input_path, "record.json"), b_use_suggested_converter=True)
|
32
36
|
|
33
37
|
# 读取被处理的节点
|
34
38
|
processed_nodes = []
|
35
|
-
if
|
36
|
-
for name, value in ndl.get_nodes(
|
37
|
-
var=json_.read(file_path=os.path.join(input_path, "record.json"),
|
38
|
-
b_use_suggested_converter=True)["processed"], level=-1, b_strict=True):
|
39
|
+
if record_s:
|
40
|
+
for name, value in ndl.get_nodes(var=record_s["processed"], level=-1, b_strict=True):
|
39
41
|
if value:
|
40
42
|
processed_nodes.append(name)
|
41
43
|
else:
|
@@ -56,6 +58,11 @@ def read(input_path, **kwargs):
|
|
56
58
|
bk = SERIALIZER_BACKEND.get(name=value.pop("backend"))(folder=os.path.join(input_path, "nodes"))
|
57
59
|
ndl.set_value(var=var, name=name, value=bk.read(**value))
|
58
60
|
|
61
|
+
#
|
62
|
+
if record_s.get("b_keep_identical_relations", False):
|
63
|
+
from kevin_toolbox.nested_dict_list import value_parser
|
64
|
+
var = value_parser.replace_identical_with_reference(var=var, flag="same", b_reverse=True)
|
65
|
+
|
59
66
|
#
|
60
67
|
if temp_dir is not None:
|
61
68
|
for_os.remove(path=temp_dir, ignore_errors=True)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Saved_Node_Name_Builder:
|
2
|
+
"""
|
3
|
+
生成保存节点内容时的文件夹/文件名称
|
4
|
+
"""
|
5
|
+
|
6
|
+
def __init__(self, format_):
|
7
|
+
try:
|
8
|
+
temp = format_.format(**{k: k + "_" * 3 for k in {"raw_name", "id", "hash_name", "count"}})
|
9
|
+
assert len(temp) > len(format_)
|
10
|
+
except:
|
11
|
+
raise ValueError(f'invalid saved_node_name_format {format_}')
|
12
|
+
|
13
|
+
self.format_ = format_
|
14
|
+
self.count = 0
|
15
|
+
|
16
|
+
def __call__(self, name, value):
|
17
|
+
from kevin_toolbox.nested_dict_list import get_hash
|
18
|
+
|
19
|
+
res = self.format_.format(
|
20
|
+
**{"raw_name": name, "id": id(value), "hash_name": get_hash(name, length=12), "count": self.count})
|
21
|
+
self.count += 1
|
22
|
+
return res
|
23
|
+
|
24
|
+
|
25
|
+
if __name__ == '__main__':
|
26
|
+
bd = Saved_Node_Name_Builder(format_="{raw_name}_{count}_{hash_name}_{id}")
|
27
|
+
print(bd(":a@0", 1))
|
28
|
+
print(bd(":b:c", []))
|
29
|
+
|
30
|
+
# bd = Saved_Node_Name_Builder(format_="")
|
31
|
+
# bd = Saved_Node_Name_Builder(format_="{raw_name2}_{count}")
|
@@ -7,16 +7,18 @@ from kevin_toolbox.patches import for_os
|
|
7
7
|
import kevin_toolbox.nested_dict_list as ndl
|
8
8
|
from kevin_toolbox.nested_dict_list.traverse import Traversal_Mode
|
9
9
|
from .enum_variable import Strictness_Level
|
10
|
+
from .saved_node_name_builder import Saved_Node_Name_Builder
|
10
11
|
|
11
12
|
|
12
13
|
def write(var, output_dir, settings=None, traversal_mode=Traversal_Mode.BFS, b_pack_into_tar=True,
|
13
|
-
strictness_level=Strictness_Level.COMPATIBLE,
|
14
|
+
strictness_level=Strictness_Level.COMPATIBLE, saved_node_name_format='{count}_{hash_name}',
|
15
|
+
b_keep_identical_relations=False, **kwargs):
|
14
16
|
"""
|
15
17
|
将输入的嵌套字典列表 var 的结构和节点值保存到文件中
|
16
18
|
遍历 var,匹配并使用 settings 中设置的保存方式来对各部分结构/节点进行序列化
|
17
19
|
将会生成一个文件夹或者 .tar 文件,其中包含:
|
18
20
|
- var.json: 用于保存结构、简单节点值、复杂节点值/结构的序列化方式
|
19
|
-
- nodes
|
21
|
+
- nodes/: 该目录中包含一系列 <name>.<suffix> 文件或者 <name> 文件夹,其中包含复杂节点值/结构的序列化结果
|
20
22
|
- record.json: 其中记录了:
|
21
23
|
{
|
22
24
|
"processed": ... # 对哪些节点/部分进行了处理
|
@@ -82,25 +84,50 @@ def write(var, output_dir, settings=None, traversal_mode=Traversal_Mode.BFS, b_p
|
|
82
84
|
- "low" / Strictness_Level.IGNORE_FAILURE 匹配不完整,或者某些节点尝试过所有匹配到
|
83
85
|
的 backend 之后仍然无法写入
|
84
86
|
默认是 "normal"
|
87
|
+
saved_node_name_format: <str> nodes/目录下节点文件/文件夹的命名方式。
|
88
|
+
基本结构为: '{<part_0>}...{<part_1>}...'
|
89
|
+
其中 {} 内将根据 part 指定的类型进行自动填充。目前支持以下几种选项:
|
90
|
+
- "raw_name" 该节点对应位置的 name。
|
91
|
+
- "id" 该节点在当前内存中的 id。
|
92
|
+
- "hash_name" 该节点位置 name 的 hash 值。
|
93
|
+
- "count" 累加计算,表示是保存的第几个节点。
|
94
|
+
!!注意:
|
95
|
+
"raw_name" 该选项在 v1.3.3 前被使用,但是由于其可能含有 : 和 / 等特殊符号,当以其作为文件夹名时,
|
96
|
+
可能会引发错误。因此对于 windows 用户,禁止使用该选项,对于 mac 和 linux 用户,同样也不建议使用该选项。
|
97
|
+
"id" 虽然具有唯一性,但是其值对于每次运行是随机的。
|
98
|
+
"hash_name" 有极低的可能会发生 hash 碰撞。
|
99
|
+
综合而言:
|
100
|
+
建议使用 "hash_name" 和 "count" 的组合。
|
101
|
+
默认值为:
|
102
|
+
'{count}_{hash_name}'
|
103
|
+
b_keep_identical_relations: <boolean> 是否保留不同节点之间的 id 相等关系。
|
104
|
+
具体而言,就是使用 value_parser.replace_identical_with_reference() 函数将具有相同 id 的多个节点,
|
105
|
+
替换为单个节点和其多个引用的形式。
|
106
|
+
对于 ndl 中存在大量具有相同 id 的重复节点的情况,使用该操作可以额外达到压缩的效果。
|
107
|
+
默认为 False
|
85
108
|
"""
|
86
109
|
from kevin_toolbox.nested_dict_list.serializer.variable import SERIALIZER_BACKEND
|
87
110
|
|
88
|
-
#
|
111
|
+
# 检查参数
|
89
112
|
traversal_mode = Traversal_Mode(traversal_mode)
|
90
113
|
strictness_level = Strictness_Level(strictness_level)
|
91
114
|
os.makedirs(output_dir, exist_ok=True)
|
92
115
|
var = ndl.copy_(var=var, b_deepcopy=False)
|
116
|
+
if b_keep_identical_relations:
|
117
|
+
from kevin_toolbox.nested_dict_list import value_parser
|
118
|
+
var = value_parser.replace_identical_with_reference(var=var, flag="same", b_reverse=False)
|
93
119
|
if settings is None:
|
94
120
|
settings = [{"match_cond": "<level>-1", "backend": (":skip:simple", ":numpy:npy", ":torch:tensor", ":pickle")}]
|
121
|
+
snn_builder = Saved_Node_Name_Builder(format_=saved_node_name_format)
|
95
122
|
|
96
123
|
# 构建 processed_s
|
97
124
|
# 为了避免重复处理节点/结构,首先构建与 var 具有相似结构的 processed_s 来记录处理处理进度。
|
98
125
|
# 对于 processed_s,其节点值为 True 时表示该节点已经被处理,当节点值为 False 或者 list/dict 类型时表示该节点或者节点下面的结构中仍然
|
99
126
|
# 存在未处理的部分。
|
100
127
|
# 对于中间节点,只有其下所有叶节点都未处理时才会被匹配。
|
101
|
-
processed_s =
|
128
|
+
processed_s = ndl.copy_(var=var, b_deepcopy=False, b_keep_internal_references=False)
|
102
129
|
for n, _ in ndl.get_nodes(var=var, level=-1, b_strict=True):
|
103
|
-
ndl.set_value(var=processed_s, name=n, value=False, b_force=
|
130
|
+
ndl.set_value(var=processed_s, name=n, value=False, b_force=False)
|
104
131
|
# processed_s_bak 用于记录 var 的原始结构
|
105
132
|
processed_s_bak = ndl.copy_(var=processed_s, b_deepcopy=True)
|
106
133
|
if "_hook_for_debug" in kwargs:
|
@@ -126,27 +153,21 @@ def write(var, output_dir, settings=None, traversal_mode=Traversal_Mode.BFS, b_p
|
|
126
153
|
_process = _process_from_top_to_down
|
127
154
|
else:
|
128
155
|
_process = _process_from_down_to_top
|
129
|
-
paras = dict(
|
130
|
-
var=var, processed_s=processed_s, match_cond=setting["match_cond"],
|
131
|
-
traversal_mode=t_mode, strictness_level=strictness_level
|
132
|
-
)
|
156
|
+
paras = dict(var=var, match_cond=setting["match_cond"], traversal_mode=t_mode)
|
133
157
|
elif setting["match_cond"].startswith("<level>"):
|
134
158
|
_process = _process_for_level
|
135
|
-
paras = dict(
|
136
|
-
var=var, processed_s=processed_s, processed_s_bak=processed_s_bak,
|
137
|
-
level=int(setting["match_cond"][7:]), strictness_level=strictness_level
|
138
|
-
)
|
159
|
+
paras = dict(var=var, processed_s_bak=processed_s_bak, level=int(setting["match_cond"][7:]))
|
139
160
|
elif setting["match_cond"].startswith("<node>"):
|
140
161
|
_process = _process_for_name
|
141
|
-
paras = dict(var=var,
|
142
|
-
strictness_level=strictness_level)
|
162
|
+
paras = dict(var=var, name=setting["match_cond"][6:])
|
143
163
|
else:
|
144
164
|
raise ValueError(f'invalid match_cond: {setting["match_cond"]}')
|
145
165
|
# 执行
|
146
166
|
for i in backend_name_ls:
|
147
167
|
# print(processed_s)
|
148
168
|
# print(f'backend: {i}')
|
149
|
-
_process(backend=backend_s[i],
|
169
|
+
_process(backend=backend_s[i], strictness_level=strictness_level, processed_s=processed_s,
|
170
|
+
snn_builder=snn_builder, **paras)
|
150
171
|
if "_hook_for_debug" in kwargs:
|
151
172
|
kwargs["_hook_for_debug"]["processed"].append([i, ndl.copy_(var=processed_s, b_deepcopy=True)])
|
152
173
|
|
@@ -171,7 +192,8 @@ def write(var, output_dir, settings=None, traversal_mode=Traversal_Mode.BFS, b_p
|
|
171
192
|
json_.write(content=var, file_path=os.path.join(output_dir, "var.json"), b_use_suggested_converter=True)
|
172
193
|
# 保存处理结果(非必要)
|
173
194
|
json_.write(content=dict(processed=processed_s, raw_structure=processed_s_bak, timestamp=time.time(),
|
174
|
-
kt_version=kevin_toolbox.__version__
|
195
|
+
kt_version=kevin_toolbox.__version__,
|
196
|
+
b_keep_identical_relations=b_keep_identical_relations),
|
175
197
|
file_path=os.path.join(output_dir, "record.json"), b_use_suggested_converter=True)
|
176
198
|
|
177
199
|
# 打包成 .tar 文件
|
@@ -196,13 +218,13 @@ def _judge_processed_or_not(processed_s, name):
|
|
196
218
|
return b_processed
|
197
219
|
|
198
220
|
|
199
|
-
def _process_for_level(var, processed_s, processed_s_bak, level, backend, strictness_level):
|
221
|
+
def _process_for_level(var, processed_s, processed_s_bak, level, backend, strictness_level, snn_builder):
|
200
222
|
for name, _ in ndl.get_nodes(var=processed_s_bak, level=level, b_strict=True):
|
201
223
|
_process_for_name(var=var, processed_s=processed_s, name=name, backend=backend,
|
202
|
-
strictness_level=strictness_level)
|
224
|
+
strictness_level=strictness_level, snn_builder=snn_builder)
|
203
225
|
|
204
226
|
|
205
|
-
def _process_for_name(var, processed_s, name, backend, strictness_level):
|
227
|
+
def _process_for_name(var, processed_s, name, backend, strictness_level, snn_builder):
|
206
228
|
if _judge_processed_or_not(processed_s=processed_s, name=name) is True:
|
207
229
|
# has been processed
|
208
230
|
return
|
@@ -212,8 +234,9 @@ def _process_for_name(var, processed_s, name, backend, strictness_level):
|
|
212
234
|
return
|
213
235
|
|
214
236
|
# write by backend
|
237
|
+
snn_name = snn_builder(name=name, value=value)
|
215
238
|
try:
|
216
|
-
res = backend.write(name=
|
239
|
+
res = backend.write(name=snn_name, var=value)
|
217
240
|
except:
|
218
241
|
assert strictness_level in (Strictness_Level.IGNORE_FAILURE, Strictness_Level.COMPATIBLE), \
|
219
242
|
f'An error occurred when node {name} was saved using the first matched backend {backend}'
|
@@ -222,7 +245,7 @@ def _process_for_name(var, processed_s, name, backend, strictness_level):
|
|
222
245
|
ndl.set_value(var=var, name=name, value=res, b_force=False)
|
223
246
|
|
224
247
|
|
225
|
-
def _process_from_top_to_down(var, processed_s, match_cond, backend, traversal_mode, strictness_level):
|
248
|
+
def _process_from_top_to_down(var, processed_s, match_cond, backend, traversal_mode, strictness_level, snn_builder):
|
226
249
|
def match_cond_(parent_type, idx, value):
|
227
250
|
nonlocal match_cond, processed_s
|
228
251
|
|
@@ -237,8 +260,9 @@ def _process_from_top_to_down(var, processed_s, match_cond, backend, traversal_m
|
|
237
260
|
nonlocal processed_s, backend, strictness_level
|
238
261
|
|
239
262
|
# write by backend
|
263
|
+
snn_name = snn_builder(name=idx, value=value)
|
240
264
|
try:
|
241
|
-
res = backend.write(name=
|
265
|
+
res = backend.write(name=snn_name, var=value)
|
242
266
|
except:
|
243
267
|
assert strictness_level in (Strictness_Level.IGNORE_FAILURE, Strictness_Level.COMPATIBLE), \
|
244
268
|
f'An error occurred when node {name} was saved using the first matched backend {backend}'
|
@@ -250,7 +274,7 @@ def _process_from_top_to_down(var, processed_s, match_cond, backend, traversal_m
|
|
250
274
|
b_use_name_as_idx=True, traversal_mode=traversal_mode, b_traverse_matched_element=False)
|
251
275
|
|
252
276
|
|
253
|
-
def _process_from_down_to_top(var, processed_s, match_cond, backend, traversal_mode, strictness_level):
|
277
|
+
def _process_from_down_to_top(var, processed_s, match_cond, backend, traversal_mode, strictness_level, snn_builder):
|
254
278
|
processed_s_raw, processed_s = processed_s, ndl.copy_(var=processed_s, b_deepcopy=True)
|
255
279
|
|
256
280
|
def match_cond_(parent_type, idx, value):
|
@@ -268,8 +292,9 @@ def _process_from_down_to_top(var, processed_s, match_cond, backend, traversal_m
|
|
268
292
|
nonlocal processed_s, backend, processed_s_raw, strictness_level
|
269
293
|
|
270
294
|
# write by backend
|
295
|
+
snn_name = snn_builder(name=idx, value=value)
|
271
296
|
try:
|
272
|
-
res = backend.write(name=
|
297
|
+
res = backend.write(name=snn_name, var=value)
|
273
298
|
except:
|
274
299
|
assert strictness_level in (Strictness_Level.IGNORE_FAILURE, Strictness_Level.COMPATIBLE), \
|
275
300
|
f'An error occurred when node {name} was saved using the first matched backend {backend}'
|
@@ -305,7 +330,7 @@ if __name__ == '__main__':
|
|
305
330
|
{"match_cond": lambda _, __, value: not isinstance(value, (list, dict)),
|
306
331
|
"backend": (":skip:simple",)},
|
307
332
|
]
|
308
|
-
write(var=var_, output_dir=os.path.join(os.path.dirname(__file__), "
|
333
|
+
write(var=var_, output_dir=os.path.join(os.path.dirname(__file__), "temp"), traversal_mode="bfs",
|
309
334
|
b_pack_into_tar=True, settings=settings_, _hook_for_debug=_hook_for_debug)
|
310
335
|
|
311
336
|
for bk_name, p in _hook_for_debug["processed"]:
|
@@ -2,3 +2,4 @@ from .cal_relation_between_references import cal_relation_between_references
|
|
2
2
|
from .eval_references import eval_references
|
3
3
|
from .parse_references import parse_references
|
4
4
|
from .parse_and_eval_references import parse_and_eval_references
|
5
|
+
from .replace_identical_with_reference import replace_identical_with_reference
|
@@ -0,0 +1,127 @@
|
|
1
|
+
from collections import defaultdict
|
2
|
+
from kevin_toolbox.nested_dict_list import get_nodes, get_value, set_value
|
3
|
+
from kevin_toolbox.nested_dict_list import value_parser
|
4
|
+
|
5
|
+
|
6
|
+
def replace_identical_with_reference(var, flag="same", match_cond=None, b_reverse=False):
|
7
|
+
"""
|
8
|
+
将具有相同 id 的多个节点,替换为单个节点和其多个引用的形式
|
9
|
+
一般用于去除冗余部分,压缩 ndl 的结构
|
10
|
+
|
11
|
+
参数:
|
12
|
+
var:
|
13
|
+
flag: <str> 引用标记头,表示该节点应该替换为指定节点的内容
|
14
|
+
默认为 "same"
|
15
|
+
注意,在正向过程中,如果遇到本身就是以该 flag 开头的字符串,会自动在前添加多一个 flag 以示区分,
|
16
|
+
然后在逆向过程中,遇到两个 flag 标记开头的字符串将删除一个,然后跳过不处理。
|
17
|
+
match_cond: <func> 仅对匹配上(返回True视为匹配上)的节点进行处理
|
18
|
+
函数类型为 def(name, value)
|
19
|
+
其中:
|
20
|
+
name 该节点在结构体中的位置
|
21
|
+
value 节点的值
|
22
|
+
默认不对 int、float、bool、str、None 等类型进行处理
|
23
|
+
b_reverse: <boolean> 是否进行逆向操作
|
24
|
+
"""
|
25
|
+
if match_cond is None:
|
26
|
+
match_cond = lambda name, value: not isinstance(value, (int, float, bool, str, type(None)))
|
27
|
+
assert callable(match_cond)
|
28
|
+
|
29
|
+
if b_reverse:
|
30
|
+
return _reverse(var, flag)
|
31
|
+
else:
|
32
|
+
return _forward(var, flag, match_cond)
|
33
|
+
|
34
|
+
|
35
|
+
def _forward(var, flag, match_cond):
|
36
|
+
id_to_height_s = defaultdict(set)
|
37
|
+
id_to_name_s = defaultdict(set)
|
38
|
+
height = 1
|
39
|
+
while True:
|
40
|
+
node_ls = get_nodes(var=var, level=-height, b_strict=True)
|
41
|
+
if not node_ls:
|
42
|
+
break
|
43
|
+
for name, value in node_ls:
|
44
|
+
if not match_cond(name, value):
|
45
|
+
continue
|
46
|
+
id_to_name_s[id(value)].add(name)
|
47
|
+
id_to_height_s[id(value)].add(height)
|
48
|
+
height += 1
|
49
|
+
|
50
|
+
#
|
51
|
+
for k, v in list(id_to_height_s.items()):
|
52
|
+
# 仅保留有多个节点对应的 id
|
53
|
+
if len(id_to_name_s[k]) <= 1:
|
54
|
+
id_to_height_s.pop(k)
|
55
|
+
id_to_name_s.pop(k)
|
56
|
+
continue
|
57
|
+
# 具有相同 id 的节点所处的高度应该相同
|
58
|
+
assert len(v) == 1, f'nodes {id_to_name_s[k]} have different heights: {v}'
|
59
|
+
# 按高度排序
|
60
|
+
id_vs_height = sorted([(k, v.pop()) for k, v in id_to_height_s.items()], key=lambda x: x[1], reverse=True)
|
61
|
+
|
62
|
+
# 从高到低,依次将具有相同 id 的节点替换为 单个节点和多个引用 的形式
|
63
|
+
temp = []
|
64
|
+
processed_name_set = set()
|
65
|
+
for k, _ in id_vs_height:
|
66
|
+
# 找出父节点仍然未被处理的节点(亦即仍然能够访问到的节点)
|
67
|
+
unprocessed_name_set = {n for n in id_to_name_s[k] if id(get_value(var=var, name=n, default=temp)) == k}
|
68
|
+
if len(unprocessed_name_set) <= 1:
|
69
|
+
continue
|
70
|
+
# 任选其一进行保留,其余改为引用
|
71
|
+
keep_name = unprocessed_name_set.pop()
|
72
|
+
for name in unprocessed_name_set:
|
73
|
+
try:
|
74
|
+
var = set_value(var=var, name=name, value=f'<{flag}>{{{keep_name}}}', b_force=False)
|
75
|
+
except:
|
76
|
+
breakpoint()
|
77
|
+
processed_name_set.update(unprocessed_name_set)
|
78
|
+
|
79
|
+
# 将叶节点中,未被处理过,且是 str,且以 flag 开头的字符串,添加多一个 flag,以示区分
|
80
|
+
for name, value in get_nodes(var=var, level=-1, b_strict=True):
|
81
|
+
if name not in processed_name_set and isinstance(value, str) and value.startswith(f'<{flag}>'):
|
82
|
+
var = set_value(var=var, name=name, value=f'<{flag}>' + value, b_force=False)
|
83
|
+
|
84
|
+
return var
|
85
|
+
|
86
|
+
|
87
|
+
class _My_Str:
|
88
|
+
def __init__(self, s):
|
89
|
+
self.s = s
|
90
|
+
|
91
|
+
|
92
|
+
def _reverse(var, flag):
|
93
|
+
# 找出叶节点中,带有2个以上 flag 标记的字符串,删除其中一个标记,并使用 _My_Str 包裹,以便与普通引用节点区分
|
94
|
+
for name, value in get_nodes(var=var, level=-1, b_strict=True):
|
95
|
+
if isinstance(value, str) and value.startswith(f'<{flag}><{flag}>'):
|
96
|
+
var = set_value(var=var, name=name, value=_My_Str(value[len(flag) + 2:]), b_force=False)
|
97
|
+
# 解释引用
|
98
|
+
var, _ = value_parser.parse_and_eval_references(var=var, flag=flag)
|
99
|
+
# 解除 _My_Str 包裹
|
100
|
+
for name, value in get_nodes(var=var, level=-1, b_strict=True):
|
101
|
+
if isinstance(value, _My_Str):
|
102
|
+
var = set_value(var=var, name=name, value=value.s, b_force=False)
|
103
|
+
return var
|
104
|
+
|
105
|
+
|
106
|
+
if __name__ == '__main__':
|
107
|
+
import numpy as np
|
108
|
+
from kevin_toolbox.nested_dict_list import copy_
|
109
|
+
|
110
|
+
a = np.array([1, 2, 3])
|
111
|
+
b = np.ones((2, 3))
|
112
|
+
c = [a, b]
|
113
|
+
d = {"a": a, "b": b}
|
114
|
+
e = {"c1": c, "c2": c}
|
115
|
+
x = [e, a, d, c, "<same>{@1}", "<same><same>{@1}"]
|
116
|
+
|
117
|
+
print(x)
|
118
|
+
|
119
|
+
y = replace_identical_with_reference(var=copy_(x, b_deepcopy=True), flag="same")
|
120
|
+
print(y)
|
121
|
+
|
122
|
+
x1 = replace_identical_with_reference(var=y, flag="same", b_reverse=True)
|
123
|
+
print(x1)
|
124
|
+
|
125
|
+
from kevin_toolbox.patches.for_test import check_consistency
|
126
|
+
|
127
|
+
check_consistency(x, x1)
|
@@ -0,0 +1,67 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: kevin-toolbox-dev
|
3
|
+
Version: 1.3.4
|
4
|
+
Summary: 一个常用的工具代码包集合
|
5
|
+
Home-page: https://github.com/cantbeblank96/kevin_toolbox
|
6
|
+
Download-URL: https://github.com/username/your-package/archive/refs/tags/v1.0.0.tar.gz
|
7
|
+
Author: kevin hsu
|
8
|
+
Author-email: xukaiming1996@163.com
|
9
|
+
License: MIT
|
10
|
+
Keywords: mathematics,pytorch,numpy,machine-learning,algorithm
|
11
|
+
Platform: UNKNOWN
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
13
|
+
Classifier: Programming Language :: Python
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
15
|
+
Requires-Python: >=3.6
|
16
|
+
Description-Content-Type: text/markdown
|
17
|
+
Requires-Dist: torch (>=1.2.0)
|
18
|
+
Requires-Dist: numpy (>=1.19.0)
|
19
|
+
Provides-Extra: plot
|
20
|
+
Requires-Dist: matplotlib (>=3.0) ; extra == 'plot'
|
21
|
+
Provides-Extra: rest
|
22
|
+
Requires-Dist: pytest (>=6.2.5) ; extra == 'rest'
|
23
|
+
Requires-Dist: line-profiler (>=3.5) ; extra == 'rest'
|
24
|
+
|
25
|
+
# kevin_toolbox
|
26
|
+
|
27
|
+
一个通用的工具代码包集合
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
环境要求
|
32
|
+
|
33
|
+
```shell
|
34
|
+
numpy>=1.19
|
35
|
+
pytorch>=1.2
|
36
|
+
```
|
37
|
+
|
38
|
+
安装方法:
|
39
|
+
|
40
|
+
```shell
|
41
|
+
pip install kevin-toolbox --no-dependencies
|
42
|
+
```
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
[项目地址 Repo](https://github.com/cantbeblank96/kevin_toolbox)
|
47
|
+
|
48
|
+
[使用指南 User_Guide](./notes/User_Guide.md)
|
49
|
+
|
50
|
+
[免责声明 Disclaimer](./notes/Disclaimer.md)
|
51
|
+
|
52
|
+
[版本更新记录](./notes/Release_Record.md):
|
53
|
+
|
54
|
+
- v 1.3.4 (2024-04-06)【bug fix】【new feature】
|
55
|
+
- nested_dict_list
|
56
|
+
- 【new feature】add replace_identical_with_reference() to value_parser,新增该函数用于将具有相同 id 的多个节点,替换为单个节点和其多个引用的形式。一般用于去除冗余部分,压缩 ndl 的结构。
|
57
|
+
- 【bug fix】【new feature】fix bug in write(),添加了 saved_node_name_format 参数控制 nodes 下文件名的生成。
|
58
|
+
- bug:在 v1.3.3 前直接使用原始的 node_name 来作为 nodes/ 目录下的文件名,这导致当 node_name 中带有特殊字符时,比如 "/"(在linux下) 和 ":"(在windows下),将会导致保存失败。
|
59
|
+
- fix:使用 saved_node_name_format 指定生成文件名的方式,默认方式 '{count}_{hash_name}' 可以避免出现特殊字符。
|
60
|
+
- 【bug fix】fix bug in write()
|
61
|
+
- bug:在 v1.3.3 前 processed_s 通过 ndl.set_value() 来逐个节点构建,但是由于根据节点名创建的结果可能和原结构存在差异(详见 ndl.set_value() 中b_force参数的介绍),因此导致 processed_s 和 var 结构不一致,导致出错。
|
62
|
+
- fix:使用 ndl.copy_() 来创建结构与 var 一致的 processed_s。
|
63
|
+
- 【new feature】add b_keep_identical_relations to write(),增加该参数用于决定是否保留不同节点之间的 id 相等关系。
|
64
|
+
- 添加了对应的测试用例。
|
65
|
+
|
66
|
+
|
67
|
+
|
@@ -1,4 +1,4 @@
|
|
1
|
-
kevin_toolbox/__init__.py,sha256=
|
1
|
+
kevin_toolbox/__init__.py,sha256=ryG5DAc-XozLnxNQjuTKTRynJFnwdQHKBZ2vaN1upx4,410
|
2
2
|
kevin_toolbox/computer_science/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
3
|
kevin_toolbox/computer_science/algorithm/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
4
4
|
kevin_toolbox/computer_science/algorithm/cache_manager/__init__.py,sha256=p2hddkZ1HfYF9-m2Hx-o9IotwQHd4QwDCePy2ADpTDA,41
|
@@ -227,11 +227,12 @@ kevin_toolbox/nested_dict_list/name_handler/__init__.py,sha256=P_pWq78oN6NdvWg2h
|
|
227
227
|
kevin_toolbox/nested_dict_list/name_handler/build_name.py,sha256=VPWyjE8i8l-4Zm4tkD06Ie4J2NCsmI32ecOxZQqqmok,989
|
228
228
|
kevin_toolbox/nested_dict_list/name_handler/escape_node.py,sha256=niT9MxmsyrSZYhKXlWzdoKXVYhWRCR-kmQBkZopznpA,1163
|
229
229
|
kevin_toolbox/nested_dict_list/name_handler/parse_name.py,sha256=vUlAXPocpVSxtb3EnRi7U5K40Tz9plFG-_sbwLfYiy4,2280
|
230
|
-
kevin_toolbox/nested_dict_list/serializer/__init__.py,sha256=
|
230
|
+
kevin_toolbox/nested_dict_list/serializer/__init__.py,sha256=79dd9l-mNz0bycFKjNm7YsfWPR-JsVx9NoG_Ofqy-HQ,153
|
231
231
|
kevin_toolbox/nested_dict_list/serializer/enum_variable.py,sha256=RWPydtXI4adOJYGo_k5CWHSL0Odzj_bsahb24p1ranY,847
|
232
|
-
kevin_toolbox/nested_dict_list/serializer/read.py,sha256=
|
232
|
+
kevin_toolbox/nested_dict_list/serializer/read.py,sha256=yessvu7msmP2kV3ZhOTVmI2ENI-R1-TdhVgZdS8eWDk,2843
|
233
|
+
kevin_toolbox/nested_dict_list/serializer/saved_node_name_builder.py,sha256=qsD-rmDmVaKZP4owN3Wm3QY2Ksi71XlYETqw4VmIsSU,1011
|
233
234
|
kevin_toolbox/nested_dict_list/serializer/variable.py,sha256=ZywG6obipRBCGY1cY42gdvsuWk8GLZXr6eCYcW7ZJ9c,392
|
234
|
-
kevin_toolbox/nested_dict_list/serializer/write.py,sha256=
|
235
|
+
kevin_toolbox/nested_dict_list/serializer/write.py,sha256=kP_sM-NtI3vJT7KwBZsz_ReZprfVefzUFplW7Kh0zVQ,21024
|
235
236
|
kevin_toolbox/nested_dict_list/serializer/backends/__init__.py,sha256=8g7y-L3cmctxao616dVkGiot00FJzKNmNl_69V2bSmE,39
|
236
237
|
kevin_toolbox/nested_dict_list/serializer/backends/_json_.py,sha256=oJXIc28yjxsD9ZJuw120pVHTVsTzCdaXEhVUSQeydq4,2145
|
237
238
|
kevin_toolbox/nested_dict_list/serializer/backends/_ndl.py,sha256=QMF4DFAnt1sp35atds6t44nfCYuIOeGgW1-SPfJq6KM,1652
|
@@ -243,11 +244,12 @@ kevin_toolbox/nested_dict_list/serializer/backends/_skip_simple.py,sha256=dS9kKh
|
|
243
244
|
kevin_toolbox/nested_dict_list/serializer/backends/_torch_all.py,sha256=aw9M1Hep65lkvb8_QU3VkecE6q3l4UqXansh4lnMv7s,1282
|
244
245
|
kevin_toolbox/nested_dict_list/serializer/backends/_torch_tensor.py,sha256=Wiimzc0XGxFRYJiipzVnUd27scaJZZTOYp5OxYo3cKg,1353
|
245
246
|
kevin_toolbox/nested_dict_list/serializer/backends/backend_base.py,sha256=SZpLRhdSIykHZ_Ds3HX96lKLfXCyKzMQ_lx139XXBtc,1741
|
246
|
-
kevin_toolbox/nested_dict_list/value_parser/__init__.py,sha256=
|
247
|
+
kevin_toolbox/nested_dict_list/value_parser/__init__.py,sha256=MgbpkiXzVuA_i3VZ4VDNrQZfzpUZN3uaHrgOt_wq-4E,313
|
247
248
|
kevin_toolbox/nested_dict_list/value_parser/cal_relation_between_references.py,sha256=m3P7q5-pbbWqtjOjJUT-499q4mCyjtFEtFpGgovShSg,3119
|
248
249
|
kevin_toolbox/nested_dict_list/value_parser/eval_references.py,sha256=YQyOm3awKVRusXxSNObjJ2yPf0oE4gleobOn_RN_nzU,2301
|
249
250
|
kevin_toolbox/nested_dict_list/value_parser/parse_and_eval_references.py,sha256=RQEDFFNAhQQcX9H8curwja-pI2gKZlVx4M2qeneBOhA,2370
|
250
251
|
kevin_toolbox/nested_dict_list/value_parser/parse_references.py,sha256=G470xNzrRpYlS5To8R5yV0M6nX4iE5LLMp_eV49bh3Y,2116
|
252
|
+
kevin_toolbox/nested_dict_list/value_parser/replace_identical_with_reference.py,sha256=4nd5q3qTi4sTfOqHF0-HyLMDS9a5x8wgwETfpDI8jh8,5419
|
251
253
|
kevin_toolbox/patches/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
252
254
|
kevin_toolbox/patches/for_logging/__init__.py,sha256=xymF6mjwY4Cin7CoEwanFY5ZVk8oY0pDLqjZAGa7_Rg,39
|
253
255
|
kevin_toolbox/patches/for_logging/build_logger.py,sha256=0UoRMKaERd8wlHGTiNR3dbLQRAtXcb0QZuGkX2oFgGo,3071
|
@@ -302,7 +304,7 @@ kevin_toolbox/patches/for_torch/math/get_y_at_x.py,sha256=bfoVcasZ_tMdhR_1Me0Jli
|
|
302
304
|
kevin_toolbox/patches/for_torch/math/my_around.py,sha256=ptpU3ids50gwf663EpHbw7raj9tNrDGBFZ5t_uMNH14,1378
|
303
305
|
kevin_toolbox/patches/for_torch/nn/__init__.py,sha256=aJs3RMqRzQmd8KKDmQW9FxwCqS5yfPqEdg-m0PwlQro,39
|
304
306
|
kevin_toolbox/patches/for_torch/nn/lambda_layer.py,sha256=KUuLiX_Dr4bvRmpAaCW5QTDWDcnMPRnw0jg4NNXTFhM,223
|
305
|
-
kevin_toolbox_dev-1.3.
|
306
|
-
kevin_toolbox_dev-1.3.
|
307
|
-
kevin_toolbox_dev-1.3.
|
308
|
-
kevin_toolbox_dev-1.3.
|
307
|
+
kevin_toolbox_dev-1.3.4.dist-info/METADATA,sha256=NANoXo3yczTlDCHbDo38F_QLlPf0LBYWIwMS4fRHipY,2710
|
308
|
+
kevin_toolbox_dev-1.3.4.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
|
309
|
+
kevin_toolbox_dev-1.3.4.dist-info/top_level.txt,sha256=S5TeRGF-PwlhsaUEPTI-f2vWrpLmh3axpyI6v-Fi75o,14
|
310
|
+
kevin_toolbox_dev-1.3.4.dist-info/RECORD,,
|
@@ -1,75 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: kevin-toolbox-dev
|
3
|
-
Version: 1.3.3
|
4
|
-
Summary: 一个常用的工具代码包集合
|
5
|
-
Home-page: https://github.com/cantbeblank96/kevin_toolbox
|
6
|
-
Download-URL: https://github.com/username/your-package/archive/refs/tags/v1.0.0.tar.gz
|
7
|
-
Author: kevin hsu
|
8
|
-
Author-email: xukaiming1996@163.com
|
9
|
-
License: MIT
|
10
|
-
Keywords: mathematics,pytorch,numpy,machine-learning,algorithm
|
11
|
-
Platform: UNKNOWN
|
12
|
-
Classifier: License :: OSI Approved :: MIT License
|
13
|
-
Classifier: Programming Language :: Python
|
14
|
-
Classifier: Programming Language :: Python :: 3
|
15
|
-
Requires-Python: >=3.6
|
16
|
-
Description-Content-Type: text/markdown
|
17
|
-
Requires-Dist: torch (>=1.2.0)
|
18
|
-
Requires-Dist: numpy (>=1.19.0)
|
19
|
-
Provides-Extra: plot
|
20
|
-
Requires-Dist: matplotlib (>=3.0) ; extra == 'plot'
|
21
|
-
Provides-Extra: rest
|
22
|
-
Requires-Dist: pytest (>=6.2.5) ; extra == 'rest'
|
23
|
-
Requires-Dist: line-profiler (>=3.5) ; extra == 'rest'
|
24
|
-
|
25
|
-
# kevin_toolbox
|
26
|
-
|
27
|
-
一个通用的工具代码包集合
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
环境要求
|
32
|
-
|
33
|
-
```shell
|
34
|
-
numpy>=1.19
|
35
|
-
pytorch>=1.2
|
36
|
-
```
|
37
|
-
|
38
|
-
安装方法:
|
39
|
-
|
40
|
-
```shell
|
41
|
-
pip install kevin-toolbox --no-dependencies
|
42
|
-
```
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
[项目地址 Repo](https://github.com/cantbeblank96/kevin_toolbox)
|
47
|
-
|
48
|
-
[使用指南 User_Guide](./notes/User_Guide.md)
|
49
|
-
|
50
|
-
[免责声明 Disclaimer](./notes/Disclaimer.md)
|
51
|
-
|
52
|
-
[版本更新记录](./notes/Release_Record.md):
|
53
|
-
|
54
|
-
- v 1.3.3 (2024-04-01)【bug fix】【new feature】
|
55
|
-
- math.utils
|
56
|
-
- 【bug fix】更正拼写错误,将原有的 spilt_integer_most_evenly() 中的 "spilt" 改正为 "split",新的函数名为 split_integer_most_evenly
|
57
|
-
|
58
|
-
- patches.for_numpy.random
|
59
|
-
- 【bug fix】modify get_rng(),改进以避免遇到 rng 中不存在的函数时报错。
|
60
|
-
|
61
|
-
- data_flow.file
|
62
|
-
- json_
|
63
|
-
- 【new feature】将 escape_tuple() 改成 escape_tuple_and_set(),新增对 set 进行处理。
|
64
|
-
- 【new feature】将 unescape_tuple() 改成 unescape_tuple_and_set()。
|
65
|
-
|
66
|
-
- core.reader
|
67
|
-
- 【new feature】为 File_Iterative_Reader 新增了 file_obj 参数,支持直接输入文件对象。
|
68
|
-
|
69
|
-
- kevin_notation
|
70
|
-
- 【new feature】根据 File_Iterative_Reader 的修改,为 Kevin_Notation_Reader 和 read() 对应增加 file_obj 参数,以支持直接输入文件对象。
|
71
|
-
- 补充了对应的测试用例。
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
File without changes
|
File without changes
|