Ian Bicking: the old part of his blog

Comment

This got me out of a similar pickle.

All my classes were in one module though...

#-------------------------------------------------------------------------------
# This is a fix.
# The module name is saved in the pickle so if you move your module hiearchy
# around you cant unpickle. This replaces the lookup function so that a load
# will ignore the module name.
#-------------------------------------------------------------------------------
CLASSES = {}

def __find_global(modulename, classname):
global CLASSES
return CLASSES[classname]

def loads(s):
f = cStringIO.StringIO(s)
p = cPickle.Unpickler(f)
p.find_global = __find_global
return p.load()


... your classes here


################################################################################
#
# This MUST be the last function/code defined in this module so that if finds
# ALL the classes in this module.
#
################################################################################
def findClasses(frame):
d = {}
for o in frame.f_locals.itervalues():
if inspect.isclass(o):
d[o.__name__] = o

return d


CLASSES = findClasses(inspect.currentframe())

Comment on Persistent Persistence
by lcm