pak.types.numeric¶
Types for numbers.
- class Bool¶
Bases:
StructTypeA single byte truth-value.
See also
- fmt = '?'¶
- class Int8¶
Bases:
StructTypeA signed 8-bit integer.
- fmt = 'b'¶
- class UInt8¶
Bases:
StructTypeAn unsigned 8-bit integer.
- fmt = 'B'¶
- class Int16¶
Bases:
StructTypeA signed 16-bit integer.
- fmt = 'h'¶
- class UInt16¶
Bases:
StructTypeAn unsigned 16-bit integer.
- fmt = 'H'¶
- class Int32¶
Bases:
StructTypeA signed 32-bit integer.
- fmt = 'i'¶
- class UInt32¶
Bases:
StructTypeAn unsigned 32-bit integer.
- fmt = 'I'¶
- class Int64¶
Bases:
StructTypeA signed 64-bit integer.
- fmt = 'q'¶
- class UInt64¶
Bases:
StructTypeAn unsigned 64-bit integer.
- fmt = 'Q'¶
- class Float32¶
Bases:
StructTypeA 32-bit floating point value.
- fmt = 'f'¶
- class Float64¶
Bases:
StructTypeA 64-bit floating point value.
- fmt = 'd'¶
- class LEB128¶
Bases:
TypeA variable length signed integer following the
LEB128format.- class Limited(*, max_bytes)¶
Bases:
TypeAn
LEB128which is limited to a certain number of bytes.It may be useful to limit the number of bytes which may be marshaled in order to prevent a malicious actor from endlessly setting the top bit of each byte so that unpacking never ends.
By limiting the number of bytes, then unpacking is always guaranteed to end.
When unpacking, if the number of bytes exceeds the specified maximum, then a
MaxBytesExceededErrorwill be raised.When packing, if the to-be-packed value exceeds the associated range of values for the maximum number of bytes, then a
ValueErrorwill be raised.- Parameters:
max_bytes (
int) – The number of bytes to limit theLEB128to.
- max_bytes = None¶
- class ULEB128¶
Bases:
TypeA variable length unsigned integer following the
LEB128format.- class Limited(*, max_bytes)¶
Bases:
TypeAn
ULEB128which is limited to a certain number of bytes.It may be useful to limit the number of bytes which may be marshaled in order to prevent a malicious actor from endlessly setting the top bit of each byte so that unpacking never ends.
By limiting the number of bytes, then unpacking is always guaranteed to end.
When unpacking, if the number of bytes exceeds the specified maximum, then a
MaxBytesExceededErrorwill be raised.When packing, if the to-be-packed value exceeds the associated range of values for the maximum number of bytes, then a
ValueErrorwill be raised.- Parameters:
max_bytes (
int) – The number of bytes to limit theULEB128to.
- max_bytes = None¶
- class Not(underlying)¶
Bases:
TypeA boolean value transformed by the
notoperation.In certain cases the plain value of a boolean marshaled from its raw data will sometimes be inverted from what would be more straightforward for understanding what the value really represents.
For instance, if one were unpacking information about a particular animal, and one of the values was a
Boolwhich represented the attributeis_not_a_fish, then it could be better to invert that tois_a_fishusing aNot.The default value of a
Notwill be the same as the default value of its underlyingType. This is because thenotoperation is not generally relevant to the end user’s preferences, but rather only towards packing and unpacking.- Parameters:
underlying (typelike) – The underlying boolean
Type.
Examples
>>> import pak >>> NotBool = pak.Not(pak.Bool) >>> NotBool.unpack(b"\x01") # Underlying value of 'True'. False >>> NotBool.unpack(b"\x00") # Underlying value of 'False'. True >>> NotBool.pack(False) b'\x01' >>> NotBool.pack(True) b'\x00'
- underlying = None¶
- class ScaledInteger(elem_type, divisor)¶
Bases:
TypeA floating-point value derived from scaling an integer.
- Parameters:
elem_type (typelike) – The underlying integer
Type.divisor (
intorfloat) – The divisor to use for scaling the underlying integer.
Examples
>>> import pak >>> # Make a 'ScaledInteger' that will divide the >>> # underlying 'Int8' value by '2'. >>> Scaled = pak.ScaledInteger(pak.Int8, 2) >>> Scaled.unpack(b"\x01") # Underlying value of '1'. 0.5 >>> Scaled.pack(0.5) b'\x01'
- divisor = None¶
- elem_type = None¶