LSST Applications
21.0.0-142-gef555c1e+42c9bccae2,22.0.0+052faf71bd,22.0.0+1c4650f311,22.0.0+40ce427c77,22.0.0+5b6c068b1a,22.0.0+7589c3a021,22.0.0+81ed51be6d,22.0.1-1-g7d6de66+6cae67f2c6,22.0.1-1-g87000a6+314cd8b7ea,22.0.1-1-g8760c09+052faf71bd,22.0.1-1-g8e32f31+5b6c068b1a,22.0.1-10-g779eefa+a163f08322,22.0.1-12-g3bd7ecb+bbeacc25a9,22.0.1-15-g63cc0c1+2a7037787d,22.0.1-17-ge5a99e88+3d2c1afe2e,22.0.1-19-g88addfe+6cae67f2c6,22.0.1-2-g1cb3e5b+84de06d286,22.0.1-2-g8ef0a89+6cae67f2c6,22.0.1-2-g92698f7+1c4650f311,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gb66926d+5b6c068b1a,22.0.1-2-gcb770ba+0723a13595,22.0.1-2-ge470956+ff9f1dc8d5,22.0.1-22-g608e23ac+2ac85e833c,22.0.1-29-g184b6e44e+8b185d4e2d,22.0.1-3-g59f966b+11ba4df19d,22.0.1-3-g8c1d971+f90df4c6d0,22.0.1-3-g997b569+d69a7aa2f8,22.0.1-3-gaaec9c0+4d194bf81c,22.0.1-4-g1930a60+283d9d2f1a,22.0.1-4-g5b7b756+c1283a92b8,22.0.1-4-g8623105+6cae67f2c6,22.0.1-7-gba73697+283d9d2f1a,22.0.1-8-g47d23f5+43acea82f3,master-g5f2689bdc5+40ce427c77,w.2021.38
LSST Data Management Base Package
|
Public Member Functions | |
def | __new__ (cls, name, bases, attrs) |
def | __call__ (cls, *args, **kwds) |
def | __subclasscheck__ (cls, subclass) |
def | __instancecheck__ (cls, instance) |
def | __subclasses__ (cls) |
def | register (cls, key, subclass) |
def | alias (cls, key, subclass) |
def | __getitem__ (cls, key) |
def | __iter__ (cls) |
def | __len__ (cls) |
def | __contains__ (cls, key) |
def | keys (cls) |
def | values (cls) |
def | items (cls) |
def | get (cls, key, default=None) |
A metaclass for abstract base classes that tie together wrapped C++ template types. C++ template classes are most easily wrapped with a separate Python class for each template type, which results in an unnatural Python interface. TemplateMeta provides a thin layer that connects these Python classes by giving them a common base class and acting as a factory to construct them in a consistent way. To use, simply create a new class with the name of the template class, and use ``TemplateMeta`` as its metaclass, and then call ``register`` on each of its subclasses. This registers the class with a "type key" - usually a Python representation of the C++ template types. The type key must be a hashable object - strings, type objects, and tuples of these (for C++ classes with multiple template parameters) are good choices. Alternate type keys for existing classes can be added by calling ``alias``, but only after a subclass already been registered with a "primary" type key. For example:: .. code-block:: python import numpy as np from ._image import ImageF, ImageD class Image(metaclass=TemplateMeta): pass Image.register(np.float32, ImageF) Image.register(np.float64, ImageD) Image.alias("F", ImageF) Image.alias("D", ImageD) We have intentionally used ``numpy`` types as the primary keys for these objects in this example, with strings as secondary aliases simply because the primary key is added as a ``dtype`` attribute on the the registered classes (so ``ImageF.dtype == numpy.float32`` in the above example). This allows user code to construct objects directly using ``Image``, as long as an extra ``dtype`` keyword argument is passed that matches one of the type keys:: .. code-block:: python img = Image(52, 64, dtype=np.float32) This simply forwards additional positional and keyword arguments to the wrapped template class's constructor. The choice of "dtype" as the name of the template parameter is also configurable, and in fact multiple template parameters are also supported, by setting a ``TEMPLATE_PARAMS`` class attribute on the ABC to a tuple containing the names of the template parameters. A ``TEMPLATE_DEFAULTS`` attribute can also be defined to a tuple of the same length containing default values for the template parameters, allowing them to be omitted in constructor calls. When the length of these attributes is more than one, the type keys passed to ``register`` and ``alias`` should be tuple of the same length; when the length of these attributes is one, type keys should generally not be tuples. As an aid for those writing the Python wrappers for C++ classes, ``TemplateMeta`` also provides a way to add pure-Python methods and other attributes to the wrapped template classes. To add a ``sum`` method to all registered types, for example, we can just do:: .. code-block:: python class Image(metaclass=TemplateMeta): def sum(self): return np.sum(self.getArray()) Image.register(np.float32, ImageF) Image.register(np.float64, ImageD) .. note:: ``TemplateMeta`` works by overriding the ``__instancecheck__`` and ``__subclasscheck__`` special methods, and hence does not appear in its registered subclasses' method resolution order or ``__bases__`` attributes. That means its attributes are not inherited by registered subclasses. Instead, attributes added to an instance of ``TemplateMeta`` are *copied* into the types registered with it. These attributes will thus *replace* existing attributes in those classes with the same name, and subclasses cannot delegate to base class implementations of these methods. Finally, abstract base classes that use ``TemplateMeta`` define a dict- like interface for accessing their registered subclasses, providing something like the C++ syntax for templates:: .. code-block:: python Image[np.float32] -> ImageF Image["D"] -> ImageD Both primary dtypes and aliases can be used as keys in this interface, which means types with aliases will be present multiple times in the dict. To obtain the sequence of unique subclasses, use the ``__subclasses__`` method. .. warning:: Python's built-in `super` function does not behave properly in classes that have `TemplateMeta` as their metaclass (which should be rare, as TemplateMeta ABCs will have base classes of their own)..
Definition at line 152 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.__call__ | ( | cls, | |
* | args, | ||
** | kwds | ||
) |
Reimplemented in lsst.afw.typehandling._GenericMap.AutoKeyMeta.
Definition at line 293 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.__contains__ | ( | cls, | |
key | |||
) |
Definition at line 458 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.__getitem__ | ( | cls, | |
key | |||
) |
Reimplemented in lsst.afw.table._base.Catalog, and lsst.afw.cameraGeom._detectorCollection.DetectorCollectionBase.
Definition at line 449 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.__instancecheck__ | ( | cls, | |
instance | |||
) |
Definition at line 326 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.__iter__ | ( | cls | ) |
Reimplemented in lsst.afw.cameraGeom._detectorCollection.DetectorCollectionBase.
Definition at line 452 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.__len__ | ( | cls | ) |
Definition at line 455 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.__new__ | ( | cls, | |
name, | |||
bases, | |||
attrs | |||
) |
Definition at line 261 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.__subclasscheck__ | ( | cls, | |
subclass | |||
) |
Definition at line 316 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.__subclasses__ | ( | cls | ) |
Return a tuple of all classes that inherit from this class.
Definition at line 336 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.alias | ( | cls, | |
key, | |||
subclass | |||
) |
Add an alias that allows an existing subclass to be accessed with a different key.
Definition at line 429 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.get | ( | cls, | |
key, | |||
default = None |
|||
) |
Return the subclass associated with the given key (including aliases), or ``default`` if the key is not recognized.
Definition at line 477 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.items | ( | cls | ) |
def lsst.utils.wrappers.TemplateMeta.keys | ( | cls | ) |
Return an iterable containing all keys (including aliases).
Definition at line 461 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.register | ( | cls, | |
key, | |||
subclass | |||
) |
Register a subclass of this ABC with the given key (a string, number, type, or other hashable). Register may only be called once for a given key or a given subclass.
Definition at line 344 of file wrappers.py.
def lsst.utils.wrappers.TemplateMeta.values | ( | cls | ) |
Return an iterable of registered subclasses, with duplicates corresponding to any aliases.
Definition at line 466 of file wrappers.py.