Skip to content

Commit

Permalink
Add SChunk.__len__()
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescAlted committed Jan 4, 2024
1 parent d74538b commit be4ab1d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Support for slices in ``blosc2.get_slice_nchunks()`` when using SChunk
objects.

* Added a new `Schunk.__len__()` method.

## Changes from 2.3.2 to 2.4.0

* Updated to latest C-Blosc2 2.12.0.
Expand Down
13 changes: 10 additions & 3 deletions blosc2/schunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,12 @@ def __init__(self, chunksize=None, data=None, **kwargs):
if chunksize is None:
chunksize = 2**24
if data is not None:
chunksize = data.size * data.itemsize
# Make that a multiple of typesize
chunksize = chunksize // data.itemsize * data.itemsize
if hasattr(data, "itemsize"):
chunksize = data.size * data.itemsize
# Make that a multiple of typesize
chunksize = chunksize // data.itemsize * data.itemsize
else:
chunksize = len(data)
# Use a cap of 256 MB (modern boxes should all have this RAM available)
if chunksize > 2**28:
chunksize = 2**28
Expand All @@ -236,6 +239,10 @@ def __init__(self, chunksize=None, data=None, **kwargs):
self._cparams = super(SChunk, self).get_cparams()
self._dparams = super(SChunk, self).get_dparams()

def __len__(self):
"""Return the number of items in the SChunk."""
return self.nbytes // self.typesize

@property
def cparams(self):
"""
Expand Down

0 comments on commit be4ab1d

Please sign in to comment.