-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsutil.py
More file actions
62 lines (42 loc) · 1.19 KB
/
sutil.py
File metadata and controls
62 lines (42 loc) · 1.19 KB
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
import functools
import re
import string
import time
ASCII_ISO = 'iso-8859-1'
class LightTimer:
def pretty(self) -> str:
return f"{self.fetch():.3f}s"
def fetch(self) -> float:
return time.time() - self.start_time
def restart(self):
self.start_time = time.time()
def __init__(self):
self.start_time: float = 0.0
self.restart()
@functools.lru_cache(maxsize=None)
def get_safe(val: str) -> str:
if val is None:
val = ''
res = val.encode('unicode-escape').replace(b"\"", b"\\\"")
return f'"{res.decode(ASCII_ISO)}"'
@functools.lru_cache(maxsize=None)
def get_norm(val: str) -> str:
name: str = get_safe(val)
res: list = []
for s in name.split():
nstr = ' '.join(re.findall('(?!_)(\w+)', s))
nstr = nstr.title().replace(' ', '')
res.append(nstr)
final: str = ''.join(res)
if len(final) == 0:
return "Unknown"
else:
return final[:0x10]
def is_hex_str(what: str) -> bool:
return all(c in string.hexdigits for c in what)
def sizeof_fmt(num, suffix='B'): # https://stackoverflow.com/a/1094933/9419412
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return f"{num:.1f}Yi{suffix}"