165 lines
3.9 KiB
Python
165 lines
3.9 KiB
Python
"""File for Table class."""
|
|
|
|
import json
|
|
|
|
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) -> None:
|
|
"""
|
|
Default constructor for Table class.
|
|
Creates object and fills out class variables with defaults.
|
|
|
|
Args:
|
|
name (str): Table's name
|
|
"""
|
|
self._name: str = name
|
|
self._message_id: int = 0
|
|
self._categories_map: dict = {}
|
|
self._size = 3
|
|
|
|
def __str__(self) -> str:
|
|
"""
|
|
Returns Table class as a string for printing.
|
|
|
|
Returns:
|
|
str: Table class as a string
|
|
"""
|
|
return self.to_str()
|
|
|
|
@classmethod
|
|
def from_dict(cls, table_name: str, data: dict):
|
|
"""
|
|
Creates a Table class from the passed data.
|
|
|
|
Args:
|
|
table_name (str): Table name
|
|
data (dict): Rest of Table data in dictionary format
|
|
|
|
Returns:
|
|
Table: Populated Table object
|
|
"""
|
|
table = cls(table_name)
|
|
|
|
table.message_id = data["message_id"]
|
|
table.size = data["table_size"]
|
|
|
|
for cat_name, cat in data["categories"].items():
|
|
category = Category.from_dict(cat_name, cat)
|
|
table.add_category(category)
|
|
|
|
return table
|
|
|
|
@property
|
|
def categories(self) -> "list[Category]":
|
|
"""
|
|
Getter for Categories list.
|
|
|
|
Returns:
|
|
list[Category]: List of Categories in this Table
|
|
"""
|
|
return self._categories_map.values()
|
|
|
|
@property
|
|
def message_id(self) -> int:
|
|
"""
|
|
Getter for message id
|
|
|
|
Returns:
|
|
int: message id
|
|
"""
|
|
return self._message_id
|
|
|
|
@message_id.setter
|
|
def message_id(self, m_id: int) -> None:
|
|
"""
|
|
Setter for message id.
|
|
|
|
Args:
|
|
m_id (int): message id
|
|
"""
|
|
self._message_id = m_id
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
"""
|
|
Getter for name.
|
|
|
|
Returns:
|
|
str: name
|
|
"""
|
|
return self._name
|
|
|
|
@property
|
|
def size(self) -> int:
|
|
"""
|
|
Getter for size of table.
|
|
|
|
Returns:
|
|
int: Size of table
|
|
"""
|
|
return self._size
|
|
|
|
@size.setter
|
|
def size(self, size: int) -> None:
|
|
"""
|
|
Setter for size of table.
|
|
|
|
Args:
|
|
size (int): New size of table
|
|
"""
|
|
self._size = size
|
|
|
|
def add_category(self, category: Category) -> None:
|
|
"""
|
|
Adds a Category to categories map. Shouldn't exist already.
|
|
|
|
Args:
|
|
category (Category): New Category to add
|
|
"""
|
|
if category.name not in self._categories_map:
|
|
self._categories_map[category.name] = category
|
|
|
|
def get_category(self, category_name: str) -> Category:
|
|
"""
|
|
Getter for specific category. Returns None if not found.
|
|
|
|
Args:
|
|
category_name (str): name of category to return
|
|
|
|
Returns:
|
|
Category: Category corresponding to name. None if not found.
|
|
"""
|
|
if category_name in self._categories_map:
|
|
return self._categories_map[category_name]
|
|
return None
|
|
|
|
def to_dict(self) -> dict:
|
|
"""
|
|
Saves Table as a python dictionary.
|
|
Does not include table name. It should be the key of above data structure.
|
|
|
|
Returns:
|
|
dict: Table class in dictionary format
|
|
"""
|
|
data = {}
|
|
data["message_id"] = self.message_id
|
|
data["table_size"] = self.size
|
|
data["categories"] = {}
|
|
for category in self.categories:
|
|
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)
|