pak.types.optional¶
Types for marshaling data which might exist.
- class Optional(elem_type, exists=None)[source]¶
Bases:
TypeA
Typewhich might exist.- Parameters:
elem_type (typelike) – The underlying
Type.exists (typelike or
Noneorstrorfunction) –If a typelike, then the
Optionalis prefixed by the typelike and its value determines whether or not theOptionalexists. This case will be deferred toOptional.PrefixChecked.If
None, then whether theOptionalexists is not checked, and is eagerly tried to be unpacked. This is usually used forOptionals at the end of the buffer. This case will be deferred toOptional.Unchecked.If a
str, then whether theOptionalexists is determined by getting the attribute of the same name from thePacketinstance. This case will be deferred toOptional.FunctionChecked.If a
function, then whether theOptionalexists is determined by passing thePacketinstance to thefunction. This case will be deferred toOptional.FunctionChecked.
- elem_type = None¶
- exists = None¶
- class PrefixChecked(elem_type, exists)[source]¶
Bases:
OptionalAn
Optionalwhich exists if its prefix says so.- Parameters:
Examples
>>> import pak >>> Prefixed = pak.Optional(pak.Int8, pak.Bool) >>> Prefixed.unpack(b"\x01\x02") 2 >>> Prefixed.pack(2) b'\x01\x02' >>> >>> assert Prefixed.unpack(b"\x00") is None >>> Prefixed.pack(None) b'\x00'
- class Unchecked(elem_type)[source]¶
Bases:
OptionalAn
Optionalwhich does not check whether it exists.Such
Optionals are usually placed at the end of raw data.The
Optionaldoes not exist if anyExceptionis thrown while unpacking.If a
Type.UnsuppressedErroris thrown while unpacking, then thatExceptionwill not be suppressed.- Parameters:
elem_type (typelike) – The underlying
Type.
Examples
>>> import pak >>> Unchecked = pak.Optional(pak.Int8) >>> Unchecked.unpack(b"\x01") 1 >>> Unchecked.pack(1) b'\x01' >>> >>> assert Unchecked.unpack(b"") is None >>> Unchecked.pack(None) b''
- class FunctionChecked(elem_type, exists)[source]¶
Bases:
OptionalAn
Optionalwhich exists if afunctionsays so.The
functionwill be passed the relevantPacketinstance, and should return abooldetermining whether theOptionalexists.- Parameters:
elem_type (typelike) – The underlying
Type.exists (
functionorstr) –If a
function, then whether theOptionalexists is determined by calling thefunctionwith the relevantPacketinstance.If a
str, then afunctionwhich gets and returns the attribute named by thestris used as the effectivefunctionfor the existence of theOptional.
Examples
If a
stris used asexists:>>> import pak >>> class MyPacket(pak.Packet): ... exists: pak.Bool ... optional: pak.Optional(pak.Int8, "exists") ... >>> packet = MyPacket.unpack(b"\x01\x02") >>> packet MyPacket(exists=True, optional=2) >>> packet.pack() b'\x01\x02' >>> >>> packet = MyPacket.unpack(b"\x00") >>> packet MyPacket(exists=False, optional=None) >>> packet.pack() b'\x00'
If a
functionis used asexists:>>> import pak >>> class MyPacket(pak.Packet): ... flag: pak.Int8 ... optional: pak.Optional(pak.Int8, lambda p: p.flag == 3) ... >>> packet = MyPacket.unpack(b"\x03\x02") >>> packet MyPacket(flag=3, optional=2) >>> packet.pack() b'\x03\x02' >>> >>> packet = MyPacket.unpack(b"\x01") >>> packet MyPacket(flag=1, optional=None) >>> packet.pack() b'\x01'