pystructtype ============ .. py:module:: pystructtype .. autoapi-nested-parse:: pystructtype: Public API for pystructtype package. Submodules ---------- .. toctree:: :maxdepth: 1 /autoapi/pystructtype/bitstype/index /autoapi/pystructtype/structdataclass/index /autoapi/pystructtype/structtypes/index /autoapi/pystructtype/utils/index Attributes ---------- .. autoapisummary:: pystructtype.bool_t pystructtype.char_t pystructtype.double_t pystructtype.float_t pystructtype.int8_t pystructtype.int16_t pystructtype.int32_t pystructtype.int64_t pystructtype.string_t pystructtype.uint8_t pystructtype.uint16_t pystructtype.uint32_t pystructtype.uint64_t Classes ------- .. autoapisummary:: pystructtype.BitsType pystructtype.StructDataclass pystructtype.TypeInfo pystructtype.TypeMeta Package Contents ---------------- .. py:class:: BitsType Bases: :py:obj:`pystructtype.structdataclass.StructDataclass` Base class for bitfield structs. Subclasses must define __bits_type__ and __bits_definition__. .. py:attribute:: __bits_type__ :type: ClassVar[type] .. py:attribute:: __bits_definition__ :type: ClassVar[dict[str, int | list[int]] | collections.abc.Mapping[str, int | list[int]]] .. py:attribute:: _raw :type: int .. py:attribute:: _meta :type: dict[str, int | list[int]] .. py:method:: __init_subclass__(**kwargs: object) -> None :classmethod: 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. .. py:method:: __post_init__() -> None Post-initialization to set up the _meta attribute from the class definition. .. py:method:: _decode(data: list[int]) -> None Decode the bitfield from a list of integers, updating the boolean attributes according to the bit positions defined in _meta. .. py:method:: _encode() -> list[int] Encode the boolean attributes into a list of integers representing the bitfield. Updates _raw and returns the encoded list for further processing. .. py:class:: StructDataclass Class that will auto-magically decode and encode data for the defined subclass. .. py:method:: __init_subclass__(**kwargs: object) -> None :classmethod: 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. .. py:method:: __post_init__() -> None Initialize instance state and struct format after dataclass construction. Computes struct format string and byte length for encoding/decoding. .. py:method:: _simplify_format() -> None Simplify the struct format that has been defined for this class. Essentially we turn things like `ccbbbbh` into `2c4bh` .. py:method:: size() -> int The size of this struct is defined as the sum of the sizes of all attributes :return: Combined size of the struct .. py:method:: _endian(little_endian: bool) -> str :staticmethod: Return "<" or ">" depending on endianness, to pass to struct decode/encode :param little_endian: True if we expect little_endian, else False :return: "<" if little_endian else ">" .. py:method:: _to_bytes(data: list[int] | bytes) -> bytes :staticmethod: Convert a list of ints into bytes :param data: a list of ints or a bytes object :return: a bytes object .. py:method:: _to_list(data: list[int] | bytes) -> list[int] :staticmethod: Convert a bytes object into a list of ints :param data: a list of ints or a bytes object :return: a list of ints .. py:method:: _decode(data: list[int]) -> None Internal decoding function for the StructDataclass. Extend this function if you wish to add extra processing to your StructDataclass decoding processing :param data: A list of ints to decode into the StructDataclass .. py:method:: decode(data: list[int] | bytes, little_endian: bool = False) -> None Decode the given data into this subclass of StructDataclass :param data: list of ints or a bytes object :param 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 .. py:method:: _encode() -> list[int] Internal encoding function for the StructDataclass. Extend this function if you wish to add extra processing to your StructDataclass encoding processing :return: list of encoded int data .. py:method:: encode(little_endian: bool = False) -> bytes Encode the data from this subclass of StructDataclass into bytes :param little_endian: True if encoding little_endian formatted data, else False :return: encoded bytes .. py:class:: TypeInfo Class used to define Annotated Type Metadata for format and byte size .. py:attribute:: format :type: str .. py:attribute:: byte_size :type: int .. py:class:: TypeMeta[T](size: int = 1, chunk_size: int = 1, default: T | None = None) Class used to define Annotated Type Metadata for size and default values .. py:attribute:: size :value: 1 .. py:attribute:: chunk_size :value: 1 .. py:attribute:: default :value: None .. py:method:: __hash__() -> int .. py:method:: __eq__(other: Any) -> bool .. py:data:: bool_t 1 Byte bool Type .. py:data:: char_t 1 Byte char Type .. py:data:: double_t 8 Byte double Type .. py:data:: float_t 4 Byte float Type .. py:data:: int8_t 1 Byte Signed int Type .. py:data:: int16_t 2 Byte Signed int Type .. py:data:: int32_t 4 Byte Signed int Type .. py:data:: int64_t 8 Byte Signed int Type .. py:data:: string_t 1 Byte char[] Type .. py:data:: uint8_t 1 Byte Unsigned int Type .. py:data:: uint16_t 2 Byte Unsigned int Type .. py:data:: uint32_t 4 Byte Unsigned int Type .. py:data:: uint64_t 8 Byte Unsigned int Type