pak.types.array¶
Types for contiguous data of the same Type.
- class Array(elem_type, size=None)¶
Bases:
TypeA
Typefor contiguous data.- Parameters:
size (
intor typelike orstrorfunctionorNone) –The size of the
Array.If an
int, then theArrayhas a fixed size ofsize. This case will be deferred toArray.FixedSize.If a typelike, then the
Arrayis prefixed by the typelike, and its value determines the number of elements in theArray. This case will be deferred toArray.SizePrefixed.If
None, then theArrayis read until the end of the buffer. This case will be deferred toArray.Unbounded.If a
str, then the size is determined by getting the attribute of the same name from thePacketinstance. This case will be deferred toArray.FunctionSized.If a
function, then the size is determined by passing thePacketinstance to thefunction. This case will be deferred toArray.FunctionSized.
- class FixedSize(elem_type, size)¶
Bases:
ArrayAn
Arraywith a fixed size.- Parameters:
Examples
>>> import pak >>> pak.Int8[2].unpack(b"\x00\x01") [0, 1] >>> pak.Int8[2].pack([0, 1]) b'\x00\x01'
- class FunctionSized(elem_type, size)¶
Bases:
ArrayAn
Arraywhose size is determined by calling afunction.The
functionwill be passed the relevantPacketinstance, and should return the number of elements in theArray.- Parameters:
Examples
If a
stris used assize:>>> import pak >>> class MyPacket(pak.Packet): ... length: pak.Int8 ... array: pak.Int8["length"] ... >>> packet = MyPacket.unpack(b"\x02\x00\x01") >>> packet MyPacket(length=2, array=[0, 1]) >>> packet.pack() b'\x02\x00\x01'
If a normal
functionis used assize:>>> import pak >>> class MyPacket(pak.Packet): ... half_length: pak.Int8 ... array: pak.Int8[lambda p: 2 * p.half_length] ... >>> packet = MyPacket.unpack(b'\x01\x00\x01') >>> packet MyPacket(half_length=1, array=[0, 1]) >>> packet.pack() b'\x01\x00\x01'
- class SizePrefixed(elem_type, size)¶
Bases:
ArrayAn
Arraywhich is prefixed by the number of its elements.- Parameters:
Examples
>>> import pak >>> pak.Int8[pak.Int8].unpack(b"\x02\x00\x01") [0, 1] >>> pak.Int8[pak.Int8].pack([0, 1]) b'\x02\x00\x01'
- class Unbounded(elem_type)¶
Bases:
ArrayAn unbounded
Array.An unbounded
Arraywill read its elements until the end of the buffer.Unless customized, an unbounded
Arraywill read its elements until anExceptionis thrown while unpacking them, at which point theExceptionwill be suppressed and theArraywill contain all the elements that were able to be successfully read.If a
Type.UnsuppressedErroris thrown while unpacking an element, then thatExceptionwill not be suppressed.Examples
>>> import pak >>> pak.Int8[None].unpack(b"\x00\x01\x02") [0, 1, 2] >>> pak.Int8[None].pack([0, 1, 2]) b'\x00\x01\x02'
- array_size = None¶
- elem_type = None¶