Skip to main content

Utilities for tstring

Project description

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'

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

tstring_util-2.1.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

tstring_util-2.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file tstring_util-2.1.tar.gz.

File metadata

  • Download URL: tstring_util-2.1.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for tstring_util-2.1.tar.gz
Algorithm Hash digest
SHA256 90e358f7c705fcc60ff4f7668134c1f8e82b1f69211229e823099312d7cbca31
MD5 b97f3bd8a35478e3282ad48c506f302e
BLAKE2b-256 5e28dbce38c51521d2e417797291a71048149ffb796752ce25ba113ff315e159

See more details on using hashes here.

File details

Details for the file tstring_util-2.1-py3-none-any.whl.

File metadata

  • Download URL: tstring_util-2.1-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for tstring_util-2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 70da98c70fd903d7647fdcd5f442805ab88ca203d08128ac3c4375f266264540
MD5 dfbbedcac5417a6d47d77176aad7a294
BLAKE2b-256 ce050a5c96e2c660d7e06099aefe406c05fb41cb30445927be3ffe5ac2c814e7

See more details on using hashes here.

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