-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
61 lines (41 loc) · 1.3 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
__author__ = "Jie Lei"
import os
import json
import pickle as pickle
def read_json_lines(file_path):
with open(file_path, "r") as f:
lines = []
for l in f.readlines():
loaded_l = json.loads(l.strip("\n"))
lines.append(loaded_l)
return lines
def save_json(data, file_path):
with open(file_path, "w") as f:
json.dump(data, f)
def save_json_pretty(data, file_path):
"""save formatted json, use this one for some json config files"""
with open(file_path, "w") as f:
f.write(json.dumps(data, indent=4, sort_keys=True))
def load_json(file_path):
with open(file_path, "r") as f:
return json.load(f)
def save_pickle(data, data_path):
with open(data_path, "wb") as f:
pickle.dump(data, f)
def load_pickle(file_path):
with open(file_path, "rb") as f:
return pickle.load(f)
def mkdirp(p):
if not os.path.exists(p):
os.makedirs(p)
def files_exist(filepath_list):
"""check whether all the files exist"""
for ele in filepath_list:
if not os.path.exists(ele):
return False
return True
# Added by jumperkables
def merge_two_dicts(x, y):
z = x.copy() # start with x's keys and values
z.update(y) # modifies z with y's keys and values & returns None
return z