jam.utils.codec.composite.bit_sequences module

class jam.utils.codec.composite.bit_sequences.BitSequenceCodec(bit_length: int | None = None, bit_order: Literal['msb', 'lsb'] = 'msb')[source]

Bases: Codec[Sequence[bool]]

Codec for encoding and decoding sequences of bits.

Bits are packed into octets (bytes) from least significant to most significant. IMP: It adds bit length encoded as a single byte at the beginning of the sequence ONLY if the bit length is not provided.

If dynamic length sequence:
  • Initialise with None

  • No need to pass bit length to decode_from

If fixed length sequence:
  • Initialise with bit length

  • Pass bit length to decode_from

__init__(bit_length: int | None = None, bit_order: Literal['msb', 'lsb'] = 'msb')[source]
bit_length: int | None = None
bit_order: Literal['msb', 'lsb'] = 'msb'
encode_size(value: Sequence[bool]) int[source]

Calculate the number of bytes needed to encode the value.

encode_into(value: Sequence[bool], buffer: bytearray, offset: int = 0) int[source]

Encode the value into the provided buffer at the specified offset.

static decode_from(buffer: bytes | bytearray | memoryview, offset: int = 0, bit_length: int | None = None, bit_order: Literal['msb', 'lsb'] = 'msb') Tuple[Sequence[bool], int][source]

Decode bit sequence from buffer.

Parameters:
  • buffer – Source buffer

  • offset – Starting offset

  • bit_length – Expected number of bits (required)

Returns:

Tuple of (decoded bit list, bytes read)

Raises:

DecodeError – If buffer too small or bit_length not specified