A Python utility for flexible variable substitution in templates, supporting complex data structures and nested access patterns.
The Parameter Substitution Utility provides a flexible way to substitute variables into templates using a familiar {{ variable_name }} syntax. It supports nested dictionaries, list indexing, and complex data navigation, along with error handling and a stringify filter.
Features
Simple variable substitution
Nested dictionary access using dot notation
Array indexing with zero-based indices
Complex data structure navigation (nested arrays and dictionaries)
JSON stringify filter
Robust error handling
Usage Examples
1. Basic String Substitution
from shared.utils.param_substitute import substitute_params
data = {"name": "John", "age": 30}
template = "Hello {{ name }}, you are {{ age }} years old"
result = substitute_params(data, template)
# => "Hello John, you are 30 years old"
2. Nested Dictionary Access
data = {"user": {"info": {"name": "Alice", "age": 25}}}
template = "{{ user.info.name }} is {{ user.info.age }}"
result = substitute_params(data, template)
# => "Alice is 25"
3. Array Operations
data = {"items": ["apple", "banana", "orange"]}
template = "{{ items[1] }}"
result = substitute_params(data, template)
# => "banana"