From: Christian Heller Date: Thu, 25 Apr 2024 04:30:35 +0000 (+0200) Subject: Remove more redundant code. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/static/conditions?a=commitdiff_plain;h=7eb81e526c45118a295dbfc12be01f92dc809974;p=plomtask Remove more redundant code. --- diff --git a/plomtask/conditions.py b/plomtask/conditions.py index b2ecda1..8d67e5a 100644 --- a/plomtask/conditions.py +++ b/plomtask/conditions.py @@ -30,20 +30,6 @@ class Condition(BaseModel[int]): getattr(condition, name).history_from_row(row_) return condition - @classmethod - def all(cls, db_conn: DatabaseConnection) -> list[Condition]: - """Collect all Conditions and their VersionedAttributes.""" - conditions = {} - for id_, condition in cls.cache_.items(): - conditions[id_] = condition - already_recorded = conditions.keys() - for id_ in db_conn.column_all('conditions', 'id'): - if id_ not in already_recorded: - condition = cls.by_id(db_conn, id_) - assert isinstance(condition.id_, int) - conditions[condition.id_] = condition - return list(conditions.values()) - @classmethod def by_id(cls, db_conn: DatabaseConnection, id_: int | None, create: bool = False) -> Condition: diff --git a/plomtask/days.py b/plomtask/days.py index ce11200..258d38d 100644 --- a/plomtask/days.py +++ b/plomtask/days.py @@ -28,7 +28,6 @@ class Day(BaseModel[str]): """Individual days defined by their dates.""" table_name = 'days' to_save = ['comment'] - id_type = str def __init__(self, date: str, comment: str = '') -> None: self.id_: str = valid_date(date) diff --git a/plomtask/db.py b/plomtask/db.py index 634862c..2d9ae27 100644 --- a/plomtask/db.py +++ b/plomtask/db.py @@ -196,3 +196,19 @@ class BaseModel(Generic[BaseModelId]): def empty_cache(cls) -> None: """Empty class's cache.""" cls.cache_ = {} + + @classmethod + def all(cls: type[BaseModelInstance], + db_conn: DatabaseConnection) -> list[BaseModelInstance]: + """Collect all objects of class.""" + items: dict[BaseModelId, BaseModelInstance] = {} + for k, v in cls.cache_.items(): + assert isinstance(v, cls) + items[k] = v + already_recorded = items.keys() + for id_ in db_conn.column_all(cls.table_name, 'id'): + if id_ not in already_recorded: + # pylint: disable=no-member + item = cls.by_id(db_conn, id_) # type: ignore[attr-defined] + items[item.id_] = item + return list(items.values()) diff --git a/plomtask/processes.py b/plomtask/processes.py index 654e5fc..9705f17 100644 --- a/plomtask/processes.py +++ b/plomtask/processes.py @@ -34,20 +34,6 @@ class Process(BaseModel[int], ConditionsRelations): self.enables: list[Condition] = [] self.disables: list[Condition] = [] - @classmethod - def all(cls, db_conn: DatabaseConnection) -> list[Process]: - """Collect all Processes and their connected VersionedAttributes.""" - processes = {} - for id_, process in cls.cache_.items(): - processes[id_] = process - already_recorded = processes.keys() - for id_ in db_conn.column_all('processes', 'id'): - if id_ not in already_recorded: - process = cls.by_id(db_conn, id_) - assert isinstance(process.id_, int) - processes[process.id_] = process - return list(processes.values()) - @classmethod def by_id(cls, db_conn: DatabaseConnection, id_: int | None, create: bool = False) -> Process: