-
Notifications
You must be signed in to change notification settings - Fork 0
/
auxilliaries.py
126 lines (86 loc) · 2.65 KB
/
auxilliaries.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
import colorama as cr
import keyboard as kb
import random as rnd
import time
cr.init()
colors_config_r = {
'warning': cr.Fore.RED,
'accepted': cr.Fore.GREEN,
'fatal_error': cr.Fore.BLUE,
'highlight': cr.Fore.CYAN,
'atmospheric': cr.Fore.LIGHTYELLOW_EX,
'fiasco': cr.Fore.MAGENTA,
}
kb.add_hotkey('ctrl+enter', lambda: kb.write('__________'))
# text decorations
def printf(text, output_format):
print(f"{colors_config_r[output_format]}{text}{cr.Style.RESET_ALL}")
def reformat(text, fmt):
return f"{colors_config_r[fmt]}{text}{cr.Style.RESET_ALL}"
def multiline_input(): # Ctr + Enter + Enter to end
res = ""
while True:
line = input()
if line == '__________':
break
res += f"{line}\n"
return res
# string decorations
def unify_line_length(line, limit):
try:
if len(line) <= limit:
return line + ' '*(limit - len(line) + 2)
else:
return line[:limit-1] + '...'
except TypeError:
return ' ' * (limit + 2)
def unify_num_length(num, limit):
s_num = str(num)
if len(s_num) <= limit:
return s_num + ' ' * (limit - len(s_num) + 1)
else:
return '9'*limit + '+'
# operations with date and time
def convert_to_sec(time_string):
t_struct = time.strptime(time_string, '%d.%m.%y')
return time.mktime(t_struct)
def show_time_rem(d, mints=None):
if mints is None:
mints = False
def extra_zero(x):
return f'0{x}' if len(str(x)) == 1 else x
factor = int(d.days >= 0)
sign = '-' if d.days == -1 else ''
days = d.days if factor else d.days + 1
full_sec = d.seconds if factor else 86400 - d.seconds
hours = extra_zero(full_sec // 3600)
minutes = extra_zero((full_sec % 3600) // 60)
if mints:
time_string = unify_line_length(f"{sign}{days}d {hours}h {minutes}m", 12)
else:
time_string = unify_line_length(f"{sign}{days}d {hours}h", 9)
return time_string if factor else reformat(time_string, 'warning')
# common operations
def sign(x):
if x > 0:
return 1
elif x < 0:
return -1
else:
return 0
# random staff
def array_union(arr):
ans = []
for item in arr:
try:
for sub_item in item:
ans.append(sub_item)
except TypeError:
ans.append(item)
return ans
def get_random_sequence(length):
global alphabet
return ''.join([rnd.choice(alphabet) for _ in range(length)])
alphabet = array_union([[chr(ord('a') + x) for x in range(26)],
[chr(ord('A') + x) for x in range(26)],
['x' for x in range(10)]])