PythonScriptActivity
Category: Common Steps Namespace: CMS.Plugin.FlowManagement.Domain.FlowBusiness.Activitys Base Class: ActionActivity Module: FlowManagement.Domain
Overview
PythonScriptActivity is a node used to execute Python scripts directly within a flow. Inheriting from ActionActivity, it uses the IronPython engine to execute inline Python script code at runtime. This node supports parameter mapping and result extraction, allowing flexible data calculation, transformation, and processing.
Unlike ScriptCallActivity, PythonScriptActivity allows writing Python code directly in the node configuration without pre-configuration in script management, suitable for simple data processing and calculation scenarios.
Business Scenarios
Applicable Scenarios
- Data Calculation: Execute mathematical calculations, statistical analysis, etc.
- Data Transformation: String processing, format conversion, encoding/decoding
- Condition Judgment: Complex business rule judgment
- Data Validation: Data format validation, range checking
- Rapid Prototyping: Quickly implement and test business logic
Role in the System
PythonScriptActivity acts as an inline script executor in the LMES flow system:
- Directly executes configured Python scripts
- Supports passing parameters from flow context
- Supports writing execution results back to flow context
- Uses IronPython engine, compatible with Python 2.x syntax
Configuration Instructions
Basic Configuration
| Property Name | Type | Required | Default Value | Description |
|---|---|---|---|---|
| Script | string | Yes | - | Execution script, Python script for calculation |
| Mapping | ParameterMapCollection | No | Empty Collection | Parameter matching, pass flow parameters as call arguments |
| ResultParameter | ResultParameterCollection | No | Empty Collection | Result parameters, names to store results in flow context |
| Action | ActionType | No | Execute | Execution timing (Inherited from ActionActivity) |
| IsSynchronous | bool | No | true | Whether to execute synchronously (Inherited from ActionActivity) |
Configuration Details
Script
Description: Python script code to run during flow execution. Script executes using IronPython engine, compatible with Python 2.x syntax.
Value Range: String, valid Python code
Notes:
- Uses IronPython engine, some Python 3.x features may not be supported
- Script automatically adds current working directory to sys.path
- Can use import to import standard libraries and custom modules
Mapping
Description: Parameter mapping configuration, defining how to pass parameters from flow context to Python script.
Parameter Mapping Model:
- Source: Parameter name in flow context (Data Source)
- Target: Variable name in Python script (Target Variable)
Notes:
- Mapped parameters will be available as Python variables in the script
- Parameter types are automatically converted to corresponding Python types
ResultParameter
Description: Result parameter configuration, defining how to write Python script execution results back to flow context.
Result Parameter Model:
- Name: Variable name in Python script, also the key name written to flow context
Notes:
- After script execution, value is retrieved from Python scope for the specified variable
- Results are written to flow context in order
Flow Context
Input Parameters
| Parameter Name | Type | Description |
|---|---|---|
| Mapping.Source | any | Parameters passed to script via Mapping configuration |
Output Parameters
| Parameter Name | Type | Description |
|---|---|---|
| ResultParameter.Name | any | Results retrieved from script via ResultParameter configuration |
Usage Examples
Basic Example: Simple Calculation
{
"Type": "PythonScriptActivity",
"Name": "Calculate Total",
"Alias": "CalcTotal",
"Script": "total = price * quantity",
"Mapping": [
{ "Source": "Price_Value", "Target": "price" },
{ "Source": "Quantity_Value", "Target": "quantity" }
],
"ResultParameter": [
{ "Name": "total" }
]
}
String Processing Example
{
"Type": "PythonScriptActivity",
"Name": "Format Barcode",
"Alias": "FormatBarcode",
"Script": "formatted = prefix + barcode.zfill(10) + suffix",
"Mapping": [
{ "Source": "Barcode_Value", "Target": "barcode" },
{ "Source": "Prefix_Value", "Target": "prefix" },
{ "Source": "Suffix_Value", "Target": "suffix" }
],
"ResultParameter": [
{ "Name": "formatted" }
]
}
Notes
- ⚠️ IronPython Limitations: Uses IronPython engine, some Python 3.x features not supported
- ⚠️ Performance: New Python engine created for each execution, frequent calls may affect performance
- ⚠️ Type Conversion: Parameters automatically convert types between .NET and Python
- ⚠️ Exception Handling: Script execution exceptions interrupt flow, add exception handling in script
- ⚠️ Security: Scripts can access system resources, note security risks
Related Nodes
- ActionActivity: Base class for Python script node
- ScriptCallActivity: Node to call pre-configured scripts
- MethodCallActivity: Node to call C# methods
- ApiCallActivity: Node to call APIs
Update History
| Date | Version | Description |
|---|---|---|
| 2025-11-28 | 1.0 | Initial version |
Last updated: 2025-11-28