struct
Larky module containing functions to work with Struct.
This module provides access to C-like structs represented as Larky bytes objects. This can be used in handling binary data. It uses Format Strings as compact descriptions of the layout of the C structs and the intended conversion to/from Larky values.
Similar to struct.
struct.calcsize(fmt)
Return the size of the struct (and hence of the bytes object produced by pack(format, …)) corresponding to the format string format.
Parameters:
fmt – format.
Returns: size.
struct.pack(fmt, *values)
Return a bytes object containing the values v1, v2, ... packed according to the format string format. The arguments must match the values required by the format exactly.
Example:
values = (1, 'ab', 2.7)
struct.pack('I 2s f', *values)
(1, 'ab', 2.7)
Parameters:
fmt – format.
values – values to pack.
Returns: struct object containing values.
struct.pack_into(fmt, buffer, offset, *v)
Pack the values v1, v2, ... according to the format string format and write the packed bytes into the writable buffer buffer starting at position offset. Note that offset is a required argument.
Parameters:
fmt – format.
buffer – writable buffer.
offset – starting positions.
v – values to pack.
Returns: struct object containing values.
struct.unpack(fmt, buffer)
Unpack from the buffer buffer (presumably packed by pack(format, ...)) according to the format string format. The result is a tuple even if it contains exactly one item. The buffer’s size in bytes must match the size required by the format, as reflected by calcsize().
Parameters:
fmt – format.
buffer – writable buffer.
Returns: string representation of struct.
struct.unpack_from(fmt, buffer, offset=0)
Unpack from buffer starting at position offset, according to the format string format. The result is a tuple even if it contains exactly one item. The buffer’s size in bytes, starting at position offset, must be at least the size required by the format, as reflected by calcsize().
Parameters:
fmt – format.
buffer – writable buffer.
offset – starting positions.
Last updated