FME Transformers: 2024.1

Categories
Workflows
Related Transformers
PythonCreator
RCaller

PythonCaller

Executes a user-supplied Python script to manipulate features.

Jump to Configuration

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 FME 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 FME Workbench. However, the logic introduced into a workspace is less visible and can therefore be more difficult to maintain than logic built using FME Workbench’s built-in transformers. It is recommended that other transformers be used when possible instead of Python scripts.

Class Interface

The PythonCaller can interface with a class defined in a Python script. The calling sequence on the methods defined in a class depends on which mode the PythonCaller is in. There are two modes - Standard and Group By.

Standard Mode

This is the operation mode when no attributes are set in the Group By parameter. In this mode, which is the most common, the PythonCaller will have the following calling sequence on a 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.

When the class interface is processing incoming FMEFeatures one at a time, the pyoutput() method is to be called once per incoming FMEFeature in the input() method. Conversely, when the class interface is operating on a group of FMEFeatures, the incoming FMEFeatures can be stored in a list, then processed and written out through pyoutput() in the close() method.

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:

Complete Groups

This is the operation mode when one or more attributes are set in the Group By parameter. In this mode, the PythonCaller will have the following calling sequence on a class:

  • __init__() - Called once, whether or not any features are processed.
  • input() - Called for each FMEFeature in a group.
  • process_group() - Called after all FMEFeatures in a group have been sent to input(). After this is called and executed, PythonCaller will send the next group of FMEFeatures to input() and then call process_group() again. This is repeated until all groups have been exhausted.
  • close() - Called once, after all rounds of input() and process_group() have been called to process all incoming features (when no more FMEFeatures remain). If no features are processed, the close() method will still be called.

In this mode, the class interface will be working on groups of FMEFeatures. The incoming FMEFeatures must be stored in a list class member variable, then processed and written out through pyoutput() in the process_group() method. After processing is complete in process_group(), all class member variables should be cleared for the next round of group by handling. In the next round, FMEFeatures of the next group are passed through input() calls followed again by process_group(). This is repeated until all the groups are exhausted.

The example below calculates the total area of all features grouped by the _shape attribute and then outputs all the features with a new attribute containing the total area for each group:

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

Output Ports

Parameters

Editing Transformer Parameters

Transformer parameters can be set by directly entering values, using expressions, or referencing other elements in the workspace such as attribute values or user parameters. Various editors and context menus are available to assist. To see what is available, click beside the applicable parameter.

For more information, see Transformer Parameter Menu Options.

Reference

Processing Behavior

Feature-Based or Group-Based, conditional on Python script

Feature Holding

Conditional on Python script

Dependencies
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, Open Government Licence - British Columbia, and/or Open Government Licence – Canada.