How to generate C code from Python? - Stack Overflow
C++ code generation with Python - Stack Overflow
python - Generating C code from script: how to template? - Stack Overflow
Are there any libraries that can easily convert Python to C/C#/or C++? Ones where a person doesn't have to "calibrate" it, just, pip install library and then they can have their Python code in C,C#,or C++?
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.
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.
Let me explain a trick. Jinja2 is a template script that can generate text from templates. What you need to do is write your C code template main.c.j2:
#include <stdio.h>
int main()
{
printf("hello {{name}}.\n");
return 0;
}
then you can transform it into C code with the trans.py:
import os
from jinja2 import PackageLoader, Environment, FileSystemLoader
TemplateLoader = FileSystemLoader(os.path.abspath("."))
env = Environment(loader = TemplateLoader)
template = env.get_template("main.c.j2")
print(template.render(name = "Jack"))
run python trans.py and you will see this C code on screen:
#include <stdio.h>
int main()
{
printf("hello Jack.\n");
return 0;
}
That's real C code, {{name}} is replaced into Jack. See the Jinja2 doc for more interesting features.
This could be done with the re library in Python.
Search for the blockname and replace it with the new block:
re.sub(f"// start generated block \({blockname}\).*// end generated block \({blockname}\)", new_code, fileContents, flags=re.DOTALL)
Are there any libraries that can easily convert Python to C/C#/or C++? Ones where a person doesn't have to "calibrate" it, just, pip install library and then they can have their Python code in C,C#,or C++?