@@ -77,6 +77,9 @@ def encode(d, opt={}):
7777 'blosc2lz4hc','blosc2zlib','blosc2zstd'] for compression codec, default is None
7878 'nthread': number of compression thread of the codec is of the blosc2 class, default is 1
7979 """
80+
81+ opt .setdefault ("inplace" , False )
82+
8083 if "compression" in opt :
8184 if opt ["compression" ] == "lzma" :
8285 try :
@@ -189,7 +192,7 @@ def encode(d, opt={}):
189192 newobj .pop ("_ArrayData_" )
190193 return newobj
191194 else :
192- return copy .deepcopy (d )
195+ return copy .deepcopy (d ) if opt [ "inplace" ] else d
193196
194197
195198##====================================================================================
@@ -207,6 +210,9 @@ def decode(d, opt={}):
207210 @param[in] opt: options, can contain a dict with the following keys
208211 'nthread': number of decompression thread of the codec is of the blosc2 class, default is 1
209212 """
213+
214+ opt .setdefault ("inplace" , False )
215+
210216 if (isinstance (d , str ) or type (d ) == "unicode" ) and len (d ) <= 6 and len (d ) > 4 and d [- 1 ] == "_" :
211217 if d == "_NaN_" :
212218 return float ("nan" )
@@ -249,7 +255,7 @@ def decode(d, opt={}):
249255 newobj = lz4 .frame .decompress (bytes (newobj ))
250256 except Exception :
251257 print ('Warning: you must install "lz4" module to decompress a data record in this file, ignoring' )
252- return copy .deepcopy (d )
258+ return copy .deepcopy (d ) if opt [ "inplace" ] else d
253259 elif d ["_ArrayZipType_" ].startswith ("blosc2" ):
254260 try :
255261 import blosc2
@@ -260,7 +266,7 @@ def decode(d, opt={}):
260266 newobj = blosc2 .decompress2 (bytes (newobj ), as_bytearray = False , nthreads = blosc2nthread )
261267 except Exception :
262268 print ('Warning: you must install "blosc2" module to decompress a data record in this file, ignoring' )
263- return copy .deepcopy (d )
269+ return copy .deepcopy (d ) if opt [ "inplace" ] else d
264270 newobj = np .frombuffer (newobj , dtype = np .dtype (d ["_ArrayType_" ])).reshape (d ["_ArrayZipSize_" ])
265271 if "_ArrayIsComplex_" in d and newobj .shape [0 ] == 2 :
266272 newobj = newobj [0 ] + 1j * newobj [1 ]
@@ -300,7 +306,7 @@ def decode(d, opt={}):
300306 )
301307 return decodedict (d , opt )
302308 else :
303- return copy .deepcopy (d )
309+ return copy .deepcopy (d ) if opt [ "inplace" ] else d
304310
305311
306312##====================================================================================
@@ -344,7 +350,7 @@ def encodedict(d0, opt={}):
344350
345351
346352def encodelist (d0 , opt = {}):
347- d = copy .deepcopy (d0 )
353+ d = copy .deepcopy (d0 ) if opt [ "inplace" ] else d0
348354 for i , s in enumerate (d ):
349355 d [i ] = encode (s , opt )
350356 return d
@@ -367,7 +373,7 @@ def decodedict(d0, opt={}):
367373
368374
369375def decodelist (d0 , opt = {}):
370- d = copy .deepcopy (d0 )
376+ d = copy .deepcopy (d0 ) if opt [ "inplace" ] else d0
371377 for i , s in enumerate (d ):
372378 d [i ] = decode (s , opt )
373379 return d
0 commit comments