Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

Execute a Python script for each row in a table, where the script does the processing of the values in the table that are passed as arguments.

The script must be uploaded to the LogicHub (drag and drop script file to Automations > Scripts).

Operator usage in easy mode

  1. Click + on the parent node.

  2. Enter the Call Script operator in the search field and select the operator from the Results to open the operator form.

  3. In the Input Table drop-down, enter or select the table containing the data to run this operator on.

  4. In the Script Name drop-down, select the name of the script.

  5. In the Args drop-down, select one or multiple column names to use as arguments for the script.

  6. In the Time between Calls field, enter a value in seconds, minutes, hours, days, or weeks.

  7. Click Run to view the result.

  8. Click Save to add the operator to the playbook.

  9. Click Cancel to discard the operator form.

Usage details

LQL Command

callScript(inputTable, scriptName, args, timeBetweenCalls)

Input:
inputTable: Table containing the data to run this operator on.
scriptName: Name of the script as appear on the Scripts page
args: Column names to use as arguments for the script
timeBetweenCalls: Time between calls

Output:
new lhub_id, exitCode, error and output for each row. If the output is a list of JSON objects (e.g. rows), the output explodes the array into rows and extracts JSON values in the JSON objects into new columns.

args are the columns names (such as ["user", "accessCode"]). From the script they are accessed using sys.argv[1] and so on. To produce output to the table, you must print the result. See examples below for more information.

Example 1

Table = "users"

firstName

lastName

emailAddress

Tom

Toms

tom@devo.com

Create the following Python script file, save it as scriptOne.py, then drag and drop this file to Automations > Scripts page.

Python

import sys

# sys.argv[1] is value of firstName column
# sys.argv[2] is value of lastName column
# sys.argv[3] is value of emailAddress column
# ordering is important

email = sys.argv[3] # tom@devo.com
work = email.split('@')[1].split()[0] #devo

print("%s %s works at %s" % (sys.argv[2].upper(), sys.argv[1], work.upper()))

Create a computation step and in the LQL write following line.

LQL command

callScript(users, "scriptOne.py", ["firstName", "lastName", "emailAddress"], "1 s")
-- Note: arguments (columns) ordering are important, you need to remember which columns is at which index

Output

firstName

lastName

emailAddress

output

exitCode

error

Tom

Toms

tom@devo.com

TOMS Tom works at DEVO

Example 2

If the script produces multiple rows for each row, you must print them as an array of JSON objects that will get populated into columns and rows.

Python

import sys
fn = sys.argv[1]
ln = sys.argv[2]
ea = sys.argv[3]
print('[{"FIRSTNAME":"%s", "length":%d}, {"LASTNAME":"%s", "length":%d}, {"EMAILADDRESS":"%s", "length":%d}]'%(fn, len(fn), ln, len(ln), ea, ln(ea)))

Output

firstName

lastName

emailAddress

output

FIRSTNAME

LASTNAME

EMAILADDRESS

length

exitCode

error

Tom

Toms

tom@devo.com

JSON object

TOM

4

Tom

Toms

tom@devo.com

JSON object

TOMS

9

Tom

Toms

tom@devo.com

JSON object

tom@devo.com

17

For 1 row, 3 rows were produced. Because each row has a different JSON schema.

  • FIRSTNAME, length

  • LASTNAME, length

  • EMAILADDRESS, length

The operator did the union of all field names, and produced new columns for each field name.

  • No labels