1

I am looking for some way to pass variables that have been created and initialized in C++ to Python.

The idea is to create a class "WorkSpace" in c++. We indicate in this class which variables of C++ we want to pass to the interpreter of python. 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. I don't want to create a "Wrapper" around C++ code to be executed in python. 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. In such a way that to this class we can request him to pass a variable of c++ to python and vice versa. We can request that we return a variable created in python to C++.

For example we have two matrix created and initializated in 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. Once these matrix are in Python, we execute a script that makes use of these 2 matrix.

I have been looking for some time but all I find is information to embed C++ code in python.

Could someone help me with this matter? Is it possible to carry out this implementation? What should I look for?

asked Sep 23, 2020 at 11:02
3
  • Take a look at docs.python.org/3/extending/embedding.html. You could also consider saving your data to a file (for example in CSV format) and then invoking a standalone Python interpreter, either from your C++ code or from some external driver script. Commented Sep 23, 2020 at 12:09
  • I would go with 2 process and some kind of IPC, if data is small pass it as a request or rpc call, if is big, pass a path to a file, and there you go, no embedding complexity Commented Sep 23, 2020 at 12:18
  • even simpler, you can execute a script and communicate over stdio, no need for server/client infrastructure Commented Sep 23, 2020 at 12:20

1 Answer 1

0

I think what you want is embedding python to c++. Here is a link for the documentation: 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. Embedding python allows it. 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.

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. In this case, you can execute external python script (something like here: Running python script from c++ code and use pythons output in c++).

answered Sep 23, 2020 at 12:06
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.