Skip to content

Commit

Permalink
Merge pull request #1 from r888800009/main
Browse files Browse the repository at this point in the history
Add `to_dict()` function to allow card serialization
  • Loading branch information
vlopezferrando authored Oct 2, 2024
2 parents c6012f9 + e008a4f commit 615d021
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
17 changes: 17 additions & 0 deletions simple_spaced_repetition.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,20 @@ def options(self):
Card("reviewing", td(days=4), self.ease),
]
return list(zip(["again", "hard", "good", "easy"], options))

def to_dict(self):
return {
"status": self.status,
"step": self.step,
"interval": self.interval.total_seconds() if self.interval else None,
"ease": self.ease,
}

@classmethod
def from_dict(cls, card_dict):
return cls(
status=card_dict["status"],
interval=td(seconds=card_dict["interval"]) if card_dict["interval"] else None,
ease=card_dict["ease"],
step=card_dict["step"],
)
11 changes: 11 additions & 0 deletions test_simple_spaced_repetition.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ def test_relearning(self):
check_values(card, "ease", 3.0, 3.0, 3.0, 3.0)
check_values(card, "interval", _minutes(1), _minutes(6), _days(1), _days(4))

def test_serialize(self):
card = Card(status=RELEARNING, ease=3.0, step=0, interval=_days(1))
card_dict = card.to_dict()
assert card_dict["status"] == RELEARNING
assert card_dict["interval"] == 86400.0
assert card_dict["ease"] == 3.0
assert card_dict["step"] == 0

card2 = Card.from_dict(card_dict)
assert card2.to_dict() == card_dict
assert card2.interval == _days(1)

if __name__ == "__main__":
unittest.main()

0 comments on commit 615d021

Please sign in to comment.