22 from __future__
import absolute_import, division, print_function
24 __all__ = [
"Key",
"Field",
"SchemaItem"]
26 from past.builtins
import basestring
27 from builtins
import str
as futurestr
28 from future.utils
import with_metaclass
36 from ...geom
import Angle
37 from lsst.utils
import continueClass, TemplateMeta
39 from .schema
import _Key, _Field, _SchemaItem, Schema
54 class Key(with_metaclass(TemplateMeta, object)):
58 class Field(with_metaclass(TemplateMeta, object)):
66 def _registerInstantiations(abc, types):
67 """Iterate over a private dict (filled by template instantiations in C++) 68 to register template instantiations a TemplateMeta ABCs. 70 If an entry for the type string exists in _dtypes, we use that instead of 71 the string as the key, and use the string as an alias. 73 for k, v
in types.items():
74 dtype = _dtypes.get(k,
None)
76 abc.register(dtype, v)
84 _registerInstantiations(Key, _Key)
85 _registerInstantiations(Field, _Field)
86 _registerInstantiations(SchemaItem, _SchemaItem)
90 Key.alias(float, _Key[
"D"])
91 Field.alias(float, _Field[
"D"])
92 SchemaItem.alias(float, _SchemaItem[
"D"])
93 if futurestr
is not str:
94 Key.alias(futurestr, _Key[
"String"])
95 Field.alias(futurestr, _Field[
"String"])
96 SchemaItem.alias(futurestr, _SchemaItem[
"String"])
103 """Return a list of field names in the order the fields were added to the Schema. 108 names.append(item.field.getName())
113 """Iterate over the items in the Schema. 116 self.forEach(items.append)
120 """Check that all units in the Schema are valid Astropy unit strings. 123 astropy.units.Unit(item.field.getUnits(),
124 parse_strict=parse_strict)
127 def addField(self, field, type=None, doc="", units="", size=None,
128 doReplace=False, parse_strict="raise"):
129 """Add a field to the Schema. 134 The string name of the Field, or a fully-constructed Field object. 135 If the latter, all other arguments besides doReplace are ignored. 137 The type of field to create. Valid types are the keys of the 138 afw.table.Field dictionary. 140 Documentation for the field. 142 Units for the field, or an empty string if unitless. 144 Size of the field; valid for string and array fields only. 146 If a field with this name already exists, replace it instead of 147 raising pex.exceptions.InvalidParameterError. 149 One of 'raise' (default), 'warn', or 'strict', indicating how to 150 handle unrecognized unit strings. See also astropy.units.Unit. 152 if isinstance(field, basestring):
153 field = Field[type](field, doc=doc, units=units,
154 size=size, parse_strict=parse_strict)
155 return field._addTo(self, doReplace)
159 Extract a dictionary of {<name>: <schema-item>} in which the field names 160 match the given shell-style glob pattern(s). 162 Any number of glob patterns may be passed; the result will be the union of all 163 the result of each glob considered separately. 165 Additional Keyword Arguments 166 ---------------------------- 167 regex : `str` or `re` pattern 168 A regular expression to be used in addition to any glob patterns 169 passed as positional arguments. Note that this will be compared 170 with re.match, not re.search. 173 A replacement string (see re.MatchObject.expand) used to set the 174 dictionary keys of any fields matched by regex. 177 If True, a collections.OrderedDict will be returned instead of a 178 standard dict, with the order corresponding to the definition 179 order of the Schema. Default is False. 181 if kwds.pop(
"ordered",
False):
182 d = collections.OrderedDict()
185 regex = kwds.pop(
"regex",
None)
186 sub = kwds.pop(
"sub",
None)
187 if sub
is not None and regex
is None:
189 "'sub' keyword argument to extract is invalid without 'regex' argument")
192 "Unrecognized keyword arguments for extract: %s" %
", ".join(kwds.keys()))
194 trueName = item.field.getName()
196 for alias, target
in self.getAliasMap().items():
197 if trueName.startswith(target):
198 names.append(trueName.replace(target, alias, 1))
200 if regex
is not None:
201 m = re.match(regex, name)
207 for pattern
in patterns:
208 if fnmatch.fnmatchcase(name, pattern):
def extract(self, patterns, kwds)
def getOrderedNames(self)
def addField(self, field, type=None, doc="", units="", size=None, doReplace=False, parse_strict="raise")
def checkUnits(self, parse_strict='raise')