Get a function from one of the registry
PARAMETER |
DESCRIPTION |
key |
The registry's name. The function will be retrieved from self.
TYPE:
str
|
function_name |
The function's name, The function will be retrieved via self..get(function_name).
Can be of the form "function_name.version"
TYPE:
str
|
RETURNS |
DESCRIPTION |
Callable
|
The registered function
|
Source code in eds_scikit/resources/reg.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 | def get(
self,
key: str,
function_name: str,
):
"""
Get a function from one of the registry
Parameters
----------
key : str
The registry's name. The function will be retrieved from self.<key>
function_name : str
The function's name, The function will be retrieved via self.<key>.get(function_name).
Can be of the form "function_name.version"
Returns
-------
Callable
The registered function
"""
if not hasattr(self, key):
raise ValueError(f"eds-scikit's registry has no {key} key !")
r = getattr(self, key)
candidates = r.get_all().keys()
if function_name in candidates:
# Exact match
func = r.get(function_name)
else:
# Looking for a match excluding version string
candidates = [
func for func in candidates if function_name == func.split(".")[0]
]
if len(candidates) > 1:
# Multiple versions available, a specific one should be specified
raise ValueError(
(
f"Multiple functions are available under the name {function_name} :\n"
f"{candidates}\n"
"Please choose one of the implementation listed above."
)
)
if not candidates:
# No registered function
raise ValueError(
(
f"No function registered under the name {function_name} "
f"was found in eds-scikit's {key} registry.\n"
"If you work in AP-HP's ecosystem, you should install "
'extra resources via `pip install "eds-scikit[aphp]"'
"You can define your own and decorate it as follow:\n"
"from eds_scikit.resources import registry\n"
f"@registry.{key}('{function_name}')"
f"def your_custom_func(args, **kwargs):",
" ...",
)
)
func = r.get(candidates[0])
return func
|