pyfcstm.render.func
Module for processing items to objects with Jinja2 templates and dynamic imports.
This module provides functionality to convert dictionary configurations into callable objects, templates, or imported objects based on their specified type. It supports creating template renderers, importing objects from modules, and extracting values from configuration dictionaries.
process_item_to_object
- pyfcstm.render.func.process_item_to_object(f, env: Environment)[source]
Process a configuration item into an object based on its type.
This function converts dictionary configurations into different types of objects:
‘template’: Creates a callable template renderer function
‘import’: Imports an object from a specified module
‘value’: Extracts a value from the configuration
For other types or non-dictionary inputs, returns the input unchanged
- Parameters:
f (dict or any) – The configuration item to process, can be a dictionary or any other type
env (jinja2.Environment) – The Jinja2 environment used for template rendering
- Returns:
The processed object (function, imported object, value, or unchanged input)
- Return type:
any
Example:
>>> env = jinja2.Environment() >>> # Create a template renderer >>> template_config = {'type': 'template', 'template': 'Hello {{ name }}', 'params': ['name']} >>> renderer = process_item_to_object(template_config, env) >>> renderer('World') 'Hello World' >>> # Import an object >>> import_config = {'type': 'import', 'from': 'math.sqrt'} >>> sqrt_fn = process_item_to_object(import_config, env) >>> sqrt_fn(16) 4.0