diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/card.py | 4 | ||||
-rw-r--r-- | src/parser.py | 8 | ||||
-rw-r--r-- | src/scheduler.py | 8 | ||||
-rw-r--r-- | src/scheduler_brutal.py | 10 | ||||
-rw-r--r-- | src/scheduler_brutal_unittest.py | 52 |
5 files changed, 41 insertions, 41 deletions
diff --git a/src/card.py b/src/card.py index 9f888e6..c2243a6 100644 --- a/src/card.py +++ b/src/card.py @@ -9,5 +9,5 @@ from hashlib import md5 Card = namedtuple('Card', ['front', 'back']) -def getId(card: Card) -> int: - return int(md5((card.front + card.back).encode("utf-8")).hexdigest(), 16) +def getId(card: Card) -> str: + return md5((card.front + card.back).encode("utf-8")).hexdigest() diff --git a/src/parser.py b/src/parser.py index ba7d247..11bb0c6 100644 --- a/src/parser.py +++ b/src/parser.py @@ -32,14 +32,14 @@ from enum import Enum from typing import TextIO, Iterator from card import Card, getId -def _getCard(front_lines: list[str], back_lines: list[str]) -> tuple[int, Card]: +def _getCard(front_lines: list[str], back_lines: list[str]) -> tuple[str, Card]: front_text = "".join(front_lines).strip() back_text = "".join(back_lines).strip() card = Card(front_text, back_text) id = getId(card) return id, card -def _getCards(f: TextIO) -> Iterator[tuple[id, Card]]: +def _getCards(f: TextIO) -> Iterator[tuple[str, Card]]: class State(Enum): PARSE_FRONT = 1, PARSE_BACK = 2 @@ -90,14 +90,14 @@ def _getCards(f: TextIO) -> Iterator[tuple[id, Card]]: yield _getCard(front_lines, back_lines) -def parseFile(path: str) -> dict[int, Card]: +def parseFile(path: str) -> dict[str, Card]: """ Parse a .fcard file and return a dictionary of Card instances indexed by ID. """ with open(path, "r") as f: return {id : card for id, card in _getCards(f)} -def parseFiles(paths: list[str]) -> dict[int, Card]: +def parseFiles(paths: list[str]) -> dict[str, Card]: """ Parse a list of .fcard files and return a dictionary of Card instances indexed by ID. """ diff --git a/src/scheduler.py b/src/scheduler.py index 67e204a..575ec9d 100644 --- a/src/scheduler.py +++ b/src/scheduler.py @@ -4,16 +4,16 @@ from card import Card class Scheduler(Protocol): @abstractmethod - def __init__(self, cards: dict[int, Card], state: dict): raise NotImplementedError + def __init__(self, cards: dict[str, Card], state: dict): raise NotImplementedError @abstractmethod - def practice(self, size: int) -> list[int]: raise NotImplementedError + def practice(self, size: int) -> list[str]: raise NotImplementedError @abstractmethod - def test(self, size: int) -> list[int]: raise NotImplementedError + def test(self, size: int) -> list[str]: raise NotImplementedError @abstractmethod - def update(self, results: dict[int, int]) -> None: raise NotImplementedError + def update(self, results: dict[str, int]) -> None: raise NotImplementedError @abstractmethod def getState(self) -> dict: raise NotImplementedError diff --git a/src/scheduler_brutal.py b/src/scheduler_brutal.py index e4e7ee2..a476932 100644 --- a/src/scheduler_brutal.py +++ b/src/scheduler_brutal.py @@ -8,7 +8,7 @@ from random import shuffle HISTORY_DEPTH = 8 class SchedulerBrutal(Scheduler): - def __init__(self, cards: dict[int, Card], state: dict): + def __init__(self, cards: dict[str, Card], state: dict): self._cards = cards self._state = {} @@ -24,13 +24,13 @@ class SchedulerBrutal(Scheduler): self._state[id] = history - def practice(self, size: int) -> list[int]: + def practice(self, size: int) -> list[str]: return self._schedule(size) - def test(self, size: int) -> list[int]: + def test(self, size: int) -> list[str]: return self._schedule(size) - def update(self, results: dict[int, int]) -> None: + def update(self, results: dict[str, int]) -> None: # Add card result to sliding window, or None if card was not shown self._state = {id: history[1:] + [results.get(id, None)] for id, history in self._state.items()} @@ -51,7 +51,7 @@ class SchedulerBrutal(Scheduler): def _exposureIndex(history: list) -> float: return sum([i + 1 for i, h in enumerate(history) if h is not None]) - def _schedule(self, size: int) -> list[int]: + def _schedule(self, size: int) -> list[str]: weights = range(10, 10 + HISTORY_DEPTH) cards = [id for id, card in self._cards.items()] diff --git a/src/scheduler_brutal_unittest.py b/src/scheduler_brutal_unittest.py index 779d2c3..8dc72fe 100644 --- a/src/scheduler_brutal_unittest.py +++ b/src/scheduler_brutal_unittest.py @@ -10,22 +10,22 @@ scheduler_brutal.HISTORY_DEPTH = 3 # Scheduling behaviour #-------------------------------------------------------------------------- def test_scheduling(): - cards = {id: Card("", "") for id in range(0, 10)} + cards = {str(id): Card("", "") for id in range(0, 10)} state = { - 0: [1, 1, 1], - 1: [0, 0, 0], - 2: [0, 0, 1], - 3: [1, 0, 0], - - 4: [None, None, 1 ], - 5: [None, 1, None], - 6: [1, None, None], - 7: [None, None, 0 ], - 8: [0, 0, None], - 9: [None, None, None], + "0": [1, 1, 1], + "1": [0, 0, 0], + "2": [0, 0, 1], + "3": [1, 0, 0], + + "4": [None, None, 1 ], + "5": [None, 1, None], + "6": [1, None, None], + "7": [None, None, 0 ], + "8": [0, 0, None], + "9": [None, None, None], } - expected_priority = [9, 6, 5, 7, 8, 4, 1, 3, 2, 0] + expected_priority = ["9", "6", "5", "7", "8", "4", "1", "3", "2", "0"] uut = UUT(cards, state) @@ -36,14 +36,14 @@ def test_scheduling(): # State update #-------------------------------------------------------------------------- def test_stateUpdate(): - cards = {0: Card("f", "b"), 1: Card("a", "b"), 2: Card("c", "d")} - state = {0: [1, 0, 1], 1: [1, 0, 0], 2: [0, 0, 1]} + cards = {"0": Card("f", "b"), "1": Card("a", "b"), "2": Card("c", "d")} + state = {"0": [1, 0, 1], "1": [1, 0, 0], "2": [0, 0, 1]} uut = UUT(cards, state) # Unknown IDs in the result are silently ignored - result = {0: 1, 1: 0, 3: 0} - expected_state = {0: [0, 1, 1], 1: [0, 0, 0], 2: [0, 1, None]} + result = {"0": 1, "1": 0, "3": 0} + expected_state = {"0": [0, 1, 1], "1": [0, 0, 0], "2": [0, 1, None]} uut.update(result) @@ -53,10 +53,10 @@ def test_stateUpdate(): # State corrections #-------------------------------------------------------------------------- def test_stateWhenCardsChanged(): - cards = {0: Card("f", "b"), 1: Card("a", "b")} + 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]} + initial_state = {"0": [1, 0, 1], "2": [0, 0, 0]} + expected_state = {"0": [1, 0, 1], "1": [None, None, None]} uut = UUT(cards, initial_state) @@ -65,10 +65,10 @@ def test_stateWhenCardsChanged(): def test_stateWhenHistoryDepthIncreased(): scheduler_brutal.HISTORY_DEPTH = 5 - cards = {0: Card("f", "b"), 1: Card("a", "b"), 2: Card("new", "new")} + 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} + 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) @@ -77,10 +77,10 @@ def test_stateWhenHistoryDepthIncreased(): def test_stateWhenHistoryDepthDecreased(): scheduler_brutal.HISTORY_DEPTH = 1 - cards = {0: Card("f", "b"), 1: Card("a", "b"), 2: Card("new", "new")} + 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]} + initial_state = {"0": [1, 0, 0], "1": [0, 0, 1]} + expected_state = {"0": [0], "1": [1], "2": [None]} uut = UUT(cards, initial_state) |