blosc2.schunk.SChunk.get_slice#

SChunk.get_slice(start: int = 0, stop: int | None = None, out: object = None) str | bytes | None#

Get a slice from start to stop.

Parameters:
  • start (int) – The starting index of the slice. Default is 0.

  • stop (int) – The ending index of the slice (exclusive). Default is until the SChunk ends.

  • out (bytes-like object or bytearray) – The target object (supporting the Buffer Protocol) to fill. Verify that the buffer has enough space for the decompressed data. If None is provided, a new bytes object will be created, filled, and returned.

Returns:

out – The decompressed slice a Python str or bytes object if out is None. Otherwise, it returns None since the result will already be in out.

Return type:

str or bytes or None

Raises:
  • ValueError – If the size to get is negative. If there is not enough space in out. If start is greater or equal to the number of items in the SChunk.

  • RunTimeError – If a problem is detected.

See also

__getitem__()

Examples

>>> import blosc2
>>> import numpy as np
>>> nchunks = 4
>>> chunk_size = 200 * 1000 * 4
>>> data = np.arange(nchunks * chunk_size // 4, dtype=np.int32)
>>> cparams = blosc2.CParams(typesize=4)
>>> schunk = blosc2.SChunk(data=data, cparams=cparams)
>>> # Define the slice parameters
>>> start_index = 200 * 1000
>>> stop_index = 2 * 200 * 1000
>>> # Prepare an output buffer
>>> slice_size = stop_index - start_index
>>> out_buffer = bytearray(slice_size * 4)  # Ensure the buffer is large enough
>>> result = schunk.get_slice(start=start_index, stop=stop_index, out=out_buffer)
>>> # Convert bytearray to NumPy array for easier inspection
>>> slice_array = np.frombuffer(out_buffer, dtype=np.int32)
>>> f"Slice data: {slice_array[:10]} ..."  # Print the first 10 elements
Slice data: [200000 200001 200002 200003 200004 200005 200006 200007 200008 200009] ...