Initialize an object defined in a dictionary
The object needs to be importable as
f'{basePath}.{initDict[typeKey]}'
The positional and keyword arguments (if any) are contained in
"args" and "kwargs" entries in the dictionary, respectively.
This is used in `functors.CompositeFunctor.from_yaml` to initialize
a composite functor from a specification in a YAML file.
Parameters
----------
initDict : dictionary
Dictionary describing object's initialization. Must contain
an entry keyed by ``typeKey`` that is the name of the object,
relative to ``basePath``.
basePath : str
Path relative to module in which ``initDict[typeKey]`` is defined.
typeKey : str
Key of ``initDict`` that is the name of the object
(relative to `basePath`).
Definition at line 12 of file functors.py.
12 def init_fromDict(initDict, basePath='lsst.pipe.tasks.functors',
13 typeKey='functor', name=None):
14 """Initialize an object defined in a dictionary
16 The object needs to be importable as
17 f'{basePath}.{initDict[typeKey]}'
18 The positional and keyword arguments (if any) are contained in
19 "args" and "kwargs" entries in the dictionary, respectively.
20 This is used in `functors.CompositeFunctor.from_yaml` to initialize
21 a composite functor from a specification in a YAML file.
26 Dictionary describing object's initialization. Must contain
27 an entry keyed by ``typeKey`` that is the name of the object,
28 relative to ``basePath``.
30 Path relative to module in which ``initDict[typeKey]`` is defined.
32 Key of ``initDict`` that is the name of the object
33 (relative to `basePath`).
35 initDict = initDict.copy()
37 pythonType =
doImport(f
'{basePath}.{initDict.pop(typeKey)}')
39 if 'args' in initDict:
40 args = initDict.pop(
'args')
41 if isinstance(args, str):
44 element = pythonType(*args, **initDict)
45 except Exception
as e:
46 message = f
'Error in constructing functor "{name}" of type {pythonType.__name__} with args: {args}'
47 raise type(e)(message, e.args)