API#

class bstruct.Array(length: int)[source]#

Special annotation class to specify the length of an array.

exception bstruct.BstructError[source]#
class bstruct.Bytes(size: int)[source]#
class bstruct.CustomEncoding(target: type[T], format: str, decode_attributes: Callable[[Iterator[Union[bool, bytes, int]], Literal['big', 'little']], T], encode_attributes: Callable[[T, list[Union[bool, bytes, int]], Literal['big', 'little']], None])[source]#
static create(target: type[T], fields: Iterable[_NativeEncoding[Any]], decode_attributes: Callable[[Iterator[Union[bool, bytes, int]], Literal['big', 'little']], T], encode_attributes: Callable[[T, list[Union[bool, bytes, int]], Literal['big', 'little']], None]) CustomEncoding[T][source]#
class bstruct.Encoding(target: type[T], format: str, decode_attributes: Callable[[Iterator[Union[bool, bytes, int]], Literal['big', 'little']], T], encode_attributes: Callable[[T, list[Union[bool, bytes, int]], Literal['big', 'little']], None])[source]#
decode(data: bytes, byteorder: Literal['big', 'little'] = 'little') T[source]#

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

decode_all(data: bytes, byteorder: Literal['big', 'little'] = 'little') Iterator[T][source]#

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

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

Encode the value according to the provided byteorder.

get_size() int[source]#

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

get_struct(byteorder: Literal['big', 'little']) Struct[source]#

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

read(buffer: BufferedIOBase, byteorder: Literal['big', 'little'] = 'little') T[source]#

Read and decode an instance of cls from the buffer.

read_many(buffer: BufferedIOBase, count: int, byteorder: Literal['big', 'little'] = 'little') list[T][source]#

Read and decode count instances of cls from the buffer.

write(value: T, buffer: BufferedIOBase, byteorder: Literal['big', 'little'] = 'little') None[source]#

Encode and write value into the buffer.

write_many(items: Iterable[T], buffer: BufferedIOBase, byteorder: Literal['big', 'little'] = 'little') None[source]#

Encode and write all items into the buffer.

class bstruct.Encodings[source]#
I80F48 = <bstruct.CustomEncoding object>#
bool: _NativeEncoding[bool] = <bstruct._NativeEncoding object>#
f16 = <bstruct._NativeEncoding object>#
f32 = <bstruct._NativeEncoding object>#
f64 = <bstruct._NativeEncoding object>#
i128 = <bstruct.CustomEncoding object>#
i16 = <bstruct._NativeEncoding object>#
i256 = <bstruct.CustomEncoding object>#
i32 = <bstruct._NativeEncoding object>#
i64 = <bstruct._NativeEncoding object>#
i8 = <bstruct._NativeEncoding object>#
u128 = <bstruct.CustomEncoding object>#
u16 = <bstruct._NativeEncoding object>#
u256 = <bstruct.CustomEncoding object>#
u32 = <bstruct._NativeEncoding object>#
u64 = <bstruct._NativeEncoding object>#
u8 = <bstruct._NativeEncoding object>#
class bstruct.String(size: int)[source]#
bstruct.compile_format(fields: Iterable[_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.derive(attribute_type: type[T]) Encoding[T][source]#