LSST Applications g0265f82a02+0e5473021a,g02d81e74bb+bd2ed33bd6,g1470d8bcf6+c6d6eb38e2,g14a832a312+9d12ad093c,g2079a07aa2+86d27d4dc4,g2305ad1205+91a32aca49,g295015adf3+88246b6574,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g3ddfee87b4+c34e8be1fa,g487adcacf7+85dcfbcc36,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+ea1711114f,g5a732f18d5+53520f316c,g64a986408d+bd2ed33bd6,g858d7b2824+bd2ed33bd6,g8a8a8dda67+585e252eca,g99cad8db69+016a06b37a,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+ef4e3a5875,gb0e22166c9+60f28cb32d,gb6a65358fc+0e5473021a,gba4ed39666+c2a2e4ac27,gbb8dafda3b+b6d7b42999,gc120e1dc64+f745648b3a,gc28159a63d+0e5473021a,gcf0d15dbbd+c34e8be1fa,gdaeeff99f8+f9a426f77a,ge6526c86ff+508d0e0a30,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gf18bd8381d+8d59551888,gf1cff7945b+bd2ed33bd6,w.2024.16
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
lsst.afw.typehandling._GenericMap.AutoKeyMeta Class Reference
Inheritance diagram for lsst.afw.typehandling._GenericMap.AutoKeyMeta:
lsst.afw.typehandling._SimpleGenericMap.SimpleGenericMap

Public Member Functions

 __call__ (cls, *args, **kwargs)
 

Public Attributes

 TEMPLATE_PARAMS
 

Protected Member Functions

 _guessKeyType (cls, inputData)
 

Detailed Description

A metaclass for abstract mappings whose key type is implied by their
constructor arguments.

This metaclass requires that the mapping have a `dict`-like constructor,
i.e., it takes a mapping or an iterable of key-value pairs as its first
positional parameter.

This class differs from `~lsst.utils.TemplateMeta` only in that the dtype
(or equivalent) constructor keyword is optional. If it is omitted, the
class will attempt to infer it from the first argument.

Definition at line 122 of file _GenericMap.py.

Member Function Documentation

◆ __call__()

lsst.afw.typehandling._GenericMap.AutoKeyMeta.__call__ ( cls,
* args,
** kwargs )

Definition at line 135 of file _GenericMap.py.

135 def __call__(cls, *args, **kwargs): # noqa N805, non-self first param
136 if len(cls.TEMPLATE_PARAMS) != 1:
137 raise ValueError("AutoKeyMeta requires exactly one template parameter")
138 dtypeKey = cls.TEMPLATE_PARAMS[0]
139 dtype = kwargs.get(dtypeKey, None)
140
141 # Try to infer dtype if not provided
142 if dtype is None and len(args) >= 1:
143 dtype = cls._guessKeyType(args[0])
144 if dtype is not None:
145 kwargs[dtypeKey] = dtype
146
147 return super().__call__(*args, **kwargs)
148

◆ _guessKeyType()

lsst.afw.typehandling._GenericMap.AutoKeyMeta._guessKeyType ( cls,
inputData )
protected
Try to infer the key type of a map from its input.

Parameters
----------
inputData : `~collections.abc.Mapping` or iterable of pairs
    Any object that can be passed to a `dict`-like constructor. Keys
    are assumed homogeneous (if not, a
    `~lsst.afw.typehandling.GenericMap` constructor will raise
    `TypeError` no matter what key type, if any, is provided).

Returns
-------
keyType : `type`
    The type of the keys in ``inputData``, or `None` if the type could
    not be inferred.

Definition at line 149 of file _GenericMap.py.

149 def _guessKeyType(cls, inputData): # noqa N805, non-self first param
150 """Try to infer the key type of a map from its input.
151
152 Parameters
153 ----------
154 inputData : `~collections.abc.Mapping` or iterable of pairs
155 Any object that can be passed to a `dict`-like constructor. Keys
156 are assumed homogeneous (if not, a
157 `~lsst.afw.typehandling.GenericMap` constructor will raise
158 `TypeError` no matter what key type, if any, is provided).
159
160 Returns
161 -------
162 keyType : `type`
163 The type of the keys in ``inputData``, or `None` if the type could
164 not be inferred.
165 """
166 if inputData:
167 firstKey = None
168 if isinstance(inputData, Mapping):
169 # mapping to copy
170 firstKey = iter(inputData.keys()).__next__()
171 elif not isinstance(inputData, str):
172 # iterable of key-value pairs
173 try:
174 firstKey = iter(inputData).__next__()[0]
175 except TypeError:
176 # Not an iterable of pairs
177 pass
178 if firstKey:
179 return type(firstKey)
180 # Any other input is either empty or an invalid input to dict-like constructors
181 return None

Member Data Documentation

◆ TEMPLATE_PARAMS

lsst.afw.typehandling._GenericMap.AutoKeyMeta.TEMPLATE_PARAMS

Definition at line 136 of file _GenericMap.py.


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