Reference¶
StructDef (struct representation)¶
-
class
pycstruct.StructDef(default_byteorder='native')¶ This class represents a struct definition
Parameters: default_byteorder (str, optional) – Byte order of each element unless explicilty set for the element. Valid values are ‘native’, ‘little’ and ‘big’. -
add(type, name, length=1, byteorder='')¶ Add a new element in the struct definition. The element will be added directly after the previous element. Padding is never added.
Supported data types:
Name Size in bytes Comment int8 1 Integer uint8 1 Unsigned integer bool8 1 True (<>0) or False (0) int16 2 Integer uint16 2 Unsigned integer bool16 2 True (<>0) or False (0) float16 2 Floating point number int32 4 Integer uint32 4 Unsigned integer bool32 4 True (<>0) or False (0) float32 4 Floating point number int64 8 Integer uint64 8 Unsigned integer bool64 8 True (<>0) or False (0) float64 8 Floating point number utf-8 1 UTF-8/ASCII string. Use length parameter to set the length of the string including null termination struct struct size Embedded struct. The actual StructDef object shall be set as type and not ‘struct’ string. bitfield bitfield size - Bitfield. The actual
BitfieldDef()object shall be
set as type and not ‘bitfield’ string.
enum enum size Enum. The actual EnumDef()object shall be set as type and not ‘enum’ string.
Parameters: - type (str) – Element data type. See above.
- name (str) – Name of element. Needs to be unique.
- length (int, optional) – Number of elements. If > 1 this is an array/list of elements with equal size. Default is 1.
- byteorder (str, optional) – Byteorder of this element. Valid values are ‘native’, ‘little’ and ‘big’. If not specified the default byteorder is used.
-
deserialize(buffer)¶ Deserialize buffer into dictionary
Parameters: buffer (bytearray) – Buffer that contains the data to deserialize Returns: A dictionary keyed with the element names Return type: dict
-
serialize(data)¶ Serialize dictionary into buffer
Parameters: data (dict) – A dictionary keyed with element names. Elements can be omitted from the dictionary (defaults to value 0). Returns: A buffer that contains data Return type: bytearray
-
size()¶ Get size of structure
Returns: Number of bytes this structure represents Return type: int
-
BitfieldDef (bitfield representation)¶
-
class
pycstruct.BitfieldDef(byteorder='native')¶ This class represents a bit field definition
The size of the bit field is 1, 2, 3, .., 8 bytes depending on the number of elements added to the bit field. If a larger size is required than what is required by the elements you have to add additional, “dummy”, elements.
Parameters: byteorder (str, optional) – Byte order of the bitfield. Valid values are ‘native’, ‘little’ and ‘big’. -
add(name, nbr_of_bits=1, signed=False)¶ Add a new element in the bitfield definition. The element will be added directly after the previous element.
The size of the bitfield will expand when required, but adding more than in total 64 bits (8 bytes) will generate an exception.
Parameters: - name (str) – Name of element. Needs to be unique.
- nbr_of_bits (int, optional) – Number of bits this element represents. Default is 1.
- signed (bool, optional) – Should the bit field be signed or not. Default is False.
-
assigned_bits()¶ Get size of bitfield in bits excluding padding bits
Returns: Number of bits this bitfield represents excluding padding bits Return type: int
-
deserialize(buffer)¶ Deserialize buffer into dictionary
Parameters: buffer (bytearray) – Buffer that contains the data to deserialize (1 - 8 bytes) Returns: A dictionary keyed with the element names Return type: dict
-
serialize(data)¶ Serialize dictionary into buffer
Parameters: data (dict) – A dictionary keyed with element names. Elements can be omitted from the dictionary (defaults to value 0). Returns: A buffer that contains data Return type: bytearray
-
size()¶ Get size of bitfield in bytes
Returns: Number of bytes this bitfield represents Return type: int
-
EnumDef (enum representation)¶
-
class
pycstruct.EnumDef(byteorder='native')¶ This class represents an enum definition
The size of the enum is 1, 2, 3, .., 8 bytes depending on the value of the largest enum constant. If a larger size is required than what is required by the constants you have to add a dummy constant with a value which will force a the enum to have a particular size.
This implementation assumes that an enum is always signed.
Parameters: byteorder (str, optional) – Byte order of the enum. Valid values are ‘native’, ‘little’ and ‘big’. -
add(name, value=None)¶ Add a new constant in the enum definition. Multiple constant might be assigned to the same value.
The size of the enum will expand when required, but adding a value requiering a size larger than 64 bits will generate an exception.
Parameters: - name (str) – Name of constant. Needs to be unique.
- value (int, optional) – Value of the constant. Automatically assigned to next available value (0, 1, 2, …) if not provided.
-
deserialize(buffer)¶ Deserialize buffer into a string (constant name)
Parameters: buffer (bytearray) – Buffer that contains the data to deserialize (1 - 8 bytes) Returns: The constant name (string) Return type: str
-
get_name(value)¶ Get the name representation of the value
Returns: The constant name Return type: str
-
get_value(name)¶ Get the value representation of the name
Returns: The value Return type: int
-
serialize(data)¶ Serialize string (constant name) into buffer
Parameters: data (str) – A string representing the constant name. Returns: A buffer that contains data Return type: bytearray
-
size()¶ Get size of enum in bytes
Returns: Number of bytes this enum represents Return type: int
-