Skip to content

Commit

Permalink
Use sets instead of lists when performing checks
Browse files Browse the repository at this point in the history
  • Loading branch information
edmondchuc committed Jan 4, 2023
1 parent dba8e71 commit 890df0f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/linkeddata_api/domain/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class SubjectPredicates(BaseModel):

class PredicateObjects(BaseModel):
predicate: URI
objects: list[Union[URI, Literal]]
objects: set[Union[URI, Literal]]


class Resource(BaseModel):
Expand Down
57 changes: 26 additions & 31 deletions src/linkeddata_api/domain/viewer/resource/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,36 +130,29 @@ def get(uri: str, sparql_endpoint: str) -> domain.schema.Resource:

result = data.sparql.post(query, sparql_endpoint).json()

try:
result = _add_rows_for_rdf_list_items(result, uri, sparql_endpoint)
label = domain.label.get(uri, sparql_endpoint) or uri
types, properties = _get_types_and_properties(result, uri, sparql_endpoint)

profile = ""
if exists_uri("https://w3id.org/tern/ontologies/tern/MethodCollection", types):
profile = "https://w3id.org/tern/ontologies/tern/MethodCollection"
properties = method_profile(properties)
elif exists_uri("https://w3id.org/tern/ontologies/tern/Method", types):
profile = "https://w3id.org/tern/ontologies/tern/Method"
properties = method_profile(properties)

# incoming_properties = _get_incoming_properties(uri, sparql_endpoint)

return domain.schema.Resource(
uri=uri,
profile=profile,
label=label,
types=types,
properties=properties,
# incoming_properties=incoming_properties,
incoming_properties=[], # TODO:
)
except data.exceptions.SPARQLNotFoundError as err:
raise err
except Exception as err:
raise data.exceptions.SPARQLResultJSONError(
f"Unexpected SPARQL result.\n{result}\n{err}"
) from err
result = _add_rows_for_rdf_list_items(result, uri, sparql_endpoint)
label = domain.label.get(uri, sparql_endpoint) or uri
types, properties = _get_types_and_properties(result, uri, sparql_endpoint)

profile = ""
if exists_uri("https://w3id.org/tern/ontologies/tern/MethodCollection", types):
profile = "https://w3id.org/tern/ontologies/tern/MethodCollection"
properties = method_profile(properties)
elif exists_uri("https://w3id.org/tern/ontologies/tern/Method", types):
profile = "https://w3id.org/tern/ontologies/tern/Method"
properties = method_profile(properties)

# incoming_properties = _get_incoming_properties(uri, sparql_endpoint)

return domain.schema.Resource(
uri=uri,
profile=profile,
label=label,
types=types,
properties=properties,
# incoming_properties=incoming_properties,
incoming_properties=[], # TODO:
)


@log_time
Expand Down Expand Up @@ -319,11 +312,12 @@ def _get_types_and_properties(
)

found = False

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

if not found:
properties.append(
Expand All @@ -341,6 +335,7 @@ def _get_types_and_properties(
# Sort all property objects by label.
properties.sort(key=lambda x: x.predicate.label)
for property_ in properties:
property_.objects = list(property_.objects)
property_.objects.sort(key=sort_property_objects)

return types, properties

0 comments on commit 890df0f

Please sign in to comment.