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

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 and return captured inline. Everything else is rendered in order.

Return values of None from called functions are ignored. Not None returns are converted using the str() function.

Example

from numbers import Number
from tstring import render

def double(value:Number)->None:
    print(f"twice {value} is {2*value}")

def return_double(value:Number)->Number:
    return 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"

    embedx = t'Call return {return_double:!fn} {number} {flavor}'
    r = render(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.0.tar.gz (8.3 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.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tstring_util-2.0.tar.gz
  • Upload date:
  • Size: 8.3 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.0.tar.gz
Algorithm Hash digest
SHA256 5f7227a2fce485d364d14d4495ba361defab716687809f13ecc71d4208ea2f38
MD5 7242fe99a4245a85ce4736b9bd9c4c93
BLAKE2b-256 2709fdb0a83a36385340441b27426676b3ba492762ebbe69c5182a7016323866

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tstring_util-2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95824d2d4c1394632a23fe887b060bc3e5b89432200cc7e54c4d8179554e0d27
MD5 462bc0e439d21aaa5b1a461d3d290443
BLAKE2b-256 85b528534c39e6e889b554056843baed99e0eb219822bc13f58365c11c272a04

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