23 __all__ = [
"ListField"]
25 import collections.abc
27 from .config
import Field, FieldValidationError, _typeStr, _autocast, _joinNamePath
28 from .comparison
import compareScalars, getComparisonName
29 from .callStack
import getCallStack, getStackFrame
32 class List(collections.abc.MutableSequence):
33 """List collection used internally by `ListField`. 37 config : `lsst.pex.config.Config` 38 Config instance that contains the ``field``. 40 Instance of the `ListField` using this ``List``. 42 Sequence of values that are inserted into this ``List``. 43 at : `list` of `lsst.pex.config.callStack.StackFrame` 44 The call stack (created by `lsst.pex.config.callStack.getCallStack`). 46 Event label for the history. 47 setHistory : `bool`, optional 48 Enable setting the field's history, using the value of the ``at`` 49 parameter. Default is `True`. 54 Raised if an item in the ``value`` parameter does not have the 55 appropriate type for this field or does not pass the 56 `ListField.itemCheck` method of the ``field`` parameter. 59 def __init__(self, config, field, value, at, label, setHistory=True):
67 for i, x
in enumerate(value):
68 self.
insert(i, x, setHistory=
False)
70 msg =
"Value %s is of incorrect type %s. Sequence type expected" % (value, _typeStr(value))
76 """Validate an item to determine if it can be included in the list. 81 Index of the item in the `list`. 88 Raised if an item in the ``value`` parameter does not have the 89 appropriate type for this field or does not pass the field's 90 `ListField.itemCheck` method. 93 if not isinstance(x, self.
_field.itemtype)
and x
is not None:
94 msg =
"Item at position %d with value %s is of incorrect type %s. Expected %s" % \
95 (i, x, _typeStr(x), _typeStr(self.
_field.itemtype))
98 if self.
_field.itemCheck
is not None and not self.
_field.itemCheck(x):
99 msg =
"Item at position %d is not a valid value: %s" % (i, x)
103 """Sequence of items contained by the `List` (`list`). 107 history = property(
lambda x: x._history)
108 """Read-only history. 112 return x
in self.
_list 115 return len(self.
_list)
117 def __setitem__(self, i, x, at=None, label="setitem", setHistory=True):
120 "Cannot modify a frozen Config")
121 if isinstance(i, slice):
122 k, stop, step = i.indices(len(self))
123 for j, xj
in enumerate(x):
124 xj = _autocast(xj, self.
_field.itemtype)
129 x = _autocast(x, self.
_field.itemtype)
141 def __delitem__(self, i, at=None, label="delitem", setHistory=True):
144 "Cannot modify a frozen Config")
152 return iter(self.
_list)
154 def insert(self, i, x, at=None, label="insert", setHistory=True):
155 """Insert an item into the list at the given index. 160 Index where the item is inserted. 162 Item that is inserted. 163 at : `list` of `lsst.pex.config.callStack.StackFrame`, optional 164 The call stack (created by 165 `lsst.pex.config.callStack.getCallStack`). 166 label : `str`, optional 167 Event label for the history. 168 setHistory : `bool`, optional 169 Enable setting the field's history, using the value of the ``at`` 170 parameter. Default is `True`. 174 self.
__setitem__(slice(i, i), [x], at=at, label=label, setHistory=setHistory)
177 return repr(self.
_list)
184 if len(self) != len(other):
187 for i, j
in zip(self, other):
191 except AttributeError:
196 return not self.
__eq__(other)
199 if hasattr(getattr(self.__class__, attr,
None),
'__set__'):
201 object.__setattr__(self, attr, value)
202 elif attr
in self.__dict__
or attr
in [
"_field",
"_config",
"_history",
"_list",
"__doc__"]:
204 object.__setattr__(self, attr, value)
207 msg =
"%s has no attribute %s" % (_typeStr(self.
_field), attr)
212 """A configuration field (`~lsst.pex.config.Field` subclass) that contains 213 a list of values of a specific type. 218 A description of the field. 220 The data type of items in the list. 221 default : sequence, optional 222 The default items for the field. 223 optional : `bool`, optional 224 Set whether the field is *optional*. When `False`, 225 `lsst.pex.config.Config.validate` will fail if the field's value is 227 listCheck : callable, optional 228 A callable that validates the list as a whole. 229 itemCheck : callable, optional 230 A callable that validates individual items in the list. 231 length : `int`, optional 232 If set, this field must contain exactly ``length`` number of items. 233 minLength : `int`, optional 234 If set, this field must contain *at least* ``minLength`` number of 236 maxLength : `int`, optional 237 If set, this field must contain *no more than* ``maxLength`` number of 252 def __init__(self, doc, dtype, default=None, optional=False,
253 listCheck=None, itemCheck=None,
254 length=None, minLength=None, maxLength=None):
255 if dtype
not in Field.supportedTypes:
256 raise ValueError(
"Unsupported dtype %s" % _typeStr(dtype))
257 if length
is not None:
259 raise ValueError(
"'length' (%d) must be positive" % length)
263 if maxLength
is not None and maxLength <= 0:
264 raise ValueError(
"'maxLength' (%d) must be positive" % maxLength)
265 if minLength
is not None and maxLength
is not None \
266 and minLength > maxLength:
267 raise ValueError(
"'maxLength' (%d) must be at least" 268 " as large as 'minLength' (%d)" % (maxLength, minLength))
270 if listCheck
is not None and not hasattr(listCheck,
"__call__"):
271 raise ValueError(
"'listCheck' must be callable")
272 if itemCheck
is not None and not hasattr(itemCheck,
"__call__"):
273 raise ValueError(
"'itemCheck' must be callable")
276 self.
_setup(doc=doc, dtype=List, default=default, check=
None, optional=optional, source=source)
279 """Callable used to check the list as a whole. 283 """Callable used to validate individual items as they are inserted 288 """Data type of list items. 292 """Number of items that must be present in the list (or `None` to 293 disable checking the list's length). 297 """Minimum number of items that must be present in the list (or `None` 298 to disable checking the list's minimum length). 302 """Maximum number of items that must be present in the list (or `None` 303 to disable checking the list's maximum length). 307 """Validate the field. 311 instance : `lsst.pex.config.Config` 312 The config instance that contains this field. 316 lsst.pex.config.FieldValidationError 319 - The field is not optional, but the value is `None`. 320 - The list itself does not meet the requirements of the `length`, 321 `minLength`, or `maxLength` attributes. 322 - The `listCheck` callable returns `False`. 326 Individual item checks (`itemCheck`) are applied when each item is 327 set and are not re-checked by this method. 329 Field.validate(self, instance)
331 if value
is not None:
332 lenValue = len(value)
333 if self.
length is not None and not lenValue == self.
length:
334 msg =
"Required list length=%d, got length=%d" % (self.
length, lenValue)
337 msg =
"Minimum allowed list length=%d, got length=%d" % (self.
minLength, lenValue)
340 msg =
"Maximum allowed list length=%d, got length=%d" % (self.
maxLength, lenValue)
343 msg =
"%s is not a valid value" %
str(value)
346 def __set__(self, instance, value, at=None, label="assignment"):
353 if value
is not None:
354 value =
List(instance, self, value, at, label)
356 history = instance._history.setdefault(self.name, [])
357 history.append((value, at, label))
359 instance._storage[self.name] = value
362 """Convert the value of this field to a plain `list`. 364 `lsst.pex.config.Config.toDict` is the primary user of this method. 368 instance : `lsst.pex.config.Config` 369 The config instance that contains this field. 374 Plain `list` of items, or `None` if the field is not set. 377 return list(value)
if value
is not None else None 379 def _compare(self, instance1, instance2, shortcut, rtol, atol, output):
380 """Compare two config instances for equality with respect to this 383 `lsst.pex.config.config.compare` is the primary user of this method. 387 instance1 : `lsst.pex.config.Config` 388 Left-hand-side `~lsst.pex.config.Config` instance in the 390 instance2 : `lsst.pex.config.Config` 391 Right-hand-side `~lsst.pex.config.Config` instance in the 394 If `True`, return as soon as an **inequality** is found. 396 Relative tolerance for floating point comparisons. 398 Absolute tolerance for floating point comparisons. 400 If not None, a callable that takes a `str`, used (possibly 401 repeatedly) to report inequalities. 406 `True` if the fields are equal; `False` otherwise. 410 Floating point comparisons are performed by `numpy.allclose`. 412 l1 = getattr(instance1, self.name)
413 l2 = getattr(instance2, self.name)
415 _joinNamePath(instance1._name, self.name),
416 _joinNamePath(instance2._name, self.name)
418 if not compareScalars(
"isnone for %s" % name, l1
is None, l2
is None, output=output):
420 if l1
is None and l2
is None:
422 if not compareScalars(
"size for %s" % name, len(l1), len(l2), output=output):
425 for n, v1, v2
in zip(range(len(l1)), l1, l2):
427 rtol=rtol, atol=atol, output=output)
428 if not result
and shortcut:
430 equal = equal
and result
def __init__(self, doc, dtype, default=None, optional=False, listCheck=None, itemCheck=None, length=None, minLength=None, maxLength=None)
def validate(self, instance)
def validateItem(self, i, x)
def __setitem__(self, i, x, at=None, label="setitem", setHistory=True)
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
def __get__(self, instance, owner=None, at=None, label="default")
def insert(self, i, x, at=None, label="insert", setHistory=True)
def __init__(self, config, field, value, at, label, setHistory=True)
def _setup(self, doc, dtype, default, check, optional, source)
def __setattr__(self, attr, value, at=None, label="assignment")
def getStackFrame(relative=0)
def __delitem__(self, i, at=None, label="delitem", setHistory=True)
def toDict(self, instance)
def compareScalars(name, v1, v2, output, rtol=1E-8, atol=1E-8, dtype=None)
def __set__(self, instance, value, at=None, label="assignment")
daf::base::PropertyList * list
def __contains__(self, x)
def getComparisonName(name1, name2)