Skip to main content
Question

General class in python, where to place?

  • September 12, 2019
  • 4 replies
  • 46 views

anteboy65
Contributor
Forum|alt.badge.img+4

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?

4 replies

takashi
Influencer
  • September 12, 2019

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.

 


anteboy65
Contributor
Forum|alt.badge.img+4
  • Author
  • Contributor
  • September 13, 2019

@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
Influencer
  • September 13, 2019
anteboy65 wrote:

@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.


david_r
Celebrity
  • September 13, 2019
takashi wrote:

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. 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings