def _initMapper(self, repoData): '''Initialize and keep the mapper in a member var. Parameters ---------- repoData : RepoData The RepoData with the properties of this Repository. ''' # rule: If mapper is: # - an object: use it as the mapper. # - a string: import it and instantiate it with mapperArgs # - a class object: instantiate it with mapperArgs mapper = repoData.cfg.mapper # if mapper is a string, import it: if isinstance(mapper, basestring): mapper = doImport(mapper) # now if mapper is a class type (not instance), instantiate it: if inspect.isclass(mapper): mapperArgs = copy.copy(repoData.cfg.mapperArgs) if mapperArgs is None: mapperArgs = {} if 'root' not in mapperArgs: mapperArgs['root'] = repoData.cfg.root mapper = mapper(parentRegistry=repoData.parentRegistry, repositoryCfg=repoData.cfg, **mapperArgs) self._mapper = mapper
def init_fromDict(initDict, basePath='lsst.pipe.tasks.functors', typeKey='functor'): """Initialize an object defined in a dictionary The object needs to be importable as '{0}.{1}'.format(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`). """ initDict = initDict.copy() # TO DO: DM-21956 We should be able to define functors outside this module pythonType = doImport('{0}.{1}'.format(basePath, initDict.pop(typeKey))) args = [] if 'args' in initDict: args = initDict.pop('args') if isinstance(args, str): args = [args] return pythonType(*args, **initDict)
def init_fromDict(initDict, basePath='lsst.pipe.tasks.functors', typeKey='functor', name=None): """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`). """ initDict = initDict.copy() # TO DO: DM-21956 We should be able to define functors outside this module pythonType = doImport(f'{basePath}.{initDict.pop(typeKey)}') args = [] if 'args' in initDict: args = initDict.pop('args') if isinstance(args, str): args = [args] try: element = pythonType(*args, **initDict) except Exception as e: message = f'Error in constructing functor "{name}" of type {pythonType.__name__} with args: {args}' raise type(e)(message, e.args) return element