47 lines
947 B
Python
47 lines
947 B
Python
import json
|
|
import os
|
|
|
|
import yaml
|
|
|
|
|
|
def read_json(path: str) -> dict:
|
|
with open(path, "r") as f:
|
|
data = json.load(f)
|
|
return data
|
|
|
|
|
|
def write_json(data: dict, path: str) -> None:
|
|
with open(path, "w") as f:
|
|
data = json.dump(data, f)
|
|
|
|
|
|
def read_yaml(path: str) -> dict:
|
|
with open(path, "r") as f:
|
|
data = yaml.safe_load(f)
|
|
return data
|
|
|
|
|
|
def write_yaml(data: dict, path: str) -> None:
|
|
with open(path, "w") as f:
|
|
data = yaml.dump(data, f)
|
|
|
|
|
|
def is_exists(path: str) -> bool:
|
|
return os.path.exists(path)
|
|
|
|
|
|
def get_obj_config(obj):
|
|
config = {
|
|
k: v for k, v in obj.__dict__.items() if isinstance(v, (int, float, str, bool))
|
|
}
|
|
return config
|
|
|
|
|
|
def save_obj_config(obj, path) -> None:
|
|
config = get_obj_config(obj)
|
|
write_json(config, path)
|
|
|
|
|
|
def obj_config_to_str(obj) -> str:
|
|
config = get_obj_config(obj)
|
|
return "-".join([f"{k}={v}" for k, v in config.items()])
|