Generate files from templates
This code can be used to generate plain text files from templates.
Installing
To install the latest release from PyPI, run:
pip install qr-generate
To install the latest code from GitHub, run:
pip install git+https://github.com/quadraturerules/generate.git
Syntax
The following syntax can be used to generate text:
- For loops use the syntax
{{for VARIABLE in LIST}}and{{end for}} - Ifs use the syntax
{{if CONDITION}}and{{end if}}
Examples
Looping in a file
To generate a file that prints each value in a list, the following template can be written:
template = """{{for v in values}}
print({{v}})
{{end for}}"""
A python script can then be used to generate the file:
import generate
t = generate.parse(template)
values = [
generate.substitute.Float(i)
for i in [1.0, 4.0, 6.0]
]
print(t.substitute(loop_targets={"values": values}))
The items in the list values passed in as a loop target must be a subclass of
Substitutor.
Templating with a folder
Alternatively, you could create a folder called input_code and create a file inside it containing the file code.py with contents:
{{for v in values}}
print({{v}})
{{end for}}
and create a file in the directory called __gen__.py with contents:
import generate
loop_targets = {"values": [
generate.substitute.Float(i)
for i in [1.0, 4.0, 6.0]
]}
You can then run:
python -m generate input_code output_code
This would create a directory called output_code containing a file code.py with the template replacements having been made.
Generating multiple files from a .template file
When templating using a folder, a file for each entry in a list can be created by using a .template file. For example, if a file called
p.template was added to the input_code directory with the contents
--
template: v in values
filename: p-{{v}}.py
--
print({{v}})
You can then run:
python -m generate input_code output_code
This would generate files called p-1.0.py, p-4.0py, and p-6.0.py.
Larger example
A larger example of the use of this library can be found by looking at the code used to generate the quadraturerules libraries associated with the online encylopedia of quadrature rules.
Metadata
Release files for qr-generate 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| qr_generate-0.1.2.tar.gz | 8.5 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| qr_generate-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.9 kB
Release files / qr_generate-0.1.2.tar.gz
| Download URL | qr_generate-0.1.2.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bba5c12be7ef013ebad08e0611ec385d44d2b3c346de53af9bb9b06d6ed093f0
|
|
BLAKE2b-256 checksum How to use checksums |
13f4f10e43b0687930116990e4b91cf2152d7dc99d1b0b1c609cfa2636fd6d65
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 2, 2025.
Transparency logRelease files / qr_generate-0.1.2-py3-none-any.whl
| Download URL | qr_generate-0.1.2-py3-none-any.whl |
|---|---|
| Size | 8.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
46fa20dbc960f2e8d9c4f512d2a6c71dcb37bfec02a7af6d13fd7ba5d8417153
|
|
BLAKE2b-256 checksum How to use checksums |
131d0920c9d010069276192a71bed67814854c95c520287cfae47d16cb0cab42
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 2, 2025.
Transparency logSecurity for qr-generate 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for qr_generate-0.1.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 2, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.