pystructtype.structdataclass ============================ .. py:module:: pystructtype.structdataclass .. autoapi-nested-parse:: StructDataclass: Base class for auto-decoding/encoding struct-like dataclasses. Classes ------- .. autoapisummary:: pystructtype.structdataclass.StructState pystructtype.structdataclass.StructDataclass Module Contents --------------- .. py:class:: StructState Contains necessary struct information to correctly decode and encode the data in a StructDataclass .. py:attribute:: name :type: str .. py:attribute:: struct_fmt :type: str .. py:attribute:: size :type: int .. py:attribute:: chunk_size :type: int .. 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