core.custom.custom_msgpack ========================== .. py:module:: core.custom.custom_msgpack Attributes ---------- .. autoapisummary:: core.custom.custom_msgpack._T core.custom.custom_msgpack._TypeT core.custom.custom_msgpack.default_serializers core.custom.custom_msgpack.PACK_OPTIONS Classes ------- .. autoapisummary:: core.custom.custom_msgpack.Serializer core.custom.custom_msgpack.BytesSerializer core.custom.custom_msgpack.StringSerializer core.custom.custom_msgpack.DictionarySerializer core.custom.custom_msgpack.Serializers core.custom.custom_msgpack.Serializable Functions --------- .. autoapisummary:: core.custom.custom_msgpack.load_keyed_tuple core.custom.custom_msgpack.make_serializable core.custom.custom_msgpack.packb core.custom.custom_msgpack.unpackb core.custom.custom_msgpack.packable Module Contents --------------- .. py:data:: _T .. py:data:: _TypeT .. py:class:: Serializer(tag: int, target: type[_T]) Bases: :py:obj:`Generic`\ [\ :py:obj:`_T`\ ] Provides a way to encode all objects of a given class or its subclasses to and from MessagePack using extension types. .. py:attribute:: tag .. py:attribute:: target .. py:method:: encode(obj: _T) -> bytes :abstractmethod: .. py:method:: decode(value: bytes) -> _T :abstractmethod: .. py:class:: BytesSerializer(tag: int, target: type[_T], encode: collections.abc.Callable[[_T], bytes], decode: collections.abc.Callable[[bytes], _T]) Bases: :py:obj:`Serializer`\ [\ :py:obj:`_T`\ ] Serializes objects to a byte string. .. py:attribute:: _encode .. py:attribute:: _decode .. py:method:: encode(obj: _T) -> bytes .. py:method:: decode(value: bytes) -> _T .. py:class:: StringSerializer(tag: int, target: type[_T], encode: collections.abc.Callable[[_T], str], decode: collections.abc.Callable[[str], _T]) Bases: :py:obj:`Serializer`\ [\ :py:obj:`_T`\ ] Serializes objects to a string. .. py:attribute:: _encode .. py:attribute:: _decode .. py:method:: encode(obj: _T) -> bytes .. py:method:: decode(value: bytes) -> _T .. py:class:: DictionarySerializer(tag: int, target: type[_T], keys: collections.abc.Iterable[str]) Bases: :py:obj:`Serializer`\ [\ :py:obj:`_T`\ ] Serializes objects that can be built with keyword arguments. For example:: class Point: def __init__(self, x, y): self.x = x self.y = y Can be serialised using:: DictionarySerializer(Point, ('x', 'y')) Which results in something like this in JSON:: {'x': 1, 'y': 2} As the internal __dict__ represenation is of no concern, __slots__ may be used: class Point: __slots__ = ('x', 'y') def __init__(self, x, y): self.x = x self.y = y .. py:attribute:: constructor .. py:method:: encode(obj: _T) -> bytes .. py:method:: decode(value: bytes) -> _T .. py:class:: Serializers Organises the different serializer implementations under a unifiying interface. This allows the actual encoder/decoder to call a single class without having to worry how the various serializers need to be looked up and called. .. py:attribute:: by_tag :type: dict[int, Serializer[Any]] .. py:attribute:: by_type :type: dict[type[object], tuple[int, Serializer[Any]]] .. py:method:: register(serializer: Serializer[Any]) -> None .. py:method:: encode(value: object) -> object .. py:method:: decode(tag: int, value: bytes) -> Any .. py:data:: default_serializers .. py:function:: load_keyed_tuple(b: bytes) -> sqlalchemy.util._collections.AbstractKeyedTuple[Any] .. py:class:: Serializable Classes inheriting from this base are serialised using the :class:`DictionarySerializer` class. The keys that should be used need to be specified as follows:: class Point(Serializable, keys=('x', 'y')): def __init__(self, x, y): self.x = x self.y = y .. py:attribute:: serialized_keys :type: ClassVar[collections.abc.Collection[str]] .. py:method:: serializers() -> Serializers :classmethod: .. py:method:: __init_subclass__(tag: int, keys: collections.abc.Collection[str], **kwargs: Any) :classmethod: .. py:function:: make_serializable(*, tag: int, serializers: Serializers = default_serializers) -> collections.abc.Callable[[_TypeT], _TypeT] .. py:data:: PACK_OPTIONS .. py:function:: packb(obj: Any) -> bytes .. py:function:: unpackb(value: bytes) -> Any .. py:function:: packable(obj: Any) -> bool