Arrays and MultiValues

Schema Constructors

classmethod Array.named(name)

Return a class with name = name

Parameter:name – a string or None. str will be converted to unicode.
Returns:a new class
classmethod Array.of(*schema)

Declare the class to hold a sequence of *schema.

Params *schema:one or more flatland.Element classes
Returns:cls

Configures the member_schema of cls to hold instances of *schema.

>>> from flatland import Array, String
>>> Names = Array.of(String.named('name'))
>>> Names.member_schema
<class 'flatland.schema.scalars.String'>
>>> el = Names(['Bob', 'Biff'])
>>> el
[<String u'name'; value=u'Bob'>, <String u'name'; value=u'Biff'>]

If more than one Element is specified in *schema, an anonymous Dict is created to hold them.

>>> from flatland import Integer
>>> Points = Array.of(Integer.named('x'), Integer.named('y'))
>>> Points.member_schema
<class 'flatland.schema.containers.Dict'>
>>> el = Points([dict(x=1, y=2)])
>>> el
[{u'y': <Integer u'y'; value=2>, u'x': <Integer u'x'; value=1>}]
classmethod Array.using(**overrides)

Return a class with attributes set from **overrides.

Parameter:**overrides – new values for any attributes already present on the class. A TypeError is raised for unknown attributes.
Returns:a new class

Factory Methods

classmethod Array.from_defaults(**kw)

Return a new element with its value initialized from field defaults.

Parameter:**kw – passed through to the element_type.

This is a convenience constructor for:

element = cls(**kw)
element.set_default()
classmethod Array.from_flat(pairs, **kw)

Return a new element with its value initialized from pairs.

Parameter:**kw – passed through to the element_type.

This is a convenience constructor for:

element = cls(**kw)
element.set_flat(pairs)

Configurable Attributes

Sequence.member_schema
An Element class for sequence members.
Sequence.prune_empty

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

See `Sequences`_ for more information.

Array

class Array(value=Unspecified, **kw)

Bases: flatland.schema.containers.Sequence

A transparent homogeneous Container, for multivalued form elements.

Arrays hold a collection of values under a single name, allowing all values of a repeated (key, value) pair to be captured and used. Elements are sequence-like.

add_error(message)
Register an error message on this element, ignoring duplicates.
add_warning(message)
Register a warning message on this element, ignoring duplicates.
append(value)

Append value to end.

If value is not an instance of member_schema, it will be wrapped in a new element of that type before appending.

count(value)

Return number of occurrences of value.

If value is not an instance of member_schema, it will be wrapped in a new element of that type before searching for matching elements in the sequence.

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
el(path, sep=u'.')

Find a child element by string path.

Parameters:
  • path – a sep-separated string of element names, or an iterable of names
  • sep – optional, a string separator used to parse path
Returns:

an Element or raises KeyError.

>>> first_address = form.el('contact.addresses.0')
>>> first_address.el('street1')
<String u'street1'; value=None>

Given a relative path as above, el() searches for a matching path among the element’s children.

If path begins with sep, the path is considered fully qualified and the search is resolved from the Element.root. The leading sep will always match the root node, regardless of its name.

>>> form.el('.contact.addresses.0.city')
<String u'city'; value=None>
>>> first_address.el('.contact.addresses.0.city')
<String u'city'; value=None>
extend(iterable)

Append iterable values to the end.

If values of iterable are not instances of member_schema, they will be wrapped in a new element of that type before extending.

find(path, single=False, strict=True)

Find child elements by string path.

Parameters:
  • path – a /-separated string specifying elements to select, such as ‘child/grandchild/greatgrandchild’. Relative & absolute paths are supported, as well as container expansion. See Path Lookups.
  • single – if true, return a scalar result rather than a list of elements. If no elements match path, None is returned. If multiple elements match, a LookupError is raised. If multiple elements are found and strict is false, an unspecified element from the result set is returned.
  • strict – defaults to True. If path specifies children or sequence indexes that do not exist, a :ref:`LookupError is raised.
Returns:

a list of Element instances, an Element if single is true, or raises LookupError.

>>> cities = form.find('/contact/addresses[:]/city')
>>> [el.value for el in cities]
[u'Kingsport', u'Dunwich']
>>> form.find('/contact/name', single=True)
<String u'name'; value=u'Obed Marsh'>
flatten(sep=u'_', value=<operator.attrgetter object at 0x16cdd30>)

Export an element hierarchy as a flat sequence of key, value pairs.

Parameters:
  • sep – a string, will join together element names.
  • value – a 1-arg callable called once for each element. Defaults to a callable that returns the u of each element.

Encodes the element hierarchy in a sep-separated name string, paired with any representation of the element you like. The default is the Unicode value of the element, and the output of the default flatten() can be round-tripped with set_flat().

Given a simple form with a string field and a nested dictionary:

>>> from flatland import Dict, String
>>> class Nested(Form):
...     contact = Dict.of(String.named(u'name'),
...                       Dict.named(u'address').          ...                            of(String.named(u'email')))
...
>>> element = Nested()
>>> element.flatten()
[(u'contact_name', u''), (u'contact_address_email', u'')]

The value of each pair can be customized with the value callable:

>>> element.flatten(value=operator.attrgetter('u'))
[(u'contact_name', u''), (u'contact_address_email', u'')]
>>> element.flatten(value=lambda el: el.value)
[(u'contact_name', None), (u'contact_address_email', None)]

Solo elements will return a sequence containing a single pair:

>>> element['name'].flatten()
[(u'contact_name', u'')]
flattened_name(sep=u'_')

Return the element’s complete flattened name as a string.

Joins this element’s path with sep and returns the fully qualified, flattened name. Encodes all Container and other structures into a single string.

Example:

>>> import flatland
>>> form = flatland.List('addresses',
...                      flatland.String('address'))
>>> element = form()
>>> element.set([u'uptown', u'downtown'])
>>> element.el('0').value
u'uptown'
>>> element.el('0').flattened_name()
u'addresses_0_address'
fq_name(sep=u'.')

Return the fully qualified path name of the element.

Returns a sep-separated string of el() compatible element indexes starting from the Element.root (.) down to the element.

>>> from flatland import Dict, Integer
>>> Point = Dict.named(u'point').of(Integer.named(u'x'),
...                                 Integer.named(u'y'))
>>> p = Point(dict(x=10, y=20))
>>> p.name
u'point'
>>> p.fq_name()
u'.'
>>> p['x'].name
u'x'
>>> p['x'].fq_name()
u'.x'

The index used in a path may not be the name of the element. For example, sequence members are referenced by their numeric index.

>>> from flatland import List, String
>>> Addresses = List.named('addresses').of(String.named('address'))
>>> form = Addresses([u'uptown', u'downtown'])
>>> form.name
u'addresses'
>>> form.fq_name()
u'.'
>>> form[0].name
u'address'
>>> form[0].fq_name()
u'.0'
classmethod including_descent_validators(*validators, **kw)

Return a class with additional descent *validators.

Parameters:
  • *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.
Returns:

a new class

classmethod including_validators(*validators, **kw)

Return a class with additional *validators.

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

a new class

index(value)

Return first index of value.

If value is not an instance of member_schema, it will be wrapped in a new element of that type before searching for a matching element in the sequence.

insert(index, value)

Insert value at index.

If value is not an instance of member_schema, it will be wrapped in a new element of that type before inserting.

classmethod named(name)

Return a class with name = name

Parameter:name – a string or None. str will be converted to unicode.
Returns:a new class
classmethod of(*schema)

Declare the class to hold a sequence of *schema.

Params *schema:one or more flatland.Element classes
Returns:cls

Configures the member_schema of cls to hold instances of *schema.

>>> from flatland import Array, String
>>> Names = Array.of(String.named('name'))
>>> Names.member_schema
<class 'flatland.schema.scalars.String'>
>>> el = Names(['Bob', 'Biff'])
>>> el
[<String u'name'; value=u'Bob'>, <String u'name'; value=u'Biff'>]

If more than one Element is specified in *schema, an anonymous Dict is created to hold them.

>>> from flatland import Integer
>>> Points = Array.of(Integer.named('x'), Integer.named('y'))
>>> Points.member_schema
<class 'flatland.schema.containers.Dict'>
>>> el = Points([dict(x=1, y=2)])
>>> el
[{u'y': <Integer u'y'; value=2>, u'x': <Integer u'x'; value=1>}]
remove(value)

Remove member with value value.

If value is not an instance of member_schema, it will be wrapped in a new element of that type before searching for a matching element to remove.

set(iterable)

Assign the native and Unicode value.

Attempts to adapt the given iterable and assigns this element’s value and u attributes in tandem. Returns True if the adaptation was successful. See Element.set().

Set must be supplied a Python sequence or iterable:

>>> from flatland import Integer, List
>>> Numbers = List.of(Integer)
>>> nums = Numbers()
>>> nums.set([1, 2, 3, 4])
True
>>> nums.value
[1, 2, 3, 4]
set_default()
set() the element to the schema default.
set_flat(pairs, sep=u'_')

Set element values from pairs, expanding the element tree as needed.

Given a sequence of name/value tuples or a dict, build out a structured tree of value elements.

classmethod using(**overrides)

Return a class with attributes set from **overrides.

Parameter:**overrides – new values for any attributes already present on the class. A TypeError is raised for unknown attributes.
Returns:a new class
validate(state=None, recurse=True)

Assess the validity of this element and its children.

Parameters:
  • state – optional, will be passed unchanged to all validator callables.
  • recurse – if False, do not validate children. :returns: True or False

Iterates through this element and all of its children, invoking each element’s schema.validate_element(). Each element will be visited twice: once heading down the tree, breadth-first, and again heading back up in reverse order.

Returns True if all validations pass, False if one or more fail.

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().

classmethod validated_by(*validators)

Return a class with validators set to *validators.

Parameter:*validators – one or more validator functions, replacing any validators present on the class.
Returns:a new class
classmethod with_properties(*iterable, **properties)
TODO: doc
all_children
An iterator of all child elements, breadth-first.
all_valid
True if this element and all children are valid.
default_value

A calculated “default” value.

If default_factory is present, it will be called with the element as a single positional argument. The result of the call will be returned.

Otherwise, returns default.

When comparing an element’s value to its default value, use this property in the comparison.

parents
An iterator of all parent elements.
path
An iterator of all elements from root to the Element, inclusive.
pop
L.pop([index]) -> item – remove and return item at index (default last). Raises IndexError if list is empty or index is out of range.
reverse
L.reverse() – reverse IN PLACE
root
The top-most parent of the element.
sort
L.sort(cmp=None, key=None, reverse=False) – stable sort IN PLACE; cmp(x, y) -> -1, 0, 1
x
Sugar, the xml-escaped value of u.
xa
Sugar, the xml-attribute-escaped value of u.

MultiValue

class MultiValue(value=Unspecified, **kw)

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

A transparent homogeneous Container, for multivalued form elements.

MultiValues combine aspects of Scalar and Sequence fields, allowing all values of a repeated (key, value) pair to be captured and used.

MultiValues take on the name of their child and have no identity of their own when flattened. Elements are mostly sequence-like and can be indexed and iterated. However the u or value are scalar-like, and return values from the first element in the sequence.