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!
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With