bpack.bs module

Bitstruct based codec for binary data structures.

class bpack.bs.Codec(descriptor, codec=None, decode_converters=None, encode_converters=None)[source]

Bases: bpack.codecs.BaseStructCodec

Bitstruct based codec.

Default bit-order: MSB.

baseunits: bpack.enums.EBaseUnits = 'bits'
bpack.bs.Decoder

alias of bpack.bs.Codec

bpack.bs.Encoder

alias of bpack.bs.Codec

bpack.bs.codec(cls)

Class decorator to add (de)coding methods to a descriptor class.

The decorator automatically generates a Codec object form the input descriptor class and attach to it methods for conversion form/to bytes.

bpack.bs.decoder(cls)

Class decorator to add (de)coding methods to a descriptor class.

The decorator automatically generates a Codec object form the input descriptor class and attach to it methods for conversion form/to bytes.

bpack.bs.encoder(cls)

Class decorator to add (de)coding methods to a descriptor class.

The decorator automatically generates a Codec object form the input descriptor class and attach to it methods for conversion form/to bytes.

bpack.bs.packbits(values, bits_per_sample: int, signed: bool = False, byteorder: str = '') bytes[source]

Pack integer values using the specified number of bits for each sample.

Converts a sequence of values into a string of bytes in which each sample is stored according to the specified number of bits.

Example:

           4 samples                          3 bytes

[samp_1, samp_2, samp_3, samp_4] --> |------|------|------|------|

                                     4 samples (6 bits per sample)

Please note that no check that the input values actually fits in the specified number of bits is performed is performed.

The function return a sting of bytes including same number of samples of the input plus possibly some padding bit (at the end) to fill an integer number of bytes.

If signed is set to True integers are stored as signed integers.

bpack.bs.unpackbits(data: bytes, bits_per_sample: int, signed: bool = False, byteorder: str = '')[source]

Unpack packed (integer) values form a string of bytes.

Takes in input a string of bytes in which (integer) samples have been stored using bits_per_sample bit for each sample, and returns the sequence of corresponding Python integers.

Example:

           3 bytes                            4 samples

|------|------|------|------| --> [samp_1, samp_2, samp_3, samp_4]

4 samples (6 bits per sample)

If signed is set to True integers are assumed to be stored as signed integers.