Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pickle/zodb: how to handle moving .py files with class definitions?

I'm using ZODB which, as I understand it, uses pickle to store class instances. I'm doing a bit of refactoring where I want to split my models.py file into several files. However, if I do this, I don't think pickle will be able to find the class definitions, and thus won't be able to load the objects that I already have stored in the database. What's the best way to handle this problem?

like image 622
Claudiu Avatar asked Aug 28 '12 16:08

Claudiu


2 Answers

You can create aliases; because one models.py modules is being split into multiple new modules you can only do this by importing your classes into the old location.

Both methods cause new copies of your instance pickles to refer to the new location; if you can force a write on all instances of the moved classes you don't need to retain the aliases. You can do this by setting _p_changed to True on your instances that you want to be written again.

So, to create the aliases, import your moved classes in the old location:

from newmodule1 import MyClass1, MyClass2
from newmodule2 import MyClass3

If you only rename a module (so the same classes all are found in one new location, could be a set of imports themselves), you can also create a sys.modules entry for the old name:

import sys
import newmodule

sys.modules['full.path.to.old.module] = newmodule
like image 163
Martijn Pieters Avatar answered Oct 02 '22 13:10

Martijn Pieters


As long as you want to make the pickle loadable without performing a migration to the new class model structure: you can use alias imports of the refactored classes inside the location of the old model.py.

like image 43
Andreas Jung Avatar answered Oct 02 '22 13:10

Andreas Jung