-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.py
38 lines (26 loc) · 792 Bytes
/
io.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
import json
import pickle
import numpy as np
from typing import Any
def pickle_dump(path: str, data: Any) -> None:
with open(path, "wb") as f:
pickle.dump(data, f)
def pickle_load(path: str) -> Any:
with open(path, "rb") as f:
return pickle.load(f)
def npy_save(path: str, data: np.ndarray) -> None:
with open(path, "wb") as f:
np.save(f, data)
def npy_load(path: str) -> np.ndarray:
with open(path, "rb") as f:
return np.load(f)
def save_json(p, data) -> None:
with open(p, 'w') as outfile:
json.dump(data, outfile, indent=4)
def load_json(p: str) -> Any:
try:
with open(p) as json_file:
return json.load(json_file)
except Exception as e:
print(f"Error: {e} on {p}")
raise e