Abstract Containers


class Container(value=Unspecified, **kw)

Bases: flatland.schema.base.Element

Holds other schema items.

Base class for elements that can contain other elements, such as List and Dict.

  • descent_validators – optional, a sequence of validators that will be run before contained elements are validated.
  • validators – optional, a sequence of validators that will be run after contained elements are validated.
  • **kw – other arguments common to FieldSchema.
classmethod descent_validated_by(*validators)

Return a class with descent validators set to *validators.

Parameter:*validators – one or more validator functions, replacing any descent validators present on the class.
Returns:a new class
classmethod including_descent_validators(*validators, **kw)

Return a class with additional descent *validators.

  • *validators – one or more validator functions
  • position – defaults to -1. By default, additional validators are placed after existing descent validators. Use 0 for before, or any other list index to splice in validators at that point.

a new class

validate_element(element, state, descending)

Validates on the first (downward) and second (upward) pass.

If descent_validators are defined on the schema, they will be evaluated before children are validated. If a validation function returns flatland.SkipAll or flatland.SkipFalse, downward validation will halt on this container and children will not be validated.

If validators are defined, they will be evaluated after children are validated.

See FieldSchema.validate_element().

TODO: doc descent_validators


>>> from flatland import List, String
>>> Names = List.named('names').of(String.named('name'))
>>> pruned = Names()
>>> pruned.set_flat([('names_0_name', 'first'),
...                  ('names_99_name', 'last')])
>>> pruned.value
[u'first', u'last']
>>> unpruned = Names(prune_empty=False)
>>> unpruned.set_flat([('names_0_name', 'first'),
...                    ('names_99_name', 'last')])
>>> len(unpruned.value)
>>> unpruned.value[0:3]
[u'first', None, None]
class Sequence(value=Unspecified, **kw)

Bases: flatland.schema.containers.Container, list

Abstract base of sequence-like Containers.

Instances of Sequence hold other elements and operate like Python lists. Each sequence member will be an instance of member_schema.

Python list methods and operators may be passed instances of member_schema or plain Python values. Using plain values is a shorthand for creating an member_schema instance and set()ting it with the value:

>>> from flatland import Array, Integer
>>> Numbers = Array.of(Integer)
>>> ones = Numbers()
>>> ones.append(1)
>>> ones
[<Integer None; value=1>]
>>> another_one = Integer()
>>> another_one.set(1)
>>> ones.append(another_one)
>>> ones
[<Integer None; value=1>, <Integer None; value=1>]

If true, skip missing index numbers in set_flat(). Default True.

See Sequences for more information.


class Mapping(value=Unspecified, **kw)
Base of mapping-like Containers.