Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pickled Object Versioning

I am working on a project where we have a large number of objects being serialized and stored to disk using pickle/cPickle.

As the life of the project progresses (after release to customers in the field) it is likely that future features/fixes will require us to change the signature of some of our persisted objects. This could be the addition of fields, removing of fields, or even just changing the invariants on a piece of data.

Is there a standard way to mark an object that will be pickled as having a certain version (like serialVersionUID in Java)? Basically, if I am restoring an instance of Foo version 234 but the current code is 236 I want to receive some notification on unpickle. Should I just go ahead and roll out my own solution (could be a PITA).

Thanks

like image 863
Paul Osborne Avatar asked Mar 02 '10 05:03

Paul Osborne


People also ask

Does pickle dump overwrite file?

Save this question. Show activity on this post. When I use pickle, it works fine and I can dump any load.

How do I know my pickle version?

Check pickle Version Windows To check which version of pickle is installed, use pip show pickle or pip3 show pickle in your Windows CMD, command line, or PowerShell.

Is pickling same as serialization?

Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” 1 or “flattening”; however, to avoid confusion, the terms used here are “pickling” and “unpickling”.

How do I Unpickle a pickle file?

As we said earlier, the load() method can be used to unpickle the pickled Python object. You have to first open the pickled file using rb (read-binary) permission and pass the opened file to the load() method, as shown below. The load() method unpickles the data and returns the actual object.


1 Answers

The pickle format has no such proviso. Why don't you just make the "serial version number" part of the object's attributes, to be pickled right along with the rest? Then the "notification" can be trivially had by comparing actual and desired version -- don't see why it should be a PITA.

like image 105
Alex Martelli Avatar answered Sep 30 '22 04:09

Alex Martelli