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 Overflow
🌐
Reddit
reddit.com › r/learnpython › generate python code from a generic template
r/learnpython on Reddit: Generate python code from a generic template
February 7, 2023 -

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:

  1. 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.

  2. 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!!

Discussions

Python Code Generation Tools
I'd probably use metaclasses if I had the kind of problem where code generation could actually help. The last time I saw a project using code generation where I thought that it ought have to used metaclasses was a tool to generate python code from swagger descriptions. Django's ORM is a good example where metaclasses were used to good effect (there are very few of those). In general I'm extremely suspicious of anybody who claims to have the kind of problem that requires either metaclasses or code generation would help though. It is suitable only for problems that are very, very, very abstract (e.g. like an ORM or REST API interaction framework). In 90% of cases I've seen the person attempting to use it vastly overgeneralized the problem that they actually had in an attempt to prepare for future problems never arose. In the process of doing that they then created a code clusterfuck that was hellishly hard to understand and debug (and in some cases I was left to clean up the mess). It wasn't simple and it can't, intrinsically, ever be simple (there is some language theory behind this). I could be convinced otherwise by describing your problem, but right now I'm 80% convinced that what you have described is an XY problem and that you're attempting to do this because you overgeneralized your problem. If you describe the actual problem you're solving it might help. More on reddit.com
🌐 r/Python
18
8
March 9, 2019
code generation - Generate Python templates with parameters - Stack Overflow
It allows to generate code for different kinds of languages including Python. In your case I suggest to create a basic DSL model (or download the DSL model example from here http://www.telosys.org/download/models-v3/ ) and use the following templates for Python : More on stackoverflow.com
🌐 stackoverflow.com
Create a template code based on existing code base
I have a use case where I need to create an initial draft python code based on existing corpus of code. I am thinking whether retrieval augmented generation will work in this case. Not sure if text embeddings will do a good job in similarity against code embeddings. More on community.openai.com
🌐 community.openai.com
0
0
May 18, 2023
Generate python code from a generic template
This sounds like an X-Y question, I think you should describe your larger goal rather than asking how to implement this specific solution. Generating code is usually a bad idea and usually not necessary. More on reddit.com
🌐 r/learnpython
7
0
February 7, 2023
🌐
PyPI
pypi.org › project › pymultigen
Client Challenge
JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
w3resource
w3resource.com › python-exercises › metaprogramming › python-metaprogramming-exercise-9.php
Python Code Generation: Transform Templates
December 21, 2024 - Learn to generate Python code from templates using the 'generate_code' function. Replace placeholders with provided values to create dynamic code. Code example & explanation included.
🌐
GitHub
github.com › pyscaffold › pyscaffold
GitHub - pyscaffold/pyscaffold: 🛠 Python project template generator with batteries included
🛠 Python project template generator with batteries included - pyscaffold/pyscaffold
Starred by 2.3K users
Forked by 185 users
Languages   Python
🌐
YouTube
youtube.com › codegpt
python code template generator - YouTube
Instantly Download or Run the code at https://codegive.com in software development, maintaining a consistent code structure and style is crucial for readabi...
Published   February 19, 2024
Views   44
🌐
Python
wiki.python.org › moin › Templating
Templating - Python Wiki
moody-templates - A fast, extensible templating engine for Python 3 with Django-like syntax. Myghty inspired by Perl's Mason, replaced by Mako and MyghtyUtils. Qpy provides a convenient mechanism for generating safely-quoted html text from python code.
Find elsewhere
🌐
Reddit
reddit.com › r/python › python code generation tools
r/Python on Reddit: Python Code Generation Tools
March 9, 2019 -

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.

Top answer
1 of 3
2
I'd probably use metaclasses if I had the kind of problem where code generation could actually help. The last time I saw a project using code generation where I thought that it ought have to used metaclasses was a tool to generate python code from swagger descriptions. Django's ORM is a good example where metaclasses were used to good effect (there are very few of those). In general I'm extremely suspicious of anybody who claims to have the kind of problem that requires either metaclasses or code generation would help though. It is suitable only for problems that are very, very, very abstract (e.g. like an ORM or REST API interaction framework). In 90% of cases I've seen the person attempting to use it vastly overgeneralized the problem that they actually had in an attempt to prepare for future problems never arose. In the process of doing that they then created a code clusterfuck that was hellishly hard to understand and debug (and in some cases I was left to clean up the mess). It wasn't simple and it can't, intrinsically, ever be simple (there is some language theory behind this). I could be convinced otherwise by describing your problem, but right now I'm 80% convinced that what you have described is an XY problem and that you're attempting to do this because you overgeneralized your problem. If you describe the actual problem you're solving it might help.
2 of 3
2
If you're looking for some codegen support stuffs in python, I'd give you some info Python std module ast AST to python source code emitter, unparse. https://github.com/python/cpython/blob/master/Tools/parser/unparse.py AST based scoping analyzer, https://github.com/thautwarm/scoping-resolver
🌐
GitHub
gist.github.com › 677b3dc3db0fd742d30b06ec31106fa5
Generate template files for python · GitHub
October 24, 2016 - Generate template files for python. GitHub Gist: instantly share code, notes, and snippets.
🌐
YouTube
youtube.com › watch
How to template files in Python ? (For better code management) - YouTube
An important tutorial on how to separate configuration out of your code, by using templates.#Python #templates #filetemplating☕ Buy me a coffee: https://www....
Published   May 23, 2022
🌐
OpenAI Developer Community
community.openai.com › api
Create a template code based on existing code base - API - OpenAI Developer Community
May 18, 2023 - I have a use case where I need to create an initial draft python code based on existing corpus of code. I am thinking whether retrieval augmented generation will work in this case. Not sure if text embeddings will do a g…
🌐
Ericmacedo
ericmacedo.com › generating-code-from-templates-using-python-and-jinja2.html
Generating code from templates using Python and Jinja2
You can adapt the template file to generate what you want/need. The testbench file we want to generate, should have a module name equal to tb_module_name and it must instantiate the module that will be tested and connect its ports to a interface _if. ... The name of a module and a list of port ...
🌐
GitHub
github.com › topics › template-generator
template-generator · GitHub Topics · GitHub
A tool to help automate template creation for artworks. ... Py_Maker is a simple script that generates a simple python project file structure with the necessary files.
🌐
Etlcpp
etlcpp.com › generators_tutorial.html
generators_tutorial
To this end, the ETL supplies 'generators' to enable the creation of customised variants of these template classes. The code generation is handled by a Python library called COG.
🌐
Microsoft Learn
learn.microsoft.com › en-us › visualstudio › python › quickstart-02-python-in-visual-studio-project-from-template
Quickstart: Create Python project from template - Visual Studio (Windows) | Microsoft Learn
April 18, 2024 - In this quickstart, you follow guided steps to quickly create a Flask application by using a built-in Python project template. Visual Studio makes it easy to develop Python projects with boilerplate code and functionality in templates for several Python frameworks, including Flask, Django, and Bottle.
🌐
Easy-Peasy.AI
easy-peasy.ai › templates › python-code-generator
AI Python Code Generator | Easy-Peasy.AI
Otherwise, it prints the error status code and response text. Remember to install the requests library if you haven't already by running pip install requests in your terminal or command prompt. ... import random def print_slow(text): for char in text: print(char, end='', flush=True) time.sleep(0.05) print() def game(): print_slow("You awake in a dimly lit room. It's eerily quiet, and a chilling breeze passes through the cracked window. The door is locked from the outside.
🌐
GitHub
gist.github.com › nhoffman › 3006600
Python script template · GitHub
Python script template. GitHub Gist: instantly share code, notes, and snippets.
🌐
Python
python.org › about › success › cog
Cog: A Code Generation Tool Written in Python
We could then use this general purpose tool to solve our specific generation problem. The tool I wrote is called Cog. Its requirements were: We needed to be able to perform interesting computation on the schema to create the code we needed. Cog would have to provide a powerful language to write the code generators in.