-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyrilload.py
56 lines (54 loc) · 1.69 KB
/
cyrilload.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
import json
import pickle
import jsonpickle
import logging
def load(path):
"""
Intelligent loading data from pickle and json
:param path: db file path
:return: the pickle object
"""
ext = path.split(".")[-1]
logging.info(f"Load {path}")
if ext == "pickle":
with open(path, "rb") as f:
db = pickle.load(f)
elif ext == "json":
with open(path) as f:
db = json.load(f)
else:
raise ValueError(f"Unknown extension {ext}")
return db
def save(db, name, prefix="", method="pickle"):
"""
Intelligent data save
:param db: The object to save
:param name: File name without extension
:param prefix: extension prefix file.prefix.extension
:param method: pickle | jsonpickle | json | pretty (pretty json)
"""
if prefix != "":
name += f".{prefix}"
if method == "pickle":
name += ".pickle"
elif method == "json" or method == "jsonpickle":
name += ".json"
elif method == "pretty":
name += ".pretty.json"
else:
raise ValueError(f"Unknown method {method}")
logging.info(f"Save {name}")
if method == "pickle":
with open(name,"wb") as f:
pickle.dump(db, f)
else:
with open(name,"w") as f:
if method == "json" or method == "pretty":
try:
json.dump(db, f,indent = 4 if method == "pretty" else None)
except TypeError:
s = jsonpickle.dumps(db, unpicklable=False, indent = 4 if method == "pretty" else None)
f.write(s)
else:
s = jsonpickle.dumps(db, unpicklable=False,indent=4)
f.write(s)