-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdata_storage.py
49 lines (41 loc) · 1.26 KB
/
data_storage.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
import bz2
import pickle
from typing import Any, Callable, Optional
import pickletools
def load_object(
path: str, unpickler: Optional[Callable[[bz2.BZ2File], pickle.Unpickler]] = None
) -> Any:
"""
Load an arbitrary object from the specified file.
"""
with bz2.BZ2File(path, "rb") as fd:
if unpickler is None:
return pickle.load(fd)
else:
return unpickler(fd).load()
def save_object(
path: str, obj: Any, optimize: bool = True, compress_level: int = 9
) -> None:
"""
Save an arbitrary object to the specified path.
Compression level must be in 1-9 where 9 is the highest level.
"""
with bz2.BZ2File(path, "w", compresslevel=compress_level) as fd:
content = pickle.dumps(obj)
if optimize:
content = pickletools.optimize(content)
fd.write(content)
def legacy_load_object(path: str, **kwargs: Any) -> Any:
"""
DEPRECATED
Load an arbitrary object from the specified file.
"""
with open(path, "rb") as fd:
return pickle.load(fd)
def legacy_save_object(path: str, obj: Any, **kwargs: Any) -> None:
"""
DEPRECATED
Save an arbitrary object to the specified path.
"""
with open(path, "wb") as fd:
pickle.dump(obj, fd)