Registry¶
-
class
lsst.pex.config.Registry(configBaseType=<class 'lsst.pex.config.config.Config'>)¶ Bases:
collections.abc.MappingA base class for global registries, which map names to configurables.
A registry acts like a read-only dictionary with an additional
registermethod to add targets. Targets in the registry are configurables (see Notes).- Parameters
- configBaseType
lsst.pex.config.Config-type The base class for config classes in the registry.
- configBaseType
Notes
A configurable is a callable with call signature
(config, *args)Configurables typically create an algorithm or are themselves the algorithm. Often configurables arelsst.pipe.base.Tasksubclasses, but this is not required.A
Registryhas these requirements:All configurables added to a particular registry have the same call signature.
All configurables in a registry typically share something important in common. For example, all configurables in
psfMatchingRegistryreturn a PSF matching class that has apsfMatchmethod with a particular call signature.
Examples
This examples creates a configurable class
Fooand adds it to a registry. First, creating the configurable:>>> from lsst.pex.config import Registry, Config >>> class FooConfig(Config): ... val = Field(dtype=int, default=3, doc="parameter for Foo") ... >>> class Foo: ... ConfigClass = FooConfig ... def __init__(self, config): ... self.config = config ... def addVal(self, num): ... return self.config.val + num ...
Next, create a
Registryinstance calledregistryand register theFooconfigurable under the"foo"key:>>> registry = Registry() >>> registry.register("foo", Foo) >>> print(list(registry.keys())) ["foo"]
Now
Foois conveniently accessible from the registry itself.Finally, use the registry to get the configurable class and create an instance of it:
>>> FooConfigurable = registry["foo"] >>> foo = FooConfigurable(FooConfigurable.ConfigClass()) >>> foo.addVal(5) 8
Methods Summary
get(k[,d])items()keys()makeField(doc[, default, optional, multi])Create a
RegistryFieldconfiguration field from this registry.register(name, target[, ConfigClass])Add a new configurable target to the registry.
values()Methods Documentation
-
get(k[, d]) → D[k] if k in D, else d. d defaults to None.¶
-
items() → a set-like object providing a view on D’s items¶
-
keys() → a set-like object providing a view on D’s keys¶
-
makeField(doc, default=None, optional=False, multi=False)¶ Create a
RegistryFieldconfiguration field from this registry.- Parameters
- doc
str A description of the field.
- defaultobject, optional
The default target for the field.
- optional
bool, optional When
False,lsst.pex.config.Config.validatefails if the field’s value isNone.- multi
bool, optional A flag to allow multiple selections in the
RegistryFieldifTrue.
- doc
- Returns
- field
lsst.pex.config.RegistryField RegistryFieldConfiguration field.
- field
-
register(name, target, ConfigClass=None)¶ Add a new configurable target to the registry.
- Parameters
- name
str Name that the
targetis registered under. The target can be accessed later withdict-like patterns usingnameas the key.- targetobj
A configurable type, usually a subclass of
lsst.pipe.base.Task.- ConfigClass
lsst.pex.config.Config-type, optional A subclass of
lsst.pex.config.Configused to configure the configurable. IfNonethen the configurable’sConfigClassattribute is used.
- name
- Raises
- RuntimeError
Raised if an item with
nameis already in the registry.- AttributeError
Raised if
ConfigClassisNoneandtargetdoes not have aConfigClassattribute.
Notes
If
ConfigClassis provided then thetargetconfigurable is wrapped in a new object that forwards function calls to it. Otherwise the originaltargetis stored.
-
values() → an object providing a view on D’s values¶