From 68ea71038885597fd426703b677b2d4e8d4fdf6b Mon Sep 17 00:00:00 2001 From: Alexander Watzinger Date: Tue, 26 Nov 2024 17:05:19 +0100 Subject: [PATCH] Refactoring for loops --- openatlas/database/network.py | 2 +- openatlas/models/checks.py | 4 +--- openatlas/models/entity.py | 12 ++++-------- openatlas/models/imports.py | 4 ++-- openatlas/models/type.py | 37 +++++++++++++++-------------------- 5 files changed, 24 insertions(+), 35 deletions(-) diff --git a/openatlas/database/network.py b/openatlas/database/network.py index cdf47e474..c825121c1 100644 --- a/openatlas/database/network.py +++ b/openatlas/database/network.py @@ -16,7 +16,7 @@ def get_ego_network(ids: set[int]) -> list[dict[str, Any]]: def get_edges( classes: list[str], - properties: list[str]) -> list[dict[str, int]]: + properties: list[str]) -> list[dict[Any, int]]: g.cursor.execute( """ SELECT l.id, l.domain_id, l.range_id diff --git a/openatlas/models/checks.py b/openatlas/models/checks.py index 3cf441b89..98e6a765c 100644 --- a/openatlas/models/checks.py +++ b/openatlas/models/checks.py @@ -28,9 +28,7 @@ def single_type_duplicates() -> list[dict[str, Any]]: def invalid_dates() -> list[Entity]: - return [ - Entity.get_by_id(row['id'], types=True) - for row in date.invalid_dates()] + return [Entity.get_by_id(row['id'], True) for row in date.invalid_dates()] def orphans() -> list[Entity]: diff --git a/openatlas/models/entity.py b/openatlas/models/entity.py index 99ebb50b6..64d923b31 100644 --- a/openatlas/models/entity.py +++ b/openatlas/models/entity.py @@ -328,8 +328,8 @@ def check_too_many_single_type_links(self) -> bool: for type_ in self.types: if type_.root[0] in type_dict: type_dict[type_.root[0]] += 1 - else: - type_dict[type_.root[0]] = 1 + continue + type_dict[type_.root[0]] = 1 for id_, count in type_dict.items(): if count > 1 and not g.types[id_].multiple: return True @@ -648,15 +648,11 @@ def invalid_preceding_dates() -> list[Link]: @staticmethod def invalid_sub_dates() -> list[Link]: - return [ - Link.get_by_id(row['id']) - for row in date.invalid_sub_dates()] + return [Link.get_by_id(row['id']) for row in date.invalid_sub_dates()] @staticmethod def get_invalid_link_dates() -> list[Link]: - return [ - Link.get_by_id(row['id']) - for row in date.invalid_link_dates()] + return [Link.get_by_id(row['id']) for row in date.invalid_link_dates()] @staticmethod def check_link_duplicates() -> list[dict[str, Any]]: diff --git a/openatlas/models/imports.py b/openatlas/models/imports.py index db501a8d3..b324cc371 100644 --- a/openatlas/models/imports.py +++ b/openatlas/models/imports.py @@ -100,9 +100,9 @@ def import_data_(project: Project, class_: str, data: list[Any]) -> None: entities: dict[str | int, dict[str, Any]] = {} for row in data: if value := row.get('openatlas_class'): - if (value.lower().replace(' ', '_') in ( + if value.lower().replace(' ', '_') in ( g.view_class_mapping['place'] + - g.view_class_mapping['artifact'])): + g.view_class_mapping['artifact']): class_ = value.lower().replace(' ', '_') entity = Entity.insert(class_, row['name'], row.get('description')) db.import_data( diff --git a/openatlas/models/type.py b/openatlas/models/type.py index 773c5b2a4..5c0a1ff95 100644 --- a/openatlas/models/type.py +++ b/openatlas/models/type.py @@ -99,12 +99,11 @@ def move_entities(self, new_type_id: int, checkbox_values: str) -> None: if new_type_id: # A new type was selected if root.multiple: cleaned_entity_ids = [] - for entity in Entity.get_by_ids(entity_ids, types=True): - if any(type_.id == int(new_type_id) - for type_ in entity.types): - delete_ids.append(entity.id) - else: - cleaned_entity_ids.append(entity.id) + for e in Entity.get_by_ids(entity_ids, types=True): + if any(type_.id == int(new_type_id) for type_ in e.types): + delete_ids.append(e.id) + continue + cleaned_entity_ids.append(e.id) entity_ids = cleaned_entity_ids if entity_ids: data = { @@ -151,14 +150,14 @@ def populate_subs(types: dict[int, Type]) -> None: type_.root[-1], type_.root) type_.category = hierarchies[type_.root[0]]['category'] - else: - type_.category = hierarchies[type_.id]['category'] - type_.multiple = hierarchies[type_.id]['multiple'] - type_.required = hierarchies[type_.id]['required'] - type_.directional = hierarchies[type_.id]['directional'] - for class_ in g.classes.values(): - if class_.hierarchies and type_.id in class_.hierarchies: - type_.classes.append(class_.name) + continue + type_.category = hierarchies[type_.id]['category'] + type_.multiple = hierarchies[type_.id]['multiple'] + type_.required = hierarchies[type_.id]['required'] + type_.directional = hierarchies[type_.id]['directional'] + for class_ in g.classes.values(): + if class_.hierarchies and type_.id in class_.hierarchies: + type_.classes.append(class_.name) @staticmethod def get_root_path( @@ -175,16 +174,12 @@ def get_root_path( @staticmethod def check_hierarchy_exists(name: str) -> list[Type]: - hierarchies = [ - root for root in g.types.values() - if root.name == name and not root.root] - return hierarchies + return [x for x in g.types.values() if x.name == name and not x.root] @staticmethod def get_hierarchy(name: str) -> Type: - return [ - root for root in g.types.values() - if root.name == name and not root.root][0] + return \ + [x for x in g.types.values() if x.name == name and not x.root][0] @staticmethod def get_tree_data(