Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, cPickle, pickling lambda functions

Tags:

I have to pickle an array of objects like this:

import cPickle as pickle from 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 it gives the following error:

TypeError: can't pickle function objects 

Is there a way around that?

like image 540
Saullo G. P. Castro Avatar asked May 18 '13 16:05

Saullo G. P. Castro


People also ask

Can you pickle lambda functions?

Functions are pickled by name, not by code. Unpickling will only work if a function with the same name is present in in the same module (__main__ in your example) This is why pickling a lambda won't work: they have no individual names.

What is pickling and Unpickling in Python with example?

“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy.

Does pickle overwrite?

Pickle dump replaces current file data.

How do I save a pickle in Python?

To save a pickle, use pickle. dump . A convention is to name pickle files *. pickle , but you can name it whatever you want.


1 Answers

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

The picloud package includes a more robust pickler, that can pickle lambda functions.

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

PiCloud-serialized objects can be de-serialized using the normal pickle/cPickle load and loads functions.

Dill also 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.' 
like image 135
ChrisB Avatar answered Nov 14 '22 07:11

ChrisB