FME Transformers: 2024.1
FME Transformers: 2024.1
Type: Scripted Python
You can write a Python script that assigns the value of a parameter to the workspace at runtime. This script will compute a value and assign it to a global parameter before the data is even read (for example, you might want to open a database, extract a value, and then run the workspace).
You can also use these parameters in a custom transformer.
FME Hub Templates
Templates that contain Scripted Python parameters are available for download from FME Hub. To download:
- Select File > Workspace from Template.
- Select FME Hub Templates.
- In the Search bar, type python.
- Choose one of the templates from the list at the top right.
Example - Group Layers in a Workspace (Scripted Python)
Define the Parameter in the Workspace
You can use the Scripted parameter to allow grouping of feature types (layers) in a workspace. In this example, taken from one of the templates described above, the parameter allows you to choose to group by different layers when you run the workspace.
Open the Workbench User Parameters dialog.
The parameter is defined using the following options:
Write the Python Script
Click the browse button beside the Value field to display the source code editor. The code in the editor is described here:
Code | Description |
---|---|
Initially set featureTypes variable to empty. |
|
If you choose Bus Information for global parameter Layers, set the feature types to Bus Routes and BusStops. | |
If you choose Road and Rail for global parameter Layers, set the feature types to metrorail and Roads. | |
If you choose All Transit for global parameter Layers , set the feature types to metrorail, Roads, BusRoutes, BusStops and Labels. | |
Note Your code must contain a return statement on the last line of code. If there is no return statement (and assuming there is more than one line of code) FME will add return to the beginning of the line. Example for single lines of code:
When finding the last line of code, multiline statements or comments are not supported. |
Return the featureTypes variable. The feature_types_to_read global parameter will be set to the value of this return. The feature_types_to_read global parameter is linked to the reader Feature Types To Read parameter; therefore, the feature types that are read will be the feature types specified in this return value. |
Note Take note of the style of code block indentation. The FME source code editor uses tab stops for indenting blocks, whereas other Python development environments (like IDLE) substitute tab stops by four spaces (by default). Mixed styles in the same block are not allowed. To avoid errors, you may have to replace tabs or spaces in code copied from another environment.
Run the Workspace
To run the workspace, select Run > Prompt for Parameters or press Ctrl+r.
Prompt and Run will display a Translation Parameters dialog. The translation will pause until you choose which layers to group.
If you Select All, the features are set to all feature types:
If you select Bus Information only, for example, the feature types are set to Bus Routes and Bus Stops only:
Additional Python Actions
This table gives examples of actions you might want to include in your Python script.
Action | Example |
---|---|
Access other parameters |
FME_MacroValues['MacroName'] FME_MacroValues is a dictionary where the key is the macro name and the value is the value of the macro. |
Set other parameters | Not applicable |
Write to the log file. The message will appear in the log window in Workbench in execution context with the color indicated by the error level. | Not applicable |
To write to the end of the log file. The message will appear in the log at the end in the ‘info’ level. |
print ‘my message’ or sys.stdout.write('dive in error') If you have multiple print or stdout.write statements, only the first will appear. |