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

Python Logic Step

  • Dark
    Light
  • PDF

Article Summary

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.
    image.png

  2. Set up a variable ('temp' is an example).

Please 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.
  • Users can encrypt sensitive values stored in River variables.
  1. Click Apply Changes.

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)

Encrypted Variables

Users have the option to encrypt sensitive data kept in River variables.

The variable's value is encrypted and hidden when the "Encrypt" option is used. In-line Python code cannot decrypt encrypted variables, and encrypted variables cannot store new values.

For further information, consult our Encrypted Variables documentation.


How To Use Environment Variables

Environment variables can be used throughout the entire platform. When it comes to Python steps, Environment variables are "read only" variables, they can only have their values changed 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. in the upper right corner, click + Add Variable.

    image.png

  3. Add a value to the variable.

    image.png

Please Note:
To create an Array, use square brackets to save it as the variable's value.

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

from rivery_environment_variables import Test
print(Test)


Please Note:

  • If you wish to import both Environment and River variables, and a variable with the same name exists in both, only the second importing command will be carried out.
    Here's an Example:

    from rivery_variables import Temp_Var 
    from rivery_environment_variables import Temp_Var
    

    Only the command "from rivery_environment_variables import Temp_Var" will be carried out.

  • If this is the case and the River and Environment variables actually share the same name, you can add aliases to these variables and specifically use them in that Logic step. Other logic steps won't recognize these aliases.
    Here's an example:

    from rivery_variables import Temp_Var as Temp1
    from rivery_environment_variables import Temp_Var as Temp2
    
    print(Temp1)
    print(Temp2)
    
  • Environment variables in Python logic steps are "read only" variables; their values cannot be changed or saved.

 

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 SizeRPU per MinuteRPU per Hour
XS0.0211.2
S0.0412.5
M0.0824.9
L0.1659.9
XL0.32919.7
XXL0.388423.304
XXXL0.49229.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).
  • Rivery CLI does not support the Python Logic step.
  • The use of the os and sys libraries is strictly prohibited due to security regulations, and commenting the imports for these libraries is not allowed.
  • Python reserved words cannot be used in Rivery as it may cause syntax errors and conflicts during River runs.
    Here is a comprehensive list of all the reserved words in Python:
    image.png

Was this article helpful?