Generate python code out of C++

I would like to know if anyone of you knows about a library which is able to generate python code. Would that maybe be possible with boost.python?

If there is nothing like that, could you point me to the direction how I could do it myself?

best regards
1
2
3
4
5
#include <iostream>

int main(int argc, char *argv[]) {
    std::cout << "#!/usr/bin/python2.7\nprint \'Hello, world!\'\n";
}


Without more context, it's harder to give more help than that. It might also help to answer the following questions: What are you trying to do? Why do you think you need python to do this? What criteria do you want to base the python code gen on? Is the code intended for use in your app? If so, I would suggest reading this: https://twistedmatrix.com/users/glyph/rant/extendit.html
Thank you for your answer.

To make things more clear:

Yes, I have to use python, because the python script will be used as a configuration which has to be in python.

I have parsed values from a file, and I want these values as variables in a python function.




closed account (N36fSL3A)
You can follow this code and adapt it to your needs:
http://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I
Thank you, I will check that

Edit:

Just saw, it's just calling python from C++, I have already found a lot on this, but I would need to generate python code out of c++...
Last edited on
closed account (N36fSL3A)
Oh, I misread your topic. Sorry, I'm a bit low on sleep right now :P

What does the configuration file look like?
Thats fine I am happy you are trying to help me 😊
There is no real configuration file, what I need is a dynamic way to generate python function from given values parsed before.

Do you want to convert C++ code to the equivalent Python code? I don't think you will find an automatic way to do this. You probably will have to convert it yourself. To do this you need a good knowledge of both C++ and Python.

Do you want a program written in C++ to output Python code? In that case you just make the program output the python code that you want to output like nchambers showed. If you don't want to output all Python code at the same time you can split it up and use cout multiple times in your code. If you want to write the output to a file rather than the standard output stream you could use a std::ofstream instead of std::cout, but you don't have to because std::cout can be redirected to write to a file so it's up to you what you find most convenient.
Last edited on
Topic archived. No new replies allowed.