Skip to main content

I have a FME-script with several Python callers. To avoid duplicate code in the callers I want to write a general python class that I can instantiate in the callers. In short I want to be able to access object methods to set specific object variables. Finally I want to call a main-method on the class to do the real work.

 

Where should the Class be placed? In the startup script? In another caller or elsewhere?

Hi @anteboy65, in either Startup script or a PytonCreator/Caller that runs at first, you can define a class which will be used in multiple PythonCallers in the same workspace.

Alternatively, if you define classes or functions in an external *.py file and save it in the same folder as the workspace, you can use them in any PythonCreator/Caller in the workspace, by just importing the file as a module.

 


@takashi Thank you very much. That brought clarity to the matter. If I use a .py-file to store the general class, what would the best solution be when I publish the workspace to the server? Do I manually have to put the py-file on the server in the same directory as where the fme-script are deployed or are there any smarter solutions to this issue?


@takashi Thank you very much. That brought clarity to the matter. If I use a .py-file to store the general class, what would the best solution be when I publish the workspace to the server? Do I manually have to put the py-file on the server in the same directory as where the fme-script are deployed or are there any smarter solutions to this issue?

A possible workaround I can think of is to upload the *.py file into a folder under the FME_SHAREDRESOURCE_DATA (i.e. Resources/Data) directory  - e.g. Resources/Data/python, then append the folder path to Python system paths list temporarily in the PythonCaller script, like this.

import fmeobjects
import sys
sys.path.append('$(FME_SHAREDRESOURCE_DATA)python')
import my_module
class FeatureProcessor(object):
    ... blablabla

 

The FME_SHAREADSOURCE_DATA parameter would be empty when you run the workspace with Desktop, so the workspace can also be run on your local machine if you create "python" folder under the same level as the workspace and save the *.py file under that.


A possible workaround I can think of is to upload the *.py file into a folder under the FME_SHAREDRESOURCE_DATA (i.e. Resources/Data) directory  - e.g. Resources/Data/python, then append the folder path to Python system paths list temporarily in the PythonCaller script, like this.

import fmeobjects
import sys
sys.path.append('$(FME_SHAREDRESOURCE_DATA)python')
import my_module
class FeatureProcessor(object):
    ... blablabla

 

The FME_SHAREADSOURCE_DATA parameter would be empty when you run the workspace with Desktop, so the workspace can also be run on your local machine if you create "python" folder under the same level as the workspace and save the *.py file under that.

Yep, I do the same thing, it works very nicely and allows you to share modules etc between workspaces.