Python Script

This article applies to P2 Sentinel 4.14 and later.

Python is a versatile, readable high-level programming language with a wide range of applications.

The Python Script component is conceptually similar to the R Script component, where you can configure input pins, write a script to use these inputs and perform some logic, and set the output value. That output value would then be forwarded to the next node in the UDP graph.

This component sends input data to Python for processing, using a defined Python script. The resultant value declared by the ‘output’ symbol is sent to the ‘out’ output pin. 

Once the Python Script has finished executing, the Global Environment is cleared. This means that any state which is stored in the Global Environment is lost between each run of the script.

For security reasons, imports and meta programming are not allowed in Python scripts.


Properties

The Python Script component has four properties: Name, Python Values, and Python Script.

Name: Give the Python Script component a name, to differentiate it from other Python Script components in the process. In our example, the Name is ‘value>value2’.

Python values: Use the Python values to add and name data inputs to the Python Script. These can then be used in the Python Script.

In our example, we’ve added ‘value’ and ‘value2’ as the Python values. As they are added as properties, they are assigned as inputs to the component.

Python Script: The Python Script should use the Python values in a script that uses the Python language. You can type the Python script in, or you can load it from a Python file that is stored in a file directory.

In our example, the Python Script is: ‘output = value > value2’. Note that Python language syntax is used here.

  • To edit the Python Script, click the Edit Python Script… button.

This opens the Edit Python Script dialog.

OK or Cancel

  • Click OK to validate and save changes to the script or click Cancel to lose any changes and close the dialog. If the script is not valid, an error dialog will be displayed and the script cannot be saved.

Load Python Script…

  • Click Load Python Script to load the Python script from a Python file stored in a file directory. This overwrites any text that’s in the Python Script edit box.

Save Python Script…

  • Click Save Python Script to save the script from the Python Script edit box to a new Python file.

Inputs

The input has a flow, and any defined Python values.

Flow

  • The component that acts as a trigger for this component should be connected to this pin.

[value]

  • Define the first Python value in the component’s Properties. Connect input data to this pin.

[value2] 

  • Define the second Python value in the component’s Properties. Connect input data to this pin.

Outputs

out 

  • A mixed value. This is the output value from the Python Script’s processing from the ‘output’ symbol set in the Python script. Not connecting this pin will produce a warning. If the ‘out’ pin is connected to a DataSet pin (green pin colour), then the data is passed along as a DataSet (Array) otherwise it is passed along as a single value. If the data is passed as a single value but the ‘output’ symbol is an array, the first element of the array is passed.

noData 

  • If the ‘output’ symbol is not declared in the Python script, the ‘output’ symbol is set to NULL or an array of zero length is returned, and the ‘noData’ pin is followed.

err 

  • If this pin is not connected, any errors which occur while processing the Python script are raised as errors and the processing is halted. If this pin is connected, the error is caught, the ‘err’ pin is followed, and processing continues.

Example

In the following example, the process checks whether the Python Script input 'value' is greater than the input 'value2'. If True, a True event is raised; if False, a False event is raised.

The Python script in the Python Script component used in this example is: ‘output = value > value2’.

Related: Sentinel Studio PrimersCreating a User Process, Usage Examples


Configuration Options

The following parameters have been added to the SentinelConfig.xml file for additional configurability.

MaximumPythonEngines Maximum number of Python Engines. 
PythonDLLOverride The full path to the python DLL to use in the engine, for custom Python installations only. 
PythonModules Comma separated list of python modules to load into a running python script. 

Comments are closed