pystructtype.structdataclass

StructDataclass: Base class for auto-decoding/encoding struct-like dataclasses.

Classes

StructState

Contains necessary struct information to correctly

StructDataclass

Class that will auto-magically decode and encode data for the defined

Module Contents

class pystructtype.structdataclass.StructState[source]

Contains necessary struct information to correctly decode and encode the data in a StructDataclass

name: str[source]
struct_fmt: str[source]
size: int[source]
chunk_size: int[source]
class pystructtype.structdataclass.StructDataclass[source]

Class that will auto-magically decode and encode data for the defined subclass.

classmethod __init_subclass__(**kwargs: object) None[source]

Automatically configure the subclass as a dataclass and set up default values for fields. Handles special logic for list and non-list fields, default factories, and class variables.

__post_init__() None[source]

Initialize instance state and struct format after dataclass construction. Computes struct format string and byte length for encoding/decoding.

_simplify_format() None[source]

Simplify the struct format that has been defined for this class.

Essentially we turn things like ccbbbbh into 2c4bh

size() int[source]

The size of this struct is defined as the sum of the sizes of all attributes

Returns:

Combined size of the struct

static _endian(little_endian: bool) str[source]

Return “<” or “>” depending on endianness, to pass to struct decode/encode

Parameters:

little_endian – True if we expect little_endian, else False

Returns:

“<” if little_endian else “>”

static _to_bytes(data: list[int] | bytes) bytes[source]

Convert a list of ints into bytes

Parameters:

data – a list of ints or a bytes object

Returns:

a bytes object

static _to_list(data: list[int] | bytes) list[int][source]

Convert a bytes object into a list of ints

Parameters:

data – a list of ints or a bytes object

Returns:

a list of ints

_decode(data: list[int]) None[source]

Internal decoding function for the StructDataclass.

Extend this function if you wish to add extra processing to your StructDataclass decoding processing

Parameters:

data – A list of ints to decode into the StructDataclass

decode(data: list[int] | bytes, little_endian: bool = False) None[source]

Decode the given data into this subclass of StructDataclass

Parameters:
  • data – list of ints or a bytes object

  • little_endian – True if decoding little_endian formatted data, else False

Raises:

ValueError – If the input data is not the correct length for the struct

_encode() list[int][source]

Internal encoding function for the StructDataclass.

Extend this function if you wish to add extra processing to your StructDataclass encoding processing

Returns:

list of encoded int data

encode(little_endian: bool = False) bytes[source]

Encode the data from this subclass of StructDataclass into bytes

Parameters:

little_endian – True if encoding little_endian formatted data, else False

Returns:

encoded bytes