Bitfield Class Reference
Bitfield
Bases: Generic[ContextT]
A base class for creating bitfields.
ctx
class-attribute
instance-attribute
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
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
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
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
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
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
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 |
to_bits
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
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. |