-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
177 lines (173 loc) · 5.6 KB
/
common.py
File metadata and controls
177 lines (173 loc) · 5.6 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
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
172
173
174
175
176
177
from math import log2, log
from math import inf as INFINITY
# MAJOR, MINOR, PATCH, PRE-RELEASE IDENTIFIER, BUILD (https://semver.org)
VERSION = ['5','0','2','BETA','10']
# display like "MAJOR.MINOR.PATCH[-PRE-RELEASE IDENT][+BUILD]", [] = omitted if empty
VER_FMT = "{0}.{1}.{2}[-{3}][+{4}]"
# Size prefixes
SIZE_PREFIX_DEFS = {
'k':1, # kibi-
'm':2, # mebi-
'g':3, # gibi- # weird flex but ok
't':4, # tebi- # why the fuck do you need this much memory??
'p':5, # pebi- # because I can-
'e':6, # exbi- # and nobody can stop me!!
'z':7, # zebi-
'y':8. # yobi-
}
# Full names for prefixes
SIZE_PREFIX_FULL = {
'k':'kibi-',
'm':'Mebi-',
'g':'Gibi-',
't':'Tebi-',
'p':'Pebi-',
'e':'Exbi-',
'z':'Zebi-',
'y':'Yobi-', # yo? that's deprecated!
}
# Display error message
def fatal_error(origin, message):
exit(f"{origin}: fatal error: {message}")
# how do I explain this
def find_nz(string:str, delimiter:str, start:int=0):
output=str.find(string, delimiter, start)
if(output==-1):
return output+len(string)+1
else:
return output
# str.find() but with multi-delimiter support
def strfind(string, delimiters, start=0):
output=[]
idx=start
try:
while(string[idx] not in delimiters):
idx += 1
except IndexError:
return -1
return idx
# strfind() but it skips over characters with a backslash behind them
def strfind_escape(string, delimiters, start=0):
output=[]
idx=start
try:
while(string[idx] not in delimiters):
if(string[idx] == '\\'):
idx += 1
idx += 1
except IndexError:
return -1
return idx
# inverted ffind()
def inverted_strfind(string, delimiters, start=0):
output=[]
idx=start
try:
while(string[idx] in delimiters):
idx += 1
except IndexError:
return -1
return idx
# str.split() but with multi-delimiter support
def split_string(string:str, delimiters:str):
idx=0
output=[]
while(idx<len(string)):
idx=inverted_strfind(string, delimiters, idx)
if(idx==-1):
break
idx_end=strfind(string, delimiters, idx)
if(idx_end==-1):
output.append(string[idx:])
break
output.append(string[idx:idx_end])
idx=idx_end
return output
# Version renderer
def render_version(version, version_format):
to_print = ""
idx=0
while(idx<len(version_format)):
if(version_format[idx]=='['):
idx_end=find_nz(version_format, ']',idx)
substr=version_format[idx:idx_end]
keep=True
idx2=0
while(idx2<(idx_end-idx)):
idx2=substr.find('{',idx2)+1
if(idx2==0):
break
idx2_end=find_nz(substr, '}',idx2)
if(version[int(substr[idx2:idx2_end])]==''):
keep=False
break
idx2=idx2_end
if(keep):
to_print+=version_format[idx+1:idx_end]
idx=idx_end+1
else:
idx_end=find_nz(version_format, '[',idx)
to_print+=version_format[idx:idx_end]
idx=idx_end
return (to_print.format(*version), to_print.format('MAJOR','MINOR','PATCH','PRE-RELEASE','BUILD'))
# Special print
def type_print(x):
if(type(x) == str):
return f"\'{x}\'"
# elif(type(x) == list):
# return '[\n' + dump_array(x) + ']'
# elif(type(x) == dict):
# return '{\n' + dump_dict(x) + '}'
else:
return f"{x}"
# Dump dictionary
def dump_dict(dictionary):
output = "{\n"
for key in dictionary:
element = dictionary[key]
output += f" {type_print(key)} = {type_print(element)}\n"
output += "}\n"
return output
# Dump array
def dump_array(array):
output = "[\n"
for element in array:
output += f" {type_print(element)}\n"
output += "]\n"
return output
# Recursive deep copy
def deep_copy(array):
array_copy = []
for x in array:
if(type(x) == list):
array_copy.append(deep_copy(x))
else:
array_copy.append(x)
return array_copy
# Garbfield
def word_dissect(word, size, word_length):
mask = (1 << word_length) - 1
return [((word >> x) & mask) for x in range((size - 1) * word_length, -1, -word_length)]
# Find the maximum value in an array, after applying a key function to the elements
def find_max(array, key=lambda x:x):
max_val = key(array[0])
for element in array[1:]:
max_val = max(max_val, key(element))
return max_val
# Recursive dump array
def rec_dump_array(array, depth = 1, level = 1):
output = "["
for i in range(len(array)):
if((type(array[i]) == list) and (depth != 0) and (i == 0)):
output = output + rec_dump_array(array[i], depth - 1, level + 1) + ','
else:
output = output + '\n' + (' ' * level)
if(type(array[i]) == str):
output = output + '\'' + array[i] + '\''
elif((type(array[i]) == list) and (depth != 0)):
output = output + rec_dump_array(array[i], depth - 1, level + 1)
else:
output = output + str(array[i])
output = output + ','
output = output + '\n' + (' ' * (level - 1)) + ']'
return output