Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a python script be used to generate and write a C++ file/program?

Tags:

python

I am doing unit testing as part of my intern-ship. My boss is always telling me that finding a way to automate things is the best way and I was wondering if it would be possible to use python, to write a script that would read a text file with a certain structure and syntax and then generate a c++ file based off of said text file?

I am using Google-Test to do unit testing and I just think it would be a good addition to the companies tools but I would just like to know if its possible and if anyone else has done something similar.

Thanks!

like image 937
Puddinglord Avatar asked Jun 10 '14 13:06

Puddinglord


People also ask

Can you mix C and Python?

You can mix things up. Code your high-level logic in Python, and for all those parts where you need more speed, you can use C. You can learn more about it here.

How do you write a program to a file in Python?

Writing to Files in Python In order to write into a file in Python, we need to open it in write w , append a or exclusive creation x mode. We need to be careful with the w mode, as it will overwrite into the file if it already exists. Due to this, all the previous data are erased.

Can you make scripts with Python?

Writing Your First ScriptA script in python is almost exactly the same as a Shell script you've already met, i.e. it is a plain text file that contains lines of Python code that will be executed one after another. To create and edit a Python script, it is almost essential to use a text editor with syntax highlighting.


2 Answers

Yes, this is certainly doable. I've done this before (we use Python internally to create some C++ code to read and write structures). You can just use custom scripts (like @Kevin says in his comment, it's easy), or you can use a Python tool like Cog if you want a more structured approach.

As with anything, there are tradeoffs. Using Python to generate C++ code means that the developers have another language to learn (not a big deal, Python's worth learning and complements C++ well), and there's an extra level of indirection in your development and build process, and there's extra (Python) code to maintain. If you go this route, remember that simplicity is a virtue; don't get sucked into trying to write a singing, dancing "generate every kind of C++ code imaginable" framework.

like image 133
Josh Kelley Avatar answered Nov 14 '22 22:11

Josh Kelley


The answer from Josh is good if you want to scale up, but I thought you'd like to see what it looks like in action for a simple example. Most of the work is done with the subprocess module. Here I'm using a Linux machine so my compiler is gcc, but it is relatively easy to see how you can make this dependent on your specific system architecture.

import subprocess

code = r'''
#include <stdio.h>
int main() {
  for(int i=0;i<5;i++) {
    printf("Testing %d\n",i);
  }
  return 0;
}
'''

f_cpp = "test.cpp"
with open(f_cpp,'w') as FOUT:
    FOUT.write(code)

# Compile the program
f_exec = "./myprogram"
compile_cmd = "gcc {} -o {}".format(f_cpp, f_exec)
subprocess.call(compile_cmd, shell=True)

# Run the program
p = subprocess.Popen(f_exec, shell=True,
                     stdin=subprocess.PIPE, 
                     stdout=subprocess.PIPE)

# Step through the output 
for line in p.stdout:
    print line

This gives as output:

Testing 0   
Testing 1
Testing 2
Testing 3
Testing 4
like image 42
Hooked Avatar answered Nov 14 '22 21:11

Hooked