pystructtype

pystructtype: Public API for pystructtype package.

Submodules

Attributes

bool_t

1 Byte bool Type

char_t

1 Byte char Type

double_t

8 Byte double Type

float_t

4 Byte float Type

int8_t

1 Byte Signed int Type

int16_t

2 Byte Signed int Type

int32_t

4 Byte Signed int Type

int64_t

8 Byte Signed int Type

string_t

1 Byte char[] Type

uint8_t

1 Byte Unsigned int Type

uint16_t

2 Byte Unsigned int Type

uint32_t

4 Byte Unsigned int Type

uint64_t

8 Byte Unsigned int Type

Classes

BitsType

Base class for bitfield structs. Subclasses must define __bits_type__ and __bits_definition__.

StructDataclass

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

TypeInfo

Class used to define Annotated Type Metadata

TypeMeta

Class used to define Annotated Type Metadata for

Package Contents

class pystructtype.BitsType[source]

Bases: pystructtype.structdataclass.StructDataclass

Base class for bitfield structs. Subclasses must define __bits_type__ and __bits_definition__.

__bits_type__: ClassVar[type]
__bits_definition__: ClassVar[dict[str, int | list[int]] | collections.abc.Mapping[str, int | list[int]]]
_raw: int
_meta: dict[str, int | list[int]]
classmethod __init_subclass__(**kwargs: object) None[source]

Initialize subclass by setting up bitfield attributes and type annotations. Ensures __bits_type__ and __bits_definition__ are present, wraps definition in MappingProxyType, and sets up class-level fields and annotations for each bitfield.

__post_init__() None[source]

Post-initialization to set up the _meta attribute from the class definition.

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

Decode the bitfield from a list of integers, updating the boolean attributes according to the bit positions defined in _meta.

_encode() list[int][source]

Encode the boolean attributes into a list of integers representing the bitfield. Updates _raw and returns the encoded list for further processing.

class pystructtype.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

class pystructtype.TypeInfo[source]

Class used to define Annotated Type Metadata for format and byte size

format: str
byte_size: int
class pystructtype.TypeMeta[T](size: int = 1, chunk_size: int = 1, default: T | None = None)[source]

Class used to define Annotated Type Metadata for size and default values

size = 1
chunk_size = 1
default = None
__hash__() int[source]
__eq__(other: Any) bool[source]
pystructtype.bool_t[source]

1 Byte bool Type

pystructtype.char_t[source]

1 Byte char Type

pystructtype.double_t[source]

8 Byte double Type

pystructtype.float_t[source]

4 Byte float Type

pystructtype.int8_t[source]

1 Byte Signed int Type

pystructtype.int16_t[source]

2 Byte Signed int Type

pystructtype.int32_t[source]

4 Byte Signed int Type

pystructtype.int64_t[source]

8 Byte Signed int Type

pystructtype.string_t[source]

1 Byte char[] Type

pystructtype.uint8_t[source]

1 Byte Unsigned int Type

pystructtype.uint16_t[source]

2 Byte Unsigned int Type

pystructtype.uint32_t[source]

4 Byte Unsigned int Type

pystructtype.uint64_t[source]

8 Byte Unsigned int Type