5

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!

1
  • 8
    Sure. Python can generate C++ files as easily as it can generate any other kind of file.. with open("test.cpp", "w") as file: file.write("int main(){printf('Hello, World!'); return 0;}") Commented Jun 10, 2014 at 13:39

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

3 Comments

Interesting. I hadn't considered having Python invoke gcc itself. In general, I think, it's easier to have Python simply generate the C++ code and let your regular build system take care of building it.
@JoshKelley Sure, I guess it all depends on what you (and your team) is comfortable with. In my experience, I keep everything in python so I can easily catch anything pushed out to stderr and do something with it. This was when I was trying to write auto generated code with a genetic algorithm so your milage may vary!
@Hooked I do like your answer and I thank you but Josh is right in the fact that I have to use my companies building procedures so just a cpp would be the best to avoid different clashes as others would just want a file generated.

Your Answer

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.