struct_parse package

Module contents

Parse the format defined in the struct standard library module. Single-file parser for the string format specified by the struct standard library module. This enables the use of strings describing the layout of packed binary data for other things besides just packing and unpacking, like generating C struct definitions.

class struct_parse.ByteOrder[source]

Bases: enum.Enum

An enumeration.

BIG = 2
LITTLE = 1
NATIVE = 0
NETWORK = 3
class struct_parse.FieldList(fields: List[struct_parse.FieldType] = [], byte_order: struct_parse.ByteOrder = <ByteOrder.NATIVE: 0>)[source]

Bases: object

Simply a list of fields, along with a byte_order.

classmethod from_string(fmt_string: str) → struct_parse.FieldList[source]

Construct a FieldList from a format string.

Parameters:fmt_string – Format string conformant to the specification in struct.
Returns:FieldList object representing the parsed string as a flat AST.
to_string() → str[source]

Convert the list of fields back into a format string.

Returns:Format string.
class struct_parse.FieldType[source]

Bases: enum.Enum

Type of a field in the struct. Represented by a single character in the format string.

BOOL = 4
CHAR = 1
CHAR_ARRAY = 18
DOUBLE = 17
FLOAT = 16
HALF_PRECISION_FLOAT = 15
INT = 7
LONG = 9
LONG_LONG = 11
PAD = 0
SHORT = 5
SIGNED_CHAR = 2
SIZE_T = 14
SSIZE_T = 13
UNSIGNED_CHAR = 3
UNSIGNED_INT = 8
UNSIGNED_LONG = 10
UNSIGNED_LONG_LONG = 12
UNSIGNED_SHORT = 6
VOID_POINTER = 19
struct_parse.parse(format_str: str) → struct_parse.FieldList[source]

Helper to parse a format string into a FieldList.

Parameters:format_str – Format string, as specified by struct.
Returns:FieldList object representing the parsed format string.