[sage-devel] Re: Pickling functions
On Jun 14, 2008, at 1:25 PM, Daniel Bump wrote: Some code that has been proposed by Nicolas Thiery for sage/combinat/families.py would create classes that have as attributes dictionaries of functions. However dumps(s) will raise an exception if s is such a class instance. Example: the simple reflections in a Weyl group. See: http://groups.google.com/group/sage-combinat-devel/msg/ 8b987cd471db3493?hl=en What it boils down to is this. The following is fine in native Python: import pickle def f(x): return x+1 ... pickle.dumps(f) 'c__main__\nf\np0\n.' pickle.dumps({1:f}) '(dp0\nI1\nc__main__\nf\np1\ns.' But if you try to run this from within Sage, both calls to dumps() will raise exceptions. Is this a bug in Sage? I actually thought you couldn't really pickle functions, even in plain python. http://docs.python.org/lib/node317.html Note that functions (built-in and user-defined) are pickled by ``fully qualified'' name reference, not by value. This means that only the function name is pickled, along with the name of module the function is defined in. Neither the function's code, nor any of its function attributes are pickled. Thus the defining module must be importable in the unpickling environment, and the module must contain the named object, otherwise an exception will be raised. david --~--~-~--~~~---~--~~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-devel] Re: Pickling functions
One way to get around this limitation in python is to use callable classes instead of functions. David On Sat, Jun 14, 2008 at 10:42 AM, David Harvey [EMAIL PROTECTED] wrote: On Jun 14, 2008, at 1:25 PM, Daniel Bump wrote: Some code that has been proposed by Nicolas Thiery for sage/combinat/families.py would create classes that have as attributes dictionaries of functions. However dumps(s) will raise an exception if s is such a class instance. Example: the simple reflections in a Weyl group. See: http://groups.google.com/group/sage-combinat-devel/msg/8b987cd471db3493?hl=en What it boils down to is this. The following is fine in native Python: import pickle def f(x): return x+1 ... pickle.dumps(f) 'c__main__\nf\np0\n.' pickle.dumps({1:f}) '(dp0\nI1\nc__main__\nf\np1\ns.' But if you try to run this from within Sage, both calls to dumps() will raise exceptions. Is this a bug in Sage? I actually thought you couldn't really pickle functions, even in plain python. http://docs.python.org/lib/node317.html Note that functions (built-in and user-defined) are pickled by ``fully qualified'' name reference, not by value. This means that only the function name is pickled, along with the name of module the function is defined in. Neither the function's code, nor any of its function attributes are pickled. Thus the defining module must be importable in the unpickling environment, and the module must contain the named object, otherwise an exception will be raised. david --~--~-~--~~~---~--~~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-devel] Re: Pickling functions
This code pickles and unpickles functions. Note it is simi-hackish and I make no guarentee about it working in python 3.0 (but I'll be maintaining a copy for distributed stuff I'm working on for dev1. import new, types, copy_reg, cPickle #See python cookbook for more details def code_ctor(*args): return new.code(*args) def reduce_code(co): if co.co_freevars or co.co_cellvars: raise ValueError, Cannot pickle code objects from closures return code_ctor, (co.co_argcount, co.co_nlocals, co.co_stacksize, \ co.co_flags, co.co_code, co.co_consts, co.co_names, \ co.co_varnames, co.co_filename, co.co_name, \ co.co_firstlineno, co.co_lnotab) copy_reg.pickle(types.CodeType, reduce_code) def picklefunction(func): return cPickle.dumps(func.func_code) def unpicklefunction(pickled): recovered = cPickle.loads(pickled) ret = new.function(recovered, globals()) return ret On Sat, Jun 14, 2008 at 11:25 AM, David Roe [EMAIL PROTECTED] wrote: One way to get around this limitation in python is to use callable classes instead of functions. David On Sat, Jun 14, 2008 at 10:42 AM, David Harvey [EMAIL PROTECTED] wrote: On Jun 14, 2008, at 1:25 PM, Daniel Bump wrote: Some code that has been proposed by Nicolas Thiery for sage/combinat/families.py would create classes that have as attributes dictionaries of functions. However dumps(s) will raise an exception if s is such a class instance. Example: the simple reflections in a Weyl group. See: http://groups.google.com/group/sage-combinat-devel/msg/8b987cd471db3493?hl=en What it boils down to is this. The following is fine in native Python: import pickle def f(x): return x+1 ... pickle.dumps(f) 'c__main__\nf\np0\n.' pickle.dumps({1:f}) '(dp0\nI1\nc__main__\nf\np1\ns.' But if you try to run this from within Sage, both calls to dumps() will raise exceptions. Is this a bug in Sage? I actually thought you couldn't really pickle functions, even in plain python. http://docs.python.org/lib/node317.html Note that functions (built-in and user-defined) are pickled by ``fully qualified'' name reference, not by value. This means that only the function name is pickled, along with the name of module the function is defined in. Neither the function's code, nor any of its function attributes are pickled. Thus the defining module must be importable in the unpickling environment, and the module must contain the named object, otherwise an exception will be raised. david --~--~-~--~~~---~--~~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~--~~~~--~~--~--~---