Skip to main content

generate files from templates

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qr_generate-0.1.2.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qr_generate-0.1.2-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file qr_generate-0.1.2.tar.gz.

File metadata

  • Download URL: qr_generate-0.1.2.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for qr_generate-0.1.2.tar.gz
Algorithm Hash digest
SHA256 bba5c12be7ef013ebad08e0611ec385d44d2b3c346de53af9bb9b06d6ed093f0
MD5 9d90a53ddbb37062574f8fe2045c395d
BLAKE2b-256 13f4f10e43b0687930116990e4b91cf2152d7dc99d1b0b1c609cfa2636fd6d65

See more details on using hashes here.

Provenance

The following attestation bundles were made for qr_generate-0.1.2.tar.gz:

Publisher: release.yml on quadraturerules/generate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qr_generate-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: qr_generate-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for qr_generate-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 46fa20dbc960f2e8d9c4f512d2a6c71dcb37bfec02a7af6d13fd7ba5d8417153
MD5 22ad9c6f537452b0b73f9e72b96ca086
BLAKE2b-256 131d0920c9d010069276192a71bed67814854c95c520287cfae47d16cb0cab42

See more details on using hashes here.

Provenance

The following attestation bundles were made for qr_generate-0.1.2-py3-none-any.whl:

Publisher: release.yml on quadraturerules/generate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page