pyschema package¶
Submodules¶
pyschema.core module¶
Schema definition toolkit using Python classes
Usage example:
>>> class Foo(Record):
... bin = Bytes()
...
... class MyRecord(Record):
... a_string = Text()
... a_float = Float()
... record = List(SubRecord(Foo))
...
... rec = MyRecord(a_string="hej")
... rec.record = [Foo(bin="bar")]
...
... s = dumps(rec)
... print loads(s)
Internals:
A valid PySchema class contains the following class variables:
- _fields
- An OrderedDict of field_name => field_type where field_type is an instance of a Field subclass
- _schema_name
- The qualifying name for this schema. This is used for registering a record in a SchemaStore and for auto-identification of serialized records. Should be unique within a specific SchemaStore, so if auto registering is used it should be unique within the execution chain of the current program.
- class pyschema.core.Field(description=None, nullable=True, default=<object object at 0x10a7a4870>)[source]¶
Bases: object
- avro_default_value()¶
- avro_dump(o)¶
- avro_load(o)¶
- avro_type_schema(state)¶
Full type specification for the field
I.e. the same as would go into the “type” field. For most field, only simplified_avro_type_schema has to be implemented.
- jsonschema_type_schema(state)¶
- classmethod mixin(mixin_cls)[source]¶
Decorator for mixing in additional functionality into field type
Example:
>>> @Integer.mixin ... class IntegerPostgresExtensions: ... postgres_type = 'INT' ... ... def postgres_dump(self, obj): ... self.dump(obj) + "::integer"
Is roughly equivalent to:
>>> Integer.postgres_type = 'INT' ... ... def postgres_dump(self, obj): ... self.dump(obj) + "::integer" ... ... Integer.postgres_dump = postgres_dump
- simplified_avro_type_schema(state)¶
The basic avro type for this field
Not including nullability.
- exception pyschema.core.ParseError[source]¶
Bases: exceptions.Exception
Generic exception type for Record parse errors
- class pyschema.core.PySchema[source]¶
Bases: abc.ABCMeta
Metaclass for Records
Builds schema on Record declaration and remembers Record types for easy generic parsing
- auto_register = True¶
- class pyschema.core.Record(*args, **kwargs)[source]¶
Bases: object
Abstract base class for structured logging records
- class pyschema.core.SchemaStore[source]¶
Bases: object
- pyschema.core.ispyschema(schema)[source]¶
Is object PySchema instance?
Returns true for PySchema Record classes i.e. NOT when schema is a Record instance
>>> class FooRecord(Record): ... pass >>> ispyschema(FooRecord) True >>> ispyschema(FooRecord()) False
- pyschema.core.load_json_dct(dct, record_store=None, schema=None, loader=<function from_json_compatible at 0x10ab08b90>)[source]¶
Create a Record instance from a json-compatible dictionary
The dictionary values should have types that are json compatible, as if just loaded from a json serialized record string.
Parameters: - dct – Python dictionary with key/value pairs for the record
- record_store – Record store to use for schema lookups (when $schema field is present)
- schema – PySchema Record class for the record to load. This will override any $schema fields specified in dct
- pyschema.core.loads(s, record_store=None, schema=None, loader=<function from_json_compatible at 0x10ab08b90>)[source]¶
Create a Record instance from a json serialized dictionary
Parameters: - s – String with a json-serialized dictionary
- record_store – Record store to use for schema lookups (when $schema field is present)
- schema – PySchema Record class for the record to load. This will override any $schema fields specified in s
pyschema.types module¶
- class pyschema.types.Boolean(description=None, nullable=True, default=<object object at 0x10a7a4870>)[source]¶
Bases: pyschema.core.Field
- VALUE_MAP = {False: '0', True: '1'}¶
- avro_type_name = 'boolean'¶
- jsonschema_type_name = 'boolean'¶
- pg_type = 'BOOLEAN'¶
- class pyschema.types.Bytes(custom_encoding=False, **kwargs)[source]¶
Bases: pyschema.core.Field
Binary data
- avro_type_name = 'bytes'¶
- class pyschema.types.Date(description=None, nullable=True, default=<object object at 0x10a7a4870>)[source]¶
Bases: pyschema.types.Text
- pg_type = 'DATE'¶
- class pyschema.types.DateTime(description=None, nullable=True, default=<object object at 0x10a7a4870>)[source]¶
Bases: pyschema.types.Text
- pg_type = 'TIMESTAMP WITHOUT TIME ZONE'¶
- class pyschema.types.Enum(values, **kwargs)[source]¶
Bases: pyschema.core.Field
- avro_type_name = 'ENUM'¶
- jsonschema_type_name = 'string'¶
- jsonschema_type_schema(state)¶
- simplified_avro_type_schema(state)¶
- class pyschema.types.Float(size=8, **kwargs)[source]¶
Bases: pyschema.core.Field
- avro_type_name¶
- jsonschema_type_name = 'number'¶
- pg_type = 'FLOAT'¶
- class pyschema.types.Integer(size=8, **kwargs)[source]¶
Bases: pyschema.core.Field
- avro_type_name¶
- jsonschema_type_name = 'integer'¶
- pg_type = 'INT'¶
- class pyschema.types.List(field_type=Text, nullable=False, default=[], **kwargs)[source]¶
Bases: pyschema.core.Field
List of one other Field type
Differs from other fields in that it is not nullable and defaults to empty array instead of null
- avro_dump(obj)¶
- avro_load(obj)¶
- avro_type_name = 'array'¶
- jsonschema_type_name = 'array'¶
- jsonschema_type_schema(state)¶
- simplified_avro_type_schema(state)¶
- class pyschema.types.Map(value_type, nullable=False, default={}, **kwargs)[source]¶
Bases: pyschema.core.Field
List of one other Field type
Differs from other fields in that it is not nullable and defaults to empty array instead of null
- avro_dump(obj)¶
- avro_load(obj)¶
- avro_type_name = 'object'¶
- jsonschema_type_schema(state)¶
- simplified_avro_type_schema(state)¶
- class pyschema.types.SubRecord(schema, **kwargs)[source]¶
Bases: pyschema.core.Field
“Field for storing other :class:`record.Record`s
- avro_dump(obj)¶
- avro_load(obj)¶
- avro_type_name¶
- jsonschema_type_name¶
- jsonschema_type_schema(state)¶
- set_parent(schema)[source]¶
This method gets called by the metaclass once the container class has been created to let the field store a reference to its parent if needed. Its needed for SubRecords in case it refers to the container record.
- simplified_avro_type_schema(state)¶