From 7eb81e526c45118a295dbfc12be01f92dc809974 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Thu, 25 Apr 2024 06:30:35 +0200 Subject: [PATCH] Remove more redundant code. --- plomtask/conditions.py | 14 -------------- plomtask/days.py | 1 - plomtask/db.py | 16 ++++++++++++++++ plomtask/processes.py | 14 -------------- 4 files changed, 16 insertions(+), 29 deletions(-) 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: -- 2.30.2