18
18
19
19
"""
20
20
21
- from numcodecs .abc import Codec
22
21
import numpy as np
23
22
24
23
import blosc
25
24
from blosc import (
26
25
BITSHUFFLE ,
27
- SHUFFLE ,
28
- NOSHUFFLE ,
29
26
MAX_BUFFERSIZE ,
30
27
MAX_THREADS ,
31
28
MAX_TYPESIZE ,
32
- VERSION_STRING ,
29
+ NOSHUFFLE ,
30
+ SHUFFLE ,
33
31
VERSION_DATE ,
32
+ VERSION_STRING ,
34
33
)
34
+ from numcodecs .abc import Codec
35
35
36
36
__all__ = [
37
37
"BITSHUFFLE" ,
38
- "SHUFFLE" ,
39
- "NOSHUFFLE" ,
40
38
"MAX_BUFFERSIZE" ,
41
39
"MAX_THREADS" ,
42
40
"MAX_TYPESIZE" ,
43
- "VERSION_STRING" ,
41
+ "NOSHUFFLE" ,
42
+ "SHUFFLE" ,
44
43
"VERSION_DATE" ,
45
- "list_compressors " ,
44
+ "VERSION_STRING " ,
46
45
'get_nthreads' ,
46
+ "list_compressors" ,
47
47
]
48
48
49
49
AUTOBLOCKS = 0
52
52
53
53
54
54
def list_compressors () -> list [str ]:
55
+ """Get a list of compressors supported in blosc."""
55
56
return blosc .compressor_list ()
56
57
57
58
58
59
def get_nthreads () -> int :
60
+ """
61
+ Get the number of threads that Blosc uses internally for compression and
62
+ decompression.
63
+ """
59
64
nthreads = blosc .set_nthreads (1 )
60
65
blosc .set_nthreads (nthreads )
61
66
return nthreads
62
67
63
68
64
69
def set_nthreads (nthreads : int ) -> None :
70
+ """
71
+ Set the number of threads that Blosc uses internally for compression and
72
+ decompression.
73
+ """
65
74
blosc .set_nthreads (nthreads )
66
75
67
76
68
- def cbuffer_complib (source ):
77
+ def cbuffer_complib (source ) -> str :
78
+ """Return the name of the compression library used to compress `source`."""
69
79
return blosc .get_clib (source )
70
80
71
81
@@ -86,6 +96,32 @@ def _check_buffer_size(buf, max_buffer_size):
86
96
87
97
88
98
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
+ """
89
125
if shuffle == AUTOSHUFFLE :
90
126
if source .itemsize == 1 :
91
127
shuffle = BITSHUFFLE
@@ -109,6 +145,23 @@ def compress(source, cname: str, clevel: int, shuffle: int = SHUFFLE, blocksize=
109
145
110
146
111
147
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
+ """
112
165
if dest is None :
113
166
return blosc .decompress (source )
114
167
elif isinstance (dest , np .ndarray ):
@@ -119,7 +172,8 @@ def decompress(source, dest: np.ndarray | bytearray | None = None):
119
172
120
173
121
174
class Blosc (Codec ):
122
- """Codec providing compression using the Blosc meta-compressor.
175
+ """
176
+ Codec providing compression using the Blosc meta-compressor.
123
177
124
178
Parameters
125
179
----------
@@ -170,11 +224,5 @@ def decode(self, buf, out=None):
170
224
return decompress (buf , out )
171
225
172
226
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 } )'
180
228
return r
0 commit comments