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

Example

from tstring import render
def double(value):
    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"

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-1.1.tar.gz (7.9 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-1.1-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tstring_util-1.1.tar.gz
Algorithm Hash digest
SHA256 ecfe5a0f8d83600592c176066d3f889712b5d0e04bfecd12856a4ab2f059f332
MD5 d02461eeb934043c174b569e0364b80b
BLAKE2b-256 c34673d1febf768353e26d8d9332f2b2bf230adf71f7ea2451edb8e496a210e3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tstring_util-1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.6 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-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6a88d188bc629963b264693c2cda9836693cd431e852328fe25c8ff579353c72
MD5 5e95f030088ccb48a4a8ea2129f327c2
BLAKE2b-256 bc54ee4ace7a904ea6257503dd2c096ed0227b4c49abf66fda8865b502b42788

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