Skip to content

Commit

Permalink
add Card object to ReviewLog class instead of just box
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdavham committed Oct 25, 2024
1 parent 729faf3 commit 286682f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
19 changes: 10 additions & 9 deletions src/leitner_box/leitner_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from enum import IntEnum
from datetime import datetime, timedelta
from typing import Optional, Union, Any, Literal
from copy import deepcopy

class Rating(IntEnum):
"""
Expand Down Expand Up @@ -70,39 +71,39 @@ class ReviewLog:
Represents the log entry of a Card object that has been reviewed.
Attributes:
card (Card): Copy of the card object that was reviewed.
rating (Rating): The rating given to the card during the review.
review_datetime (datetime): The date and time of the review.
box (int): The box that the card was in when it was reviewed.
"""

card: Card
rating: Rating
review_datetime: datetime
box: int

def __init__(self, rating: Rating, review_datetime: datetime, box: int) -> None:
def __init__(self, card: Card, rating: Rating, review_datetime: datetime) -> None:

self.card = deepcopy(card)
self.rating = rating
self.review_datetime = review_datetime
self.box = box

def to_dict(self) -> dict[str, Union[int, str]]:
def to_dict(self) -> dict[str, Union[dict, int, str]]:

return_dict = {
"card": self.card.to_dict(),
"rating": self.rating.value,
"review_datetime": self.review_datetime.isoformat(),
"box": self.box
}

return return_dict

@staticmethod
def from_dict(source_dict: dict[str, Any]) -> "ReviewLog":

card = Card.from_dict(source_dict['card'])
rating = Rating(int(source_dict["rating"]))
review_datetime = datetime.fromisoformat(source_dict["review_datetime"])
box = int(source_dict["box"])

return ReviewLog(rating=rating, review_datetime=review_datetime, box=box)
return ReviewLog(card=card, rating=rating, review_datetime=review_datetime)

class LeitnerScheduler:
"""
Expand Down Expand Up @@ -163,7 +164,7 @@ def review_card(self, card: Card, rating: Rating, review_datetime: Optional[date
if not card_is_due:
raise RuntimeError(f"Card is not due for review until {new_card.due}.")

review_log = ReviewLog(rating, review_datetime, new_card.box)
review_log = ReviewLog(card=card, rating=rating, review_datetime=review_datetime)

if rating == Rating.Fail:

Expand Down
1 change: 0 additions & 1 deletion tests/test_leitner_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,5 +399,4 @@ def test_serialize(self):
# review_log can be serialized and de-serialized while remaining the same
review_log_dict = review_log.to_dict()
copied_review_log = ReviewLog.from_dict(review_log_dict)
assert vars(review_log) == vars(copied_review_log)
assert review_log.to_dict() == copied_review_log.to_dict()

0 comments on commit 286682f

Please sign in to comment.