-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
executable file
·77 lines (56 loc) · 1.69 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
#!/usr/bin/python3
__all__ = 'subscript', 'superscript', 'cached', 'array_fallback', 'table_fallback'
subscripts = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉")
def subscript(n):
if not n >= 0: raise ValueError
return str(n).translate(subscripts)
superscripts = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")
def superscript(n):
if not n >= 0: raise ValueError
return str(n).translate(superscripts)
def cached(old_method):
name = '_cached_' + old_method.__name__
def new_method(self, *args):
try:
value = getattr(self, name)[args]
#print(f"cache hit: {old_method.__qualname__} @{id(self)} {args}")
return value
except AttributeError:
#print(f"cache miss: {old_method.__qualname__} @{id(self)} {args}")
value = old_method(self, *args)
store = {args: value}
setattr(self, name, store)
return value
except KeyError:
#print(f"cache miss: {old_method.__qualname__} @{id(self)} {args}")
value = old_method(self, *args)
getattr(self, name)[args] = value
return value
new_method.__name__ = old_method.__name__
new_method.__qualname__ = old_method.__qualname__
return new_method
def array_fallback(Array):
try:
return Array.Array
except AttributeError:
if isinstance(Array, type):
return lambda values, sizes, types: Array(values)
else:
return Array
def table_fallback(Table):
try:
return Table.Table
except AttributeError:
return lambda items, ksize, sizes, types, Array: Table(items)
'''
def sm_range(*args):
if len(args) == 1 and hasattr(args[0], 'sm_range'):
return args[0].sm_range()
else:
return range(*args)
def sm_len(a):
if hasattr(a, 'sm_length'):
return a.sm_length()
else:
return len(a)
'''