From 7f79d3d9a75cedefcc8549ac4649f2c7c4ffaa46 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Wed, 26 Jun 2024 14:10:42 +0200 Subject: [PATCH] Use zip() or enumerate() instead of range(len()) --- blosc2/core.py | 14 +++++++------- tests/ndarray/test_auto_parts.py | 12 ++++++------ tests/ndarray/test_lazyexpr.py | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/blosc2/core.py b/blosc2/core.py index a3cc64bb..b00d0ba1 100644 --- a/blosc2/core.py +++ b/blosc2/core.py @@ -1275,23 +1275,23 @@ def compute_chunks_blocks( blocks = [blocks] if len(blocks) != len(shape): raise ValueError("blocks should have the same length than shape") - for i in range(len(blocks)): - if blocks[i] == 0: + for block, dim in zip(blocks, shape): + if block == 0: raise ValueError("blocks cannot contain 0 dimension") - if shape[i] == 1 and blocks[i] > shape[i]: + if dim == 1 and block > dim: raise ValueError("blocks cannot be greater than shape if it is 1") if chunks: if not isinstance(chunks, tuple | list): chunks = [chunks] if len(chunks) != len(shape): raise ValueError("chunks should have the same length than shape") - for i in range(len(chunks)): - if shape[i] == 1 and chunks[i] > shape[i]: + for chunk, dim in zip(chunks, shape): + if dim == 1 and chunk > dim: raise ValueError("chunks cannot be greater than shape if it is 1") if chunks is not None and blocks is not None: - for i in range(len(blocks)): - if blocks[i] > chunks[i]: + for block, chunk in zip(blocks, chunks): + if block > chunk: raise ValueError("blocks cannot be greater than chunks") return chunks, blocks diff --git a/tests/ndarray/test_auto_parts.py b/tests/ndarray/test_auto_parts.py index dc483b8e..482e430f 100644 --- a/tests/ndarray/test_auto_parts.py +++ b/tests/ndarray/test_auto_parts.py @@ -43,9 +43,9 @@ def test_compute_chunks_blocks(clevel, codec, shape: tuple, dtype): else: chunks, blocks = blosc2.compute_chunks_blocks(shape, **cparams) # print(chunks, blocks) - for i in range(len(shape)): - assert shape[i] >= chunks[i] - assert chunks[i] >= blocks[i] + for dim, chunk, block in zip(shape, chunks, blocks): + assert dim >= chunk + assert chunk >= block @pytest.mark.parametrize( @@ -65,9 +65,9 @@ def test_compute_chunks_blocks(clevel, codec, shape: tuple, dtype): def test_compute_chunks(shape: tuple, blocks: tuple): chunks, blocks = blosc2.compute_chunks_blocks(shape, blocks=blocks) # print(chunks, blocks) - for i in range(len(shape)): - assert shape[i] >= chunks[i] - assert chunks[i] >= blocks[i] + for dim, chunk, block in zip(shape, chunks, blocks): + assert dim >= chunk + assert chunk >= block # Invalid blocks diff --git a/tests/ndarray/test_lazyexpr.py b/tests/ndarray/test_lazyexpr.py index 39b171cf..4ad5d606 100644 --- a/tests/ndarray/test_lazyexpr.py +++ b/tests/ndarray/test_lazyexpr.py @@ -452,8 +452,8 @@ def test_save(): a4 = blosc2.asarray(na4) ops = [a1, a2, a3, a4] op_urlpaths = ["a1.b2nd", "a2.b2nd", "a3.b2nd", "a4.b2nd"] - for i in range(len(op_urlpaths)): - ops[i] = ops[i].copy(urlpath=op_urlpaths[i], mode="w") + for i, urlpath in enumerate(op_urlpaths): + ops[i] = ops[i].copy(urlpath=urlpath, mode="w") # Construct the lazy expression with the on-disk operands da1, da2, da3, da4 = ops