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. pyton-basic-usage-input-variables 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:
'''
You can reference the input variables using input["NAME"]
'''
def executeScript(input):
    itemName = input ["itemName"]
    itemDescription = input ["itemDescription"]
    
    concatenated_string = itemName + ":" + " " + itemDescription
    
    return concatenated_string

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

Building the script

  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.

python-script-import-module-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.

python-script-wrong-indent

Input variables

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. pyton-basic-usage-input-variables 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

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

** Supported Modules**

The following modules are supported from V2.0:

ModulesDescriptionNon-supported Methods
mathMathematical functions defined by the C standard
cmathMathematical functions for complex numbers
randomGenerate pseudo-random numbers
statisticsProvides functions for calculating mathematical statistics of numeric (Real-valued) datastatistics.covariance(x, y),
statistics.correlation(x, z, method='ranked')
statistics.linear_regression(x, z)
fractionsProvides support for rational number arithmeticfloor()
ceil()
round()
format()
decimalProvides support for fast correctly rounded decimal floating point arithmetic
copyConstructs a new compound object and then, recursively, inserts copies into it of the objects found in the original
arrayDefines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers
pprintProvides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter
itertoolsImplements a number of iterator building blocks inspired by constructs from APL, Haskell, and SMLitertools.batched(iterable, n)
operatorExports a set of efficient functions corresponding to the intrinsic operators of Python
datetimeSupplies classes for manipulating dates and timesdatetime.date.fromtimestamp(timestamp)
datetime.date.timetuple()
datetime.date.strftime()
calendarAllows you to output calendars like the Unix cal program, and provides additional useful functions related to the calendar.
zoneinfoProvides a concrete time zone implementation to support the IANA time zone databas
collectionsImplements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.

Application Scenarios for Supported Modules

pprint module

To view the output in the console, users will need to wrap their pprint method calls in print, as shown in the example below: python-script-pprint-module The pprint module results are sent to stdout, which can be invisible to users. python-script-pprint-module-op-1 1

itertools module

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. python-script-itertools-module

calendar module

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

Was this page helpful?