Skip to content

Commit

Permalink
Merge collapsible if statements (#1999)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriPapadopoulos authored Jun 27, 2024
1 parent 61b9404 commit b3010fc
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
15 changes: 9 additions & 6 deletions src/zarr/codecs/bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,15 @@ async def _encode_single(
chunk_spec: ArraySpec,
) -> Buffer | None:
assert isinstance(chunk_array, NDBuffer)
if chunk_array.dtype.itemsize > 1:
if self.endian is not None and self.endian != chunk_array.byteorder:
# type-ignore is a numpy bug
# see https:/numpy/numpy/issues/26473
new_dtype = chunk_array.dtype.newbyteorder(self.endian.name) # type: ignore[arg-type]
chunk_array = chunk_array.astype(new_dtype)
if (
chunk_array.dtype.itemsize > 1
and self.endian is not None
and self.endian != chunk_array.byteorder
):
# type-ignore is a numpy bug
# see https:/numpy/numpy/issues/26473
new_dtype = chunk_array.dtype.newbyteorder(self.endian.name) # type: ignore[arg-type]
chunk_array = chunk_array.astype(new_dtype)

nd_array = chunk_array.as_ndarray_like()
# Flatten the nd-array (only copy if needed) and reinterpret as bytes
Expand Down
4 changes: 1 addition & 3 deletions src/zarr/codecs/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,7 @@ def _merge_chunk_array(
)
else:
chunk_array = existing_chunk_array.copy() # make a writable copy
if chunk_selection == ():
chunk_value = value
elif is_scalar(value.as_ndarray_like(), chunk_spec.dtype):
if chunk_selection == () or is_scalar(value.as_ndarray_like(), chunk_spec.dtype):
chunk_value = value
else:
chunk_value = value[out_selection]
Expand Down
11 changes: 5 additions & 6 deletions src/zarr/codecs/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ def register_codec(key: str, codec_cls: type[Codec]) -> None:

def get_codec_class(key: str) -> type[Codec]:
item = __codec_registry.get(key)
if item is None:
if key in __lazy_load_codecs:
# logger.debug("Auto loading codec '%s' from entrypoint", codec_id)
cls = __lazy_load_codecs[key].load()
register_codec(key, cls)
item = __codec_registry.get(key)
if item is None and key in __lazy_load_codecs:
# logger.debug("Auto loading codec '%s' from entrypoint", codec_id)
cls = __lazy_load_codecs[key].load()
register_codec(key, cls)
item = __codec_registry.get(key)
if item:
return item
raise KeyError(key)
Expand Down
11 changes: 5 additions & 6 deletions src/zarr/v2/convenience.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,12 +680,11 @@ def copy_store(

# decide what to do
do_copy = True
if if_exists != "replace":
if dest_key in dest:
if if_exists == "raise":
raise CopyError("key {!r} exists in destination".format(dest_key))
elif if_exists == "skip":
do_copy = False
if if_exists != "replace" and dest_key in dest:
if if_exists == "raise":
raise CopyError("key {!r} exists in destination".format(dest_key))
elif if_exists == "skip":
do_copy = False

# take action
if do_copy:
Expand Down
7 changes: 3 additions & 4 deletions src/zarr/v2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,9 @@ def __init__(self, obj, depth=0, level=None):
self.level = level

def get_children(self):
if hasattr(self.obj, "values"):
if self.level is None or self.depth < self.level:
depth = self.depth + 1
return [TreeNode(o, depth=depth, level=self.level) for o in self.obj.values()]
if hasattr(self.obj, "values") and (self.level is None or self.depth < self.level):
depth = self.depth + 1
return [TreeNode(o, depth=depth, level=self.level) for o in self.obj.values()]
return []

def get_text(self):
Expand Down

0 comments on commit b3010fc

Please sign in to comment.