summaryrefslogtreecommitdiffstats
path: root/flashcards-project/src/flashcards/state_json.py
diff options
context:
space:
mode:
Diffstat (limited to 'flashcards-project/src/flashcards/state_json.py')
-rw-r--r--flashcards-project/src/flashcards/state_json.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/flashcards-project/src/flashcards/state_json.py b/flashcards-project/src/flashcards/state_json.py
new file mode 100644
index 0000000..673d904
--- /dev/null
+++ b/flashcards-project/src/flashcards/state_json.py
@@ -0,0 +1,23 @@
+"""
+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 {}