From 06248cfe6c5c1f8547c775a7c78aa440da6c43ab Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Sat, 30 Jul 2022 21:12:36 -0400 Subject: [PATCH] Using property decorators. --- data/score.py | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/data/score.py b/data/score.py index 38281d8..92947ad 100644 --- a/data/score.py +++ b/data/score.py @@ -6,44 +6,51 @@ from typing import Optional class Score: """Parent class for Scores. Stores common information for a Score.""" + # no user setter, it should never change. + def __init__(self, user: str, score: Optional[int] = -1) -> None: self._user: str = user self._score: int = score - def get_score(self) -> int: - """Gets score's value""" - return self._score - - def get_score_str(self) -> str: - """Gets score's value as a string.""" - return str(self._score) - - def get_user(self) -> str: + @property + def user(self) -> str: """Gets score's user.""" return self._user - def set_score(self, score: int) -> None: + @property + def score(self) -> int: + """Gets score's value""" + return self._score + + @score.setter + def score(self, score: int) -> None: """Sets score's value""" self._score = score + def get_str(self) -> str: + """Gets score's value as a string.""" + return str(self._score) -class TimeScore(Score): + +class ScoreTime(Score): """Class for a time based score.""" def __init__(self, user: str, score: float) -> None: super().__init__(user) self._score: float = score - def get_score(self) -> float: + @property + def score(self) -> float: """Gets score's value""" return self._score - def get_score_str(self) -> str: + @score.setter + def score(self, score: float) -> None: + """Sets score's value""" + self._score = score + + def get_str(self) -> str: """Gets score's value as a string. Returns in time format.""" minutes: int = self._score / 60 seconds: float = self._score % 60 return f"{minutes}:{seconds}" - - def set_score(self, score: float) -> None: - """Sets score's value""" - self._score = score