pittnerf
pittnerf

Reputation: 801

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?

Upvotes: 3

Views: 1477

Answers (1)

Naz
Naz

Reputation: 2775

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()

Upvotes: 3

Related Questions