Lists¶
Instances of List hold other elements and operate like Python
lists. Lists are configured with a member_schema, such
as an Integer. Each list member will
be an instance of that type. The List.of() schema constructor
will set member_schema:
>>> from flatland import List, Integer
>>> Numbers = List.of(Integer)
>>> Numbers.member_schema
<class 'flatland.schema.scalars.Integer'>
Python list methods and operators may be passed instances of
member_schema or plain Python values. Using plain values
is a shorthand for creating a member_schema instance and
set()ting it with the value:
>>> ones = Numbers()
>>> ones.append(1)
>>> ones.value
[1]
>>> another_one = Integer()
>>> another_one.set(1)
True
>>> ones.append(another_one)
>>> ones.value
[1, 1]
List extends Sequence and adds positional naming to its
elements. Elements are addressable via their list index in
find() and their index in the list
is reflected in their flattened name:
Example:
>>> from flatland import List, String
>>> Names = List.named('names').of(String.named('name'))
>>> names = Names(['a', 'b'])
>>> names.value
['a', 'b']
>>> names.flatten()
[('names_0_name', 'a'), ('names_1_name', 'b')]
>>> names[1].value
'b'
>>> names.find_one('1').value
'b'
Validation¶
If descent_validators is defined, these validators
will be run first, before member elements are validated.
If validators is defined, these
validators will be run after member elements are validated.
List¶
- class List(value=Unspecified, **kw)¶
An ordered, homogeneous Sequence.