bpack.np module

Numpy based codec for binary data structures.

class bpack.np.Codec(descriptor)[source]

Bases: bpack.codecs.Codec

Numpy based codec.

(Unicode) strings are treated as “utf-8” encoded byte strings. UCS4 encoded strings are not supported.

decode(data: bytes, count: int = 1)[source]

Decode binary data and return a record object.

encode(record)[source]
baseunits: bpack.enums.EBaseUnits = 'bytes'
property dtype
bpack.np.Decoder

alias of bpack.np.Codec

bpack.np.Encoder

alias of bpack.np.Codec

bpack.np.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.np.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.np.descriptor_to_dtype(descriptor) numpy.dtype[source]

Convert the descriptor of a binary record into a numpy.dtype.

Please note that (unicode) strings are treated as “utf-8” encoded byte strings. UCS4 encoded strings are not supported.

Sequences (typing.Sequence and typing.List) are always converted into numpy.ndarray.

bpack.np.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.np.unpackbits(data: bytes, bits_per_sample: int, samples_per_block: Optional[int] = None, bit_offset: int = 0, blockstride: Optional[int] = None, signed: bool = False, byteorder: str = '>') numpy.ndarray[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.