# Python - Execute Python Code Action
This action executes a custom Python script.
# Limitations
When using this action, note that:
- Scripts can be a maximum of 1MB. While scripts that exceed this limit may run, they could cause memory issues.
- This action times out after 90 seconds
- User-provided libraries aren't currently supported. Refer to the Python connector overview for a list of supported libraries.
# Input
Field | Description |
---|---|
Name | Enter a brief description of the step. For example: Transform dates |
Input fields | Define the input fields to pass to your code. You can define the input by manually adding fields one at a time or providing a JSON sample.
Note: Date and datetime data will be passed as string . Check out an example.
|
Output fields | Define the fields that will create the Output datatree. You can define the output by manually adding fields one at a time or providing a JSON sample. |
Code | Your custom Python script. When writing your script, note that:
|
# Examples
# Working with input and output data
In this example, we'll demonstrate how Input fields data can be accessed in your script then passed to your defined Output fields.
Let's say the configuration for our action looks like this:
Input Fields | Output Fields |
---|---|
The following fields are defined as Input fields for the action:
| The following fields are defined as Output fields for the action:
|
In this example, the parameter in our main
function is named input
. We'll use this to access the values of our action's defined Input fields.
To pass the resulting data to our Output fields, we'll include a dictionary with keys that match the output schema.
import numpy as np
def main(input):
# # Access the values of the action's Input fields
x = input['x']
y = input['y']
z = input['z']
size = (x, y, z)
# Create a 3D NumPy array with random integers
my3dArray = np.random.randint(low=0, high=100, size=size, dtype=int)
print("/nRandom array/n", my3dArray)
# Keep only elements that are multiples of 5
modified3dArray = np.where(my3dArray % 5 == 0, my3dArray, 0)
print("\Only multiples of 5\n", modified3dArray)
shape = str(modified3dArray.shape)
dimensions = modified3dArray.ndim
# Pass shape and dimension values to Output fields
return {"shape": shape, "dimensions": dimensions}
# Working with datetime data
In this example, we'll demonstrate how to work with datetime
data in your Python scripts.
Input fields typed as Date
will be passed as strings
to your Python code. To ensure data is formatted as you expect, we recommend accounting for this in your code.
import datetime as dt
def main(input)
when = dt.datetime.fromisoformat(input['when'])
# use datetime object when
# ...
# convert to custom format
when_converted = when.strftime("%d %b %Y %H:%M:%S")
return {"when": when_converted}
# Output
Every field defined in the Output fields section creates a datapill in the action's Output datatree.
For example: Let's say we defined the following as Output fields:
- shape (
integer
) - dimensions (
integer
)
In this case, the action's Output datatree will look like this:
└── Output
├── shape (integer)
└── dimensions (integer)
Need help passing data to your Output fields? Check out the example.
Last updated: 3/21/2023, 4:01:10 PM