From c644f52839fe320bf4c8c21661ae8534fd60b37d Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Tue, 24 Sep 2024 07:17:41 +0200 Subject: Refactor Card out of parser.py, implement brutal scheduler constructor --- src/card.py | 10 ++++++ src/parser.py | 6 ++-- src/scheduler_brutal.py | 33 +++++++++++++++++++ src/scheduler_brutal_unittest.py | 68 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 src/card.py create mode 100644 src/scheduler_brutal.py create mode 100644 src/scheduler_brutal_unittest.py 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 -- cgit v1.2.3