blosc2.decompress#
- blosc2.decompress(src: object, dst: object | bytearray = None, as_bytearray: bool = False) str | bytes | bytearray | None #
Decompresses a bytes-like compressed object.
- Parameters:
src¶ (bytes-like object) – The data to be decompressed. Must be a bytes-like object that supports the Python Buffer Protocol, like bytes, bytearray, memoryview, or numpy.ndarray.
dst¶ (NumPy object or bytearray) – The destination NumPy object or bytearray to fill, the length of which must be greater than 0. The user must ensure it has enough capacity to hold the decompressed data. Default is None, meaning that a new bytes or bytearray object is created, filled and returned.
as_bytearray¶ (bool (optional)) – If True, then return type will be a bytearray object instead of a bytes object.
- Returns:
out – If
dst
is None, the decompressed data will be returned as a Python str or bytes object. If as_bytearray is True, the return type will be a bytearray object.If
dst
is not None, the function will return None because the result will already be stored indst
.- Return type:
str or bytes or bytearray
- Raises:
RuntimeError – Raised if the compressed data is corrupted or the output buffer is not large enough. Also raised if a bytes object could not be obtained.
TypeError – Raised if
src
does not support the Buffer Protocol.ValueError – Raised if the length of
src
is smaller than the minimum required length. Also raised if dst is not None and its length is 0.
Examples
>>> import array, sys >>> a = array.array('i', range(1000*1000)) >>> a_bytesobj = a.tobytes() >>> c_bytesobj = blosc2.compress(a_bytesobj, typesize=4) >>> a_bytesobj2 = blosc2.decompress(c_bytesobj) >>> a_bytesobj == a_bytesobj2 True >>> b"" == blosc2.decompress(blosc2.compress(b"")) True >>> b"1"*7 == blosc2.decompress(blosc2.compress(b"1"*7)) True >>> type(blosc2.decompress(blosc2.compress(b"1"*7), ... as_bytearray=True)) is bytearray True >>> import numpy as np >>> arr = np.arange(10) >>> comp_arr = blosc2.compress(arr) >>> dest = np.empty(arr.shape, arr.dtype) >>> blosc2.decompress(comp_arr, dst=dest) >>> np.array_equal(arr, dest) True