PythonCaller
Executes a user-supplied Python script to manipulate features.
Typical Uses
- Tasks where a transformer is not available
- Using external modules for processing
- Performing complex manipulations on list attributes
How does it work?
The PythonCaller executes a Python script to manipulate features.
When a specialized task is required, such as custom statistical analysis of an attribute, but Workbench does not provide a transformer suited to the task, a Python script can perform specialized and complex operations on a feature's geometry, attributes, and coordinate system.
Access is provided via the FME Objects Python API.
Note: Python is a programming language external to FME. For documentation on creating Python scripts, visit The Python Foundation.
Using Python to perform arbitrary operations on features is a powerful aspect of Workbench. However, the logic introduced into a workspace is less visible and can therefore be more difficult to maintain than logic built using Workbench’s built-in transformers. It is recommended that other transformers be used when possible instead of Python scripts.
Interface Paradigm
The PythonCaller can interface with a Python script in two different ways - by a function or by a class.
- Use the Function Interface when you intend to process a single feature at a time.
- Use the Class Interface for more flexibility.
The Class Interface is useful when you want to operate on a group of features together, such as collecting all the features received and then outputting them in a specific sort order. Another common use case is to accumulate all the features, perform an operation on the whole set, and then output all of the features with a calculated value as a new attribute.
Function Interface
The PythonCaller will call the Python function with exactly one argument: an FMEFeature object.
The function will be called with each FMEFeature that comes into the input port. This feature will then continue through the workspace pipeline via the output port.
The function’s return value will be ignored by the PythonCaller. Any raised exception will terminate the translation. Any raised FMEException will be logged as an ERROR and will terminate the translation.
The example below adds a string attribute to each feature and sets it to the current time:
import fmeobjects
import time
def timestampFeature(feature):
curTime = time.ctime(time.time())
feature.setAttribute("timestamp", curTime)
Class Interface
The PythonCaller will call the following methods on the class:
- __init__() - Called once, whether or not any features are processed.
- input() - Called for each FMEFeature that comes into the input port.
- close() - Called once, after all features are processed (when no more FMEFeatures remain). If no features are processed, the close() method will still be called.
Features that need to continue through the workspace for further processing must be explicitly written out using the pyoutput() method.
The class interface can operate on a group of features, instead of processing incoming features one at a time. This is done by storing incoming features in a list, then processing them all at once before being output.
The example below calculates the total area of all the features processed and then outputs all the features with a new attribute containing the total area:
import fmeobjects
class FeatureProcessor(object):
def __init__(self):
self.featureList = []
self.totalArea = 0.0
def input(self,feature):
self.featureList.append(feature)
self.totalArea += feature.getGeometry().getArea()
def close(self):
for feature in self.featureList:
feature.setAttribute("total_area", self.totalArea)
self.pyoutput(feature)
Script Editing
A PythonCaller transformer can call scripts that are stored in the transformer itself or scripts that are stored globally for the entire workspace:
- To store a Python script with a specific PythonCaller transformer, use the Python Script parameter in the transformer.
- To store a Python script globally, click the Advanced Workspace Parameter in the Navigator, and double-click Startup Python Script. Storing scripts globally has the advantage of keeping the Python logic centralized, which makes editing and maintenance easier. This is useful when there are multiple PythonCaller transformers throughout the workspace that use the same script. For more information, see Startup and Shutdown Python Scripts in the FME Workbench help.
FME can access .py modules that are stored on the file system, including modules in external Python libraries. Use the Python "import" command to load these modules. FME will search both the standard Python module locations and the workspace location to find the module to be imported.
Configuration
Input Ports
Features to be manipulated.
Output Ports
Features, including any modifications made.
Parameters
Class or Function to Process Features | The name of the Python Function or Class within the script that PythonCaller will use to begin execution. For the above example, set this parameter FeatureProcessor. |
Python Script | The Python script to be executed. When the Python script is stored as the Startup Python Script for the Workspace, leave this parameter blank. |
Attributes to Expose | Exposes any attributes that are created by the Python script being executed so they can be used by other transformers. |
Attributes to Hide | Hides any attributes that may be removed by the Python script being executed. Other transformers will not be able to use these attributes. |
Lists to Hide |
Hides any lists that may be removed by the Python script being executed. Other transformers will not be able to use these lists. Note that if you select to hide a list, your selection will include any list attributes or nested lists. For example, if you select to hide a list called list{} then list{}.attr or list{}.sublist{} will also be hidden. |
Editing Transformer Parameters
Using a set of menu options, transformer parameters can be assigned by referencing other elements in the workspace. More advanced functions, such as an advanced editor and an arithmetic editor, are also available in some transformers. To access a menu of these options, click beside the applicable parameter. For more information, see Transformer Parameter Menu Options.
Defining Values
There are several ways to define a value for use in a Transformer. The simplest is to simply type in a value or string, which can include functions of various types such as attribute references, math and string functions, and workspace parameters. There are a number of tools and shortcuts that can assist in constructing values, generally available from the drop-down context menu adjacent to the value field.
Using the Text Editor
The Text Editor provides a convenient way to construct text strings (including regular expressions) from various data sources, such as attributes, parameters, and constants, where the result is used directly inside a parameter.
Using the Arithmetic Editor
The Arithmetic Editor provides a convenient way to construct math expressions from various data sources, such as attributes, parameters, and feature functions, where the result is used directly inside a parameter.
Conditional Values
Set values depending on one or more test conditions that either pass or fail.
Parameter Condition Definition Dialog
Content
Expressions and strings can include a number of functions, characters, parameters, and more.
When setting values - whether entered directly in a parameter or constructed using one of the editors - strings and expressions containing String, Math, Date/Time or FME Feature Functions will have those functions evaluated. Therefore, the names of these functions (in the form @<function_name>) should not be used as literal string values.
These functions manipulate and format strings. | |
Special Characters |
A set of control characters is available in the Text Editor. |
Math functions are available in both editors. | |
Date/Time Functions | Date and time functions are available in the Text Editor. |
These operators are available in the Arithmetic Editor. | |
These return primarily feature-specific values. | |
FME and workspace-specific parameters may be used. | |
Creating and Modifying User Parameters | Create your own editable parameters. |
Dialog Options - Tables
Transformers with table-style parameters have additional tools for populating and manipulating values.
Row Reordering
|
Enabled once you have clicked on a row item. Choices include:
|
Cut, Copy, and Paste
|
Enabled once you have clicked on a row item. Choices include:
Cut, copy, and paste may be used within a transformer, or between transformers. |
Filter
|
Start typing a string, and the matrix will only display rows matching those characters. Searches all columns. This only affects the display of attributes within the transformer - it does not alter which attributes are output. |
Import
|
Import populates the table with a set of new attributes read from a dataset. Specific application varies between transformers. |
Reset/Refresh
|
Generally resets the table to its initial state, and may provide additional options to remove invalid entries. Behavior varies between transformers. |
Note: Not all tools are available in all transformers.
Reference
Processing Behavior |
Feature-Based or Group-Based, conditional on Python script |
Feature Holding |
Conditional on Python script |
Dependencies |
Specifying a Python Interpreter
An FME installation includes a Python version 2.7 and Python version 3.5 interpreter. The default Python interpreter used for Python processing is the Python 2.7 interpreter. The FME Objects Python API supports Python 2.7, Python 3.4, and Python 3.5. The Python interpreter used by FME to execute Python scripts is controlled by the Python Compatibility workspace parameter and the Preferred Python Interpreter setting under Tools > FME Options > Translation. Python Compatibility specifies the version of Python with which Python scripts are compatible. FME loads the Preferred Python Interpreter if it is compatible with the Python Compatibility. If not, FME loads an appropriate Python interpreter matching Python Compatibility. For more information, see the FME Workbench help. Installing Python Packages
If you would like to install a third-party package for use by Python in FME, see Installing Python Packages to FME Desktop in the FME Workbench help. |
FME Licensing Level | FME Base Edition and above |
Aliases | |
History |
FME Community
The FME Community is the place for demos, how-tos, articles, FAQs, and more. Get answers to your questions, learn from other users, and suggest, vote, and comment on new features.
Search for all results about the PythonCaller on the FME Community.
Examples may contain information licensed under the Open Government Licence – Vancouver