Compound Fields

class Compound(value=Unspecified, **kw)

Bases: flatland.schema.containers.Mapping, flatland.schema.scalars.Scalar

A mapping container that acts like a scalar value.

Compound fields are dictionary-like fields that can assemble a u and value from their children, and can decompose a structured value passed to a set() into values for its children.

A simple example is a logical calendar date field composed of 3 separate Integer component fields, year, month and day. The Compound can wrap the 3 parts up into a single logical field that handles datetime.date values. Set a date on the logical field and its component fields will be set with year, month and day; alter the int value of the year component field and the logical field updates the date to match.

Compound is an abstract class. Subclasses must implement compose() and explode().

Composites run validation after their children.

compose()

Return a unicode, native tuple built from children’s state.

Returns:a 2-tuple of unicode representation, native value. These correspond to the Scalar.serialize_element() and Scalar.adapt_element() methods of Scalar objects.

For example, a compound date field may return a ‘-‘ delimited string of year, month and day digits and a datetime.date.

explode(value)

Given a compound value, assign values to children.

Parameter:value – a value to be adapted and exploded

For example, a compound date field may read attributes from a datetime.date value and set() them on child fields.

The decision to perform type checking on value is completely up to you and you may find you want different rules for different compound types.

is_empty
True if all subfields are empty.
class DateYYYYMMDD(value=Unspecified, **kw)
Bases: flatland.schema.compound.Compound, flatland.schema.scalars.Date
class JoinedString(value=Unspecified, **kw)

Bases: flatland.schema.containers.Array, flatland.schema.scalars.String

A sequence container that acts like a compounded string such as CSV.

Marshals child element values to and from a single string:

>>> from flatland import JoinedString
>>> el = JoinedString(['x', 'y', 'z'])
>>> el.value
u'x,y,z'
>>> el2 = JoinedString('foo,bar')
>>> el2[1].value
u'bar'
>>> el2.value
u'foo,bar'

Only the joined representation is considered when flattening or restoring with set_flat(). JoinedStrings run validation after their children.

member_schema
The default child type is String, but can be customized with Integer or any other type.
separator
The string used to join children’s u representations. Will also be used to split incoming strings, unless separator_regex is also defined.
separator_regex

Optional, a regular expression, used preferentially to split an incoming separated value into components. Used in combination with separator, a permissive parsing policy can be combined with a normalized representation, e.g.:

>>> import re
>>> schema = JoinedString.using(separator=', ',
...                             separator_regex=re.compile('\s*,\s*'))
...
>>> schema('a  ,  b,c,d').value
u'a, b, c, d'
u
A read-only separator-joined string of child values.
value
A read-only separator-joined string of child values.