Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a dict type variable content cannot be pickled?

I have to use the numba's dict (numba.typed.typeddict.Dict) type to define a dictionary:

@njit
def init_numba_dict():
    x = numpy.array([0], dtype=numpy.int32)
    return {"01234-5": x[0], "0-1": x[0]}

myDict = init_numba_dict()

Now I need to save it to a file. I thought the following code shall work:

import pickle
filehandler = open("file.dat", 'wb') 
pickle.dump(myDict , filehandler, pickle.HIGHEST_PROTOCOL)
filehandler.close()

I got this error: "TypeError: can't pickle _nrt_python._MemInfo objects"

Any hint? How can I save and restorethe content of a numba.typed.typeddict.Dict to a file?

like image 431
user898160 Avatar asked Jun 27 '26 15:06

user898160


1 Answers

Great question. I just faced the same error and found the solution. The error occurs because numba (C language) returns a pointer instead of values like python in term of array or dict. On the other hand, pickle is asking for an python object as input. So, all you need to do is to change the numba dict to an python dict object before pickling it using dict() function.

import pickle
filehandler = open("file.dat", 'wb') 
pickle.dump(dict(myDict), filehandler, pickle.HIGHEST_PROTOCOL)
filehandler.close()
like image 194
NazKazi Avatar answered Jun 29 '26 07:06

NazKazi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!