aiomas.codecs

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.

aiomas.codecs.serializable(repr=True)[source]

Class decorator that makes the decorated class serializable by aiomas.codecs.

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 for Codec.add_serializer()
  • __repr__(): Returns a generic instance representation. Adding this method can be deactivated by passing repr=False to the decorator.

Example:

>>> import aiomas.codecs
>>>
>>> @aiomas.codecs.serializable
... class A:
...     def __init__(self, x, y):
...         self.x = x
...         self._y = y
...
...     @property
...     def y(self):
...         return self._y
>>>
>>> codec = aiomas.codecs.JSON()
>>> codec.add_serializer(*A.__serializer__())
>>> a = codec.decode(codec.encode(A(1, 2)))
>>> a
A(x=1, y=2)
class aiomas.codecs.Codec[source]

Base class for all Codecs.

Subclasses must implement encode() and decode().

encode(data)[source]

Encode the given data and return a bytes object.

decode(data)[source]

Decode data from bytes to the original data structure.

add_serializer(type, serialize, deserialize)[source]

Add methods to serialize and deserialize objects typed type.

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.

serialize_obj(obj)[source]

Serialize obj to something that the codec can encode.

deserialize_obj(obj_repr)[source]

Deserialize the original object from obj_repr.

class aiomas.codecs.JSON[source]

A Codec that uses JSON to encode and decode messages.

class aiomas.codecs.MsgPack[source]

A Codec that uses msgpack to encode and decode messages.

class aiomas.codecs.MsgPackBlosc[source]

A Codec that uses msgpack to encode and decode messages and blosc to compress them.