1818
1919"""
2020
21- from numcodecs .abc import Codec
2221import numpy as np
2322
2423import blosc
2524from blosc import (
2625 BITSHUFFLE ,
27- SHUFFLE ,
28- NOSHUFFLE ,
2926 MAX_BUFFERSIZE ,
3027 MAX_THREADS ,
3128 MAX_TYPESIZE ,
32- VERSION_STRING ,
29+ NOSHUFFLE ,
30+ SHUFFLE ,
3331 VERSION_DATE ,
32+ VERSION_STRING ,
3433)
34+ from numcodecs .abc import Codec
3535
3636__all__ = [
3737 "BITSHUFFLE" ,
38- "SHUFFLE" ,
39- "NOSHUFFLE" ,
4038 "MAX_BUFFERSIZE" ,
4139 "MAX_THREADS" ,
4240 "MAX_TYPESIZE" ,
43- "VERSION_STRING" ,
41+ "NOSHUFFLE" ,
42+ "SHUFFLE" ,
4443 "VERSION_DATE" ,
45- "list_compressors " ,
44+ "VERSION_STRING " ,
4645 'get_nthreads' ,
46+ "list_compressors" ,
4747]
4848
4949AUTOBLOCKS = 0
5252
5353
5454def list_compressors () -> list [str ]:
55+ """Get a list of compressors supported in blosc."""
5556 return blosc .compressor_list ()
5657
5758
5859def 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
6469def 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
8898def 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
111147def 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
121174class 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