From 7f6ab3e4c535eb0cc8b8dfdaa591e1cd7131e537 Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Wed, 25 Sep 2024 19:10:24 +0200 Subject: Extract state save/load to helper module --- src/state_json.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/state_json.py (limited to 'src/state_json.py') 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 {} -- cgit v1.2.3