python - Migrate cPickled objects to a new class with the same __getstate__ format -


i'm using cpickle serialize , deserialize instances of class. i've moved few classes different packages , i'm noticing cpickle/pickle stores class name , package comes from.

>>> class a(object):     pass  >>> dumps(a()) 'ccopy_reg\n_reconstructor\np1\n(c__main__\na\np2\nc__builtin__\nobject\np3\nntrp4\n.' 

notice how __main__ stored because ran code in main python interpreter.

if try unpickle many objects have stored way, i'll importerror complaining since moved classes around, old class doesn't exist in location expected.

i haven't changed format of __getstate__ or __setstate__. i've changed location of class needs deserialized. there way migrate these objects don't run problems?

if want migrate data, must provide reference new object @ old location, , dump data again:

old_location.a = new_location.a data = loads(pickle_data) pickle_data = dumps(data) 

and now, pickle_data contains reference new_location.a.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -