Abstract Containers

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.

Parameters:
  • 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.
descent_validators = ()

Todo

doc descent_validators

classmethod descent_validated_by(*validators)

Return a class with descent validators set to *validators.

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

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

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

Sequences

>>> 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)
100
>>> 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)
True
>>> ones.append(another_one)
>>> ones
[<Integer None; value=1>, <Integer None; value=1>]
member_schema = None

An Element class for sequence members.

prune_empty = True

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

See Sequences for more information.

classmethod of(*schema)

Declare the class to hold a sequence of *schema.

Params *schema:one or more 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>}]
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.

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.

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.

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.

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.

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.

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.

Mappings

class Mapping(value=Unspecified, **kw)

Bases: flatland.schema.containers.Container, dict

Base of mapping-like Containers.

field_schema = ()

Todo

doc field_schema

may_contain(key)

Return True if the element schema allows a field named key.

clear() → None. Remove all items from D.
popitem() → (k, v), remove and return some (key, value) pair as a

2-tuple; but raise KeyError if D is empty.

pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised

update(*dictish, **kwargs)

Update with keys from dict-like *dictish and **kwargs

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
get(k[, d]) → D[k] if k in D, else d. d defaults to None.
set(value)

Todo

doc set()

set_default()

set() the element to the schema default.

u

A string repr of the element.

value

The element as a regular Python dictionary.

is_empty

Mappings are never empty.

Contents

Topics