Thread: Generate python code out of C++

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    7

    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
    Last edited by merl111; 08-14-2015 at 10:50 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Boost.Python is intended to execute C++ code from Python and vice versa. Did you try googling for a C++ to Python translator?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    7
    Thank you for your answer.


    I have been searching the net for hours now, but I could not find anything useful, most things are for creating c++ code from python.

    I don't really want to translate it, I think things are to complex to get useful output, maybe it is possible somehow to generate the python function code with variables I parsed from a different source and create it in a c++ function?

    Maybe there is also a way to have a python template somewhere in my code and just fill it up with the values I parsed?

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Just wondering, why do you want to go from C++ to Python?

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    7
    because the parser for the values is in c++

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Perhaps a bit of clarification is in order.

    Do you want to convert C++ code to Python?
    Do you want your C++ program to generate unrelated Python code for some purpose and possibly execute it?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Aug 2015
    Posts
    7
    I don't want to convert c++ code to python.

    As you said, I want my program to generate unrelated python code with values parsed before and generate a .py file from it. It does not need to execute it.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Have you learned how to read/write files? It's relatively easy to print text to a file and save it with a particular file name.

    A basic example:

    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
      std::ofstream pythonFile("test.py");
    
      pythonFile << "print 'Hello, World!'" << std::endl;
      pythonFile << "for x in range(10):" << std::endl;
      pythonFile << "    print 'x: ', x" << std::endl;
    
      return 0;
    }
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    Aug 2015
    Posts
    7
    I already know I could do it like that, but python is pretty strict on format, it would be so much work to write every line into a file, maybe I have to think about a function that generates the python functions dynamically.

    Is there a possibility in C++ to lets say have a template file with placeholders and then replace them?

    Edit:

    Or maybe it would be easier to provide a interface to python and pass the parsed values to a python script which generates the rest...
    Last edited by merl111; 08-14-2015 at 02:28 PM.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Then you could create a tree hierarchy that automatically formats itself properly when you dump it to the file.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  11. #11
    Registered User
    Join Date
    Aug 2015
    Posts
    7
    Thats a great idea, but could you give me a short example how I could do that?

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    If it was my project, I would create a class called "Scope." The constructor for Scope would take, as its parameter, the opening statement (for, if, etc.). Scope would contain a list of statements and child scopes (hint: they can be derived from a common base class), and functions to add and optionally remove statements and child scopes. I would then use recursion to walk down the tree when dumping it to a file. You can make it as simple or as complex as you want it to be.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  13. #13
    Registered User
    Join Date
    Aug 2015
    Posts
    7
    Ok, thank you. I am going to try that tomorrow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-27-2014, 08:09 AM
  2. Can someone explain how Python code parser works?
    By 33volx in forum C Programming
    Replies: 5
    Last Post: 02-27-2014, 08:09 AM
  3. Generate Assembly code and Binary code also
    By Hannibal2010 in forum C Programming
    Replies: 16
    Last Post: 07-07-2011, 05:43 AM
  4. To generate bar code from a number
    By darkducke in forum C Programming
    Replies: 18
    Last Post: 01-16-2008, 06:33 AM
  5. My code wont generate randon numbers
    By n00b_101 in forum C++ Programming
    Replies: 4
    Last Post: 10-20-2005, 05:39 AM

Tags for this Thread