forked from openMetadataInitiative/openMINDS_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathby_name.py.txt
More file actions
54 lines (50 loc) · 2.13 KB
/
by_name.py.txt
File metadata and controls
54 lines (50 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
@classmethod
def instances(cls):
return [value for value in cls.__dict__.values() if isinstance(value, cls)]
@classmethod
def by_name(
cls,
name: str,
match: str = "equals",
all: bool = False,
):
"""
Search for instances in the openMINDS instance library based on their name.
This includes properties "name", "lookup_label", "family_name", "full_name", "short_name", "abbreviation", and "synonyms".
Note that not all metadata classes have a name.
Args:
name (str): a string to search for.
match (str, optional): either "equals" (exact match - default) or "contains".
all (bool, optional): Whether to return all objects that match the name, or only the first. Defaults to False.
"""
namelike_properties = ("name", "lookup_label", "family_name", "full_name", "short_name", "abbreviation")
if cls._instance_lookup is None:
cls._instance_lookup = {}
for instance in cls.instances():
keys = []
for prop_name in namelike_properties:
if hasattr(instance, prop_name):
keys.append(getattr(instance, prop_name))
if hasattr(instance, "synonyms"):
for synonym in instance.synonyms or []:
keys.append(synonym)
for key in keys:
if key in cls._instance_lookup:
cls._instance_lookup[key].append(instance)
else:
cls._instance_lookup[key] = [instance]
if match == "equals":
matches = cls._instance_lookup.get(name, None)
elif match == "contains":
matches = []
for key, instances in cls._instance_lookup.items():
if name in key:
matches.extend(instances)
else:
raise ValueError("'match' must be either 'equals' or 'contains'")
if all:
return matches
elif len(matches) > 0:
return matches[0]
else:
return None