Bases: flatland.schema.base.Element
Holds other schema items.
Base class for elements that can contain other elements, such as List and Dict.
Parameters: |
|
---|
Todo
doc descent_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 |
Return a class with additional descent *validators.
Parameters: |
|
---|---|
Returns: | a new class |
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().
>>> 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]
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>]
If true, skip missing index numbers in set_flat(). Default True.
See Sequences for more information.
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>}]
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() the element to the schema default.
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.
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 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 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.
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.
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.
Bases: flatland.schema.containers.Container, dict
Base of mapping-like Containers.
Todo
doc field_schema
Return True if the element schema allows a field named key.
2-tuple; but raise KeyError if D is empty.
If key is not found, d is returned if given, otherwise KeyError is raised
Update with keys from dict-like *dictish and **kwargs
Todo
doc set()
set() the element to the schema default.
A string repr of the element.
The element as a regular Python dictionary.
Mappings are never empty.