Registry¶
- class lsst.pex.config.Registry(configBaseType=<class 'lsst.pex.config.config.Config'>)¶
Bases:
Mapping
A base class for global registries, which map names to configurables.
A registry acts like a read-only dictionary with an additional
register
method 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.Task
subclasses, but this is not required.A
Registry
has 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
psfMatchingRegistry
return a PSF matching class that has apsfMatch
method with a particular call signature.
Examples
This examples creates a configurable class
Foo
and 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
Registry
instance calledregistry
and register theFoo
configurable under the"foo"
key:>>> registry = Registry() >>> registry.register("foo", Foo) >>> print(list(registry.keys())) ["foo"]
Now
Foo
is 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
RegistryField
configuration 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, on_none=None)¶
Create a
RegistryField
configuration 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.validate
fails if the field’s value isNone
.- multi
bool
, optional A flag to allow multiple selections in the
RegistryField
ifTrue
.- on_none: `Callable`, optional
A callable that should be invoked when
apply
is called but the selected name or names isNone
. Will be passed the field attribute proxy (RegistryInstanceDict
) and then all positional and keyword arguments passed toapply
.
- doc
- Returns:
- field
lsst.pex.config.RegistryField
RegistryField
Configuration field.
- field
- register(name, target, ConfigClass=None)¶
Add a new configurable target to the registry.
- Parameters:
- name
str
Name that the
target
is registered under. The target can be accessed later withdict
-like patterns usingname
as 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.Config
used to configure the configurable. IfNone
then the configurable’sConfigClass
attribute is used.
- name
- Raises:
- RuntimeError
Raised if an item with
name
is already in the registry.- AttributeError
Raised if
ConfigClass
isNone
andtarget
does not have aConfigClass
attribute.
Notes
If
ConfigClass
is provided then thetarget
configurable is wrapped in a new object that forwards function calls to it. Otherwise the originaltarget
is stored.
- values() an object providing a view on D's values ¶