Scripting API¶
The Scripting API provides read-only access to DATAMIMIC context data from custom Python scripts. Use it to create reusable functions that can be called from script= expressions.
Note
This page documents the Python helper API available during expression evaluation. If you are looking for DSL variable lookup rules such as this.*, this.parent.*, or named nested paths inside <generate>, see Variable Scoping in Nested Generates.
DateTimeGenerator(reference=...) supports only the lookup-only subset: bare identifiers, this.<field>, this.parent.<field>, and root.<field>.
Overview¶
| Feature | Description |
|---|---|
| Purpose | Access context data from custom Python functions |
| Access | Read-only (returns copies, not live references) |
| Scope | Only works during expression evaluation |
Quick Start¶
1. Create a Python Script¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
2. Use in XML Model¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Datetime Helpers In Expressions¶
Datetime arithmetic is also available directly inside script=... expressions without imports.
add_days(dt, n)add_hours(dt, n)add_minutes(dt, n)add_seconds(dt, n)add_weeks(dt, n)add_months(dt, n)add_years(dt, n)date_offset(dt, '30d')start_of_day(dt),end_of_day(dt)start_of_month(dt),end_of_month(dt)start_of_year(dt),end_of_year(dt)
Datetime values in the evaluator also expose chainable methods such as issue_at_raw.add_days(30) and created_at.add_months(1).end_of_month().
1 2 3 4 | |
Important:
- Arithmetic is raw-only.
- After
outDateFormathas been applied, downstream expressions see a string, not a datetime. - If a formatted value is used by mistake, the runtime raises a typed script error and points back to the raw-value rule.
API Reference¶
Import¶
1 2 3 4 5 6 7 8 9 | |
Functions¶
load_memstore(product_name) -> list[dict]¶
Load all records from a memstore.
1 2 3 | |
load_memstore_page(product_name, skip, limit) -> list[dict]¶
Load a page of records from memstore (for large datasets).
1 2 | |
skip: Number of records to skip (>= 0)limit: Maximum records to return (> 0)
get_variable(name, default=None) -> Any¶
Get a variable value from the current record context.
1 2 3 | |
get_property(name, default=None) -> Any¶
Get a property from the root context (e.g., from .properties files).
1 2 | |
get_current_product() -> dict | None¶
Get a copy of the current product (all keys generated so far).
1 2 3 | |
get_record_position() -> int | None¶
Get the 1-based position of the current record.
1 2 | |
get_context_info() -> dict¶
Get a read-only snapshot of context metadata.
1 2 3 | |
Returns:
task_id: Current task identifierdescriptor_name: Name of the XML descriptorrecord_position: Current record number (1-based)current_name: Name of the current generate block
Important: Evaluation-Only¶
Call Inside Functions Only
The Scripting API only works during expression evaluation. Do not call these functions at module import time.
Correct:
1 2 3 | |
Incorrect:
1 2 | |
Common Patterns¶
Lookup Tables¶
1 2 3 4 5 6 7 8 9 10 11 | |
Cache Timing
The cache is populated on first function call. Ensure the memstore is fully populated before calling lookup functions. In your XML model, place the <generate target="mem"> statement before any generate that uses the lookup.
Conditional Logic¶
1 2 3 4 5 6 7 8 9 10 11 | |
Cross-Record Aggregation¶
1 2 3 4 5 6 7 8 9 | |
Dynamic Formatting¶
1 2 3 4 5 6 7 8 9 | |
Best Practices¶
- Define functions, don't execute at import - API only works during evaluation
- Use default values - Handle missing variables gracefully
- Cache expensive lookups - Use module-level caches for repeated lookups
- Keep functions pure - Don't modify context; return computed values
- Document your functions - Help other team members understand usage
Error Codes¶
| Code | Description |
|---|---|
| I858 | Scripting API called outside expression evaluation |
| I859 | Invalid product_name (must be non-empty string) |
| I860 | Invalid pagination (skip must be >= 0, limit must be > 0) |
See Error Codes Reference for more details.