Codecs¶
Codec implementations¶
This package imports the codecs that can be used for de- and encoding incoming and outgoing messages:
All codecs should implement the base class Codec
.
Most of this code is taken and adapted from Stefan Scherfkes aiomas: https://gitlab.com/sscherfke/aiomas/
- class mango.messages.codecs.Codec[source]¶
Bases:
object
Base class for all Codecs.
Subclasses must implement
encode()
anddecode()
.- add_serializer(otype, serialize, deserialize, type_id=None)[source]¶
Add methods to serialize and deserialize objects typed otype.
This can be used to de-/encode objects that the codec otherwise couldn’t encode.
serialize will receive the unencoded object and needs to return an encodable serialization of it.
deserialize will receive an objects representation and should return an instance of the original object.
- exception mango.messages.codecs.DecodeError[source]¶
Bases:
Exception
Raised when an object representation can not be decoded.
- class mango.messages.codecs.JSON[source]¶
Bases:
Codec
A
Codec
that uses JSON to encode and decode messages.
- exception mango.messages.codecs.SerializationError[source]¶
Bases:
Exception
Raised when an object cannot be serialized.
- mango.messages.codecs.json_serializable(cls=None, repr=True)[source]¶
This is a direct copy from aiomas: https://gitlab.com/sscherfke/aiomas/-/blob/master/src/aiomas/codecs.py
Class decorator that makes the decorated class serializable by the json codec (or any codec that can handle python dictionaries).
The decorator tries to extract all arguments to the class’
__init__()
. That means, the arguments must be available as attributes with the same name.The decorator adds the following methods to the decorated class:
__asdict__()
: Returns a dict with all __init__ parameters__fromdict__(dict)
: Creates a new class instance from dict__serializer__()
: Returns a tuple with args forCodec.add_serializer()
__repr__()
: Returns a generic instance representation. Adding this method can be deactivated by passingrepr=False
to the decorator.