diff options
author | Eddy Pedroni <epedroni@pm.me> | 2024-09-25 19:10:24 +0200 |
---|---|---|
committer | Eddy Pedroni <epedroni@pm.me> | 2024-09-25 19:10:24 +0200 |
commit | 7f6ab3e4c535eb0cc8b8dfdaa591e1cd7131e537 (patch) | |
tree | c8343dc13296b760ea94144115b758df28d4d328 /src/state_json.py | |
parent | ebc193873c382bd814730e8ea3032604ebb4a851 (diff) |
Extract state save/load to helper module
Diffstat (limited to 'src/state_json.py')
-rw-r--r-- | src/state_json.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/state_json.py b/src/state_json.py new file mode 100644 index 0000000..a0b487e --- /dev/null +++ b/src/state_json.py @@ -0,0 +1,24 @@ +""" +Helper functions to store scheduler state as json +""" + +import json +from pathlib import Path + +def save(file: str, state: dict) -> None: + """ + Dump the specified state dictionary in JSON format + """ + with open(file, "w") as f: + json.dump(state, f) + +def load(file: str) -> dict: + """ + Load the state from the specified file and return + an empty dictionary silently if the file doesn't exist. + """ + try: + with open(file, "r") as f: + return json.load(f) + except: + return {} |