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 all template variable names from a string. |
|
Check if template is exactly one variable placeholder (no other text). |
|
Safely format a string, leaving undefined placeholders unchanged. |
|
Perform type-preserving substitution on a template value. |
|
Apply substitution for multiple syntax styles. |
Classes
A string formatter that doesn't raise errors for missing keys. |
|
Defines the syntax patterns for template variable substitution. |
- class embody.substitution.SafeFormatter[source]¶
Bases:
FormatterA string formatter that doesn’t raise errors for missing keys.
- class embody.substitution.SubstitutionSyntax[source]¶
Bases:
objectDefines 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:
- 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:
- Return type:
- 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:
- Return type:
- 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:
- 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:
Exact match: ${var} -> returns params[‘var’] with type preserved
Partial match: “Count is ${var}” -> string interpolation
Non-string templates: returned unchanged
- Parameters:
- Return type:
- 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:
- Return type:
- Returns:
The substituted value
Examples
>>> substitute_all_syntaxes('${name} is {age}', {'name': 'Alice', 'age': 30}) 'Alice is 30'