LSST Applications g0265f82a02+d6b5cd48b5,g02d81e74bb+a41d3748ce,g1470d8bcf6+6be6c9203b,g2079a07aa2+14824f138e,g212a7c68fe+a4f2ea4efa,g2305ad1205+72971fe858,g295015adf3+ab2c85acae,g2bbee38e9b+d6b5cd48b5,g337abbeb29+d6b5cd48b5,g3ddfee87b4+31b3a28dff,g487adcacf7+082e807817,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+b2918d57ae,g5a732f18d5+66d966b544,g64a986408d+a41d3748ce,g858d7b2824+a41d3748ce,g8a8a8dda67+a6fc98d2e7,g99cad8db69+7fe4acdf18,g9ddcbc5298+d4bad12328,ga1e77700b3+246acaaf9c,ga8c6da7877+84af8b3ff8,gb0e22166c9+3863383f4c,gb6a65358fc+d6b5cd48b5,gba4ed39666+9664299f35,gbb8dafda3b+d8d527deb2,gc07e1c2157+b2dbe6b631,gc120e1dc64+61440b2abb,gc28159a63d+d6b5cd48b5,gcf0d15dbbd+31b3a28dff,gdaeeff99f8+a38ce5ea23,ge6526c86ff+39927bb362,ge79ae78c31+d6b5cd48b5,gee10cc3b42+a6fc98d2e7,gf1cff7945b+a41d3748ce,v24.1.5.rc1
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | List of all members
lsst.pex.config.registry.Registry Class Reference
Inheritance diagram for lsst.pex.config.registry.Registry:
lsst.meas.base.pluginRegistry.PluginRegistry

Public Member Functions

 __init__ (self, configBaseType=Config)
 
 register (self, name, target, ConfigClass=None)
 
 __getitem__ (self, key)
 
 __len__ (self)
 
 __iter__ (self)
 
 __contains__ (self, key)
 
 makeField (self, doc, default=None, optional=False, multi=False, on_none=None)
 

Protected Attributes

 _configBaseType
 
 _dict
 

Detailed Description

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.

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

Definition at line 59 of file registry.py.

Constructor & Destructor Documentation

◆ __init__()

lsst.pex.config.registry.Registry.__init__ ( self,
configBaseType = Config )

Definition at line 123 of file registry.py.

123 def __init__(self, configBaseType=Config):
124 if not issubclass(configBaseType, Config):
125 raise TypeError(
126 "configBaseType=%s must be a subclass of Config"
127 % _typeStr(
128 configBaseType,
129 )
130 )
131 self._configBaseType = configBaseType
132 self._dict = {}
133

Member Function Documentation

◆ __contains__()

lsst.pex.config.registry.Registry.__contains__ ( self,
key )

Definition at line 186 of file registry.py.

186 def __contains__(self, key):
187 return key in self._dict
188

◆ __getitem__()

lsst.pex.config.registry.Registry.__getitem__ ( self,
key )

Definition at line 177 of file registry.py.

177 def __getitem__(self, key):
178 return self._dict[key]
179

◆ __iter__()

lsst.pex.config.registry.Registry.__iter__ ( self)

Definition at line 183 of file registry.py.

183 def __iter__(self):
184 return iter(self._dict)
185

◆ __len__()

lsst.pex.config.registry.Registry.__len__ ( self)

Definition at line 180 of file registry.py.

180 def __len__(self):
181 return len(self._dict)
182

◆ makeField()

lsst.pex.config.registry.Registry.makeField ( self,
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.
default : object, optional
    The default target for the field.
optional : `bool`, optional
    When `False`, `lsst.pex.config.Config.validate` fails if the
    field's value is `None`.
multi : `bool`, 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
-------
field : `lsst.pex.config.RegistryField`
    `~lsst.pex.config.RegistryField` Configuration field.

Reimplemented in lsst.meas.base.pluginRegistry.PluginRegistry.

Definition at line 189 of file registry.py.

189 def makeField(self, doc, default=None, optional=False, multi=False, on_none=None):
190 """Create a `RegistryField` configuration field from this registry.
191
192 Parameters
193 ----------
194 doc : `str`
195 A description of the field.
196 default : object, optional
197 The default target for the field.
198 optional : `bool`, optional
199 When `False`, `lsst.pex.config.Config.validate` fails if the
200 field's value is `None`.
201 multi : `bool`, optional
202 A flag to allow multiple selections in the `RegistryField` if
203 `True`.
204 on_none : `Callable`, optional
205 A callable that should be invoked when ``apply`` is called but the
206 selected name or names is `None`. Will be passed the field
207 attribute proxy (`RegistryInstanceDict`) and then all positional
208 and keyword arguments passed to ``apply``.
209
210 Returns
211 -------
212 field : `lsst.pex.config.RegistryField`
213 `~lsst.pex.config.RegistryField` Configuration field.
214 """
215 return RegistryField(doc, self, default, optional, multi, on_none=on_none)
216
217

◆ register()

lsst.pex.config.registry.Registry.register ( self,
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 with `dict`-like patterns using ``name`` as
    the key.
target : obj
    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. 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.

Reimplemented in lsst.meas.base.pluginRegistry.PluginRegistry.

Definition at line 134 of file registry.py.

134 def register(self, name, target, ConfigClass=None):
135 """Add a new configurable target to the registry.
136
137 Parameters
138 ----------
139 name : `str`
140 Name that the ``target`` is registered under. The target can
141 be accessed later with `dict`-like patterns using ``name`` as
142 the key.
143 target : obj
144 A configurable type, usually a subclass of `lsst.pipe.base.Task`.
145 ConfigClass : `lsst.pex.config.Config`-type, optional
146 A subclass of `lsst.pex.config.Config` used to configure the
147 configurable. If `None` then the configurable's ``ConfigClass``
148 attribute is used.
149
150 Raises
151 ------
152 RuntimeError
153 Raised if an item with ``name`` is already in the registry.
154 AttributeError
155 Raised if ``ConfigClass`` is `None` and ``target`` does not have
156 a ``ConfigClass`` attribute.
157
158 Notes
159 -----
160 If ``ConfigClass`` is provided then the ``target`` configurable is
161 wrapped in a new object that forwards function calls to it. Otherwise
162 the original ``target`` is stored.
163 """
164 if name in self._dict:
165 raise RuntimeError("An item with name %r already exists" % name)
166 if ConfigClass is None:
167 wrapper = target
168 else:
169 wrapper = ConfigurableWrapper(target, ConfigClass)
170 if not issubclass(wrapper.ConfigClass, self._configBaseType):
171 raise TypeError(
172 "ConfigClass=%s is not a subclass of %r"
173 % (_typeStr(wrapper.ConfigClass), _typeStr(self._configBaseType))
174 )
175 self._dict[name] = wrapper
176

Member Data Documentation

◆ _configBaseType

lsst.pex.config.registry.Registry._configBaseType
protected

Definition at line 131 of file registry.py.

◆ _dict

lsst.pex.config.registry.Registry._dict
protected

Definition at line 132 of file registry.py.


The documentation for this class was generated from the following file: