简体   繁体   English

有没有办法将在 C++ 中创建的变量加载到 python 解释器?

[英]Is there any way to load a variable created in C++, to the python interpreter?

I am looking for some way to pass variables that have been created and initialized in C++ to Python.我正在寻找某种方法将在 C++ 中创建和初始化的变量传递给 Python。

The idea is to create a class "WorkSpace" in c++.这个想法是在 C++ 中创建一个类“WorkSpace”。 We indicate in this class which variables of C++ we want to pass to the interpreter of python.我们在这个类中指明我们想要传递给 python 的解释器的 C++ 变量。 With the objective of once time the variables have been loaded to Python, to be able to execute a script that makes use of these variables that we have just passed to the interpreter.目的是一次性将变量加载到 Python 中,以便能够执行使用我们刚刚传递给解释器的这些变量的脚本。 I don't want to create a "Wrapper" around C++ code to be executed in python.我不想围绕要在 python 中执行的 C++ 代码创建“包装器”。 The objective is not optimization ( speed processing ), it is versatility.目标不是优化(速度处理),而是多功能性。

In the end, the "Workspace" class would function as an intermediary between C++ and Python.最后,“Workspace”类将充当 C++ 和 Python 之间的中介。 In such a way that to this class we can request him to pass a variable of c++ to python and vice versa.通过这种方式,我们可以要求他将 c++ 的变量传递给 python,反之亦然。 We can request that we return a variable created in python to C++.我们可以请求将在 python 中创建的变量返回给 C++。

For example we have two matrix created and initializated in C++:例如,我们在 C++ 中创建并初始化了两个矩阵:

#include "armadillo.h"

arma::mat A = { {1, 3, 5},
                {2, 4, 6},
                {7, 8, 9} };

arma::mat B = A.t();

We want to pass these two matrix (or any other variable) to Python.我们想将这两个矩阵(或任何其他变量)传递给 Python。 Once these matrix are in Python, we execute a script that makes use of these 2 matrix.一旦这些矩阵在 Python 中,我们就会执行一个使用这两个矩阵的脚本。

I have been looking for some time but all I find is information to embed C++ code in python.我一直在寻找一段时间,但我找到的只是将 C++ 代码嵌入到 python 中的信息。

Could someone help me with this matter?有人可以帮我解决这个问题吗? Is it possible to carry out this implementation?是否可以执行此实施? What should I look for?我应该寻找什么?

I think what you want is embedding python to c++.我认为你想要的是将 python 嵌入到 C++ 中。 Here is a link for the documentation: https://docs.python.org/3.6/extending/embedding.html#embedding-python-in-another-application这是文档的链接: https : //docs.python.org/3.6/extending/embedding.html#embedding-python-in-another-application

You need to execute a python function from the c++ code.您需要从 c++ 代码执行 python 函数。 Embedding python allows it.嵌入 python 允许它。 Passing of matrices as parameters is not so straight-forward.将矩阵作为参数传递并不是那么简单。 You would need to convert them to numpy arrays and then pass them as parameters to your python function.您需要将它们转换为 numpy 数组,然后将它们作为参数传递给您的 Python 函数。

Another easy but not nice way is to save the matrices to a file, and read them from python as a binary stream, and convert to matrices.另一种简单但不是很好的方法是将矩阵保存到文件中,然后将它们作为二进制流从 python 中读取,然后转换为矩阵。 In this case, you can execute external python script (something like here: Running python script from c++ code and use pythons output in c++ ).在这种情况下,您可以执行外部 python 脚本(类似于此处: 从 c++ 代码运行 python 脚本并在 c++ 中使用 pythons 输出)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM