-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtilities.py
148 lines (117 loc) · 3.93 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
from datetime import datetime
import uuid
import IO.SettingsIO as SettingsIO
from lxml import etree as ET
from xml.dom import minidom
from typing import List
from faker import Factory
# Collection of static helper methods
def format_time_string(time: str) -> str:
return "{:02d}:{:02d}".format(int(float(time)/60), int(float(time)%60))
def format_time_min(time: str) -> str:
return int(float(time)/60)
def format_time_sec(time: str) -> str:
return int(float(time)%60)
def format_time_int(time_string: str) -> int:
time_split = time_string.split(":")
min = int(time_split[0])
sec = int(time_split[1])
return (min*60) + sec
def format_time_int_min(time_string: str) -> int:
time_split = time_string.split(":")
return int(time_split[0])
def format_time_int_sec(time_string: str) -> int:
time_split = time_string.split(":")
return int(time_split[1])
def format_time_datetime(time: float) -> datetime:
return datetime.fromtimestamp(float(time))
def format_time_from_datetime_int(time_as_dt: datetime) -> int:
return time_as_dt.minute
def format_time_from_datetime_sec(time_as_dt: datetime) -> int:
return time_as_dt.second
def get_current_time() -> str:
return datetime.now().strftime("%H:%M:%S.%f")
def get_new_uuid() -> str:
return str(uuid.uuid4())
def get_colours(number: int) -> List[str]:
default_colours = ['#e6194b', # 1. Red
'#3cb44b', # 2. Green
'#ffe119', # 3. Yellow
'#4363d8', # 4. Blue
'#f58231', # 5. Orange
'#911eb4', # 6. Purple
'#42d4f4', # 7. Cyan
'#f032e6', # 8. Magenta
'#bfef45', # 9. Lime
'#fabebe', # 10. Pink
'#469990', # 11. Teal
'#dcbeff', # 12. Lavender
'#9a6324', # 13. Brown
#'#fffac8', # 14. Beige
'#800000', # 15. Maroon
'#aaffc3', # 16. Mint
'#808000', # 17. Olive
'#ffd8b1', # 18. Apricot
'#000075', # 19. Navy
'#a9a9a9', # 20. Grey
'#167E4A', # 21. Forest Green
'#470387', # 22. Dark Purple
'#FF91F7', # 23. Bright Pink
'#1EFA46', # 24. Bright Green
'#000000', # 25. Black
]
colours = []
fake = Factory.create()
for i in range(number):
if i < len(default_colours):
colours.append(default_colours[i])
else:
colour_added = False
while not colour_added:
new_colour = fake.hex_color()
if new_colour not in colours:
colours.append(new_colour)
colour_added = True
return colours
def to_float(value: str) -> float:
try:
val_float = float(value)
except ValueError:
return None
return val_float
def is_float(value: str) -> bool:
try:
val_float = float(value)
if val_float:
return True
except ValueError:
return False
def is_integer(value: str) -> bool:
try:
val_float = int(value)
if val_float:
return True
except ValueError:
return False
def convert_float_to_sf(value, sfnum: int = None) -> str:
if not sfnum:
# On initialisation settings file may not exist so default to 3 until it is.
try:
sfnum = SettingsIO.load_preference_decdp()
except:
sfnum = 3
if value != '':
decimal_val = float(value)
if decimal_val < 1000 and decimal_val > -1000:
return str(round(decimal_val, sfnum))
else:
return ("{val:.{fig}e}".format(val=decimal_val, fig=sfnum))
else:
return ''
def prettify_xml(etree: ET.Element):
et_string = ET.tostring(etree)
md_string = minidom.parseString(et_string)
return md_string.toprettyxml(indent="\t")
## Debugging methods
def trace(message: str):
print(" ".join([message, get_current_time()]))