communication.packers.packer module

Provides a generic interface for packing and unpacking messages.

class communication.packers.packer.DefaultPacker(serializer_opts=SerializerOpts(fallback_pickle=False, ormsgpack_option=408), deserializer_opts=DeserializerOpts(fallback_pickle=False, ormsgpack_option=None))[source]

Bases: Packer

The packer that uses the default serializer.

__init__(serializer_opts=SerializerOpts(fallback_pickle=False, ormsgpack_option=408), deserializer_opts=DeserializerOpts(fallback_pickle=False, ormsgpack_option=None))[source]

Initialise the packer.

Parameters:
  • serializer_opts (SerializerOpts) – Options to change the behaviour of serialization.

  • deserializer_opts (DeserializerOpts) – Options to change the behaviour of deserialization.

pack(content, msg_id)[source]

Pack the message with given content and message id.

Parameters:
  • content (Any) – The content of the message.

  • msg_id (str) – The message identifier.

Return type:

bytes

Returns:

The resulting packet.

pack_multiple(content, msg_ids)[source]

Pack the message with given content and for every message id.

Parameters:
  • content (Any) – The content of the message.

  • msg_ids (Iterable[str]) – The message identifiers.

Return type:

tuple[bytes, ...]

Returns:

The resulting packet.

unpack(packet)[source]

Unpack a packet into a message id and content.

Parameters:

packet (bytes) – The packed packet.

Raises:

ValueError – If the deserialized packet does not contain the expected keys.

Return type:

tuple[str, Any]

Returns:

Tuple containing the message id and the content of the packet.

class communication.packers.packer.Packer[source]

Bases: ABC

Pack a message into bytes for sending it to a peer.

abstractmethod pack(content, msg_id)[source]

Pack the message with given content and message id.

Parameters:
  • content (Any) – The content of the message.

  • msg_id (str) – The message identifier.

Return type:

bytes

Returns:

The resulting packet.

abstractmethod pack_multiple(content, msg_ids)[source]

Pack the message with given content and for every message id.

Parameters:
  • content (Any) – The content of the message.

  • msg_ids (Iterable[str]) – The message identifiers.

Return type:

tuple[bytes, ...]

Returns:

The resulting packet.

abstractmethod unpack(packet)[source]

Unpack a packet into a message id and content.

Parameters:

packet (bytes) – The packed packet.

Return type:

tuple[str, Any]

Returns:

Tuple containing the message id and the content of the packet.