From b3010fcddb5f14aaed51bf456888e636df8c2697 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos Orfanos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Thu, 27 Jun 2024 10:10:03 +0200 Subject: [PATCH] Merge collapsible if statements (#1999) --- src/zarr/codecs/bytes.py | 15 +++++++++------ src/zarr/codecs/pipeline.py | 4 +--- src/zarr/codecs/registry.py | 11 +++++------ src/zarr/v2/convenience.py | 11 +++++------ src/zarr/v2/util.py | 7 +++---- 5 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/zarr/codecs/bytes.py b/src/zarr/codecs/bytes.py index 0b9a5c089e..80b596a157 100644 --- a/src/zarr/codecs/bytes.py +++ b/src/zarr/codecs/bytes.py @@ -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://github.com/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://github.com/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 diff --git a/src/zarr/codecs/pipeline.py b/src/zarr/codecs/pipeline.py index a7f47661b8..f75491d29f 100644 --- a/src/zarr/codecs/pipeline.py +++ b/src/zarr/codecs/pipeline.py @@ -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] diff --git a/src/zarr/codecs/registry.py b/src/zarr/codecs/registry.py index 2f2b09499f..0956ce75d0 100644 --- a/src/zarr/codecs/registry.py +++ b/src/zarr/codecs/registry.py @@ -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) diff --git a/src/zarr/v2/convenience.py b/src/zarr/v2/convenience.py index aa322bfb98..c066ee59e0 100644 --- a/src/zarr/v2/convenience.py +++ b/src/zarr/v2/convenience.py @@ -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: diff --git a/src/zarr/v2/util.py b/src/zarr/v2/util.py index 6233687086..7e3bd788ec 100644 --- a/src/zarr/v2/util.py +++ b/src/zarr/v2/util.py @@ -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):