It is sometimes useful to import external Python modules into Test Procedures developed within the Introspect ESP Software. Similarly, it might be useful to export test methods or entire Test Procedures from the Introspect ESP Software for use in external Python scripts. This Technical Insights brief describes ways to achieve both of these requirements.
The Introspect ESP Software has a built-in component class called PythonModule, and this is the recommended way of importing external Python functions or modules into Test Procedures being executed from within the Introspect ESP Software. This section describes the basic concept of this component class and illustrates real-life examples of how it is used.
A PythonModule component can be instantiated by adding it to the Introspect ESP Software Test Procedure using the “Add Component” menu. Note that this component class is listed under the “utility” category of the menu as shown in the following image.
When the PythonModule component is instantiated, the Introspect ESP Software takes a couple of automated actions. First, it adds the module to the Components tab as is the case with all other instantiated components. Second, it creates a blank .py file inside the Params sub-folder of the Test Procedure folder in Windows. The name of this file matches the name of the component class that was instantiated. That is, referring to the two following images, the software creates a module called pythonModule1 and creates a corresponding file called pythonModule1.py.
By default, the pythonModule1.py file is blank, and the Introspect ESP Software typically launches a code editor to allow the user to edit this file. This is when external code can be added. In the following example, we show two function declarations: one to create a custom print method and the second to compute a Fibonacci sequence. As can be seen, any Python code can be placed in this file, including function declarations, function calls, and library imports.
After saving the pythonModule1.py file, we can go back to the Introspect ESP Software and never have to worry about the source code anymore. Instead, we import the file’s contents by adding the call pythonModule1.run() in the main Test Procedure pane. Once the file is imported this way, we can call any function that was declared inside it. For example, the following image shows how to call the Fibonacci Series calculator that was declared in the above example; and the image after it shows the execution log of the Test Procedure, confirming that the external Python file was indeed imported and that the Fibonacci Series function was executed.
Placing a python file in the params folder
If you want to import an already existing Python file without having to paste its contents into a blank file, then you can simply place this file in the Params sub-folder of your target Introspect ESP Software folder. When you do so, the Introspect ESP Software automatically creates an instance of the PythonModule component class. In the following example, we place a file called mySecondPythonModule.py in the Params folder as shown in the following image.
The file itself has one line, which is an import call for the built-in Python uuid library, included here only for exemplary reasons.
Opening the Test Procedure in the Introspect ESP Software, we see that the PythonModule class is automatically instantiated as in the following image.
We now proceed to using this module in the Test Procedure pane. Specifically, we import the module by executing the method mySecondPythonModule.run() as before. Then, we are able to use any built-in function within the uuid library that we have just imported. In the following example, we use it to create a unique user ID and then print this ID to the log window.
The result of executing the above Test Procedure is shown in the following image. As can be seen, the Python module was successfully imported without cluttering the Test Procedure window.
Apart from the PythonModule class, it is possible to manually import files into Test Procedures created within the Introspect ESP Software. This is done using common Python language constructs. For example, the following image illustrates importing the uuid library directly from within the Test Procedure.
Note that the Introspect ESP Software automatically searches the following path for external Python files:
Any file stored in this directory can simply be imported using the call
The Introspect ESP Software has a built-in component class called TestAsPythonScript, and this is an automated code-generation utility that allows you to export algorithms developed inside the Introspect ESP Software for use in external Python scripts. The advantage of this tool is that it automatically takes care of initializing form factors, creating component contexts, and connecting to the hardware. This section describes the basic concept of this component class.
A TestAsPythonScript component can be instantiated by adding it to the Introspect ESP Software Test Procedure using the “Add Component” menu. Note that this component class is listed under the “utility” category of the menu as shown in the following image.
NOTE: The TestAsPythonScript has been introduced in version 3.6.79 of the Introspect ESP Software and is not available in earlier releases.
When the TestAsPythonScript component is instantiated, it is added to the Components pane just like any other component class. However, unlike other component classes, this class does not have any callable methods. As such, the Test Procedure pane is not modified as shown in the following screenshot. Instead, the Introspect ESP Software uses the existence of the testAsPythonScript1 component as a trigger to save an output Python file that can be used in external scripts. This output file is an exact representation of the Test Procedure that was edited from within the Software.
The automatically generated code for the above Test Procedure is shown on the next page. As can be seen, all aspects of external instantiation of Introspect components are taken care of automatically.
Introspect ESP component classes can be instantiated in external Python scripts by following the instructions in the application note:
This application note is included in the Doc folder of the Introspect ESP Installation.
With the built-in components named PythonModule & TestAsPythonScript in the Introspect ESP Software, users now have the advantage of easily importing external Python functions or modules into Test Procedures and exporting scripts for external usage. The award-winning software has increased efficacy in the test procedures of many engineers. Learn more about the Introspect ESP Software by clicking here.
For more information on our products, please contact firstname.lastname@example.org.