Skip to content

Commit

Permalink
Refactoring for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWatzinger committed Nov 26, 2024
1 parent 5de3bff commit 68ea710
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 35 deletions.
2 changes: 1 addition & 1 deletion openatlas/database/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions openatlas/models/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
12 changes: 4 additions & 8 deletions openatlas/models/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]]:
Expand Down
4 changes: 2 additions & 2 deletions openatlas/models/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
37 changes: 16 additions & 21 deletions openatlas/models/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 68ea710

Please sign in to comment.