Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ code generation with Python

Can anyone point me to some documentation on how to write scripts in Python (or Perl or any other Linux friendly script language) that generate C++ code from XML or py files from the command line. I'd like to be able to write up some xml files and then run a shell command that reads these files and generates .h files with fully inlined functions, e.g. streaming operators, constructors, etc.

like image 669
user357525 Avatar asked Jun 03 '10 13:06

user357525


People also ask

Can you code C in Python?

Conclusion. The python default implementation is written in C programming and it's called CPython. So it's not very uncommon to use C functions in a python program.

Can C and Python work together?

Extending Python with C or C++ It is quite easy to add new built-in modules to Python, if you know how to program in C. Such extension modules can do two things that can't be done directly in Python: they can implement new built-in object types, and they can call C library functions and system calls.

How do you create a code generator in Python?

You can create a generator model with the YAKINDU Statechart generator model wizard by selecting File → New → Code generator model. The code generation is performed automatically whenever the statechart or the generator file is modified. See also chapter Running a generator for more information.

Can you mix Python with C++?

Python Library is a open source framework for interfacing Python and C++. It allows you to quickly and seamlessly expose C++ classes functions and objects to Python, and vice-versa, using no special tools, just your C++ compiler.


3 Answers

If you want to do this simply with just standard Python stuff you might try making template files that use the Python 3 style string formatting. For example, a class template might look something like this:

{className}::{className}()
{{
}}

{className}::~{className}()
{{
}}

{className}::{className}(const {className}& other)
{{
}}

{className}& {className}::operator=(const {className}& other)
{{
    return *this;
}}

Then your Python code is super simple:

d = {}
d['className'] = 'MyCPlusPlusClassName'
with open(yourTemplateFile, 'r') as ftemp:
    templateString = ftemp.read()
with open(generatedFile, 'w') as f:
    f.write(templateString.format(**d))

Of course you can add lots of other fields alongside 'className' using the same trick. If you don't need stuff like conditional code generation you can get a lot of mileage out of something this simple.

like image 87
Ross Avatar answered Oct 17 '22 18:10

Ross


I'm afraid you will not find an already-built in solution that takes your particular xml or python files and transforms them onto your required output "out of the box".

You will have to implement the parsing, the data treatment and output yourself. Not all by yourself, though; here are some pointers regarding the parsing and output.

Python comes with 2 different XML parsers (SAX and DOM -scroll down to see some examples). You will have to use one of them in order to read the source files.

For generating the output more easily, you can probably use a templating library, such as StringTemplate, or just generate the code manually, if it's small.

like image 30
kikito Avatar answered Oct 17 '22 17:10

kikito


You could take a look at Shedskin, a project that generates C++ code from Python code.

Depending on what your reasons are, it may be a little pointless as Satanicpuppy pointed out.

like image 1
Wayne Werner Avatar answered Oct 17 '22 16:10

Wayne Werner