Skip to content

Bitfield Class Reference

Bitfield

Bases: Generic[ContextT]

A base class for creating bitfields.

ctx class-attribute instance-attribute

ctx: ContextT | None = None

A context object that can be referenced by dynamic fields while serializing and deserializing the bitfield. Set by to_* and from_* methods when serializing and deserializing the bitfield.

from_bits classmethod

from_bits(
    bits: Sequence[bool], ctx: ContextT | None = None
) -> t.Tuple[Self, t.Tuple[bool, ...]]

Parse a bitfield from a sequence of bits, returning the parsed object and any remaining bits.

Parameters:

Name Type Description Default
bits Sequence[bool]

The bits to parse.

required
ctx ContextT | None

An optional context object to use when parsing.

None

Returns:

Type Description
Tuple[Self, Tuple[bool, ...]]

t.Tuple[Self, t.Tuple[bool, ...]]: A tuple containing the parsed object and any remaining bits.

from_bits_exact classmethod

from_bits_exact(
    bits: Sequence[bool], ctx: ContextT | None = None
)

Parses a bitfield from a sequence of bits, returning the parsed object. Raises a ValueError if there are any bits left over after parsing.

Parameters:

Name Type Description Default
bits Sequence[bool]

The bits to parse.

required
ctx ContextT | None

An optional context object to use when parsing.

None

Returns:

Name Type Description
Self

The parsed object.

Raises:

Type Description
ValueError

If there are any bits left over after parsing.

from_bytes classmethod

from_bytes(
    data: ByteString, ctx: ContextT | None = None
) -> t.Tuple[Self, bytes]

Parses a bitfield from a byte string, returning the parsed object and any remaining bytes.

Parameters:

Name Type Description Default
data ByteString

The bytes to parse.

required
ctx ContextT | None

An optional context object to use when parsing.

None

Returns:

Type Description
Tuple[Self, bytes]

t.Tuple[Self, bytes]: A tuple containing the parsed object and any remaining bytes.

from_bytes_batch classmethod

from_bytes_batch(
    data: ByteString, ctx: ContextT | None = None
) -> t.Tuple[t.List[Self], bytes]

Parses a batch of bitfields from a byte string, returning the parsed objects and any remaining bytes.

Parameters:

Name Type Description Default
data ByteString

The bytes to parse.

required
ctx ContextT | None

An optional context object to use when parsing.

None

Returns:

Type Description
Tuple[List[Self], bytes]

t.Tuple[t.List[Self], bytes]: A tuple containing the parsed objects and any remaining bytes.

from_bytes_exact classmethod

from_bytes_exact(
    data: ByteString, ctx: ContextT | None = None
)

Parses a bitfield from a byte string, returning the parsed object. Raises a ValueError if there are any bytes left over after parsing.

Parameters:

Name Type Description Default
data ByteString

The bytes to parse.

required
ctx ContextT | None

An optional context object to use when parsing.

None

Returns:

Name Type Description
Self

The parsed object.

Raises:

Type Description
ValueError

If there are any bytes left over after parsing.

length classmethod

length() -> int | None

Get the length of a bitfield in bits. If the bitfield has dynamic fields, None is returned.

Returns:

Type Description
int | None

int | None: The length of the bitfield in bits, or None if it has dynamic fields.

to_bits

to_bits(ctx: ContextT | None = None) -> t.Tuple[bool, ...]

Serializes the bitfield to a sequence of bits.

Parameters:

Name Type Description Default
ctx ContextT | None

An optional context object to use when serializing.

None

Returns:

Type Description
Tuple[bool, ...]

t.Tuple[bool, ...]: The serialized bitfield as a sequence of bits.

to_bytes

to_bytes(ctx: ContextT | None = None) -> bytes

Serializes the bitfield to a byte string.

Parameters:

Name Type Description Default
ctx ContextT | None

An optional context object to use when serializing.

None

Returns:

Name Type Description
bytes bytes

The serialized bitfield as a byte string.