pystructtype
pystructtype: Public API for pystructtype package.
Submodules
Attributes
1 Byte bool Type |
|
1 Byte char Type |
|
8 Byte double Type |
|
4 Byte float Type |
|
1 Byte Signed int Type |
|
2 Byte Signed int Type |
|
4 Byte Signed int Type |
|
8 Byte Signed int Type |
|
1 Byte char[] Type |
|
1 Byte Unsigned int Type |
|
2 Byte Unsigned int Type |
|
4 Byte Unsigned int Type |
|
8 Byte Unsigned int Type |
Classes
Base class for bitfield structs. Subclasses must define __bits_type__ and __bits_definition__. |
|
Class that will auto-magically decode and encode data for the defined |
|
Class used to define Annotated Type Metadata |
|
Class used to define Annotated Type Metadata for |
Package Contents
- class pystructtype.BitsType[source]
Bases:
pystructtype.structdataclass.StructDataclassBase 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.
- 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
- class pystructtype.TypeInfo[source]
Class used to define Annotated Type Metadata for format and byte size
- format: str
- byte_size: int