Added a to_str function for all classes. __str__ function uses it. highscores_data uses the to_str function for saving.
This commit is contained in:
+10
-1
@@ -30,7 +30,7 @@ class Category:
|
||||
Returns:
|
||||
str: Category class as a string
|
||||
"""
|
||||
return json.dumps(self.to_dict(), indent=4)
|
||||
return self.to_str()
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, name: str, data: dict):
|
||||
@@ -158,3 +158,12 @@ class Category:
|
||||
data["scores"][score.user] = score.value
|
||||
|
||||
return data
|
||||
|
||||
def to_str(self) -> str:
|
||||
"""
|
||||
Returns Category object as a string.
|
||||
|
||||
Returns:
|
||||
str: string of json representation of Category
|
||||
"""
|
||||
return json.dumps(self.to_dict(), indent=4)
|
||||
|
||||
+10
-1
@@ -31,7 +31,7 @@ class Guild:
|
||||
Returns:
|
||||
str: Guild class as a string
|
||||
"""
|
||||
return json.dumps(self.to_dict(), indent=4)
|
||||
return self.to_str()
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, guild_id: int, data: dict):
|
||||
@@ -226,3 +226,12 @@ class Guild:
|
||||
data["tables"][table.name] = table.to_dict()
|
||||
|
||||
return data
|
||||
|
||||
def to_str(self) -> str:
|
||||
"""
|
||||
Returns Guild object as a string.
|
||||
|
||||
Returns:
|
||||
str: string of json representation of Guild
|
||||
"""
|
||||
return json.dumps(self.to_dict(), indent=4)
|
||||
|
||||
@@ -125,4 +125,4 @@ class HighscoresData:
|
||||
with open(
|
||||
f"{HIGHSCORES_DATA_DIR}/{guild.guild_id}.json", encoding="utf-8", mode="w"
|
||||
) as file:
|
||||
file.write(guild) # should use str function
|
||||
file.write(guild.to_str()) # should use str function
|
||||
|
||||
+10
-1
@@ -26,7 +26,7 @@ class Score:
|
||||
Returns:
|
||||
str: Score class as a string
|
||||
"""
|
||||
return f"{self.user}: {self.get_str()}"
|
||||
return self.to_str()
|
||||
|
||||
@property
|
||||
def user(self) -> str:
|
||||
@@ -67,6 +67,15 @@ class Score:
|
||||
"""
|
||||
return str(self._value)
|
||||
|
||||
def to_str(self) -> str:
|
||||
"""
|
||||
Returns Score object as a string.
|
||||
|
||||
Returns:
|
||||
str: string of json representation of Score
|
||||
"""
|
||||
return f"{self.user}: {self.get_str()}"
|
||||
|
||||
|
||||
class ScoreTime(Score):
|
||||
"""Class for a time based score. Inherits from Score."""
|
||||
|
||||
+10
-1
@@ -28,7 +28,7 @@ class Table:
|
||||
Returns:
|
||||
str: Table class as a string
|
||||
"""
|
||||
return json.dumps(self.to_dict(), indent=4)
|
||||
return self.to_str()
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, table_name: str, data: dict):
|
||||
@@ -129,3 +129,12 @@ class Table:
|
||||
data["categories"][category.name] = category.to_dict()
|
||||
|
||||
return data
|
||||
|
||||
def to_str(self) -> str:
|
||||
"""
|
||||
Returns Table object as a string.
|
||||
|
||||
Returns:
|
||||
str: string of json representation of Table
|
||||
"""
|
||||
return json.dumps(self.to_dict(), indent=4)
|
||||
|
||||
Reference in New Issue
Block a user