-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
281 lines (220 loc) · 7.17 KB
/
utilities.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"a module w/ useful tools"
import os
import random
import secrets
from typing import Any, Sequence, Union, Optional, List, Dict, Callable
def letter_randomizer(words: Union[str, list]) -> list:
"""
Take all characters in a string (separate words by a space) and randomize them to return an "anagram"
>>> letter_randomizer("hello world")
["d", "l", "r", "w", "o", " ", "e", "l", "l", "h", "o"]
>>> letter_randomizer(["hello", "world"])
[["d", "l", "r", "w", "o"], ["l", "h", "e", "l", "o"]]
"""
if not(isinstance(words, (str, list))):
raise TypeError(
"argument must be a string (sentence or single word) or a list of words (order will be kept)")
if type(words) == str:
words = words.split(" ")
result = list()
for word in words:
result.append(list(word))
for word in result:
random.shuffle(word)
final = result.copy()
result = list()
for word in final:
string = "".join(word)
for letter in word:
string += letter
result.append(string)
return result
def list_cycle(entry: list) -> None:
"""
Enable to loop through values in a list
>>> list_cycle(["a", "b", "c"])
["b", "c", "a"]
"""
if not(isinstance(entry, list)):
raise TypeError("argument must be a list")
entry.insert(len(entry), entry.pop(0))
return
def dec2base(n: int, base: int) -> str:
"""
Convert number {n} from base 10 to base {base}
>>> dec2base(42, 2)
"101010"
"""
result = ""
if n == 0:
result = "0"
while n != 0:
r = n % base
result = str(r) + result
n = n // base
return result
def base2dec(n: int, base: int) -> int:
"""
Convert number {n} from base {base} to base 10
>>> base2dec("101010", 2)
42
"""
result = 0
power = 0
while n > 0:
result += base**power*(n % 10)
n //= 10
power += 1
return result
def strfill(string: Any, length: int, fill: str = " ", before: bool = False):
"""
Same as str.zfill but more customizable
>>> strfill("hello", 10, " ", True)
" hello"
>>> strfill("hello", 10, " ", False)
"hello "
"""
sub = str()
if not(isinstance(string, str)):
string = repr(string)
assert isinstance(string, str)
length = length - len(string)
i = 0
while len(sub) < length:
sub += fill[i % len(fill)]
i += 1
if before:
string = sub + string
else:
string = string + sub
return string
def search(iterable: Sequence, substring: Any) -> list:
"""
Returns every entry in {iterable} that contains {substring}
>>> search(["hello world", "hello", "world"], "hello")
["hello world", "hello"]
"""
result = list()
for x in iterable:
if substring in x:
result.append(x)
return result
def find_all(iterable: Sequence, x: Any) -> list:
"""
Find all occurences of {x} in {iterable}
>>> find_all([1, 2, 3, 1, 2, 3, 1, 2, 3], 2)
[1, 4, 7]
"""
result = []
if isinstance(iterable, str):
current = iterable.find(x)
while current != -1:
result.append(current)
current = iterable.find(x, current+1)
else:
for i, it in enumerate(iterable):
if x == it:
result.append(i)
return result
def chunks(data: Sequence, chunk_size: int, *, callback: Optional[Callable[[Sequence], Sequence]] = None, reverse: bool = False):
"""
Split {data} into n-sized chunks
:param data: data to split
:param chunk_size: size of each chunk
:param callback: function to apply to each chunk
:param reverse: if True, chunks will be returned in reverse order
:return: list of chunks
"""
if reverse:
result = list()
for i in range(len(data), 0, -chunk_size):
i_min = i-chunk_size if i > chunk_size else 0
if callable(callback):
temp = callback(data[i_min:i])
else:
temp = data[i_min:i]
result.append(temp)
return result[::-1]
else:
return [callback(data[i:i+chunk_size])
if callable(callback)
else data[i:i+chunk_size]
for i in range(0, len(data), chunk_size)]
def password_generator(length: int, chars: List = None):
"""
returns a randomly-generated password of length {length} containing only {chars} characters.
If chars is omitted, character used will be all printable ASCII characters
>>> password_generator(8)
"9c!;[1E@"
"""
if chars is None:
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
return ''.join(secrets.choice(chars) for _ in range(length))
def menu_generator(title: str, inputs: Sequence, output: Optional[Sequence] = None, hidden: Optional[Dict]=None) -> "choice in output":
"""
Generate a menu w/ inputs & outputs
:param title: title of the menu
:param inputs: list of inputs
:param output: list of outputs
:param hidden: dictionary of hidden choices
:return: choice in output
"""
if hidden is None:
hidden = {}
if not(isinstance(title, str) and isinstance(inputs, list) and isinstance(output, (list, type(None)))):
raise TypeError("arguments provided are incorrects")
errors = ["launch loop"]
while errors:
errors = []
os.system("cls")
print(title.upper())
for x in range(len(inputs)):
print(str(x+1) + ".", inputs[x])
choice = input("\nEnter your choice :\n")
try:
choice = int(choice)
except ValueError:
if choice in hidden:
return hidden[choice]
else:
print("your choice must be an integer")
os.system("pause")
errors.append("not int")
continue
if choice in hidden:
return hidden[choice]
elif choice < 1 or choice > len(inputs):
print("this option is out of range")
os.system("pause")
errors.append("OOB")
continue
return inputs[choice-1] if output is None else output[choice-1]
def draw_table(header: list[str], lines: list[list[str]]):
"""
Draw a table with {header} and {lines}
:param header: list of header
:param lines: list of lines
:return: None
"""
lines = [header] + lines
column_size = [0 for i in range(len(header))]
for line in lines:
for i, cell in enumerate(line):
column_size[i] = max(column_size[i], len(cell))
for i in range(1 + len(lines) * 2):
if i % 2 == 0:
print("+", end="")
for size in column_size:
print("-" * (size + 1) + "-+", end="")
print()
else:
print("|", end="")
for data, size in zip(lines[i // 2], column_size):
print(" " + data.center(size) + " |", end="")
print()
def clrscr():
"""
Clears the console
"""
from os import name, system
system('cls' if name == 'nt' else 'clear')