Skip to main content

Template string utilities and backports

Project description

tstr - PEP 750 Template String Utilities & Backports

PyPI Version Python Version License Tests Coverage Status

tstr provides utilities for working with PEP 750 template strings, along with robust backports for older Python versions.

This library streamlines template string usage by offering practical helpers and common processing patterns. For Python versions prior to 3.14, tstr seamlessly backports template string functionality.

Installation

Install tstr via pip:

pip install tstr

Features

  • render (alias: f): Render a template to a string, mimicking f-string behavior.
  • generate_template (alias: t): Create a Template object from a string and context. This function is especially useful on Python versions that do not support template strings natively.
  • bind: Apply a function to all interpolations and join all the parts.
  • binder: Decorator to create template processors from an interpolation processor.
  • normalize / normalize_str: Apply conversion and format to value.
  • convert: Apply conversion to a value.
  • template_eq: Check if two templates are equivalent.
  • interpolation_replace: Create a new Interpolation by selectively replacing attributes of an existing one.
  • dedent: textwrap.dedent for template strings.
  • template_from_parts: Construct template strings from iterable.

This library also provides several useful extensions where template strings can be effectively utilized. These extensions are available in the tstr.ext submodule, and below is a list with brief descriptions:

  • ext._html: Render templates with HTML escaping.
  • ext._sqlite: Safely execute SQL with templates, preventing SQL injection attacks.
  • ext._logging: Enable Python's logging module to accept template strings.

For more details, see the API documentation (coming soon) or docstrings.

Compatibility

tstr automatically detects native template string support (PEP 750):

  • Python 3.14+: Uses native template strings.
  • Python 3.10–3.13: Uses a compatible backport.

Use the TEMPLATE_STRING_SUPPORTED constant to check if template strings are natively supported in your Python version.

For details on how the compatible backport of template string works and what similarities and differences it has with native template strings, see the compatible template strings (coming soon) documentation.

Contributing

This project welcomes contributions of all kinds from anyone willing to help improve it! Whether you're fixing a typo in documentation, reporting a bug, proposing a new feature, or implementing code changes - every contribution matters and is highly appreciated.

Releases

  • 0.4.0: Support debug specifier on t(), use new build script, improve docs
  • 0.3.0: Revamp various things
  • 0.2.0: Rename html_render to render_html, add _logging module, fix various bugs and improve documentation
  • 0.1.1.post1: Initial release

License

This library is licensed under the Apache License 2.0.

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

tstr-0.4.1b1.post1.tar.gz (47.2 kB view details)

Uploaded Source

Built Distribution

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

tstr-0.4.1b1.post1-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file tstr-0.4.1b1.post1.tar.gz.

File metadata

  • Download URL: tstr-0.4.1b1.post1.tar.gz
  • Upload date:
  • Size: 47.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for tstr-0.4.1b1.post1.tar.gz
Algorithm Hash digest
SHA256 67f58c9aa039397ef99a5318749866009e92313055c24619cca5e576b14753b6
MD5 4191ed9f6e1e6112dc6a4a02965b6dda
BLAKE2b-256 e48ae9a7e66143aadcec4b233551e3ecc1a85bfaf501db370da98ffde0f40aea

See more details on using hashes here.

Provenance

The following attestation bundles were made for tstr-0.4.1b1.post1.tar.gz:

Publisher: build.yml on ilotoki0804/tstr

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

File details

Details for the file tstr-0.4.1b1.post1-py3-none-any.whl.

File metadata

  • Download URL: tstr-0.4.1b1.post1-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for tstr-0.4.1b1.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 27343b77ae8783ba6b20752cac883c425621794bf91f1ed7c97bb5ae228a92fe
MD5 27608385acb21ecd369a306bc3a10c6c
BLAKE2b-256 66cc8cce6dbefb42d37bf5b39e427650627547d621029ee087593c2e2a3c1f55

See more details on using hashes here.

Provenance

The following attestation bundles were made for tstr-0.4.1b1.post1-py3-none-any.whl:

Publisher: build.yml on ilotoki0804/tstr

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