保存复杂的脚本化对象的状态
在 C++ 中,我将以下两个类公开(使用 Boost)给 Python:
struct Foo {
// Empty
};
struct FooContainer {
// I use boost::shared_ptr for compatibility with Boost.Python
vector<boost::shared_ptr<Foo>> foos_;
};
在 Python 端,我可能会创建一种特殊类型的 Foo,它实际上会执行某些操作,而不仅仅是一个空类,然后将其添加到 FooContainer:
class Useful(Foo):
def __init__(self, a, b):
self.a = a
self.b = b
x = Useful(3, 5);
# Add 'x' to a `FooContainer`
回到 C++ 端,FooContainer 现在有一些 Foo,但它不知道也不关心它们来自 Python。应用程序运行了一段时间,Foo 对象中的数据发生了变化...
然后我决定保存程序的状态,以便稍后加载它。但问题是 FooContainer 对它的 Foo 对象了解不多,它甚至不知道它们来自 Python,我不想用不属于它的数据污染我的 FooContainer(单-责任原则等等)。
您对我应该如何组织我的应用程序有什么建议,以便可以以清晰的方式保存和加载数据以及加载新数据(即不是来自我过去保存的状态)?
In C++ I have the following two classes that I expose (using Boost) to Python:
struct Foo {
// Empty
};
struct FooContainer {
// I use boost::shared_ptr for compatibility with Boost.Python
vector<boost::shared_ptr<Foo>> foos_;
};
In the Python side I might create a special type of Foo that actually does something instead of being just an empty class, and then add it to a FooContainer:
class Useful(Foo):
def __init__(self, a, b):
self.a = a
self.b = b
x = Useful(3, 5);
# Add 'x' to a `FooContainer`
Back in the C++ side, the FooContainer now has some Foos, but it doesn't know or care that they are from Python. The application runs for a while and the data in the Foo objects changes...
Then I decide I want to save the state of my program so I can load it at a later time. But the problem is that FooContainer doesn't know much about its Foo objects, it doesn't even know that they come from Python and I wouldn't want to pollute my FooContainer with data that doesn't really belong in it (single-responsibility principle and all that).
Do you have any advice on how I should organize my application such that saving and loading data, as well as loading fresh data (ie. not from a state that I saved in the past) can be done in a clear way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 boost::python/pickle< /a>,并保存来自python的数据。我对酸洗套件的经验有限,但如果您在Python派生的类中重写适当的酸洗方法,它应该可以工作(请参阅我对 这个问题)。
You can use boost::python/pickle, and save the data from python. I only have limited experience with the pickling suite, but it should work provided you override appropriate pickling methods in your classes derived in python (see my answer to this question).
您已经有了创建 Foos 的 Python 代码,我们将其称为
populateFoos
,并且您的程序以某种方式调用它。现在您需要的下一件事是执行保存和加载的
storeFoos
和loadFoos
函数。如果您想保持通用,请将它们定义为storeFunc
和loadFunc
(或callback
,具体取决于上下文)。根据您的程序结构,您可能还需要在 python 中保留创建的(或与容器关联的)所有 foo 的列表。
You already have python code that creates the Foos, lets call it
populateFoos
and somehow you have your program call it.Now the next thing you need is a
storeFoos
andloadFoos
function that does the saving and loading. If you want to keep it generic define them asstoreFunc
andloadFunc
(orcallback
, depending on the context).Depending on your program structure you might also need to keep in python a list of all foos created (or associated to a container).