Skip to content

Commit 050477d

Browse files
committed
Implement __setitem__ in DelayedBuffer
1 parent 23fbc55 commit 050477d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/zarr/core/buffer/core.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,47 @@ def __getitem__(self, key: slice) -> Self:
584584
assert sum(map(len, new_list)) == stop - start
585585
return self.__class__(new_list)
586586

587+
def __setitem__(self, key: slice, value: Any) -> None:
588+
# This assumes that `value` is a broadcasted array
589+
check_item_key_is_1d_contiguous(key)
590+
start, stop = key.start, key.stop
591+
if start is None:
592+
start = 0
593+
if stop is None:
594+
stop = len(self)
595+
new_list = []
596+
offset = 0
597+
found_last = False
598+
value = memoryview(np.asanyarray(value))
599+
for chunk in self._data_list:
600+
chunk_size = len(chunk)
601+
skip = False
602+
if offset <= start < offset + chunk_size:
603+
# first chunk
604+
if stop <= offset + chunk_size:
605+
# also last chunk
606+
chunk = chunk[start-offset:stop-offset]
607+
found_last = True
608+
else:
609+
chunk = chunk[start-offset:]
610+
elif offset <= stop <= offset + chunk_size:
611+
# last chunk
612+
chunk = chunk[:stop-offset]
613+
found_last = True
614+
elif offset + chunk_size <= start:
615+
skip = True
616+
617+
if not skip:
618+
chunk[:] = value[:len(chunk)]
619+
value = value[len(chunk):]
620+
if len(value) == 0:
621+
# nothing left to write
622+
break
623+
if found_last:
624+
break
625+
offset += chunk_size
626+
return self.__class__(new_list)
627+
587628

588629
# The default buffer prototype used throughout the Zarr codebase.
589630
def default_buffer_prototype() -> BufferPrototype:

0 commit comments

Comments
 (0)