-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMiscKit.py
65 lines (39 loc) · 1.17 KB
/
MiscKit.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
import datetime, sys, traceback, json
from threading import Lock
locks = {}
# logger for errors
def log_error(*args):
print("\n", datetime.datetime.now())
for i in args:
print(i)
print(traceback.print_exception(*sys.exc_info()))
# logger for messages
def log_message(*args):
print("\n", datetime.datetime.now())
for i in args:
print(i)
# load json file
def load(file_name, data_dict):
global locks
if file_name not in locks:
locks[file_name] = Lock()
locks[file_name].acquire()
try:
with open(file_name) as json_file:
data = json.load(json_file)
for key, value in data.items():
data_dict[key] = value
except FileNotFoundError:
locks[file_name].release()
save(file_name, data_dict)
load(file_name, data_dict)
locks[file_name].release()
# save json file
def save(file_name, data_dict):
global locks
if file_name not in locks:
locks[file_name] = Lock()
locks[file_name].acquire()
with open("data_list.json", "w") as json_file:
json.dump(data_dict, json_file, indent=4)
locks[file_name].release()