INNER CODE UNIT · Python
load_state
DogsTailFarmer/martin-binance · martin_binance/backup.py:181
def load_state(file_path: Path, response: type[ModelT], probe: bool = False) -> Optional[object]:
"""
Safely and ultra-fast loads the strategy state from disk.
Automatically restores data from the .bak copy if the main file is corrupted.
"""
file_path = file_path.resolve()
bak_file_path = file_path.with_suffix(file_path.suffix + ".bak")
def _try_load(path: Path) -> Optional[object]:
if not path.exists():
return None
raw_bytes = path.read_bytes()
parsed_dict = orjson.loads(raw_bytes)
return response.model_validate(parsed_dict)
try:
if file_path.exists():
model_instance = _try_load(file_path)