Python Code
Type:
python_exec
• Category:flow
• Tags:python
,exec
,sandbox
Description
Execute limited Python code (numpy, pandas, math)
Parameters
Name | Type | Description | Required | Default |
---|---|---|---|---|
code | code | Multiline Python code | no | |
persistent | boolean | Use persistent worker process (faster for multiple calls) | no | false |
Help
Overview
This worker provides a sandboxed environment for executing short Python snippets that rely on the core scientific libraries NumPy, pandas, and the standard math module. It is designed for quick calculations on financial market data, such as price series transformations, statistical summaries, or simple model evaluations. The execution is deliberately limited to keep the runtime safe and deterministic, while still offering enough functionality for most data‑analysis tasks in finance.
Inputs
code
(string, required) – A multiline Python script. The script may importnumpy
,pandas
, andmath
, and should contain the logic you want to run. The final result should be assigned to a variable namedresult
(or printed) so the worker can return it.persistent
(boolean, optional, default = false) – When set totrue
, the worker keeps the Python interpreter alive across multiple calls. This reduces start‑up overhead and is useful when you need to run several related snippets in succession (e.g., loading a large DataFrame once and reusing it).
Minimal Example Usage
**Request**
{
"code": "import pandas as pd\nprices = pd.Series([101, 102, 98, 105])\nresult = prices.pct_change().fillna(0).tolist()",
"persistent": false
}
Response
{
"result": [0.0, 0.009900990099009901, -0.0392156862745098, 0.07142857142857144]
}
In this example, the snippet imports pandas, creates a simple price series, computes the percentage change for each entry, replaces the initial NaN
with 0
, and returns the list of returns. Because persistent
is false
, the worker starts a fresh interpreter for this single call and shuts it down afterward.
Additional Notes
- Only the whitelisted libraries (
numpy
,pandas
,math
) are available; attempts to import other modules will raise an error. - The worker runs each script in isolation, so variables defined in one call are not retained unless
persistent
is enabled. - For reproducibility, avoid relying on external state (e.g., network access or file I/O) within the code block.
This documentation provides a concise yet complete guide to using the Python execution worker for financial data processing tasks.