Skip to content

Commit

Permalink
Use dict and set for performance and reduce time complexity of lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
edmondchuc committed Jan 5, 2023
1 parent 890df0f commit 151008b
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/linkeddata_api/domain/viewer/resource/json/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
from typing import Union
from collections import defaultdict

from rdflib import RDF

Expand Down Expand Up @@ -224,7 +226,9 @@ def _get_types_and_properties(
) -> tuple[list[domain.schema.URI], list[domain.schema.PredicateObjects]]:

types: list[domain.schema.URI] = []
properties: list[domain.schema.PredicateObjects] = []
properties: dict[
str, set[Union[domain.schema.URI, domain.schema.Literal]]
] = defaultdict(set)

# An index of URIs with label values.
uri_label_index = _get_uri_label_index(result, uri, sparql_endpoint)
Expand Down Expand Up @@ -311,18 +315,14 @@ def _get_types_and_properties(
f"Expected type to be uri or literal but got {row['o']['type']}"
)

found = False
# Use dict and set for performance
properties[predicate].add(item)

for p in properties:
if p.predicate.value == predicate.value:
found = True
if item not in p.objects:
p.objects.add(item)

if not found:
properties.append(
domain.schema.PredicateObjects(predicate=predicate, objects=[item])
)
# Convert to a list of PredicateObjects
properties = [
domain.schema.PredicateObjects(predicate=k, objects=v)
for k, v in properties.items()
]

# Duplicates may occur due to processing RDF lists.
# Remove duplicates, if any.
Expand Down

0 comments on commit 151008b

Please sign in to comment.