Renamed scores to scores_map to avoid confusion.

This commit is contained in:
alwayscompiling
2022-07-31 11:51:50 -04:00
parent cd81ee3140
commit b4f72b9528
+9 -10
View File
@@ -18,7 +18,7 @@ class Category:
self._name: str = name self._name: str = name
self._ascending: bool = ascending self._ascending: bool = ascending
self._time_record: bool = time_record self._time_record: bool = time_record
self._scores: dict(Score) = {} self._scores_map: dict(Score) = {}
def __str__(self) -> str: def __str__(self) -> str:
"""Creates a string from the class.""" """Creates a string from the class."""
@@ -42,21 +42,20 @@ class Category:
@property @property
def scores(self) -> "list[Score]": def scores(self) -> "list[Score]":
"""Getter for scores list.""" """Getter for scores list."""
return self._scores.values() return self._scores_map.values()
def add_score(self, score: Score) -> None: def add_score(self, score: Score) -> None:
"""Adds a score to dictionary storage. Then sorts dictionary by score value.""" """Adds a score to dictionary storage. Then sorts dictionary by score value."""
if score.user in self._scores: if score.user in self._scores_map:
del self._scores[score.user] del self._scores_map[score.user]
self._scores[score.user] = score self._scores_map[score.user] = score
self._scores = dict( self._scores_map = dict(
sorted(self._scores.items(), key=lambda x: x[1].score, reverse=not self.ascending) sorted(self._scores_map.items(), key=lambda x: x[1].score, reverse=not self.ascending)
) )
def from_dict(self, data: dict) -> None: def from_dict(self, data: dict) -> None:
"""Fills out category from passed dictionary""" """Fills out category from passed dictionary"""
self._ascending = data["ascending"] self._ascending = data["ascending"]
self._time_record = data["time_record"] self._time_record = data["time_record"]
for user, score in data["scores"].items(): for user, score in data["scores"].items():
@@ -68,12 +67,12 @@ class Category:
def get_user_score(self, user: str) -> Score: def get_user_score(self, user: str) -> Score:
"""Gets a user's score. None if doesn't exist.""" """Gets a user's score. None if doesn't exist."""
if user in self.scores: if user in self.scores:
return self._scores[user] return self._scores_map[user]
return None return None
def remove_score(self, score: Score) -> None: def remove_score(self, score: Score) -> None:
"""Removes a score from dictionary storage.""" """Removes a score from dictionary storage."""
score = self._scores.pop(score.user) score = self._scores_map.pop(score.user)
del score del score
def to_dict(self) -> dict: def to_dict(self) -> dict: