Pickled 对象版本控制
我正在开发一个项目,其中有大量对象被序列化并使用 pickle
/cPickle
存储到磁盘。
随着项目生命周期的进展(在向现场客户发布之后),未来的功能/修复可能会要求我们更改一些持久对象的签名。这可能是添加字段、删除字段,甚至只是更改一条数据的不变量。
是否有一种标准方法可以将要腌制的对象标记为具有特定版本(例如 Java 中的 serialVersionUID
)?基本上,如果我要恢复 Foo 版本 234 的实例,但当前代码是 236,我希望收到一些有关 unpickle 的通知。我应该继续推出我自己的解决方案(可以是 PITA)吗?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
pickle
格式没有这样的限制。为什么不将“序列版本号”作为对象属性的一部分,与其他属性一起进行腌制呢?然后,通过比较实际版本和所需版本就可以轻松获得“通知”——不明白为什么它应该是 PITA。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.考虑 Tomasz Früboes 建议的以下类 mixin 此处。
__getstate__
方法在 pickle 时由pickle
调用,而__setstate__
在 unpickling 时由 pickle 调用。该混合类可以用作您想要跟踪其版本的类的子类。其用法如下:Consider the following class mixin suggested by Tomasz Früboes here.
The
__getstate__
method is called bypickle
upon pickling, and__setstate__
is called by pickle upon unpickling. This mix-in class can be used as a subclass of classes whose version you want to keep track of. This is to be used as follows: