latest.ipv8.messaging.serialization
Module Contents
Classes
A class that can pack and unpack objects to and from bytes. |
|
This is a special type of format. Allowing for nested packing. |
|
A packer for bits into a byte. |
|
Paste/unpack the remaining input without (un)packing. |
|
Pack/unpack from an encoded length + data string. |
|
Pack/unpack from an unencoded utf8 length + data string. |
|
Pack/unpack an IPv4 address. |
|
Any address format: IPv4, IPv6 or host format. |
|
A list of a given packer. |
|
A format known to the |
|
A format known to the |
|
The class performing serialization of Serializable objects. |
|
Interface for serializable objects. |
|
A serializable that has extra printing functionality. |
Attributes
- latest.ipv8.messaging.serialization.ADDRESS_TYPE_IPV4 = 1
- latest.ipv8.messaging.serialization.ADDRESS_TYPE_DOMAIN_NAME = 2
- latest.ipv8.messaging.serialization.ADDRESS_TYPE_IPV6 = 3
- latest.ipv8.messaging.serialization.FormatListType
- exception latest.ipv8.messaging.serialization.PackError
Bases:
RuntimeError
A given message format could not be packed to bytes or unpacked from bytes.
- latest.ipv8.messaging.serialization.T
- latest.ipv8.messaging.serialization.A
- class latest.ipv8.messaging.serialization.Packer
-
A class that can pack and unpack objects to and from bytes.
- class latest.ipv8.messaging.serialization.NestedPayload(serializer: Serializer)
Bases:
Packer
This is a special type of format. Allowing for nested packing.
You can specify which serializable to use by specifying its class in the format_list of the parent Serializable.
For example, nesting a Serializable of class B in class A:
class A(Serializable): format_list = [B] def __init__(self, b_instance): pass def to_pack_list(self): return [("payload", B())] @classmethod def from_unpack_list(cls, *args): return A(*args)
- pack(serializable: Serializable) bytes
Pack some serializable.
- Parameters:
serializable – the Serializable instance which we should serialize.
- Returns:
the serialized data
- unpack(data: bytes, offset: int, unpack_list: list, *args: type[Serializable]) int
Unpack a Serializable using a class definition for some given data and offset. This is a special unpack_from which also takes a payload class.
- Parameters:
data – the data to unpack from
offset – the offset in the list of data to unpack from
unpack_list – the list to which to append the Serializable
args – a list of one Serializable class to unpack to
- Returns:
the new offset
- class latest.ipv8.messaging.serialization.Raw
Bases:
Packer
Paste/unpack the remaining input without (un)packing.
- class latest.ipv8.messaging.serialization.VarLen(length_format: str, base: int = 1)
Bases:
Packer
Pack/unpack from an encoded length + data string.
- class latest.ipv8.messaging.serialization.VarLenUtf8(length_format: str, base: int = 1)
Bases:
VarLen
Pack/unpack from an unencoded utf8 length + data string.
- class latest.ipv8.messaging.serialization.Address(ip_only: bool = False)
Bases:
Packer
Any address format: IPv4, IPv6 or host format.
- class latest.ipv8.messaging.serialization.ListOf(packer: Packer, length_format: str = '>B')
Bases:
Packer
A list of a given packer.
- class latest.ipv8.messaging.serialization.DefaultArray(format_str: str, length_format: str)
Bases:
Packer
A format known to the
array
module (like ‘I’, ‘B’, etc.).Also adds support for ‘?’.
- class latest.ipv8.messaging.serialization.DefaultStruct(format_str: str)
Bases:
Packer
A format known to the
struct
module (like ‘I’, ’20s’, etc.).
- class latest.ipv8.messaging.serialization.Serializer
The class performing serialization of Serializable objects.
- add_packer(name: str, packer: Packer) None
Register a new packer with a certain name.
- Parameters:
name – the name to register
packer – the packer to use for it
- pack(fmt: str, item: object) bytes
Pack data without using a Serializable. Using a Serializable is the preferred method.
- Parameters:
fmt – the name of the packer to use while packing
item – object to pack
- Returns:
the packed data
- unpack(fmt: FormatListType, data: bytes, offset: int = 0) tuple[object, int]
Unpack data without using a Serializable. Using a Serializable is the preferred method.
- Parameters:
fmt – the name of the packer to use while unpacking
data – bytes to unpack
- Returns:
the unpacked object
- pack_serializable(serializable: Serializable) bytes
Serialize a single Serializable instance.
- Parameters:
serializable (Serializable) – the Serializable to pack
- Returns:
the serialized object
- pack_serializable_list(serializables: collections.abc.Sequence[Serializable]) bytes
Serialize a list of Serializable instances.
- Parameters:
serializables ([Serializable]) – the Serializables to pack
- Returns:
the serialized list
- unpack_serializable(serializable: type[S], data: bytes, offset: int = 0) tuple[S, int]
Use the formats specified in a serializable object and unpack to it.
- Parameters:
serializable – the serializable classes to get the format from and unpack to
data – the data to unpack from
offset – the optional offset to unpack data from
- unpack_serializable_list(serializables: collections.abc.Sequence[type[Serializable]], data: bytes, offset: int = 0, consume_all: bool = True) list[Serializable | bytes]
Use the formats specified in a list of serializable objects and unpack to them.
- Parameters:
serializables – the serializable classes to get the format from and unpack to
data – the data to unpack from
offset – position at which to start reading from data
consume_all – if having a non-empty remainder should throw an error
- Raises:
- Returns:
the list of Serializable instances
- latest.ipv8.messaging.serialization.SelfS
- class latest.ipv8.messaging.serialization.Serializable
Interface for serializable objects.
- abstract to_pack_list() list[tuple]
Serialize this object to a Serializer pack list.
E.g.:
[(format1, data1), (format2, data2), (format3, data3), ..]
- abstract classmethod from_unpack_list(*args: Any, **kwargs) SelfS
Create a new Serializable object from a list of unpacked variables.
- latest.ipv8.messaging.serialization.S
- class latest.ipv8.messaging.serialization.Payload
Bases:
Serializable
,abc.ABC
A serializable that has extra printing functionality.
- latest.ipv8.messaging.serialization.default_serializer