Changing from_dict to classmethod.

This commit is contained in:
alwayscompiling
2022-07-31 22:14:53 -04:00
parent 6e6b57c49c
commit 6758f4e90d
2 changed files with 32 additions and 31 deletions
+19 -20
View File
@@ -1,6 +1,5 @@
"""File for Category class."""
from typing import Optional
import json
from data.score import Score, ScoreTime # pylint: disable=import-error
@@ -9,21 +8,30 @@ from data.score import Score, ScoreTime # pylint: disable=import-error
class Category:
"""Stores a category's information and relative scores."""
def __init__(
self,
name: str,
ascending: Optional[bool] = False,
time_record: Optional[bool] = False,
) -> None:
def __init__(self, name: str, ascending: bool, time_record: bool, score_map: dict) -> None:
self._name: str = name
self._ascending: bool = ascending
self._time_record: bool = time_record
self._scores_map: dict(Score) = {}
self._scores_map: dict = score_map
def __str__(self) -> str:
"""Creates a string from the class."""
return json.dumps(self.to_dict(), indent=4)
@classmethod
def from_dict(cls, name: str, data: dict):
"""Fills out category from passed dictionary"""
ascending = data["ascending"]
time_record = data["time_record"]
score_map = {}
for user, score in data["scores"].items():
if time_record:
score_map[user] = ScoreTime(user, score)
else:
score_map[user] = Score(user, score)
return cls(name, ascending, time_record, score_map)
@property
def ascending(self) -> bool:
"""Getter for ascending bool."""
@@ -47,23 +55,14 @@ class Category:
def add_score(self, score: Score) -> None:
"""Adds a score to dictionary storage. Then sorts dictionary by score value."""
if score.user in self._scores_map:
del self._scores_map[score.user]
self._scores_map[score.user] = score
self._scores_map[score.user].value = score.value
else:
self._scores_map[score.user] = score
self._scores_map = dict(
sorted(self._scores_map.items(), key=lambda x: x[1].score, reverse=not self.ascending)
)
def from_dict(self, data: dict) -> None:
"""Fills out category from passed dictionary"""
self._ascending = data["ascending"]
self._time_record = data["time_record"]
for user, score in data["scores"].items():
if self.time_record:
self.add_score(ScoreTime(user, score))
else:
self.add_score(Score(user, score))
def get_user_score(self, user: str) -> Score:
"""Gets a user's score. None if doesn't exist."""
if user in self.scores:
+13 -11
View File
@@ -1,6 +1,5 @@
"""File for Table class."""
from typing import Optional
import json
from data.category import Category # pylint: disable=import-error
@@ -9,14 +8,25 @@ from data.category import Category # pylint: disable=import-error
class Table:
"""Stores a table's information a list of categories."""
def __init__(self, name: str, message_id: Optional[int] = 0) -> None:
def __init__(self, name: str, message_id: int, category_map: dict) -> None:
self._name: str = name
self._message_id: int = message_id
self._categories_map: "dict[Category]" = {}
self._categories_map: dict = category_map
def __str__(self) -> str:
return json.dumps(self.to_dict(), indent=4)
@classmethod
def from_dict(cls, table_name: str, data: dict):
"""Fills out table from passed dictionary."""
message_id = data["message_id"]
category_map = {}
for cat_name, cat in data["categories"].items():
category = Category.from_dict(cat_name, cat)
category_map[cat_name] = category
return cls(table_name, message_id, category_map)
@property
def categories(self) -> "list[Category]":
"""Returns list of categories."""
@@ -42,14 +52,6 @@ class Table:
if category.name not in self._categories_map:
self._categories_map[category.name] = category
def from_dict(self, data: dict) -> None:
"""Fills out table from passed dictionary."""
self.message_id = data["message_id"]
for name, cat in data["categories"]:
category = Category(name)
category.from_dict(cat)
self.add_category(category)
def get_category(self, category_name: str) -> Category:
"""Retrieves category with the gven name."""
return self._categories_map[category_name]