替代python泡菜对重构的敏感性不太敏感

发布于 2025-02-06 13:32:50 字数 1116 浏览 3 评论 0原文

这可能是一个相对普遍的问题,但到目前为止,我还没有找到任何真正的答案。

让我们用一个示例来说明为什么腌菜在这里不满意:我有一个文件,其中包含一个需要很长时间(数十个小时)实例化的类。一旦实例化,我然后将其保存到腌制文件中:

import pickle
import time

from foo.bar import baz

class MyClass:
    def __init__(self):
        self.message = baz.very_long_computation()

obj = MyClass()

with open('./obj.pickle', 'wb') as f:
    pickle.dump(obj)

从同一存储库中的另一个文件中,我想加载实例化对象并使用它,而不必等待:

import pickle

with open('./obj.pickle', 'rb') as f:
    obj = pickle.load(f)

print(obj.message)

有时可能发生的是我更改Baz模块在文件夹结构中的位置(例如,不必修改其内容),例如,我将其从foo.bar.baz.baz移动到qux.bar.bar.baz.baz

现在,如果我尝试再次加载obj.pickle,Python将向modulenotfounderror投诉:没有名为foo的模块。

看来,每当我进行丝毫重构时,我都必须完全重新安置myClass,涉及这么长的等待时间。在实践中,这非常令人讨厌,因此我的两个问题是:

  • 腌菜是否有其他选择不会迫使重新实现?
  • 有没有办法在仍在使用泡菜的同时避免此问题?

注意:我了解,在上面的示例中,我只需要将self.message保存到文本文件中,然后将其称为一天。实际上,以这种方式坚持对象的内容将很复杂。

This is probably a relatively common concern but I haven't found any real answer so far.

Let's use an example to illustrate why Pickle is not satisfying here: I have a file which contains a class that takes a long time (tens of hours) to instantiate. Once instantiated, I then save it to a pickle file:

import pickle
import time

from foo.bar import baz

class MyClass:
    def __init__(self):
        self.message = baz.very_long_computation()

obj = MyClass()

with open('./obj.pickle', 'wb') as f:
    pickle.dump(obj)

From a different file in the same repository, I want to load the instantiated object and use it, without having to wait:

import pickle

with open('./obj.pickle', 'rb') as f:
    obj = pickle.load(f)

print(obj.message)

What can happen sometimes is that I change e.g. the baz module's location in the folder structure (without necessarily modify its content), for instance I move it from foo.bar.baz to qux.bar.baz.

Now, if I try to load obj.pickle again, Python will complain with a ModuleNotFoundError: No module named foo.

It seems that whenever I make the slightest refactoring, I have to completely re-instantiate MyClass, involving this long waiting time. In practice this is terribly annoying, hence my two questions:

  • is there any alternative to Pickle that wouldn't force re-instantiating?
  • is there a way to avoid this problem while still using Pickle?

Note: I understand that in the above example, I would simply have to save self.message into a text file and call it a day. In reality, it would be complex to persist the object's content in such a manner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文