kylie package

Module contents

kylie - A module for mapping between JSON and Python classes.

class kylie.Attribute(struct_name=None, python_type=None, serialized_type=None)[source]

Bases: object

Used to define a persistent attribute on a Model subclass.

Parameters:
  • struct_name (str, optional) – The dict key to be used when serializing this attribute. Defaults to the name the attribute’s name on its host Model.
  • python_type (function, optional) – A function that takes the serialized value and converts it to the type that will be stored on the Model instance. This parameter is the usually used with, and is the opposite of serialized_type.
  • serialized_type (function, optional) – A function that takes the value stored on the Model instance and returns the value that should be stored in the serialized dict. This parameter is the usually used with, and is the opposite of python_type.
pack(instance, record)[source]

Store the attribute on the provided dictionary, record.

struct_name

The name of the attribute when it is persisted.

This is either calculated from the attribute’s name on the Model it is assigned to, or provided by the constructor’s struct_name parameter.

unpack(instance, value)[source]

Unpack the data item and store on the instance.

class kylie.BaseModelChoice[source]

Bases: object

Abstract class for providing Model class choice on deserialization.

If records contain an attribute that specify the class of the serialized object, then you should probably use MappedModelChoice instead. If your logic is more complex then subclass BaseModelChoice and and implement choose_model.

Instances of BaseModelChoice can be passed to the Attribute constructor in place of a Model class.

choose_model(value)[source]

Return a Model class suitable for deserializing the given value.

Params:
value: A data item.
Return: An object (or class, usually a Model) with a deserialize method
that can deserialize the given value
deserialize(value)[source]

Deserialize value into a dynamically chosen Model.

The chosen Model is specified by the abstract method choose_model.

exception kylie.DeserializationError[source]

Bases: exceptions.Exception

Error indicating deserialization failed.

class kylie.MappedModelChoice(type_map, attribute_name='__type__')[source]

Bases: kylie.kylie.BaseModelChoice

choose_model(value)[source]

Choose a Model for deserialization.

This implementation chooses a Model based on the value of the attribute specified by the attribute_name this MappedModelChoice was instantiated with. The value of this attribute is looked up in this instance’s type_map and the resulting Model class is returned.

If the value of the special attribute is not found in type_map, a DeserializationError is raised.

class kylie.Model(*args, **kwargs)[source]

Bases: object

A parent class that can map to and from JSON-style data structures.

classmethod deserialize(record)[source]

Extract the data from a dict into this Model instance.

serialize()[source]

Extract this model’s Attributes into a dict.

class kylie.Relation(deserializable, struct_name=None, sequence=False)[source]

Bases: kylie.kylie.Attribute

An Attribute that embeds to another Model.

Params:
deserializable (Model, BaseModelChoice): The Model or BaseModelChoice
subclass that will be deserialized into this attribute.
struct_name (str, optional): The name of the key that will be used
when serializing this attribute into a dict. Defaults to the name of the attribute on the host Model.
sequence (bool, optional): Indicates that this attribute will store
a sequence of deserializables, which will be serialized to a list.
pack(instance, record)[source]

Serialize the provided instance into the provided dict record.

unpack(instance, value)[source]

Unpack an embedded, serialized Model.

Create a new instance of the relation_class and deserialize the provided value into it.