Notes on using Python

The Python core connector offers a single operation:

  • Execute Script: Executes Python code

Although the connector offers just one operation, it's important to consider some key points before using it.

To grasp the fundamental concepts of the Python script connector, let's examine a scenario whereby data from a Webhook trigger and an Object helper are concatenated.

The workflow is receiving

  • The itemName data from the Webhook trigger { "itemName": "Sauce Labs Backpack" }

  • The itemDescription from the object helper. { "itemDescription": "carry all the things with the sleek, stream lined Sly Pack that melds uncompromising style with unequaled laptop and tablet protection." }

Using the following script the workflow concatenates the data:

1
'''
2
You can reference the input variables using input["NAME"]
3
'''
4
def executeScript(input):
5
itemName = input ["itemName"]
6
itemDescription = input ["itemDescription"]
7
8
concatenated_string = itemName + ":" + " " + itemDescription
9
10
return concatenated_string
11

Now let's understand the steps involved one by one:

Building the script
Copy

  1. def executeScript(input): The line marks the beginning of a function definition. The function is named executeScript and takes one parameter, input. The function's purpose is to process the input data, execute specific operations on it, and subsequently provide the resulting output.

  2. Input variables: Within the function, the values of the variables itemName and itemDescription are extracted from the input dictionary using their corresponding keys.

    For detailed information, refer to the Input Variables section below.

  3. return statement: The content returned within the script dictates what is accessible for subsequent connector steps to utilize. For example, return concatenated_string.

  4. In certain scenarios, you may require the use of supported modules in your script, such as itertools. However, there's no need to import them explicitly as this will lead to an error message.

  5. Make sure to indent your code properly. Python uses indentation to define the scope of statements, such as those within loops, conditionals, functions, and classes.

Input variables
Copy

To access the data provided to the script, we first define variables to hold this data.

In the example below, we define two variables, itemName and itemDescription, which will be used to store the data received from the Webhook trigger and object helper, respectively.

By specifying the keys itemName and itemDescription, the script retrieves the corresponding values assigned to those keys within the input dictionary.

itemName = input["itemName"]

itemDescription = input["itemDescription"]

Built-in Feature support
Copy

Python connector's V1.0 supports all of the in-built functionality for Python v3.8 excluding the following:

  • Python's Format function is not permitted as it is not in alignment with the purpose of the connector itself.

  • Since only direct scripting is supported with the connector, Class definitions are not possible.

Python connector's V2.0 supports all of the in-built functionality for Python 3.11 excluding the following:

Supported Modules
Copy

The following modules are supported from V2.0:

Modules

Description

Non-supported Methods

math

Mathematical functions defined by the C standard

cmath

Mathematical functions for complex numbers

random

Generate pseudo-random numbers

statistics

Provides functions for calculating mathematical statistics of numeric (Real-valued) data

statistics.covariance(x, y),

statistics.correlation(x, z, method='ranked')

statistics.linear_regression(x, z)

fractions

Provides support for rational number arithmetic

__floor__()

__ceil__()

__round__()

__format__()

decimal

Provides support for fast correctly rounded decimal floating point arithmetic

copy

Constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original

array

Defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers

pprint

Provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter

itertools

Implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML

itertools.batched(iterable, n)

operator

Exports a set of efficient functions corresponding to the intrinsic operators of Python

datetime

Supplies classes for manipulating dates and times

datetime.date.fromtimestamp(timestamp)

datetime.date.timetuple()

datetime.date.strftime()

calendar

Allows you to output calendars like the Unix cal program, and provides additional useful functions related to the calendar.

zoneinfo

Provides a concrete time zone implementation to support the IANA time zone databas

collections

Implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.

Application Scenarios for Supported Modules
Copy

pprint module
Copy

To view the output in the console, users will need to wrap their pprint method calls in print, as shown in the example below:

The pprint module results are sent to stdout, which can be invisible to users.

itertools module
Copy

This example showcases the application of the filterfalse method from the itertools module, effectively filtering out even numbers from a list ranging from 1 to 10.

calendar module
Copy

This example showcases the application of the month method from the calendar module. It prints the calendar for March 2024.