Skip to content

Commit 44f1c36

Browse files
committed
Replace c-blosc extension
1 parent ed2cff2 commit 44f1c36

File tree

6 files changed

+66
-1037
lines changed

6 files changed

+66
-1037
lines changed

numcodecs/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@
5252
ncores = multiprocessing.cpu_count()
5353
except OSError: # pragma: no cover
5454
ncores = 1
55-
blosc.init()
5655
blosc.set_nthreads(min(8, ncores))
57-
atexit.register(blosc.destroy)
5856

5957
with suppress(ImportError):
6058
from numcodecs import zstd

numcodecs/blosc_v2.py renamed to numcodecs/blosc.py

+57-9
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,30 @@
5252

5353

5454
def list_compressors() -> list[str]:
55+
"""Get a list of compressors supported in blosc."""
5556
return blosc.compressor_list()
5657

5758

5859
def get_nthreads() -> int:
60+
"""
61+
Get the number of threads that Blosc uses internally for compression and
62+
decompression.
63+
"""
5964
nthreads = blosc.set_nthreads(1)
6065
blosc.set_nthreads(nthreads)
6166
return nthreads
6267

6368

6469
def set_nthreads(nthreads: int) -> None:
70+
"""
71+
Set the number of threads that Blosc uses internally for compression and
72+
decompression.
73+
"""
6574
blosc.set_nthreads(nthreads)
6675

6776

68-
def cbuffer_complib(source):
77+
def cbuffer_complib(source) -> str:
78+
"""Return the name of the compression library used to compress `source`."""
6979
return blosc.get_clib(source)
7080

7181

@@ -86,6 +96,32 @@ def _check_buffer_size(buf, max_buffer_size):
8696

8797

8898
def compress(source, cname: str, clevel: int, shuffle: int = SHUFFLE, blocksize=AUTOBLOCKS):
99+
"""
100+
Compress data.
101+
102+
Parameters
103+
----------
104+
source : bytes-like
105+
Data to be compressed. Can be any object supporting the buffer
106+
protocol.
107+
cname : bytes
108+
Name of compression library to use.
109+
clevel : int
110+
Compression level.
111+
shuffle : int
112+
Either NOSHUFFLE (0), SHUFFLE (1), BITSHUFFLE (2) or AUTOSHUFFLE (-1). If AUTOSHUFFLE,
113+
bit-shuffle will be used for buffers with itemsize 1, and byte-shuffle will
114+
be used otherwise. The default is `SHUFFLE`.
115+
blocksize : int
116+
The requested size of the compressed blocks. If 0, an automatic blocksize will
117+
be used.
118+
119+
Returns
120+
-------
121+
dest : bytes
122+
Compressed data.
123+
124+
"""
89125
if shuffle == AUTOSHUFFLE:
90126
if source.itemsize == 1:
91127
shuffle = BITSHUFFLE
@@ -109,6 +145,23 @@ def compress(source, cname: str, clevel: int, shuffle: int = SHUFFLE, blocksize=
109145

110146

111147
def decompress(source, dest: np.ndarray | bytearray | None = None):
148+
"""
149+
Decompress data.
150+
151+
Parameters
152+
----------
153+
source : bytes-like
154+
Compressed data, including blosc header. Can be any object supporting the buffer
155+
protocol.
156+
dest : array-like, optional
157+
Object to decompress into.
158+
159+
Returns
160+
-------
161+
dest : bytes
162+
Object containing decompressed data.
163+
164+
"""
112165
if dest is None:
113166
return blosc.decompress(source)
114167
elif isinstance(dest, np.ndarray):
@@ -119,7 +172,8 @@ def decompress(source, dest: np.ndarray | bytearray | None = None):
119172

120173

121174
class Blosc(Codec):
122-
"""Codec providing compression using the Blosc meta-compressor.
175+
"""
176+
Codec providing compression using the Blosc meta-compressor.
123177
124178
Parameters
125179
----------
@@ -170,11 +224,5 @@ def decode(self, buf, out=None):
170224
return decompress(buf, out)
171225

172226
def __repr__(self):
173-
r = '%s(cname=%r, clevel=%r, shuffle=%s, blocksize=%s)' % (
174-
type(self).__name__,
175-
self.cname,
176-
self.clevel,
177-
_shuffle_repr[self.shuffle + 1],
178-
self.blocksize,
179-
)
227+
r = f'{type(self).__name__}(cname={self.cname!r}, clevel={self.clevel!r}, shuffle={_shuffle_repr[self.shuffle + 1]}, blocksize={self.blocksize})'
180228
return r

0 commit comments

Comments
 (0)