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 by valid_value(). If valid_value() returns false, the element is not converted and will have a value of None.

Constrained is a semi-abstract class that requires an implementation of valid_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

Enum is a subclass which provides a convenient enumerated wrapper.

Enum

class Enum(value=Unspecified, **kw)

A scalar type with a limited set of allowed values.

By default values are strings, but can be of any type you like by customizing child_type.