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.

default_value()[source]
dump(obj)[source]
jsonschema_type_schema(state)
load(obj)[source]
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
set_parent(schema)[source]
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
classmethod from_class(metacls, cls, auto_store=True)[source]

Create proper PySchema class from cls

Any methods and attributes will be transferred to the new object

class pyschema.core.Record(*args, **kwargs)[source]

Bases: object

Abstract base class for structured logging records

class pyschema.core.SchemaStore[source]

Bases: object

add_record(schema, _bump_stack_level=False)[source]

Add record class to record store for retrieval at record load time.

Can be used as a class decorator

clear()[source]
clone()[source]
get(record_name)[source]
remove_record(schema)[source]
pyschema.core.disable_auto_register()[source]
pyschema.core.dumps(obj, attach_schema_name=True)[source]
pyschema.core.enable_auto_register()[source]
pyschema.core.from_json_compatible(schema, dct)[source]

Load from json-encodable

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.core.no_auto_store()[source]

Temporarily disable automatic registration of records in the auto_store

Decorator factory. This is _NOT_ thread safe

>>> @no_auto_store()
... class BarRecord(Record):
...     pass
>>> BarRecord in auto_store
False
pyschema.core.set_schema_name_field(name)[source]
pyschema.core.to_json_compatible(record)[source]

Dump record in json-encodable object format

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'
dump(obj)[source]
jsonschema_type_name = 'boolean'
load(obj)[source]
pg_type = 'BOOLEAN'
class pyschema.types.Bytes(custom_encoding=False, **kwargs)[source]

Bases: pyschema.core.Field

Binary data

avro_type_name = 'bytes'
dump(binary_data)[source]
load(obj)[source]
class pyschema.types.Date(description=None, nullable=True, default=<object object at 0x10a7a4870>)[source]

Bases: pyschema.types.Text

dump(obj)[source]
load(obj)[source]
pg_type = 'DATE'
class pyschema.types.DateTime(description=None, nullable=True, default=<object object at 0x10a7a4870>)[source]

Bases: pyschema.types.Text

dump(obj)[source]
load(obj)[source]
pg_type = 'TIMESTAMP WITHOUT TIME ZONE'
class pyschema.types.Enum(values, **kwargs)[source]

Bases: pyschema.core.Field

avro_type_name = 'ENUM'
dump(obj)[source]
jsonschema_type_name = 'string'
jsonschema_type_schema(state)
load(obj)[source]
simplified_avro_type_schema(state)
class pyschema.types.Float(size=8, **kwargs)[source]

Bases: pyschema.core.Field

avro_type_name
dump(obj)[source]
jsonschema_type_name = 'number'
load(obj)[source]
pg_type = 'FLOAT'
class pyschema.types.Integer(size=8, **kwargs)[source]

Bases: pyschema.core.Field

avro_type_name
dump(obj)[source]
jsonschema_type_name = 'integer'
load(obj)[source]
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'
default_value()[source]
dump(obj)[source]
jsonschema_type_name = 'array'
jsonschema_type_schema(state)
load(obj)[source]
set_parent(schema)[source]
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'
default_value()[source]
dump(obj)[source]
jsonschema_type_schema(state)
load(obj)[source]
set_parent(schema)[source]
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
default_value()[source]
dump(obj)[source]
jsonschema_type_name
jsonschema_type_schema(state)
load(obj)[source]
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)
class pyschema.types.Text(description=None, nullable=True, default=<object object at 0x10a7a4870>)[source]

Bases: pyschema.core.Field

avro_type_name = 'string'
dump(obj)[source]
jsonschema_type_name = 'string'
load(obj)[source]
pg_type = 'TEXT'

Module contents