Coverage for python/lsst/daf/butler/core/dimensions/records.py : 33%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# This file is part of daf_butler. # # Developed for the LSST Data Management System. # This product includes software developed by the LSST Project # (http://www.lsst.org). # See the COPYRIGHT file at the top-level directory of this distribution # for details of code ownership. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>.
from .elements import DimensionElement
"""Unpickle implementation for `DimensionRecord` subclasses.
For internal use by `DimensionRecord`. """ return definition.RecordClass(*args)
"""Extract a `Timespan` object from the appropriate endpoint attributes.
For internal use by `DimensionRecord`. """ from ..timespan import TIMESPAN_FIELD_SPECS return Timespan( begin=getattr(record, TIMESPAN_FIELD_SPECS.begin.name), end=getattr(record, TIMESPAN_FIELD_SPECS.end.name), )
"""Create a dynamic subclass of `DimensionRecord` for the given `DimensionElement`.
For internal use by `DimensionRecord`. """ from .schema import makeElementTableSpec d = { "definition": definition, "__slots__": tuple(makeElementTableSpec(definition).fields.names) } if definition.temporal: d["timespan"] = property(_makeTimespanFromRecord) return type(definition.name + ".RecordClass", (DimensionRecord,), d)
"""Base class for the Python representation of database records for a `DimensionElement`.
Parameters ---------- args Field values for this record, ordered to match ``__slots__``.
Notes ----- `DimensionRecord` subclasses are created dynamically for each `DimensionElement` in a `DimensionUniverse`, and are accessible via the `DimensionElement.RecordClass` attribute. The `DimensionRecord` base class itself is pure abstract, but does not use the `abc` module to indicate this because it does not have overridable methods.
Record classes have attributes that correspond exactly to the fields in the related database table, a few additional methods inherited from the `DimensionRecord` base class, and two additional injected attributes:
- ``definition`` is a class attribute that holds the `DimensionElement`;
- ``timespan`` is a property that returns a `Timespan`, present only on record classes that correspond to temporal elements.
The field attributes are defined via the ``__slots__`` mechanism, and the ``__slots__`` tuple itself is considered the public interface for obtaining the list of fields.
Instances are usually obtained from a `Registry`, but in the rare cases where they are constructed directly in Python (usually for insertion into a `Registry`), the `fromDict` method should generally be used.
`DimensionRecord` instances are immutable. """
# Derived classes are required to define __slots__ as well, and it's those # derived-class slots that other methods on the base class expect to see # when they access self.__slots__.
for attrName, value in zip(self.__slots__, args): object.__setattr__(self, attrName, value) dataId = DataCoordinate( self.definition.graph, args[:len(self.definition.graph.required.names)] ) object.__setattr__(self, "dataId", dataId)
def fromDict(cls, mapping: Mapping[str, Any]): """Construct a `DimensionRecord` subclass instance from a mapping of field values.
Parameters ---------- mapping : `~collections.abc.Mapping` Field values, keyed by name. The keys must match those in ``__slots__``, with the exception that a dimension name may be used in place of the primary key name - for example, "tract" may be used instead of "id" for the "id" primary key field of the "tract" dimension.
Returns ------- record : `DimensionRecord` An instance of this subclass of `DimensionRecord`. """ # If the name of the dimension is present in the given dict, use it # as the primary key value instead of expecting the field name. # For example, allow {"instrument": "HSC", ...} instead of # {"name": "HSC", ...} when building a record for instrument dimension. primaryKey = mapping.get(cls.definition.name) if primaryKey is not None: d = dict(mapping) d[cls.definition.primaryKey.name] = primaryKey else: d = mapping values = tuple(d.get(k) for k in cls.__slots__) return cls(*values)
args = tuple(getattr(self, name) for name in self.__slots__) return (_reconstructDimensionRecord, (self.definition,) + args)
"""Return a vanilla `dict` representation of this record. """ return {name: getattr(self, name) for name in self.__slots__}
# Class attributes below are shadowed by instance attributes, and are # present just to hold the docstrings for those instance attributes.
(`DataCoordinate`). """ |