embody.substitution

Pattern matching and type-preserving substitution logic for templates.

This module provides the core substitution functionality for embody, supporting multiple syntaxes (${var}, {var}) with type preservation.

Functions

extract_template_vars(template[, syntax])

Extract all template variable names from a string.

is_exact_match(template[, syntax])

Check if template is exactly one variable placeholder (no other text).

safe_format(template, **params)

Safely format a string, leaving undefined placeholders unchanged.

substitute(template, params[, syntax, strict])

Perform type-preserving substitution on a template value.

substitute_all_syntaxes(template, params[, ...])

Apply substitution for multiple syntax styles.

Classes

SafeFormatter()

A string formatter that doesn't raise errors for missing keys.

SubstitutionSyntax()

Defines the syntax patterns for template variable substitution.

class embody.substitution.SafeFormatter[source]

Bases: Formatter

A string formatter that doesn’t raise errors for missing keys.

class embody.substitution.SubstitutionSyntax[source]

Bases: object

Defines the syntax patterns for template variable substitution.

classmethod get_pattern(syntax='dollar_brace')[source]

Get the regex pattern for the specified syntax.

Parameters:

syntax (str) – One of ‘dollar_brace’, ‘brace’, ‘double_bracket’

Return type:

Pattern

Returns:

Compiled regex pattern

Examples

>>> pattern = SubstitutionSyntax.get_pattern('dollar_brace')
>>> pattern.findall('${name} is ${age}')
['name', 'age']
embody.substitution.extract_template_vars(template, syntax='dollar_brace')[source]

Extract all template variable names from a string.

Parameters:
  • template (str) – Template string containing variables

  • syntax (str) – Variable syntax to use

Return type:

list[str]

Returns:

List of variable names found in the template

Examples

>>> extract_template_vars('Hello ${name}, you are ${age} years old')
['name', 'age']
>>> extract_template_vars('Hello {name}', syntax='brace')
['name']
embody.substitution.is_exact_match(template, syntax='dollar_brace')[source]

Check if template is exactly one variable placeholder (no other text).

This is crucial for type preservation. If the template is exactly ${var}, we should return the raw value, not convert it to a string.

Parameters:
  • template (str) – Template string to check

  • syntax (str) – Variable syntax to use

Return type:

Optional[str]

Returns:

Variable name if exact match, None otherwise

Examples

>>> is_exact_match('${name}')
'name'
>>> is_exact_match('Hello ${name}')
>>> is_exact_match('${count}')
'count'
embody.substitution.safe_format(template, **params)[source]

Safely format a string, leaving undefined placeholders unchanged.

Parameters:
  • template (str) – Template string

  • **params – Parameters for substitution

Return type:

str

Returns:

Formatted string

Examples

>>> safe_format('Hello {name}, you are {age}', name='Alice')
'Hello Alice, you are {age}'
embody.substitution.substitute(template, params, syntax='dollar_brace', strict=False)[source]

Perform type-preserving substitution on a template value.

This is the core substitution function. It handles:

  1. Exact match: ${var} -> returns params[‘var’] with type preserved

  2. Partial match: “Count is ${var}” -> string interpolation

  3. Non-string templates: returned unchanged

Parameters:
  • template (Any) – The template value (can be any type)

  • params (Dict[str, Any]) – Dictionary of parameters for substitution

  • syntax (str) – Variable syntax to use

  • strict (bool) – If True, raise KeyError for missing variables

Return type:

Any

Returns:

The substituted value with type preservation

Examples

>>> substitute('${count}', {'count': 42})
42
>>> substitute('Count: ${count}', {'count': 42})
'Count: 42'
>>> substitute('${active}', {'active': True})
True
>>> substitute('${items}', {'items': [1, 2, 3]})
[1, 2, 3]
>>> substitute(42, {})
42
embody.substitution.substitute_all_syntaxes(template, params, syntaxes=None, strict=False)[source]

Apply substitution for multiple syntax styles.

Useful when you want to support mixed syntax in templates.

Parameters:
  • template (Any) – The template value

  • params (Dict[str, Any]) – Dictionary of parameters

  • syntaxes (list[str]) – List of syntaxes to try (default: [‘dollar_brace’, ‘brace’])

  • strict (bool) – If True, raise errors for missing variables

Return type:

Any

Returns:

The substituted value

Examples

>>> substitute_all_syntaxes('${name} is {age}', {'name': 'Alice', 'age': 30})
'Alice is 30'