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
+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]