pak.types.numeric

Types for numbers.

class Bool

Bases: StructType

A single byte truth-value.

See also

Not

fmt = '?'
class Int8

Bases: StructType

A signed 8-bit integer.

fmt = 'b'
class UInt8

Bases: StructType

An unsigned 8-bit integer.

fmt = 'B'
class Int16

Bases: StructType

A signed 16-bit integer.

fmt = 'h'
class UInt16

Bases: StructType

An unsigned 16-bit integer.

fmt = 'H'
class Int32

Bases: StructType

A signed 32-bit integer.

fmt = 'i'
class UInt32

Bases: StructType

An unsigned 32-bit integer.

fmt = 'I'
class Int64

Bases: StructType

A signed 64-bit integer.

fmt = 'q'
class UInt64

Bases: StructType

An unsigned 64-bit integer.

fmt = 'Q'
class Float32

Bases: StructType

A 32-bit floating point value.

fmt = 'f'
class Float64

Bases: StructType

A 64-bit floating point value.

fmt = 'd'
class LEB128

Bases: Type

A variable length signed integer following the LEB128 format.

class Limited(*, max_bytes)

Bases: Type

An LEB128 which 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 MaxBytesExceededError will be raised.

When packing, if the to-be-packed value exceeds the associated range of values for the maximum number of bytes, then a ValueError will be raised.

Parameters:

max_bytes (int) – The number of bytes to limit the LEB128 to.

max_bytes = None
class ULEB128

Bases: Type

A variable length unsigned integer following the LEB128 format.

class Limited(*, max_bytes)

Bases: Type

An ULEB128 which 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 MaxBytesExceededError will be raised.

When packing, if the to-be-packed value exceeds the associated range of values for the maximum number of bytes, then a ValueError will be raised.

Parameters:

max_bytes (int) – The number of bytes to limit the ULEB128 to.

max_bytes = None
class Not(underlying)

Bases: Type

A boolean value transformed by the not operation.

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 Bool which represented the attribute is_not_a_fish, then it could be better to invert that to is_a_fish using a Not.

The default value of a Not will be the same as the default value of its underlying Type. This is because the not operation 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: Type

A floating-point value derived from scaling an integer.

Parameters:
  • elem_type (typelike) – The underlying integer Type.

  • divisor (int or float) – 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