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
.
-
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.
- 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
-
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 subclassBaseModelChoice
and and implementchoose_model
.Instances of BaseModelChoice can be passed to the
Attribute
constructor in place of a Model class.
-
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
thisMappedModelChoice
was instantiated with. The value of this attribute is looked up in this instance’stype_map
and the resultingModel
class is returned.If the value of the special attribute is not found in
type_map
, aDeserializationError
is raised.
-
-
class
kylie.
Model
(*args, **kwargs)[source]¶ Bases:
object
A parent class that can map to and from JSON-style data structures.
-
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.