Bases: flatland.schema.containers.Dict
A declarative collection of named elements.
Schemas behave like Dict, but are defined with Python class syntax:
>>> from flatland import Schema, String
>>> class HelloSchema(Schema):
... hello = String
... world = String
...
Elements are assigned names from the attribute declaration. If a named element schema is used, a renamed copy will be assigned to the Schema to match the declaration.
>>> class HelloSchema(Schema):
... hello = String.named('hello') # redundant
... world = String.named('goodbye') # will be renamed 'world'
...
>>> helloworld = HelloSchema()
>>> sorted(helloworld.keys())
[u'hello', u'world']
Schemas may embed other container fields and other schemas:
>>> from flatland import List
>>> class BigSchema(Schema):
... main_hello = HelloSchema
... alt_hello = List.of(String.named('alt_name'),
... HelloSchema.named('alt_hello'))
...
This would create a Schema with one HelloSchema embedded as main_hello, and a list of zero or more dicts, each containing an alt_name and another HelloSchema named alt_hello.
Schemas may inherit from other Schemas or Dicts. Element attributes declared in a subclass will override those of a superclass. Multiple inheritance is supported.
The special behavior of Schema is limited to class construction time only. After construction, the Schema acts exactly like a Dict. In particular, fields declared as class attributes as above do not remain class attributes. They are removed from the class dictionary and placed in the field_schema:
>>> hasattr(HelloSchema, 'hello')
False
>>> sorted([field.name for field in HelloSchema.field_schema])
[u'hello', u'world']
The order of field_schema is undefined.
Bases: flatland.schema.containers.SparseDict
A sparse variant of Schema.
Exactly as Schema, but based upon ~flatland.schema.containers.SparseDict`.
Bases: flatland.schema.containers.Dict
An alias for Schema, for older flatland version compatibility.