Python Example 1. Hello World
This Python example is the equivalent of a typical "Hello World" user measurement script. It is the simplest possible example designed to prove that your FlexDCA / Python environment is working correctly and to illustrate all of the basic steps required to create a user measurement. After you've been successful with this example, try the Example 2. Creating a "Show Input Variables" Script. Like all user-measurement Python scripts, HelloWorld.py consists of the following items:
- A function named algorithm. Your scripts can have additional functions, but it must have the algorithm function.
- FlexDCA passes a dictionary of input variables to the algorithm function.
- The script must return a dictionary of measurement results. These are the values that are returned to FlexDCA and get displayed in FlexDCA's measurement results table and indicate the measurement's status.
- If the measurement cannot be made, return a dictionary that has one key "ErrorMsg" with the value that is an ASCII error string that you want displayed.
Do not use spaces or hyphens when selecting a file name for your script.
Procedure
- Make sure that you have properly installed Python.
- Copy the following XML listing into a text editor. Name the file HelloWorld.xml and save it in any FlexDCA folder or the user measurements folder (\Documents\Keysight\FlexDCA\User Measurements). Later, when this XML file is imported into the User Measurement Setup dialog, it identifies the HelloWorld.py script, populates the dialog, and creates the user measurement button as shown in these figures.
- Start your Python editor and enter the following script. Name your script file HelloWorld.py. In the script notice that:
- The SrcData key is passed to the variables dictionary to obtain the input waveform.
- The output variable,
Result
, returns a pretend measurement value, 4.2, to display on FlexDCA's Results table. This is a very simple script that doesn't even calculate a result. - Four variables are returned as a dictionary (key:value pairs).
- Place FlexDCA into Oscilloscope mode and display a single waveform on the display.
- In FlexDCA's User measurement tab, click the User Meas Setup button.
- In the dialog, select an available tab.
- Click Browse and select your XML configuration file, HelloWorld.xml.
- Close the dialog and click on your new Hello World! measurement button.
- In the Results pane, notice that the Hello World! measurement result of 4.2 dB is reported.
HelloWorld.xml
<?xml version="1.0" encoding="utf-8"?>
<Measurement>
<Name>Hello World!</Name>
<Abbreviation>Hello</Abbreviation>
<Comments>Confirms environment is OK.</Comments>
<Script>HelloWorld.py</Script>
<MeasurementType>1 Source</MeasurementType>
<ValidMode>Scope</ValidMode>
</Measurement>
HelloWorld.py
def algorithm(variables):
SrcData = variables['SrcData']
num_samples = len(SrcData)
if num_samples == 0:
return {'ErrorMsg': 'No data'}
print 'Hello World!'
return {'Result': 4.2,
'Units':'dB',
'Status':'Correct',
'ErrorMsg':''}
Do not use spaces or hyphens when selecting a file name for your script.