tstring-util
Utilities for Python 3.14 t-string.
Python 3.14 supports creating objects of type string.templatelib.Template by prefixing with a "t".
lazy rendering
def render(template: Template, ctx: dict[str, object] | None = None) -> str:
Provides ability to write t-string with function calls with deferred evaluation. Any callable marked !fn will consume as many following interpolations as its positional args, be invoked, and its stdout captured inline. Everything else is rendered in order.
ctx defaults to the locals and globals of the code calling render.
Example
from numbers import Number
from tstring import render, embed
def double(value:Number)->None:
print(f"twice {value} is {2*value}")
def test_lazy():
number = 1
flavor = 'spicy'
embedx = t'Call function {double:!fn} {number} {flavor}'
number = 2
r = render(embedx)
assert r == "Call function twice 2 is 4 spicy"
def embed(template: Template, ctx: dict[str, object] | None = None) -> str:
Provides ability to write t-string with function calls with deferred evaluation. Any callable marked !fn will consume as many following interpolations as its positional args, be invoked, and its return value captured and converted to string inline. Everything else is rendered in order.
ctx defaults to the locals and globals of the code calling embed.
Example
def return_double(value:Number)->Number:
return 2 * value
def test_lazy():
number = 2
flavor = 'spicy'
embedx = t'Call return {return_double:!fn} {number} {flavor}'
r = embed(embedx)
assert r == "Call return 4 spicy"
safe split
def safe_split(tmpl: Template,sep:str|None=None) -> list[str]:
Splits a t-string while keeping interpolations intact. This can be used to safely split a string into input for subprocess
Example
import subprocess
from tstring import safe_split
injection = '/tmp;rm -fr /'
command = t'ls -l {injection}'
clist = safe_split(command)
subprocess.run(clist)
returns ls: cannot access '/tmp;rm -fr /': No such file or directory
Note that using a non None separator may produce empty strings in list. See test_split.py for examples.
safe paths
path(string.templatelib.Template)->Path
Converts t-string to a path. If any interpolations have a NUL or path separator in them, ValueError is raised. A special case of the first character of the first element being a separator is permitted to make paths absolute.
Example
from tstring import path
config = '/etc'
p = path(t'{config}/systemd')
assert p.as_posix() == '/etc/systemd'
Invalid path:
no_good = 'bob/carol'
path(t'{no_good}')
raises ValueError Invalid character '/' in interpolation 'no_good'
Metadata
Release files for tstring-util 2.1
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 | |
|---|---|---|---|---|
| tstring_util-2.1.tar.gz | 8.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tstring_util-2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.1 kB
Release files / tstring_util-2.1.tar.gz
| Download URL | tstring_util-2.1.tar.gz |
|---|---|
| Size | 8.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
90e358f7c705fcc60ff4f7668134c1f8e82b1f69211229e823099312d7cbca31
|
|
BLAKE2b-256 checksum How to use checksums |
5e28dbce38c51521d2e417797291a71048149ffb796752ce25ba113ff315e159
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.12
|
Release files / tstring_util-2.1-py3-none-any.whl
| Download URL | tstring_util-2.1-py3-none-any.whl |
|---|---|
| Size | 10.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
70da98c70fd903d7647fdcd5f442805ab88ca203d08128ac3c4375f266264540
|
|
BLAKE2b-256 checksum How to use checksums |
ce050a5c96e2c660d7e06099aefe406c05fb41cb30445927be3ffe5ac2c814e7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.12
|
Security for tstring-util 2.1
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A 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.