Basic Forms¶
-
class
Schema(value=Unspecified, **kw)¶ Bases:
flatland.schema.containers.DictA 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
HelloSchemaembedded asmain_hello, and a list of zero or more dicts, each containing analt_nameand anotherHelloSchemanamedalt_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
Schemais limited to class construction time only. After construction, theSchemaacts exactly like aDict. In particular, fields declared as class attributes as above do not remain class attributes. They are removed from the class dictionary and placed in thefield_schema:>>> hasattr(HelloSchema, 'hello') False >>> sorted([field.name for field in HelloSchema.field_schema]) [u'hello', u'world']
The order of
field_schemais undefined.
-
class
SparseSchema(value=Unspecified, **kw)¶ Bases:
flatland.schema.containers.SparseDictA sparse variant of
Schema.Exactly as
Schema, but based upon ~flatland.schema.containers.SparseDict`.
-
class
Form(value=Unspecified, **kw)¶ Bases:
flatland.schema.containers.DictAn alias for Schema, for older flatland version compatibility.