Python script

Editing a Python script

Conditions for editing

  • You must have the permission to edit the Python script. Usually you have to be logged in as a Developer.

  • You must be in manual mode.

  • The Python script is currently not being executed.

Access to modules

In the start-file, you can access all modules of the same level in the module tree.

Example 1. Access to modules

In this example, the following modules: Script, TypeData, WorkPiece, PowerSupply, Multimeter and HighVoltageTester are all on the same level within the module tree.

moduleTree en

These modules can therefore be accessed directly from the start file:

Access to a module
PowerSupply.SetVoltage(14.0)

Autocomplete and Type Hints

Open the autocomplete window with the key combination Ctrl+Space.

autoComplete en.drawio
Figure 1. Autocomplete and definition of the .NET method SetVoltage from the ScpiPowerSupplyModule

The functions and properties marked with a heart are favoured and displayed at the top of the autocomplete.

Filter function: only the selected elements are displayed
  • module Module

  • class Class

  • method Method

  • property Property

  • field Field

  • parameter Parameter

  • keyword Keyword

Python Type Hints are supported to improve autocompletion.

Python Type Hints
Python is a dynamically typed language. Therefore, it is not always possible to determine the type of a method parameter, a return value or a variable. However, with the help of Type Hints it is possible to display the autocompletion. Python 2.7 only supports type hints within comments, see also PEP484 type hints for Python 2.7

Example 2. Type Hint for a variable
clr.AddReference("UserDocumentation")
import UserDocumentation

Station = WorkPiece.Parent # type: UserDocumentation.Modules.StationModule
Station. # Now you can see here the completions for the type 'FunctionTester.UserDocumentation.Modules.StationModule'
Example 3. Type Hint for a method
def embezzle(self, account, funds=1000000, *fake_receipts):
    # type: (str, int, *str) -> None
    """Embezzle funds from account using fake receipts."""

Running and debugging a Python script

In the menu of the Python Script module, all debugging commands are listed under Debug.

debug en
Figure 2. Debug commands
  • play Run the start file F5
    To run the Python script, the following conditions must be met:

    1. The station must be in manual mode manual

    2. You must have the permission to execute a script.

    3. The project must have a start file as entry point

  • break Pause the running script

  • stop Stop the running script Shift+F5

  • stepin Execute a single step in the script F11

  • stepover Run to the next line in the script F10

  • bug Toggle the debug script option

  • togglebreakpoint Add/remove breakpoint F9

Select debug script option bug to be in break mode and see the current line highlighted in yellow. Breakpoints set in the Python script are represented by a red dot breakpoint.

scriptDebug en
Figure 3. Debugging in the Python script
If debugging is to be done in the Python script, select the debug script option bug, only then added breakpoints breakpoint will be displayed.

Debugging variable __debug__

The debugging variable __debug__ is useful, especially during debugging (the debug script option bug is selected) use the command print to send outputs to the Output window in the tab bar.

if __debug__:
    print "This line is only executed if the debug script option is selected."