65
65
# until we can pass this into our conversion functions,
66
66
# this is pretty hacky
67
67
compressor = None
68
- _IMPORTS = False
69
- _BLOSC = False
70
68
71
- def _importers ():
72
- # import things we need
73
- # but make this done on a first use basis
74
-
75
- global _IMPORTS
76
- if _IMPORTS :
77
- return
78
-
79
- _IMPORTS = True
80
-
81
- global _BLOSC
82
- import zlib
83
- try :
84
- import blosc
85
- _BLOSC = True
86
- except :
87
- pass
88
69
89
70
def to_msgpack (path_or_buf , * args , ** kwargs ):
90
71
"""
@@ -103,7 +84,6 @@ def to_msgpack(path_or_buf, *args, **kwargs):
103
84
compress : type of compressor (zlib or blosc), default to None (no
104
85
compression)
105
86
"""
106
- _importers ()
107
87
global compressor
108
88
compressor = kwargs .pop ('compress' , None )
109
89
append = kwargs .pop ('append' , None )
@@ -146,7 +126,6 @@ def read_msgpack(path_or_buf, iterator=False, **kwargs):
146
126
obj : type of object stored in file
147
127
148
128
"""
149
- _importers ()
150
129
path_or_buf , _ = get_filepath_or_buffer (path_or_buf )
151
130
if iterator :
152
131
return Iterator (path_or_buf )
@@ -232,16 +211,18 @@ def convert(values):
232
211
233
212
# convert to a bytes array
234
213
v = v .tostring ()
214
+ import zlib
235
215
return zlib .compress (v )
236
216
237
- elif compressor == 'blosc' and _BLOSC :
217
+ elif compressor == 'blosc' :
238
218
239
219
# return string arrays like they are
240
220
if dtype == np .object_ :
241
221
return v .tolist ()
242
222
243
223
# convert to a bytes array
244
224
v = v .tostring ()
225
+ import blosc
245
226
return blosc .compress (v , typesize = dtype .itemsize )
246
227
247
228
# ndarray (on original dtype)
@@ -254,18 +235,13 @@ def unconvert(values, dtype, compress=None):
254
235
return np .array (values , dtype = object )
255
236
256
237
if compress == 'zlib' :
257
-
238
+ import zlib
258
239
values = zlib .decompress (values )
259
240
return np .frombuffer (values , dtype = dtype )
260
241
261
242
elif compress == 'blosc' :
262
-
263
- if not _BLOSC :
264
- raise Exception ("cannot uncompress w/o blosc" )
265
-
266
- # decompress
243
+ import blosc
267
244
values = blosc .decompress (values )
268
-
269
245
return np .frombuffer (values , dtype = dtype )
270
246
271
247
# from a string
0 commit comments