Spaces:
Building
Building
File size: 1,671 Bytes
f6b56a2 |
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 |
from pathlib import Path
import json
import logging
import pickle
YAML_SUPPORT = True
YAML_NOT_DETECTED_MESSAGE = "yaml is not installed, consider installing it by pip install PyYAML"
try:
import yaml
from yaml.loader import SafeLoader, BaseLoader
except ImportError as e:
YAML_SUPPORT = False
logging.warning(f"{e}\n{YAML_NOT_DETECTED_MESSAGE}")
class Dump:
@staticmethod
def load_yaml(path: Path, safe_load=True) -> dict:
assert YAML_SUPPORT, YAML_NOT_DETECTED_MESSAGE
with open(path) as file:
params = yaml.load(
file, Loader=SafeLoader if safe_load else BaseLoader)
return params
@staticmethod
def save_yaml(data: dict, path: Path, **kwargs):
path.parent.mkdir(parents=True, exist_ok=True)
assert YAML_SUPPORT, YAML_NOT_DETECTED_MESSAGE
with open(path, 'w') as outfile:
yaml.dump(data, outfile, **kwargs)
@staticmethod
def load_json(path: Path,) -> dict:
with open(path) as file:
params = json.load(file)
return params
@staticmethod
def save_json(data: dict, path: Path):
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, 'w') as outfile:
json.dump(data, outfile)
@staticmethod
def load_pickle(path: Path,) -> dict:
with open(path, "rb") as file:
unpickler = pickle.Unpickler(file)
params = unpickler.load()
# params = pickle.load(file)
return params
@staticmethod
def save_pickle(data: dict, path: Path):
with open(path, 'wb') as outfile:
pickle.dump(data, outfile)
|