Reusables
The Reusables module contains template functions, filters, and decorators for workflow parameter templating and function registration.
Overview
This module provides:
- Template rendering: Dynamic parameter substitution with
${{ }}
syntax - Filter functions: Data transformation functions for template values
- Function registration: Decorator-based system for workflow callable functions
- Argument parsing: Utilities for parsing and validating function arguments
Template Functions
str2template
Converts a string with template syntax to its resolved value.
Template Syntax
from ddeutil.workflow.reusables import str2template
params = {
"name": "John",
"date": datetime(2024, 1, 1)
}
# Basic templating
result = str2template("Hello ${{ params.name }}", params)
# Output: "Hello John"
# With filters
result = str2template("Date: ${{ params.date | fmt('%Y-%m-%d') }}", params)
# Output: "Date: 2024-01-01"
param2template
Recursively processes nested data structures for template resolution.
Nested Templates
Filter Functions
Built-in Filters
Filter | Description | Example |
---|---|---|
abs |
Absolute value | ${{ -5 \| abs }} → 5 |
str |
Convert to string | ${{ 123 \| str }} → "123" |
int |
Convert to integer | ${{ "123" \| int }} → 123 |
upper |
Uppercase string | ${{ "hello" \| upper }} → "HELLO" |
lower |
Lowercase string | ${{ "HELLO" \| lower }} → "hello" |
title |
Title case | ${{ "hello world" \| title }} → "Hello World" |
Custom Filters
@custom_filter
Decorator for creating custom filter functions.
Custom Filter
Built-in Custom Filters
fmt
Formats datetime objects using strftime patterns.
Date Formatting
coalesce
Returns the first non-None value or a default.
Coalesce
getitem
Gets item from dictionary with optional default.
Get Item
getindex
Gets item from list by index.
Get Index
Function Registration
@tag
Decorator for registering workflow callable functions.
Function Registration
Registry Functions
make_registry
Creates a registry of tagged functions from specified modules.
Registry Creation
extract_call
Extracts and validates function calls from workflow strings.
Call Extraction
Utility Functions
has_template
Checks if a value contains template syntax.
Template Detection
get_args_const
Parses function call expressions to extract arguments.
Argument Parsing
Configuration
Reusables behavior can be configured through environment variables:
Variable | Default | Description |
---|---|---|
WORKFLOW_CORE_REGISTRY_FILTER |
[] |
List of modules to search for filter functions |
WORKFLOW_CORE_REGISTRY_CALLER |
[] |
List of modules to search for callable functions |
Performance
Filter and tag registries are cached at module level for optimal performance. Use make_filter_registry()
and make_registry()
to rebuild registries when needed.