def compress_ptr(address, items, typesize=8, clevel=9, shuffle=blosc.SHUFFLE, cname='blosclz'): """compress_ptr(address, items[, typesize=8, clevel=9, shuffle=blosc.SHUFFLE, cname='blosclz']]) Compress the data at address with given items and typesize. Parameters ---------- address : int or long the pointer to the data to be compressed items : int The number of items (of typesize) to be compressed. typesize : int The data type size. clevel : int (optional) The compression level from 0 (no compression) to 9 (maximum compression). The default is 9. shuffle : int (optional) The shuffle filter to be activated. Allowed values are blosc.NOSHUFFLE, blosc.SHUFFLE and blosc.BITSHUFFLE. The default is blosc.SHUFFLE. cname : string (optional) The name of the compressor used internally in Blosc. It can be any of the supported by Blosc ('blosclz', 'lz4', 'lz4hc', 'snappy', 'zlib', 'zstd' and maybe others too). The default is 'blosclz'. Returns ------- out : str / bytes The compressed data in form of a Python str / bytes object. Raises ------ TypeError If address is not of type int or long. ValueError If items * typesize is larger than the maximum allowed buffer size. If typesize is not within the allowed range. If clevel is not within the allowed range. If cname is not within the supported compressors. Notes ----- This function can be used anywhere that a memory address is available in Python. For example the Numpy "__array_interface__['data'][0]" construct, or when using the ctypes modules. Importantly, the user is responsible for making sure that the memory address is valid and that the memory pointed to is contiguous. Passing a non-valid address has a high likelihood of crashing the interpreter by segfault. Examples -------- >>> import numpy >>> items = 7 >>> np_array = numpy.arange(items) >>> c = blosc.compress_ptr(np_array.__array_interface__['data'][0], \ items, np_array.dtype.itemsize) >>> d = blosc.decompress(c) >>> np_ans = numpy.fromstring(d, dtype=np_array.dtype) >>> (np_array == np_ans).all() True >>> import ctypes >>> typesize = 8 >>> data = [float(i) for i in range(items)] >>> Array = ctypes.c_double * items >>> a = Array(*data) >>> c = blosc.compress_ptr(ctypes.addressof(a), items, typesize) >>> d = blosc.decompress(c) >>> import struct >>> ans = [struct.unpack('d', d[i:i+typesize])[0] \ for i in range(0, items*typesize, typesize)] >>> data == ans True """ _check_address(address) if items < 0: raise ValueError("items cannot be negative") length = items * typesize _check_input_length('length', length) _check_typesize(typesize) _check_shuffle(shuffle) _check_clevel(clevel) _check_cname(cname) return _ext.compress_ptr(address, length, typesize, clevel, shuffle, cname)
def compress_ptr(address, items, typesize, clevel=9, shuffle=True, cname="blosclz"): """compress_ptr(address, items, typesize[, clevel=9, shuffle=True, cname='blosclz']]) Compress the data at address with given items and typesize. Parameters ---------- address : int or long the pointer to the data to be compressed items : int The number of items (of typesize) to be compressed. typesize : int The data type size. clevel : int (optional) The compression level from 0 (no compression) to 9 (maximum compression). The default is 9. shuffle : bool (optional) Whether you want to activate the shuffle filter or not. The default is True. cname : string (optional) The name of the compressor used internally in Blosc. It can be any of the supported by Blosc ('blosclz', 'lz4', 'lz4hc', 'snappy', 'zlib' and maybe others too). The default is 'blosclz'. Returns ------- out : str / bytes The compressed data in form of a Python str / bytes object. Raises ------ TypeError If address is not of type int or long. ValueError If items * typesize is larger than the maximum allowed buffer size. If typesize is not within the allowed range. If clevel is not within the allowed range. If cname is not within the supported compressors. Notes ----- This function can be used anywhere that a memory address is available in Python. For example the Numpy "__array_interface__['data'][0]" construct, or when using the ctypes modules. Importantly, the user is responsible for making sure that the memory address is valid and that the memory pointed to is contiguous. Passing a non-valid address has a high likelihood of crashing the interpreter by segfault. Examples -------- >>> import numpy >>> items = 7 >>> np_array = numpy.arange(items) >>> c = blosc.compress_ptr(np_array.__array_interface__['data'][0], \ items, np_array.dtype.itemsize) >>> d = blosc.decompress(c) >>> np_ans = numpy.fromstring(d, dtype=np_array.dtype) >>> (np_array == np_ans).all() True >>> import ctypes >>> typesize = 8 >>> data = [float(i) for i in range(items)] >>> Array = ctypes.c_double * items >>> a = Array(*data) >>> c = blosc.compress_ptr(ctypes.addressof(a), items, typesize) >>> d = blosc.decompress(c) >>> import struct >>> ans = [struct.unpack('d', d[i:i+typesize])[0] \ for i in range(0,items*typesize,typesize)] >>> data == ans True """ _check_address(address) if items < 0: raise ValueError("items cannot be negative") length = items * typesize _check_input_length("length", length) _check_typesize(typesize) _check_clevel(clevel) _check_cname(cname) return _ext.compress_ptr(address, length, typesize, clevel, shuffle, cname)