embody.strategies¶
Traversal strategies for template embodiment.
This module provides different strategies for traversing and embodying templates:
RecursiveVisitorEngine: Dynamic traversal using the Visitor pattern
CompiledPathEngine: Pre-compiled path-based traversal for performance
Functions
|
Get an embodiment engine by name. |
Classes
|
Base class for embodiment engines. |
|
Pre-compiled path-based traversal strategy. |
|
Iterative stack-based traversal strategy. |
|
Dynamic traversal strategy using the Visitor pattern. |
Exceptions
Raised when dynamic keys resolve to the same value. |
- class embody.strategies.BaseEmbodimentEngine(syntax='dollar_brace', strict=False, key_collision='error')[source]¶
Bases:
objectBase class for embodiment engines.
- class embody.strategies.CompiledPathEngine(*args, **kwargs)[source]¶
Bases:
BaseEmbodimentEnginePre-compiled path-based traversal strategy.
This engine flattens the template into a path map, performs substitutions, and then reconstructs the structure. It’s best for:
Templates embodied repeatedly (e.g., API responses)
Static templates with known structure
Performance-critical paths
The compilation happens once, then embodiment is a linear scan.
Examples
>>> engine = CompiledPathEngine() >>> template = {'a': {'b': '${x}'}, 'c': '${y}'} >>> compiled = engine.compile(template) >>> engine.embody_compiled(compiled, {'x': 1, 'y': 2}) {'a': {'b': 1}, 'c': 2}
- compile(template)[source]¶
Compile a template into a flat path map.
Examples
>>> engine = CompiledPathEngine() >>> template = {'a': {'b': '${x}'}} >>> compiled = engine.compile(template) >>> compiled['flat'] {('a', 'b'): '${x}'}
- class embody.strategies.IterativeStackEngine(syntax='dollar_brace', strict=False, key_collision='error')[source]¶
Bases:
BaseEmbodimentEngineIterative stack-based traversal strategy.
This engine uses an explicit stack instead of recursion, which is more memory-efficient for very deep structures and avoids Python’s recursion limit.
Best for:
Very deeply nested structures (>100 levels)
When recursion depth is a concern
- exception embody.strategies.KeyCollisionError[source]¶
Bases:
ExceptionRaised when dynamic keys resolve to the same value.
- class embody.strategies.RecursiveVisitorEngine(syntax='dollar_brace', strict=False, key_collision='error')[source]¶
Bases:
BaseEmbodimentEngineDynamic traversal strategy using the Visitor pattern.
This engine recursively walks the template structure at runtime, visiting each node and performing substitutions. It’s best for:
One-off templates
Dynamic templates with conditional logic
Templates where structure changes based on parameters
Examples
>>> engine = RecursiveVisitorEngine() >>> template = {'greeting': 'Hello ${name}', 'count': '${num}'} >>> engine.embody(template, {'name': 'Alice', 'num': 42}) {'greeting': 'Hello Alice', 'count': 42}
- embody.strategies.get_engine(strategy='recursive', **kwargs)[source]¶
Get an embodiment engine by name.
- Parameters:
strategy (
str) – Name of strategy (‘recursive’, ‘compiled’, ‘iterative’)**kwargs – Arguments to pass to engine constructor
- Return type:
- Returns:
Embodiment engine instance
Examples
>>> engine = get_engine('recursive', syntax='brace') >>> isinstance(engine, RecursiveVisitorEngine) True