-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathutils.py
81 lines (65 loc) · 2.2 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import logging
from contextlib import suppress
def escape_important(text):
return text.replace("\\)", "\0\1").replace("\\(", "\0\2")
def unescape_important(text):
return text.replace("\0\1", ")").replace("\0\2", "(")
def parse_parentheses(string):
result = []
current_item = ""
nesting_level = 0
for char in string:
if char == "(":
if nesting_level == 0:
if current_item:
result.append(current_item)
current_item = "("
else:
current_item = "("
else:
current_item += char
nesting_level += 1
elif char == ")":
nesting_level -= 1
if nesting_level == 0:
result.append(current_item + ")")
current_item = ""
else:
current_item += char
else:
current_item += char
if current_item:
result.append(current_item)
return result
def _remove_weights(string):
a = parse_parentheses(string)
out = []
for x in a:
if len(x) >= 2 and x[-1] == ")" and x[0] == "(":
x = x[1:-1]
xx = x.rfind(":")
if xx > 0:
with suppress(Exception):
x = x[:xx]
out += _remove_weights(x)
else:
out += [x]
return out
def remove_weights(text: str):
text = escape_important(text)
parsed_weights = _remove_weights(text)
return "".join([unescape_important(segment) for segment in parsed_weights])
# some model patcher tools
def _property_setter_patch(cls, name: str, fn) -> None:
"""readonly property setter patcher"""
p = getattr(cls, name)
if not isinstance(p, property):
raise TypeError(f"{name} is not a property")
if p.fset is not None:
logging.warning("Property setter already exists, skip patching.")
return
setattr(cls, name, getattr(cls, name).setter(fn))
def _empty_setter(self, val):
logging.warning("An empty property setter is called. This is a patch to avoid `AttributeError`.")
def patch_device_empty_setter(cls):
_property_setter_patch(cls, "device", _empty_setter)