Enumerations¶
Constrained Types¶
- class Constrained(value=Unspecified, **kw)¶
A scalar type with a constrained set of legal values.
Wraps another scalar type and ensures that a value
set()is within bounds defined byvalid_value(). Ifvalid_value()returns false, the element is not converted and will have avalueof None.Constrainedis a semi-abstract class that requires an implementation ofvalid_value(), either by subclassing or overriding on a per-instance basis through the constructor.An example of a wrapper of int values that only allows the values of 1, 2 or 3:
>>> from flatland import Constrained, Integer >>> def is_valid(element, value): ... return value in (1, 2, 3) ... >>> schema = Constrained.using(child_type=Integer, valid_value=is_valid) >>> element = schema() >>> element.set('2') True >>> element.value 2 >>> element.set('5') False >>> element.value is None True
Enumis a subclass which provides a convenient enumerated wrapper.