refex.python.syntactic_template

Syntax-aware Python substitution templates, as described in Python Patterns and Templates.

The core problem with lexical or textual substitution of Python code, as with e.g. refex.formatting.ShTemplate, is that the substitution can be unintentionally wrong. For example:

If you replace f($x) with $x, what if $x contains a newline?

If you replace $a with $a * 2, what if $a is 1 + 2?

The template classes here aim to make replacements that match the intended syntax – i.e. the structure of the template – and will parenthesize as necessary.

class PythonExprTemplate(template: str)

A template for a Python expression.

class PythonStmtTemplate(template: str)

A template for a single Python statement.