Your search query "linkto%3A%22UsingPickle%2FRenamingModules%22" didn't return any results. Please change some terms and refer to HelpOnSearching for more information.
(!) Consider performing a full-text search with your search terms.

Clear message

If you are using pickle to store your data and decide to rename some of the modules in your project, you will have troubles loading old saved pickles. However, this can be solved overloading the load_global method in the default pickle implementation like this:

import pickle

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

renametable = {
    'old.module': 'new.module',
    'old_name': 'new_name',
    }

def mapname(name):
    if name in renametable:
        return renametable[name]
    return name

def mapped_load_global(self):
    module = mapname(self.readline()[:-1])
    name = mapname(self.readline()[:-1])
    klass = self.find_class(module, name)
    self.append(klass)

def loads(str):
    file = StringIO(str)
    unpickler = pickle.Unpickler(file)
    unpickler.dispatch[pickle.GLOBAL] = mapped_load_global
    return unpickler.load()

Unable to edit the page? See the FrontPage for instructions.