Skip to content

Commit

Permalink
Support int item in SChunk.__getitem__
Browse files Browse the repository at this point in the history
  • Loading branch information
martaiborra committed Sep 10, 2024
1 parent 5cab4d2 commit ffe80f9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/blosc2/schunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ def __getitem__(self, item):
:func:`get_slice`
"""
if isinstance(item, int):
if item == -1:
return self.get_slice(item)
return self.get_slice(item, item + 1)
if item.step is not None and item.step != 1:
raise IndexError("`step` must be 1")
return self.get_slice(item.start, item.stop)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_schunk_get_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ def test_schunk_get_slice(contiguous, urlpath, mode, cparams, dparams, nchunks,
blosc2.remove_urlpath(urlpath)


@pytest.mark.parametrize(
"cparams, nchunks, elem",
[
({"codec": blosc2.Codec.LZ4, "clevel": 6, "typesize": 4}, 10, 0),
({"typesize": 4}, 1, 7),
(
{"splitmode": blosc2.SplitMode.ALWAYS_SPLIT, "nthreads": 5, "typesize": 4},
5,
21,
),
({"blocksize": 200 * 100, "typesize": 4}, 5, -1),
({"blocksize": 100 * 100, "typesize": 4}, 2, -200 * 100 + 234),
],
)
def test_schunk_getitem_int(cparams, nchunks, elem):
storage = {"cparams": cparams}

data = np.arange(200 * 100 * nchunks, dtype="int32")
schunk = blosc2.SChunk(chunksize=200 * 100 * 4, data=data, **storage)

sl = data[elem]
res = schunk[elem]
assert res == sl.tobytes()


def test_schunk_get_slice_raises():
storage = {"contiguous": True, "urlpath": "schunk.b2frame", "cparams": {"typesize": 4}, "dparams": {}}
blosc2.remove_urlpath(storage["urlpath"])
Expand Down

0 comments on commit ffe80f9

Please sign in to comment.