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
55 class Key(with_metaclass(TemplateMeta, object)):
59 class Field(with_metaclass(TemplateMeta, object)):
67 def _registerInstantiations(abc, types):
68 """Iterate over a private dict (filled by template instantiations in C++) 69 to register template instantiations a TemplateMeta ABCs. 71 If an entry for the type string exists in _dtypes, we use that instead of 72 the string as the key, and use the string as an alias. 74 for k, v
in types.items():
75 dtype = _dtypes.get(k,
None)
77 abc.register(dtype, v)
85 _registerInstantiations(Key, _Key)
86 _registerInstantiations(Field, _Field)
87 _registerInstantiations(SchemaItem, _SchemaItem)
91 Key.alias(float, _Key[
"D"])
92 Field.alias(float, _Field[
"D"])
93 SchemaItem.alias(float, _SchemaItem[
"D"])
94 if futurestr
is not str:
95 Key.alias(futurestr, _Key[
"String"])
96 Field.alias(futurestr, _Field[
"String"])
97 SchemaItem.alias(futurestr, _SchemaItem[
"String"])
104 """Return a list of field names in the order the fields were added to the Schema. 109 names.append(item.field.getName())
114 """Iterate over the items in the Schema. 117 self.forEach(items.append)
121 """Check that all units in the Schema are valid Astropy unit strings. 124 astropy.units.Unit(item.field.getUnits(),
125 parse_strict=parse_strict)
128 def addField(self, field, type=None, doc="", units="", size=None,
129 doReplace=False, parse_strict="raise"):
130 """Add a field to the Schema. 135 The string name of the Field, or a fully-constructed Field object. 136 If the latter, all other arguments besides doReplace are ignored. 138 The type of field to create. Valid types are the keys of the 139 afw.table.Field dictionary. 141 Documentation for the field. 143 Units for the field, or an empty string if unitless. 145 Size of the field; valid for string and array fields only. 147 If a field with this name already exists, replace it instead of 148 raising pex.exceptions.InvalidParameterError. 150 One of 'raise' (default), 'warn', or 'strict', indicating how to 151 handle unrecognized unit strings. See also astropy.units.Unit. 153 if isinstance(field, basestring):
154 field = Field[type](field, doc=doc, units=units,
155 size=size, parse_strict=parse_strict)
156 return field._addTo(self, doReplace)
160 Extract a dictionary of {<name>: <schema-item>} in which the field names 161 match the given shell-style glob pattern(s). 163 Any number of glob patterns may be passed; the result will be the union of all 164 the result of each glob considered separately. 166 Additional Keyword Arguments 167 ---------------------------- 168 regex : `str` or `re` pattern 169 A regular expression to be used in addition to any glob patterns 170 passed as positional arguments. Note that this will be compared 171 with re.match, not re.search. 174 A replacement string (see re.MatchObject.expand) used to set the 175 dictionary keys of any fields matched by regex. 178 If True, a collections.OrderedDict will be returned instead of a 179 standard dict, with the order corresponding to the definition 180 order of the Schema. Default is False. 182 if kwds.pop(
"ordered",
False):
183 d = collections.OrderedDict()
186 regex = kwds.pop(
"regex",
None)
187 sub = kwds.pop(
"sub",
None)
188 if sub
is not None and regex
is None:
190 "'sub' keyword argument to extract is invalid without 'regex' argument")
193 "Unrecognized keyword arguments for extract: %s" %
", ".join(kwds.keys()))
195 trueName = item.field.getName()
197 for alias, target
in self.getAliasMap().items():
198 if trueName.startswith(target):
199 names.append(trueName.replace(target, alias, 1))
201 if regex
is not None:
202 m = re.match(regex, name)
208 for pattern
in patterns:
209 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')