Skip to content

Commit

Permalink
Avoid redundant __contains__ (#1739)
Browse files Browse the repository at this point in the history
Let's try grabbing the array.json and group.json files,
and check for `*NotFoundError`, instead of using contains first.

Co-authored-by: Davis Bennett <davis.v.bennett@gmail.com>
  • Loading branch information
dcherian and d-v-b committed Apr 4, 2024
1 parent d0fb875 commit bcb7684
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions zarr/hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from zarr.errors import (
ContainsArrayError,
ContainsGroupError,
ArrayNotFoundError,
GroupNotFoundError,
ReadOnlyError,
)
Expand Down Expand Up @@ -457,7 +458,7 @@ def __getitem__(self, item):
"""
path = self._item_path(item)
if contains_array(self._store, path):
try:
return Array(
self._store,
read_only=self._read_only,
Expand All @@ -468,7 +469,10 @@ def __getitem__(self, item):
zarr_version=self._version,
meta_array=self._meta_array,
)
elif contains_group(self._store, path, explicit_only=True):
except ArrayNotFoundError:
pass

try:
return Group(
self._store,
read_only=self._read_only,
Expand All @@ -479,7 +483,10 @@ def __getitem__(self, item):
zarr_version=self._version,
meta_array=self._meta_array,
)
elif self._version == 3:
except GroupNotFoundError:
pass

if self._version == 3:
implicit_group = meta_root + path + "/"
# non-empty folder in the metadata path implies an implicit group
if self._store.list_prefix(implicit_group):
Expand Down

0 comments on commit bcb7684

Please sign in to comment.