diff options
author | Eddy Pedroni <epedroni@pm.me> | 2024-09-24 07:17:41 +0200 |
---|---|---|
committer | Eddy Pedroni <epedroni@pm.me> | 2024-09-24 07:19:55 +0200 |
commit | c644f52839fe320bf4c8c21661ae8534fd60b37d (patch) | |
tree | 870e74fda36bba298b62eba5e4c145506496fe06 | |
parent | 22bfa00d56233ddb93394b23313efb7c2fcebbc8 (diff) |
Refactor Card out of parser.py, implement brutal scheduler constructor
-rw-r--r-- | src/card.py | 10 | ||||
-rw-r--r-- | src/parser.py | 6 | ||||
-rw-r--r-- | src/scheduler_brutal.py | 33 | ||||
-rw-r--r-- | src/scheduler_brutal_unittest.py | 68 |
4 files changed, 113 insertions, 4 deletions
diff --git a/src/card.py b/src/card.py new file mode 100644 index 0000000..c4f7541 --- /dev/null +++ b/src/card.py @@ -0,0 +1,10 @@ +""" +Defines a struct representing a single card. The struct takes the form: + +(front, back) +""" + +from collections import namedtuple + +Card = namedtuple('Card', ['front', 'back']) + diff --git a/src/parser.py b/src/parser.py index 4836bdf..d2b4ce7 100644 --- a/src/parser.py +++ b/src/parser.py @@ -24,15 +24,13 @@ FRONT The cards are represented in dictionary entries of the form: -id: (front_text, back_text) +id: card.Card """ from pathlib import Path -from collections import namedtuple from enum import Enum from typing import TextIO, Iterator - -Card = namedtuple('Card', ['front', 'back']) +from card import Card def _getCard(front_lines: list[str], back_lines: list[str]) -> tuple[int, Card]: front_text = "".join(front_lines).strip() diff --git a/src/scheduler_brutal.py b/src/scheduler_brutal.py new file mode 100644 index 0000000..0c7463f --- /dev/null +++ b/src/scheduler_brutal.py @@ -0,0 +1,33 @@ +""" +""" + +HISTORY_DEPTH = 8 + +class SchedulerBrutal: + def __init__(self, cards: dict, state: dict): + self._cards = cards + self._state = {} + + # Synchronise state with current card collection + for id, card in self._cards.items(): + history = state.get(id, [None] * HISTORY_DEPTH) + + # adjust history if depth has changed + if len(history) > HISTORY_DEPTH: + history = history[-HISTORY_DEPTH:] + elif len(history) < HISTORY_DEPTH: + history = ([None] * (HISTORY_DEPTH - len(history))) + history + + self._state[id] = history + + def practice(self, size: int) -> dict: + pass + + def test(self, size: int) -> dict: + pass + + def update(self, results: dict) -> None: + pass + + def getState(self) -> dict: + return self._state diff --git a/src/scheduler_brutal_unittest.py b/src/scheduler_brutal_unittest.py new file mode 100644 index 0000000..98de2f1 --- /dev/null +++ b/src/scheduler_brutal_unittest.py @@ -0,0 +1,68 @@ +import pytest +import scheduler_brutal +from scheduler_brutal import SchedulerBrutal as UUT +from card import Card + +# Force HISTORY_DEPTH to simplify testing +scheduler_brutal.HISTORY_DEPTH = 3 + +#-------------------------------------------------------------------------- +# +#-------------------------------------------------------------------------- + +#-------------------------------------------------------------------------- +# +#-------------------------------------------------------------------------- + +#-------------------------------------------------------------------------- +# +#-------------------------------------------------------------------------- + +#-------------------------------------------------------------------------- +# +#-------------------------------------------------------------------------- + +#-------------------------------------------------------------------------- +# +#-------------------------------------------------------------------------- + +#-------------------------------------------------------------------------- +# +#-------------------------------------------------------------------------- + +#-------------------------------------------------------------------------- +# State corrections +#-------------------------------------------------------------------------- +def test_stateWhenCardsChanged(): + cards = {"0": Card("f", "b"), "1": Card("a", "b")} + + initial_state = {"0": [1, 0, 1], "-1": [0, 0, 0]} + expected_state = {"0": [1, 0, 1], "1": [None, None, None]} + + uut = UUT(cards, initial_state) + + assert uut.getState() == expected_state + +def test_stateWhenHistoryDepthIncreased(): + scheduler_brutal.HISTORY_DEPTH = 5 + + cards = {"0": Card("f", "b"), "1": Card("a", "b"), "2": Card("new", "new")} + + initial_state = {"0": [1, 0, 1], "1": [0, 0, 0]} + expected_state = {"0": [None, None, 1, 0, 1], "1": [None, None, 0, 0, 0], "2": [None] * 5} + + uut = UUT(cards, initial_state) + + assert uut.getState() == expected_state + +def test_stateWhenHistoryDepthDecreased(): + scheduler_brutal.HISTORY_DEPTH = 1 + + cards = {"0": Card("f", "b"), "1": Card("a", "b"), "2": Card("new", "new")} + + initial_state = {"0": [1, 0, 0], "1": [0, 0, 1]} + expected_state = {"0": [0], "1": [1], "2": [None]} + + uut = UUT(cards, initial_state) + + assert uut.getState() == expected_state |