def unpack_array(packed_array, **kwargs): """unpack_array(packed_array) Unpack (decompress) a packed NumPy array. Parameters ---------- packed_array : str / bytes The packed array to be decompressed. **kwargs : fix_imports / encoding / errors Optional parameters that can be passed to the pickle.loads API https://docs.python.org/3/library/pickle.html#pickle.loads Returns ------- out : ndarray The decompressed data in form of a NumPy array. Raises ------ TypeError If packed_array is not of type bytes or string. Examples -------- >>> import numpy >>> a = numpy.arange(1e6) >>> parray = blosc.pack_array(a) >>> len(parray) < a.size*a.itemsize True >>> a2 = blosc.unpack_array(parray) >>> numpy.alltrue(a == a2) True >>> a = numpy.array(['å', 'ç', 'ø']) >>> parray = blosc.pack_array(a) >>> a2 = blosc.unpack_array(parray) >>> numpy.alltrue(a == a2) True """ _check_bytesobj(packed_array) # First decompress the pickle pickled_array = _ext.decompress(packed_array, False) # ... and unpickle if kwargs and PY3X: array = pickle.loads(pickled_array, **kwargs) if all(isinstance(x, bytes) for x in array.tolist()): import numpy array = numpy.array([x.decode('utf-8') for x in array.tolist()]) else: array = pickle.loads(pickled_array) return array
def unpack_array(packed_array, **kwargs): """unpack_array(packed_array) Unpack (decompress) a packed NumPy array. Parameters ---------- packed_array : str / bytes The packed array to be decompressed. **kwargs : fix_imports / encoding / errors Optional parameters that can be passed to the pickle.loads API https://docs.python.org/3/library/pickle.html#pickle.loads Returns ------- out : ndarray The decompressed data in form of a NumPy array. Raises ------ TypeError If packed_array is not of type bytes or string. Examples -------- >>> import numpy >>> a = numpy.arange(1e6) >>> parray = blosc.pack_array(a) >>> len(parray) < a.size*a.itemsize True >>> a2 = blosc.unpack_array(parray) >>> numpy.alltrue(a == a2) True >>> a = numpy.array(['å', 'ç', 'ø']) >>> parray = blosc.pack_array(a) >>> a2 = blosc.unpack_array(parray) >>> numpy.alltrue(a == a2) True """ _check_bytesobj(packed_array) # First decompress the pickle pickled_array = _ext.decompress(packed_array, False) # ... and unpickle if kwargs: array = pickle.loads(pickled_array, **kwargs) if all(isinstance(x, bytes) for x in array.tolist()): import numpy array = numpy.array([x.decode('utf-8') for x in array.tolist()]) else: array = pickle.loads(pickled_array) return array
def decompress(bytes_like, as_bytearray=False): """decompress(bytes_like) Decompresses a bytes-like compressed object. Parameters ---------- bytes_like : 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. as_bytearray : bool, optional If this flag is True then the return type will be a bytearray object instead of a bytesobject. Returns ------- out : str / bytes or bytearray The decompressed data in form of a Python str / bytes object. If as_bytearray is True then this will be a bytearray object, otherwise this will be a str/ bytes object. Raises ------ TypeError If bytes_like does not support Buffer Protocol Examples -------- >>> import array, sys >>> a = array.array('i', range(1000*1000)) >>> a_bytesobj = a.tobytes() if sys.version_info >= (3, 0, 0) else a.tostring() >>> c_bytesobj = blosc.compress(a_bytesobj, typesize=4) >>> a_bytesobj2 = blosc.decompress(c_bytesobj) >>> a_bytesobj == a_bytesobj2 True >>> b"" == blosc.decompress(blosc.compress(b"", 1)) True >>> b"1"*7 == blosc.decompress(blosc.compress(b"1"*7, 8)) True >>> type(blosc.decompress(blosc.compress(b"1"*7, 8), ... as_bytearray=True)) is bytearray True """ return _ext.decompress(bytes_like, as_bytearray)
def decompress(bytesobj, as_bytearray=False): """decompress(bytesobj) Decompresses a bytesobj compressed object. Parameters ---------- bytesobj : str / bytes The data to be decompressed. as_bytearray : bool, optional If this flag is True then the return type will be a bytearray object instead of a bytesobject. Returns ------- out : str / bytes or bytearray The decompressed data in form of a Python str / bytes object. If as_bytearray is True then this will be a bytearray object, otherwise this will be a str/ bytes object. Raises ------ TypeError If bytesobj is not of type bytes or string. Examples -------- >>> import array >>> a = array.array('i', range(1000*1000)) >>> a_bytesobj = a.tostring() >>> c_bytesobj = blosc.compress(a_bytesobj, typesize=4) >>> a_bytesobj2 = blosc.decompress(c_bytesobj) >>> a_bytesobj == a_bytesobj2 True >>> b"" == blosc.decompress(blosc.compress(b"", 1)) True >>> b"1"*7 == blosc.decompress(blosc.compress(b"1"*7, 8)) True >>> type(blosc.decompress(blosc.compress(b"1"*7, 8), ... as_bytearray=True)) is bytearray True """ return _ext.decompress(bytesobj, as_bytearray)
def unpack_array(packed_array): """unpack_array(packed_array) Unpack (decompress) a packed NumPy array. Parameters ---------- packed_array : str / bytes The packed array to be decompressed. Returns ------- out : ndarray The decompressed data in form of a NumPy array. Raises ------ TypeError If packed_array is not of type bytes or string. Examples -------- >>> import numpy >>> a = numpy.arange(1e6) >>> parray = blosc.pack_array(a) >>> len(parray) < a.size*a.itemsize True >>> a2 = blosc.unpack_array(parray) >>> numpy.alltrue(a == a2) True """ _check_bytesobj(packed_array) # First decompress the pickle pickled_array = _ext.decompress(packed_array, False) # ... and unpickle array = pickle.loads(pickled_array) return array
def decompress(bytesobj): """decompress(bytesobj) Decompresses a bytesobj compressed object. Parameters ---------- bytesobj : str / bytes The data to be decompressed. Returns ------- out : str / bytes The decompressed data in form of a Python str / bytes object. Raises ------ TypeError If bytesobj is not of type bytes or string. Examples -------- >>> import array >>> a = array.array('i', range(1000*1000)) >>> a_bytesobj = a.tostring() >>> c_bytesobj = blosc.compress(a_bytesobj, typesize=4) >>> a_bytesobj2 = blosc.decompress(c_bytesobj) >>> a_bytesobj == a_bytesobj2 True >>> b"" == blosc.decompress(blosc.compress(b"", 1)) True >>> b"1"*7 == blosc.decompress(blosc.compress(b"1"*7, 8)) True """ _check_bytesobj(bytesobj) return _ext.decompress(bytesobj)
def unpack_array(packed_array): """unpack_array(packed_array) Unpack (decompress) a packed NumPy array. Parameters ---------- packed_array : str / bytes The packed array to be decompressed. Returns ------- out : ndarray The decompressed data in form of a NumPy array. Examples -------- >>> import numpy >>> a = numpy.arange(1e6) >>> parray = pack_array(a) >>> len(parray) < a.size*a.itemsize True >>> a2 = unpack_array(parray) >>> numpy.alltrue(a == a2) True """ if type(packed_array) is not bytes: raise ValueError( "only string (2.x) or bytes (3.x) objects supported as input") # First decompress the pickle pickled_array = _ext.decompress(packed_array) # ... and unpickle array = cPickle.loads(pickled_array) return array
def decompress(bytesobj): """decompress(bytesobj) Decompresses a bytesobj compressed object. Parameters ---------- bytesobj : str / bytes The data to be decompressed. Returns ------- out : str / bytes The decompressed data in form of a Python str / bytes object. Examples -------- >>> import array >>> a = array.array('i', range(1000*1000)) >>> a_bytesobj = a.tostring() >>> c_bytesobj = compress(a_bytesobj, typesize=4) >>> a_bytesobj2 = decompress(c_bytesobj) >>> a_bytesobj == a_bytesobj2 True >>> "" == blosc.decompress(blosc.compress("", 1)) True >>> "1"*7 == blosc.decompress(blosc.compress("1"*7, 8)) True """ if type(bytesobj) is not bytes: raise ValueError( "only string (2.x) or bytes (3.x) objects supported as input") return _ext.decompress(bytesobj)