blosc2.compress#

blosc2.compress(src: object, typesize: int = 8, clevel: int = 1, filter: Filter = Filter.SHUFFLE, codec: Codec = Codec.ZSTD) str | bytes#

Compress the given source data with specified parameters.

Parameters:
  • src (bytes-like object) – The data to be compressed. It must support the buffer interface.

  • typesize (int (optional) from 1 to 255) – The data type size. The default is 8, or src.itemsize if it exists.

  • clevel (int (optional)) – The compression level from 0 (no compression) to 9 (maximum compression). The default is 9.

  • filter (Filter (optional)) – The filter to be activated. The default is Filter.SHUFFLE.

  • codec (Codec (optional)) – The compressor used internally in Blosc. The default is Codec.BLOSCLZ.

  • _ignore_multiple_size (bool (optional)) – If True, ignores the requirement that the length of src must be a multiple of typesize.

Returns:

out – The compressed data in as a Python str or bytes object.

Return type:

str or bytes

Raises:
  • TypeError – If src doesn’t support the buffer interface.

  • ValueError – If src is too long. If typesize is not within the allowed range. If clevel is not within the allowed range. If codec is not within the supported compressors.

Notes

The cname and shuffle parameters in python-blosc API have been replaced by codec and filter respectively. To set codec and filter, use the enumerations Codec and Filter instead of the python-blosc API variables like blosc.SHUFFLE for filter or strings like “blosclz” for codec.

Examples

>>> import array, sys
>>> a = array.array('i', range(1000*1000))
>>> a_bytesobj = a.tobytes()
>>> c_bytesobj = blosc2.compress(a_bytesobj, typesize=4)
>>> len(c_bytesobj) < len(a_bytesobj)
True