Submodule template

This file originates from the file beets/util/functemplate.py of the beets project.

This module implements a string formatter based on the standard PEP 292 string.Template class extended with function calls. Variables, as with string.Template, are indicated with $ and functions are delimited with %.

This module assumes that everything is Unicode: the template and the substitution values. Bytestrings are not supported. Also, the templates always behave like the safe_substitute method in the standard library: unknown symbols are left intact.

This is sort of like a tiny, horrible degeneration of a real templating engine like Jinja2 or Mustache.

class tmep.template.Template(template: str)[source]

A string template, including text, Symbols, and Calls.

expr: Expression
original: str
interpret(values: Dict[str, Any] = {}, functions: Dict[str, Callable[[...], str | int]] = {}) str[source]

Like substitute, but forces the interpreter (rather than the compiled version) to be used. The interpreter includes exception-handling code for missing variables and buggy template functions but is much slower.

substitute(values: Dict[str, Any] = {}, functions: Dict[str, Callable[[...], str | int]] = {}) str[source]

Evaluate the template given the values and functions.

translate() Callable[[...], str][source]

Compile the template to a Python function.