Skip to content

Commit

Permalink
Cast integer and Float values when reading from dict (#49)
Browse files Browse the repository at this point in the history
* only store integer value in card dict

serializing and deserializing breakes the state value

* Update models.py

* cast values from_dict

* treat rating the same as State in to_dict and from_dict

* Update pyproject.toml
  • Loading branch information
lomenzel authored Jul 20, 2024
1 parent 3b1d6e5 commit 48423c5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "fsrs"
version = "2.5.0"
version = "2.5.1"
description = "Free Spaced Repetition Scheduler"
readme = "README.md"
authors = [{ name = "Jarrett Ye", email = "[email protected]" }]
Expand Down
28 changes: 14 additions & 14 deletions src/fsrs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ def __init__(

def to_dict(self):
return_dict = {
"rating": self.rating,
"rating": self.rating.value,
"scheduled_days": self.scheduled_days,
"elapsed_days": self.elapsed_days,
"review": self.review.isoformat(),
"state": self.state,
"state": self.state.value,
}

return return_dict

@staticmethod
def from_dict(source_dict: Dict[str, Any]):
rating = source_dict["rating"]
scheduled_days = source_dict["scheduled_days"]
elapsed_days = source_dict["elapsed_days"]
rating = Rating(int(source_dict["rating"]))
scheduled_days = int(source_dict["scheduled_days"])
elapsed_days = int(source_dict["elapsed_days"])
review = datetime.fromisoformat(source_dict["review"])
state = source_dict["state"]
state = State(int(source_dict["state"]))

return ReviewLog(
rating,
Expand Down Expand Up @@ -116,7 +116,7 @@ def to_dict(self):
"scheduled_days": self.scheduled_days,
"reps": self.reps,
"lapses": self.lapses,
"state": self.state,
"state": self.state.value,
}

if hasattr(self, "last_review"):
Expand All @@ -127,13 +127,13 @@ def to_dict(self):
@staticmethod
def from_dict(source_dict: Dict[str, Any]):
due = datetime.fromisoformat(source_dict["due"])
stability = source_dict["stability"]
difficulty = source_dict["difficulty"]
elapsed_days = source_dict["elapsed_days"]
scheduled_days = source_dict["scheduled_days"]
reps = source_dict["reps"]
lapses = source_dict["lapses"]
state = source_dict["state"]
stability = float(source_dict["stability"])
difficulty = float(source_dict["difficulty"])
elapsed_days = int(source_dict["elapsed_days"])
scheduled_days = int(source_dict["scheduled_days"])
reps = int(source_dict["reps"])
lapses = int(source_dict["lapses"])
state = State(int(source_dict["state"]))

if "last_review" in source_dict:
last_review = datetime.fromisoformat(source_dict["last_review"])
Expand Down

0 comments on commit 48423c5

Please sign in to comment.