22__all__ = [
"Key",
"Field",
"SchemaItem"]
31from lsst.utils import continueClass, TemplateMeta
33from ._table
import _Key, _Field, _SchemaItem, Schema
49class Key(metaclass=TemplateMeta):
53class Field(metaclass=TemplateMeta):
61def _registerInstantiations(abc, types):
62 """Iterate over a private dict (filled by template instantiations in C++)
63 to register template instantiations a TemplateMeta ABCs.
65 If an entry for the type string exists
in _dtypes, we use that instead of
66 the string
as the key,
and use the string
as an alias.
68 for k, v
in types.items():
69 dtype = _dtypes.get(k,
None)
71 abc.register(dtype, v)
79_registerInstantiations(Key, _Key)
80_registerInstantiations(Field, _Field)
81_registerInstantiations(SchemaItem, _SchemaItem)
85Key.alias(float, _Key[
"D"])
86Field.alias(float, _Field[
"D"])
87SchemaItem.alias(float, _SchemaItem[
"D"])
94 """Return a list of field names in the order the fields were added to the Schema.
99 Field names in order they were added to the Schema.
104 names.append(item.field.getName())
109 """Iterate over the items in the Schema.
112 self.forEach(items.append)
116 """Check that all units in the Schema are valid Astropy unit strings.
120 parse_strict : `str`, optional
121 One of 'raise' (default),
'warn',
or 'strict', indicating how to
122 handle unrecognized unit strings. See also astropy.units.Unit.
125 astropy.units.Unit(item.field.getUnits(),
126 parse_strict=parse_strict)
129 def addField(self, field, type=None, doc="", units="", size=None,
130 doReplace=False, parse_strict="raise"):
131 """Add a field to the Schema.
135 field : `str` or `Field`
136 The string name of the Field,
or a fully-constructed Field object.
137 If the latter, all other arguments besides doReplace are ignored.
138 type : `str`, optional
139 The type of field to create. Valid types are the keys of the
142 Documentation
for the field.
144 Units
for the field,
or an empty string
if unitless.
146 Size of the field; valid
for string
and array fields only.
148 If a field
with this name already exists, replace it instead of
151 One of
'raise' (default),
'warn',
or 'strict', indicating how to
152 handle unrecognized unit strings. See also astropy.units.Unit.
157 Result of the `Field` addition.
159 if isinstance(field, str):
160 field = Field[type](field, doc=doc, units=units,
161 size=size, parse_strict=parse_strict)
162 return field._addTo(self, doReplace)
165 """Extract a dictionary of {<name>: <schema-item>} in which the field
166 names match the given shell-style glob pattern(s).
168 Any number of glob patterns may be passed; the result will be the
169 union of all the result of each glob considered separately.
173 patterns : Array of `str`
174 List of glob patterns to use to select field names.
176 Dictionary of additional keyword arguments. May contain:
178 ``regex`` : `str` or `re` pattern
179 A regular expression to be used
in addition to any
180 glob patterns passed
as positional arguments. Note
181 that this will be compared
with re.match,
not
184 A replacement string (see re.MatchObject.expand) used
185 to set the dictionary keys of any fields matched by
187 ``ordered`` : `bool`, optional
188 If
True, a collections.OrderedDict will be returned
189 instead of a standard dict,
with the order
190 corresponding to the definition order of the
191 Schema. Default
is False.
196 Dictionary of extracted name-schema item sets.
201 Raised
if the `sub` keyword argument
is invalid without
202 the `regex` argument.
204 Also raised
if an unknown keyword argument
is supplied.
206 if kwargs.pop(
"ordered",
False):
207 d = collections.OrderedDict()
210 regex = kwargs.pop(
"regex",
None)
211 sub = kwargs.pop(
"sub",
None)
212 if sub
is not None and regex
is None:
214 "'sub' keyword argument to extract is invalid without 'regex' argument")
216 kwargsStr =
", ".join(kwargs.keys())
217 raise ValueError(f
"Unrecognized keyword arguments for extract: {kwargsStr}")
219 trueName = item.field.getName()
221 for alias, target
in self.getAliasMap().
items():
222 if trueName.startswith(target):
223 names.append(trueName.replace(target, alias, 1))
225 if regex
is not None:
226 m = re.match(regex, name)
232 for pattern
in patterns:
233 if fnmatch.fnmatchcase(name, pattern):
239 """For pickle support."""
242 fields.append(item.field)
243 return (makeSchemaFromFields, (fields,))
247 """Create a Schema from a sequence of Fields. For pickle support.
251 fields : `tuple` ['lsst.afw.table.Field']
252 The fields to construct the new Schema
from.
257 The constructed Schema.
261 schema.addField(field)
std::vector< SchemaItem< Flag > > * items
def getOrderedNames(self)
def checkUnits(self, parse_strict='raise')
def extract(self, *patterns, **kwargs)
def addField(self, field, type=None, doc="", units="", size=None, doReplace=False, parse_strict="raise")
Defines the fields and offsets for a table.
A class representing an angle.
Reports invalid arguments.
def makeSchemaFromFields(fields)
A description of a field in a table.