Uniform slicing of memoryview

I am sending slices of the memoryview in numpy.array.data

class AsyncChunker:
    def __init__(self, array):
        self.chunk_size = 16777216
        self.array = array
        self.start = 0
    def __aiter__(self):
        return self
    async def __anext__(self):
        chunk = self.array.data[self.start: self.start + self.chunk_size]  # Not allowed for 0-dim
        if len(chunk):
            self.start += self.chunk_size
            return chunk
        raise StopAsyncIteration

The case when self.array.data.ndim == 0 gives TypeError: invalid indexing of 0-dim memory.

Is there a way to treat uniformly the slicing of memoryview to cover all dimensions or do I necessarily have to treat the case of 0-dim apart?

I’d say two possible answers :

  • 1 : Yes, you should
  • 2 : If you still want to, and also to be able to input simple floats as array transparently here, you might force array to be stored as minimally 1-dim array at init, by using :
        self.array = np.array(array, ndmin=1)