forked from spegelius/filaswitch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
73 lines (63 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
import os
def is_windows():
"""
Check if OS is Windows
"""
win = ['nt']
if os.name in win:
return True
return False
def load_status(status_file):
"""
Load status data from file
:param status_file: file path
:return:
"""
try:
status = {}
with open(status_file, 'r') as sf:
for line in sf.readlines():
line = line.strip()
line = line.strip("\n")
if line:
vals = line.split(":")
status[vals[0]] = vals[1]
except FileNotFoundError:
return {}
except Exception as e:
print(e)
raise IOError("Cannot open file %s" %status_file)
return status
def save_status_file(status_file: str, status: dict):
"""
Write status data to file
:param status_file: file path
:param status: status dict
:return: none
"""
try:
if os.path.exists(status_file):
os.remove(status_file)
with open(status_file, 'w') as sf:
sf.writelines(["%s:%s\n" %(key, val) for key, val in status.items()])
except Exception as e:
print(e)
raise
def is_float_zero(value: float, accuracy: int):
"""
Checks if given float value is zero
:param value: float value
:param accuracy: decimals to use for the check
:return: true or false
"""
limit = 0.1**accuracy
if value == 0.0:
return True
elif value > 0 and value < limit:
return True
elif value < 0 and value > -limit:
return True
return False
if __name__ == "__main__":
save_status_file(".teststatus", {"data1": "dtaa"})
print(load_status(".teststatus"))