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 | |
parent | ebc193873c382bd814730e8ea3032604ebb4a851 (diff) |
Extract state save/load to helper module
-rw-r--r-- | src/flashcard_cli.py | 21 | ||||
-rw-r--r-- | src/state_json.py | 24 | ||||
-rw-r--r-- | src/state_json_unittest.py | 13 |
3 files changed, 41 insertions, 17 deletions
diff --git a/src/flashcard_cli.py b/src/flashcard_cli.py index 95da182..7f5da34 100644 --- a/src/flashcard_cli.py +++ b/src/flashcard_cli.py @@ -1,10 +1,10 @@ import click -from json import load, dump from random import shuffle from card import Card from scheduler import getSchedulerClass, SCHEDULERS from parser import parseFiles +from state_json import save, load @click.group() def cli(): @@ -21,12 +21,7 @@ def practice(state_file, card_files, scheduler_name, count, random_flip): Run a practice session with the specified scheduler, using the provided state and card files. """ all_cards = parseFiles(card_files) - - try: - state = json.load(state_file) - except: - click.echo(f"Warning: could not load state from {state_file}, starting with clean state") - state = {} + state = load(state_file) scheduler = getSchedulerClass(scheduler_name)(all_cards, state) @@ -55,13 +50,7 @@ def test(state_file, card_files, scheduler_name, count, random_flip): Run a test session with the specified scheduler, using the provided state and card files. """ all_cards = parseFiles(card_files) - - try: - with open(state_file, "r") as f: - state = load(f) - except: - click.echo(f"Warning: could not load state from {state_file}, starting with clean state") - state = {} + state = load(state_file) scheduler = getSchedulerClass(scheduler_name)(all_cards, state) @@ -84,9 +73,7 @@ def test(state_file, card_files, scheduler_name, count, random_flip): results[id] = int(click.confirm(click.style("Correct?", bold=True))) scheduler.update(results) - - with open(state_file, "w") as f: - dump(scheduler.getState(), f) + save(state_file, scheduler.getState()) if __name__ == '__main__': cli() 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 {} diff --git a/src/state_json_unittest.py b/src/state_json_unittest.py new file mode 100644 index 0000000..e784f58 --- /dev/null +++ b/src/state_json_unittest.py @@ -0,0 +1,13 @@ +import pytest +import state_json + +def test_saveAndLoad(tmp_path): + file = tmp_path / "test.json" + state = {"key": [10, 20, None], "another_key": "value"} + + state_json.save(file, state) + + assert state_json.load(file) == state + +def test_missingFile(tmp_path): + assert state_json.load(tmp_path / "missing.json") == {} |