Best suggestion: try them all. It won't take long.
My favourite: Jinja2 (by a mile)
It has decent syntax, can trace errors through it, and is sandboxable.
Answer from Ali Afshar on Stack OverflowBest suggestion: try them all. It won't take long.
My favourite: Jinja2 (by a mile)
It has decent syntax, can trace errors through it, and is sandboxable.
If you're doing code generation, you might find Cog useful - it's specifically for code generation, rather than being a generally applicable templating language.
Hi,
I need to write a console wizard that will ask several questions, and based on the answers and/or input, it will generate a py file.
For example:
$ python3 wizard.py
Name? IntelBP
Country? USA
Writing file ... Include.py generated.
$ cat include.py
name='IntelBP' country='USA'
... EOF.
So basically, what i need is a wizard engine that asks questions, and based on the answers, it reads a template (python code) and fills in some variables.
So there's essentially 2 aspects to this question:
What package can I use for writing the console interface? Ideally it will support text, lists, single, and multiple values for a specific wizard question.
Are there any packages designed to populate the values from the template into a python file? My greater concern is security related issues such as code injection.
Thanks!!
Python Code Generation Tools
code generation - Generate Python templates with parameters - Stack Overflow
Create a template code based on existing code base
Generate python code from a generic template
Videos
What do you guys use to generate Python code? I saw that a lot of people just use Jinja2 with some templates, which looks simple and powerful. You still have to generate the environments and write the templates. Now I'm looking around whether someone already did the work of generalizing that step (thinking about fields in classes, inheritance, constructors etc. and how to map that data to the environment) or whether I have to do it myself.
TLDR: Are there simple but powerful Python code generation "frameworks"?
EDIT:
-
I found pymultigen. Seems like it helps with creating code in multiple files.
-
Looks like pyecoregen is a specialized multi-file code generator based on pymultigen.