Python Logic Step
  • 5 Minutes to read
  • Dark
    Light
  • PDF

Python Logic Step

  • Dark
    Light
  • PDF

Note:
Python is currently only available on Professional and Enterprise plans by default. If you have a Trial or Starter plan and would want to use it, please contact us and we will activate it for you.

Overview

Python allows for quick and easy data manipulation.
This functionality is part of the Logic River type, which allows you to create a data transformation process using steps, variables, containers, conditions, loops, actions, and River runs in straightforward method.

Use Cases

  • Transformation of data.
  • Initiate analytic process (Tableau extract, Sisense Elasticube, etc.).
  • Use Python to connect to APIs (not possible via Action River).
  • Run a Python script to start the machine learning process.

Supported Version and Packages

Rivery currently only supports Python 3.8.4.
In the future, more versions will be supported.

Packages

By default, Rivery handles the installation of the most recent packages:

  1. NumPy
  2. Pandas
  3. Matplotlib
  4. Regex
  5. Requests
  6. wordcloud
  7. Scikit Learn
  8. Sqlalchemy
  9. Tableau api lib
  10. Tableau server client

If you wish to install another package, simply add it to the 'Install Additional Packages' section:

image.png

Installing additional packages requires the following syntax:

package-name==version
Note:
  • A package that already exists in the Packages list cannot be updated or degraded.
  • If the package version is left blank, the most recent version identified in pipwill be installed.

 

Working With Variables

Dynamic variables are a feature of Python. These can be used to link Rivers, automate activities, and communicate data.

How To Use Logic River Variables

  1. Select the 'Variables' tab in the upper right corner.
    Screenshot 2022-01-30 203241.png
  2. Set up a variable ('temp' is an example).

Note:

  • To create an array, tick the 'Contains Multiple Values' box.
  • If the item 'Clear Value On Start' is checked, the value of the variable will always be updated to the original value when a new River is formed, regardless of what happens to it during a River.
  • Multiple variables can be defined.
  1. Click Save.

image.png

  1. To use the variable(s), run this script:
from rivery_variables import temp
print(temp)

Run this script if you're working with numerous variables:

from rivery_variables import temp, variable2, variable3
print(temp, variable2, variable3)


  • Use this script to save and overwrite the variable value (only strings):
from rivery_variables import temp
temp.save("insert here new content of variable")


  • Run this script if you wish to concatenate multiple variables (only strings):
from rivery_variables import variable1, variable2
concatenated_variable = f'{variable1}_{variable2}'


  • In the Logic River Variables window use square brackets to save an array as the variable's value, then check the 'Contains Multiple Values' box.

image.png

If you want to use the variable as an array, run this script (only arrays):

from rivery_variables import temp
temp.save([5,6,7,8])

Note:
When using a Multiple Value variable, you must only use square brackets to contain an array. Quotation marks (as with strings) aren't an option in this case.


  • Use this script to run a loop over a variable with multiple values.

Variables:
image.png

Script:

from rivery_variables import temp, temp_Dict
for var in temp:
    print(var)

for key, value in temp_Dict.items():
    print(key)
    print(value)


How To Use Environment Variables

Environment variables can only be used when they are defined as River variables. Values for Environment variables can be modified in the 'Variables' tab of the main menu, not anywhere along the River.

To use Environment Variables, follow the steps below:

  1. Click on 'Variables' Tab in the main menu.
    image.png
  2. At the bottom of the page, add a new Environment variable.
    image.png

3. After creating an Environment variable, go to your River and select the 'Variables' tab in the upper right corner.
Screenshot 2022-01-30 203241.png
4. Set up a new River variable and put the name of the Environment variable inside curly brackets to use it's value (Test_River_Variable's value will now be 10).

  1. Click Save.

image.png

6. To use the variable(s), run this script:

from rivery_variables import Test_River_Variable
print(Test_River_Variable)

 

Resources

Python allows for the manipulation of vast volumes of data, which is why Rivery offers 7 distinct resource types:

image.png

Each logic step is linked with a resource type, which can be found here:
image.png

Note:
Before a Python script is run, there is a one-minute server startup period.

 

Logs

All of the Python logs will be handled by the Logic river, which will post them to the Logic step's logs.

To get the log, perform the following:

1.Select Activity in the upper left corner.
Screenshot 2022-01-31 154939.png

You can also find the log in the main menu's Activities tab by searching for the name of your river.
Follow the on-screen instructions:
Group 2581124234235.png

2.Click the arrow.
image.png

  1. Choose Download Logs

ergetttr.PNG

Note:

  • Rivery does not modify the logs, therefore they will be displayed exactly as they are from the Python code.
  • Python logs guidelines.

 

Python Pricing

The RPU (Rivery Pricing Unit) of the Python Logic step is calculated by adding the script's entire time and the quantity of network usage.
Note: If a River run is unsuccessful, the Python Logic Step RPU (logicode_rpu) will not be calculated.

The python pricing is based on:

  1. Execution time of the user’s Python script (seconds)
  2. Server size they chose to execute the script (see below)
  3. Network bandwidth - 0.4 RPU for every 100MB of data transferred
Server Size RPU per Minute RPU per Hour
XS 0.021 1.2
S 0.041 2.5
M 0.082 4.9
L 0.165 9.9
XL 0.329 19.7
XXL 0.3884 23.304
XXXL 0.492 29.52

The RPU can be found in the Logic Step Details:

  1. Click on Logic Steps

wfwe.PNG
2. Logic Step Detalis is now available.

fdgfdgfdg.PNG

Recommendations

Rivery recommends the following to avoid repeated River runs and RPUs:

  1. Mock the variables using pytest, unittest, or other testing libraries. Run the scripts on your local Computer and check the results.
  2. Double-check that you've installed all of the necessary packages in the river's settings.
  3. It is advised to have as many logs as feasible to assist in the verification of script outcomes.

 

Limitations

  • When naming a River Variable, avoid using the space character (" ").
    The River Variable will not be saved, and the River will fail to run.
  • Variables could be used up to 13 MB or 1000 rows of data, whichever comes first. (This constraint applies to all logic steps, not only Python).
  • The os and sys libraries are not supported due to security regulations.

Was this article helpful?