pak.bit_field

Contains BitField.

class BitField(**fields)

Bases: object

A collection of data packed into specific bits of an underlying integer.

A definition of a BitField looks like this:

class MyBitField(pak.BitField):
    boolean_field: 1
    integer_field: 2
    other_field:   2

MyBitField inherits from BitField, and its annotations specify the bit widths of each of its fields, starting at the least significant bit.

Fields which have a bit width of 1 will have a bool value, corresponding to whether the appropriate bit is set or unset.

Fields which have a bit width larger than 1 will have an int value.

If a field is specified to have a bit width of 0, then a TypeError is raised.

Parameters:

**fields – The names and corresponding values of the fields of the BitField.

Raises:

TypeError – If there are any superfluous keyword arguments.

classmethod Type(underlying)

Makes a Type which works with BitField values.

Parameters:

underlying (typelike) – The underlying integer Type which works with the packed integer value.

Returns:

The corresponding Type.

Return type:

subclass of Type

pack_to_int()

Packs a BitField into an int.

Returns:

The corresponding integer value containing the packed data.

Return type:

int

Raises:

ValueError – If a field’s value is too wide for its corresponding width.

classmethod unpack_from_int(value)

Unpacks a BitField from an int.

Parameters:

value (int) – The integer value containing the packed data.

Returns:

The corresponding BitField.

Return type:

BitField