-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
171 lines (128 loc) · 4.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import contextlib
import os
import re
import subprocess
import threading
from traceback import print_exc
from _meta import APP_DIR, __developer_mode__
FILEBROWSER_PATH = os.path.join(os.getenv('WINDIR'), 'explorer.exe')
class MetaInitHook(type):
"""
Run _cls_init classmethod each time
when class or it's derivatives initialized
(Class init != class instance init, which __init__ does
"""
def __init__(cls, name, bases, dct):
super().__init__(name, bases, dct)
cls._cls_init()
class StrHolder(metaclass=MetaInitHook):
"""
1) String constants are better then same string written twice
2) But code like: some_constant = 'some_constant' annoying
3) Write some_constant: str and let Python do it's work
Inspired by dataclasses
If someone will find better or existing solution => leave a comment plz
Static use:
class MyConstants(StrHolder):
CONST_1: str
CONST_2: str
CONST_3: str
You'll get:
class MyConstants(StrHolder):
CONST_1 = 'CONST_1'
CONST_2 = 'CONST_2'
CONST_3 = 'CONST_3'
You may override _get_value:
class ...:
...
@staticmethod
def _get_value(field_name: str) -> str:
return field_name.capitalize().replace('_', ' ')
Result:
class MyConstants(StrHolder):
CONST_1 = 'Const 1'
CONST_2 = 'Const 2'
CONST_3 = 'Const 3'
You still can define constants by your own
And each one will be available like so:
MyConstants.CONST_1
Dynamic use:
class MyConstants(StrHolder):
CONST_1: str
CONST_2: str
CONST_3: str
instance = MyConstants(lambda x: x.replace('_', ' '))
assert instance.CONST_2 == 'CONST 2'
assert MyConstants.CONST_2 == 'CONST_2'
Inheritance free use:
class MyConstants:
CONST_1: str
CONST_2: str
CONST_3: str
StrHolder._field_names_to_values(MyConstants, lambda x: x.lower())
assert MyConstants.CONST_3 == 'const_3'
"""
def __init__(self, get_value):
self._field_names_to_values(self, get_value)
@staticmethod
def _get_value(field_name: str) -> str:
return field_name
@classmethod
def _cls_init(cls):
cls._field_names_to_values(cls, cls._get_value)
@staticmethod
def _field_names_to_values(scope, get_value=lambda x: x):
if not hasattr(scope, '__annotations__'):
return
for field in scope.__annotations__:
if not field.startswith('_'):
setattr(scope, field, get_value(field))
def ellipsis_trunc(text: str, width=12):
if len(text) <= width or width < 1:
return text
return text[:width-1].rstrip() + "…"
def rename_key(container: dict, old_key, new_key, override=True) -> bool:
if old_key not in container:
return False
if new_key in container and not override:
return False
value = container[old_key]
del container[old_key]
container[new_key] = value
return True
def cycled_shift(start_pos: int, length: int, step=1):
end_pos = (start_pos + step) % length
return end_pos if end_pos >= 0 else end_pos + length
def max_len(iterable):
return len(max(iterable, key=len))
def change_escape(text: str, escape: bool):
if escape:
return re.escape(text)
return re.sub(r'\\([^\\]+)', r'\g<1>', text)
def alternative_path(path: str):
from os.path import altsep, split
return altsep.join(split(path))
def explore(path):
if os.path.exists(path):
subprocess.run([FILEBROWSER_PATH, os.path.normpath(path)])
def public_fields(object):
return (
(k, v)
for k, v in vars(object).items()
if not k.startswith('_')
)
def app_abs_path(path: str):
return os.path.join(APP_DIR, os.path.normpath(path))
@contextlib.contextmanager
def show_exceptions():
try:
yield
except:
if __developer_mode__:
print("in Tread:", threading.current_thread().name)
print_exc()
raise
def set_between(lower_bound, upper_bound, value):
if lower_bound > upper_bound:
lower_bound, upper_bound = upper_bound, lower_bound
return max(lower_bound, min(upper_bound, value))