Skip to content

Commit 343b4a8

Browse files
committed
Fix by_name() method
1 parent 725591b commit 343b4a8

1 file changed

Lines changed: 32 additions & 23 deletions

File tree

fairgraph/kgobject.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ def by_name(
866866
"""
867867
Retrieve an instance from the Knowledge Graph based on its name.
868868
869-
This includes properties "name", "lookup_label", "family_name", "full_name", "short_name", and "synonyms".
869+
This includes properties "name", "lookup_label", "family_name", "full_name", "short_name", "abbreviation", and "synonyms".
870870
871871
Note that not all metadata classes have a name.
872872
@@ -884,7 +884,7 @@ def by_name(
884884
release_status = handle_scope_keyword(scope, release_status)
885885
# todo: move this to openminds generation, and include only in those subclasses
886886
# that have a name-like property
887-
namelike_properties = ("name", "lookup_label", "family_name", "full_name", "short_name", "synonyms")
887+
namelike_properties = ("name", "lookup_label", "family_name", "full_name", "short_name", "abbreviation", "synonyms")
888888
objects = []
889889
if client:
890890
kwargs = dict(space=space, release_status=release_status, api="query", follow_links=follow_links)
@@ -893,36 +893,45 @@ def by_name(
893893
kwargs[prop_name] = name
894894
break
895895
objects = cls.list(client, **kwargs)
896+
if match == "equals":
897+
objects = [
898+
obj for obj in objects
899+
if any(
900+
getattr(obj, prop_name, None) == name
901+
for prop_name in namelike_properties
902+
)
903+
]
896904
elif hasattr(cls, "instances"): # controlled terms, etc.
897905
if cls._instance_lookup is None:
898906
cls._instance_lookup = {}
899907
for instance in cls.instances():
900-
found = False
901-
for prop_name in namelike_properties[-1]: # handle 'synonyms' separately
908+
keys = []
909+
for prop_name in namelike_properties[:-1]: # handle 'synonyms' separately
902910
if hasattr(instance, prop_name):
903-
cls._instance_lookup[getattr(instance, prop_name)] = instance
904-
found = True
905-
break
906-
if (not found) and instance.synonyms:
907-
for synonym in instance.synonyms:
908-
cls._instance_lookup[synonym] = instance
909-
obj = cls._instance_lookup.get(name, None)
910-
if obj:
911-
objects.append(obj)
912-
if match == "equals":
913-
objects = [
914-
obj for obj in objects
915-
if any(
916-
getattr(obj, prop_name, None) == name
917-
for prop_name in namelike_properties
918-
)
919-
]
911+
keys.append(getattr(instance, prop_name))
912+
if hasattr(instance, "synonyms"):
913+
for synonym in instance.synonyms or []:
914+
keys.append(synonym)
915+
for key in keys:
916+
if key in cls._instance_lookup:
917+
cls._instance_lookup[key].append(instance)
918+
else:
919+
cls._instance_lookup[key] = [instance]
920+
if match == "equals":
921+
objects = cls._instance_lookup.get(name, None)
922+
elif match == "contains":
923+
objects = []
924+
for key, instances in cls._instance_lookup.items():
925+
if name in key:
926+
objects.extend(instances)
927+
else:
928+
raise ValueError("'match' must be either 'exact' or 'contains'")
920929
if len(objects) == 0:
921930
return None
922-
elif len(objects) == 1:
923-
return objects[0]
924931
elif all:
925932
return objects
933+
elif len(objects) == 1:
934+
return objects[0]
926935
else:
927936
warn("Multiple objects with the same name, returning the first. " "Use 'all=True' to retrieve them all")
928937
return objects[0]

0 commit comments

Comments
 (0)