Utilities for tstring
Project description
tstring-util
Utlities for Python 3.14 t-string.
Python 3.14 supports creating objects of type string.templatelib.Template by prefixing with a "t".
lazy rendering
render(string.templatelib.Template)->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.
Example
from tstring import render
def hello(name):
print(f"hello {name}")
def test_lazy():
who = 'bob'
flavor = 'spicy'
embedx = t'Call function {hello:!fn} {who} {flavor}'
who = 'jane'
r = render(embedx)
assert r == "Call function hello jane 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'
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tstring_util-1.0.tar.gz.
File metadata
- Download URL: tstring_util-1.0.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ad777da4c809eab70b7a623eeaaa8e9a9d1a8bbbb746bb9a367c9801998a49ed
|
|
| MD5 |
128cccde96a273c8133ad1635be823ed
|
|
| BLAKE2b-256 |
33a6bd8cdedf28b97162e181129d36376dc821eb1882be6e9dc6161fa1edb0fc
|
File details
Details for the file tstring_util-1.0-py3-none-any.whl.
File metadata
- Download URL: tstring_util-1.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a71ccbb725c54b921e56d8007a71ffce9c83cc7af3f99b813b8814e3d7425d34
|
|
| MD5 |
c3babb67c0e5b0b92e4224bc3e63061f
|
|
| BLAKE2b-256 |
ab3b4c8239df20499b0ea8d365965cc04abebc25d2ca4a3223cd76e825f76fef
|