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:
configBaseTypelsst.pex.config.Config-type

The base class for config classes in the registry.

Notes

A configurable is a callable with call signature (config, *args) Configurables typically create an algorithm or are themselves the algorithm. Often configurables are lsst.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 a psfMatch 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 called registry and register the Foo 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:
docstr

A description of the field.

defaultobject, optional

The default target for the field.

optionalbool, optional

When False, lsst.pex.config.Config.validate fails if the field’s value is None.

multibool, optional

A flag to allow multiple selections in the RegistryField if True.

on_none: `Callable`, optional

A callable that should be invoked when apply is called but the selected name or names is None. Will be passed the field attribute proxy (RegistryInstanceDict) and then all positional and keyword arguments passed to apply.

Returns:
fieldlsst.pex.config.RegistryField

RegistryField Configuration field.

register(name, target, ConfigClass=None)

Add a new configurable target to the registry.

Parameters:
namestr

Name that the target is registered under. The target can be accessed later with dict-like patterns using name as the key.

targetobj

A configurable type, usually a subclass of lsst.pipe.base.Task.

ConfigClasslsst.pex.config.Config-type, optional

A subclass of lsst.pex.config.Config used to configure the configurable. If None then the configurable’s ConfigClass attribute is used.

Raises:
RuntimeError

Raised if an item with name is already in the registry.

AttributeError

Raised if ConfigClass is None and target does not have a ConfigClass attribute.

Notes

If ConfigClass is provided then the target configurable is wrapped in a new object that forwards function calls to it. Otherwise the original target is stored.

values() an object providing a view on D's values