arrays - Python, cPickle, pickling lambda functions -


i have pickle array of objects this:

import cpickle pickle numpy import sin, cos, array tmp = lambda x: sin(x)+cos(x) test = array([[tmp,tmp],[tmp,tmp]],dtype=object) pickle.dump( test, open('test.lambda','w') ) 

and gives following error:

typeerror: can't pickle function objects 

is there way around that?

the built-in pickle module unable serialize several kinds of python objects (including lambda functions, nested functions, , functions defined @ command line).

the picloud package includes more robust pickler, can pickle lambda functions.

from pickle import dumps f = lambda x: x * 5 dumps(f) # error cloud.serialization.cloudpickle import dumps dumps(f) # works 

picloud-serialized objects can de-serialized using normal pickle/cpickle load , loads functions.

dill provides similar functionality

>>> import dill            >>> f = lambda x: x * 5 >>> dill.dumps(f) '\x80\x02cdill.dill\n_create_function\nq\x00(cdill.dill\n_unmarshal\nq\x01uec\x01\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00c\x00\x00\x00s\x08\x00\x00\x00|\x00\x00d\x01\x00\x14s(\x02\x00\x00\x00ni\x05\x00\x00\x00(\x00\x00\x00\x00(\x01\x00\x00\x00t\x01\x00\x00\x00x(\x00\x00\x00\x00(\x00\x00\x00\x00s\x07\x00\x00\x00<stdin>t\x08\x00\x00\x00<lambda>\x01\x00\x00\x00s\x00\x00\x00\x00q\x02\x85q\x03rq\x04c__builtin__\n__main__\nu\x08<lambda>q\x05nn}q\x06tq\x07rq\x08.' 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -