Skip to content

Instantly share code, notes, and snippets.

@ogrisel
Last active December 10, 2015 17:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ogrisel/4470212 to your computer and use it in GitHub Desktop.
Save ogrisel/4470212 to your computer and use it in GitHub Desktop.
Pickling class definitions for IPython.parallel
import inspect
from pickle import loads, dumps
from IPython.utils import pickleutil
def class_dumps(cls):
canned_dict = pickleutil.canDict(
dict((k, v) for k, v in cls.__dict__.items()
if k not in ('__weakref__', '__dict__')))
parents = tuple(cls.mro())
# TODO: recursively call class_dumps on parents that can from the same
# module as the module of cls
return dumps((cls.__name__, parents, canned_dict))
def class_loads(cls_str):
name, parents, canned_dict = loads(cls_str)
return type(name, parents, pickleutil.uncanDict(canned_dict))
# Let's define some classes to copy and %paste into an interactive ipython
# session:
"""
class AClass(object):
def __init__(self, a):
self.a = a
def a_method(self, b):
return self.a + b
class BClass(AClass):
def b_method(self, b):
return self.a_method(b) * 2
AClassClone = class_loads(class_dumps(AClass))
BClassClone = class_loads(class_dumps(BClass))
b = BClassClone(3)
b.b_method(4)
"""
@ogrisel
Copy link
Author

ogrisel commented Jan 7, 2013

Indeed, I was still running 0.13. I have now switched to the git version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment