Pallavi .V.R
Pallavi .V.R

Reputation: 11

What are the modules supported by python to generate a c code?

Are there any particular modules in python which provide the generation of c file and write into this c file ? Currently I am parsing an xml file with python and I want to use the parsed information to create a desired data structure from the python script into a c file. for example , i saw a reference from the link https://www.codeproject.com/Articles/571645/Really-simple-Cplusplus-code-generation-in-Python . Is there similar module for c code generation ??

Upvotes: 1

Views: 458

Answers (1)

jsbueno
jsbueno

Reputation: 110291

By what you describe you just have to generate a source code file you will be using in your own project.

You just don t need any special Python library to do that. The linked article you give does nothing special with the C++ strings for each line hardcoded into Python code, which linearly generates all the parts of the code: there is nothing special on that article about "creatign a C++" file - just put what you want in your final file in strings, and you can put your data values using the string format method, or the new f-strings.

However, if you want a nice and maintainable solution, you could use a template library for Python, like "jinja2". Once you have a jinja2 template for your desired C file, all you need to do is to call Jinja's render methods passing the custom data you want into the final file, and sabve the result to a file.

That is not specific about C, though, that is about creating a structered text file with data you have, and Jinja is a nice tool for that.

People are mentioning "Cython" around: Cython is a solution to convert Python-like code into C - that is, if you want a program you wrote in Python to become a C program. (Although it is seldon used like that - normally the C generating and building steps are transparent, and one just cares about the final executable program or module when using Cython.)

From my reading of your question, Cython is not what you want at this moment.

Upvotes: 1

Related Questions