Skip to content

Commit 81af93e

Browse files
committed
Better impl
1 parent a38030a commit 81af93e

File tree

2 files changed

+32
-52
lines changed

2 files changed

+32
-52
lines changed

xarray/backends/zarr.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ def __getitem__(self, key):
242242
# could possibly have a work-around for 0d data here
243243

244244
async def async_getitem(self, key):
245+
print("async getting")
245246
array = self._array
246247
if isinstance(key, indexing.BasicIndexer):
247248
method = self._async_getitem

xarray/core/indexing.py

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -523,11 +523,16 @@ class IndexingAdapter:
523523
indexing semantics.
524524
"""
525525

526+
# FIXME: @abstractmethod here
527+
def get_duck_array():
528+
raise NotImplementedError
529+
526530
async def async_get_duck_array():
527-
"""These classes are applied to in-memory arrays, so async get isn't needed."""
528-
raise RuntimeError(
529-
f"{type(self).__name__} async_get_duck_array was called. This should not happen."
530-
)
531+
"""These classes are applied to in-memory arrays, so specific async support isn't needed."""
532+
return self.get_duck_array()
533+
# raise RuntimeError(
534+
# f"{type(self).__name__} async_get_duck_array was called. This should not happen."
535+
# )
531536

532537

533538
class ExplicitlyIndexedNDArrayMixin(NDArrayMixin, ExplicitlyIndexed):
@@ -664,37 +669,23 @@ def shape(self) -> _Shape:
664669
return self._shape
665670

666671
def get_duck_array(self):
667-
if isinstance(self.array, ExplicitlyIndexedNDArrayMixin):
668-
array = apply_indexer(self.array, self.key)
669-
else:
670-
# If the array is not an ExplicitlyIndexedNDArrayMixin,
671-
# it may wrap a BackendArray so use its __getitem__
672-
array = self.array[self.key]
672+
from xarray.backends.common import BackendArray
673673

674-
# self.array[self.key] is now a numpy array when
675-
# self.array is a BackendArray subclass
676-
# and self.key is BasicIndexer((slice(None, None, None),))
677-
# so we need the explicit check for ExplicitlyIndexed
678-
if isinstance(array, ExplicitlyIndexed):
674+
if isinstance(self.array, BackendArray):
675+
array = self.array[self.key]
676+
else:
677+
array = apply_indexer(self.array, self.key)
679678
array = array.get_duck_array()
680679
return _wrap_numpy_scalars(array)
681680

682681
async def async_get_duck_array(self):
683-
if isinstance(self.array, ExplicitlyIndexedNDArrayMixin):
684-
array = apply_indexer(self.array, self.key)
685-
else:
686-
# If the array is not an ExplicitlyIndexedNDArrayMixin,
687-
# it may wrap a BackendArray so use its (async) getitem
688-
array = await self.array.async_getitem(self.key)
682+
from xarray.backends.common import BackendArray
689683

690-
# self.array[self.key] is now a numpy array when
691-
# self.array is a BackendArray subclass
692-
# and self.key is BasicIndexer((slice(None, None, None),))
693-
# so we need the explicit check for ExplicitlyIndexed
694-
if isinstance(array, ExplicitlyIndexed):
695-
# At this point, we have issued completed the possible async load from disk
696-
# and array is in-memory. So use the sync get
697-
array = array.get_duck_array()
684+
if isinstance(self.array, BackendArray):
685+
array = await self.array.async_getitem(self.key)
686+
else:
687+
array = apply_indexer(self.array, self.key)
688+
array = await array.async_get_duck_array()
698689
return _wrap_numpy_scalars(array)
699690

700691
def transpose(self, order):
@@ -758,36 +749,24 @@ def shape(self) -> _Shape:
758749
return np.broadcast(*self.key.tuple).shape
759750

760751
def get_duck_array(self):
761-
if isinstance(self.array, ExplicitlyIndexedNDArrayMixin):
762-
array = apply_indexer(self.array, self.key)
763-
else:
764-
# If the array is not an ExplicitlyIndexedNDArrayMixin,
765-
# it may wrap a BackendArray so use its __getitem__
752+
from xarray.backends.common import BackendArray
753+
754+
if isinstance(self.array, BackendArray):
766755
array = self.array[self.key]
767-
# self.array[self.key] is now a numpy array when
768-
# self.array is a BackendArray subclass
769-
# and self.key is BasicIndexer((slice(None, None, None),))
770-
# so we need the explicit check for ExplicitlyIndexed
771-
if isinstance(array, ExplicitlyIndexed):
756+
else:
757+
array = apply_indexer(self.array, self.key)
772758
array = array.get_duck_array()
773759
return _wrap_numpy_scalars(array)
774760

775761
async def async_get_duck_array(self):
776762
print("inside LazilyVectorizedIndexedArray.async_get_duck_array")
777-
if isinstance(self.array, ExplicitlyIndexedNDArrayMixin):
778-
array = apply_indexer(self.array, self.key)
779-
else:
780-
# If the array is not an ExplicitlyIndexedNDArrayMixin,
781-
# it may wrap a BackendArray so use its __getitem__
763+
from xarray.backends.common import BackendArray
764+
765+
if isinstance(self.array, BackendArray):
782766
array = await self.array.async_getitem(self.key)
783-
# self.array[self.key] is now a numpy array when
784-
# self.array is a BackendArray subclass
785-
# and self.key is BasicIndexer((slice(None, None, None),))
786-
# so we need the explicit check for ExplicitlyIndexed
787-
if isinstance(array, ExplicitlyIndexed):
788-
# At this point, we have issued completed the possible async load from disk
789-
# and array is in-memory. So use the sync get
790-
array = array.get_duck_array()
767+
else:
768+
array = apply_indexer(self.array, self.key)
769+
array = await array.async_get_duck_array()
791770
return _wrap_numpy_scalars(array)
792771

793772
def _updated_key(self, new_key: ExplicitIndexer):

0 commit comments

Comments
 (0)