API#

exception bstruct.BstructError[source]#
class bstruct.Encodings[source]#
I80F48: _CustomEncoding[Decimal] = <bstruct._CustomEncoding object>#
bool: _NativeEncoding[bool] = <bstruct._NativeEncoding object>#
static bytes(size: int) _NativeEncoding[bytes][source]#
i128: _CustomEncoding[int] = <bstruct._CustomEncoding object>#
i16 = <bstruct._IntEncoding object>#
i256: _CustomEncoding[int] = <bstruct._CustomEncoding object>#
i32 = <bstruct._IntEncoding object>#
i64 = <bstruct._IntEncoding object>#
i8 = <bstruct._IntEncoding object>#
u128: _CustomEncoding[int] = <bstruct._CustomEncoding object>#
u16 = <bstruct._IntEncoding object>#
u256: _CustomEncoding[int] = <bstruct._CustomEncoding object>#
u32 = <bstruct._IntEncoding object>#
u64 = <bstruct._IntEncoding object>#
u8 = <bstruct._IntEncoding object>#
class bstruct.Length(length: int)[source]#
class bstruct.Size(size: int)[source]#
class bstruct.Struct[source]#

Inherit from this class to automatically derive the necessary decoding/encoding information. Also transforms any subclasses into a Python dataclass.

bstruct.compile_format(fields: list[bstruct._NativeEncoding[Any]]) str[source]#

Compile a list of attribute descriptions into a struct.Struct format string. This does not contain the byteorder specifier (e.g. < or >).

bstruct.decode(cls: type[S], data: bytes, byteorder: Literal['big', 'little'] = 'little') S[source]#

Decode an instance of S from the provided data. The size of data must exactly match the size of S.

bstruct.decode_all(cls: type[S], data: bytes, byteorder: Literal['big', 'little'] = 'little') Iterator[S][source]#

Decode multiple instances of S. The size of data must be an integer multiple of the size of S.

bstruct.decode_from(cls: type[S], data_stream: BufferedIOBase, byteorder: Literal['big', 'little'] = 'little') S[source]#

Read and decode an instance of S from the data stream.

bstruct.encode(value: Struct, byteorder: Literal['big', 'little'] = 'little') bytes[source]#

Encode the value according to the provided byteorder.

bstruct.get_size(cls: type[bstruct.Struct]) int[source]#

Return the size in bytes of the serialized form of a bstruct.Struct class.

bstruct.get_struct(cls: type[bstruct.Struct], byteorder: Literal['big', 'little'] = 'little') Struct[source]#

Return the underlying struct.Struct instance used to (un)pack the binary data.

bstruct.patch(cls: type[T], size: int, decode: Callable[[bytes, Literal['big', 'little']], T], encode: Callable[[T, Literal['big', 'little']], bytes]) None[source]#

Patch an existing class. The size must be the expected size in bytes of the serialized form of T.