Skip to main content

I wanna be able to define a global python function (perhaps in the startup scirpt?) like this:

def dosomething(number)

new_number = number + ... doing something with it

return new_number

 

then, en would like to reuse this global function many times at different moment in my workspace on many different attributes that has to go through the same treatment.

 

During the workflow of data_a, I'd call a pythoncaller this way:

newNumber = dosomething(feature.getAttribute('num_1'))

feature.setAttribute('num_1',newNumber)

newNumber = dosomething(feature.getAttribute('num_2'))

feature.setAttribute('num_2',newNumber)

 

During the workflow of data_b, I'd call a pythoncaller this way too:

newNumber = dosomething(feature.getAttribute('num_a'))

feature.setAttrivute('num_a',newNumber)

newNumber = dosomething(feature.getAttribute('num_b'))

feature.setAttrivute('num_b',newNumber)

 

I tried the same using a pythonCaller inside a customTransformer, but they both end up having to define all the attributes that they will have to deal with.

This way I have a extra layer of abstraction where my global python script "dosomething(number)" doesn't have to know the name of all the attributes that it receives.

Is that possible in FME?

The simplest is probably to define these functions in a separate module file.

As an example, create the file "utilities.py" and place it in the same folder as your .fmw file.

Inside "utilities.py", place all the helper functions, such as dosomething etc. In the PythonCaller / startup or shutdown scripts, you can then import the utilities module, e.g.

import utilities

utilities.dosometing(...)

 


The simplest is probably to define these functions in a separate module file.

As an example, create the file "utilities.py" and place it in the same folder as your .fmw file.

Inside "utilities.py", place all the helper functions, such as dosomething etc. In the PythonCaller / startup or shutdown scripts, you can then import the utilities module, e.g.

import utilities

utilities.dosometing(...)

 

awesome! I'll try it right away! thanks!


Hi @jb.gariepy​ and @david_r​  !

 

I was just having this issue with one constraint : not having a file next to the fmw. I need to simplify the exchanges so it's better to keep everything in the workspace.

 

You can do that : just define your functions and variables into the startup script. You can then reuse it into the PythonCaller, the PythonCreator and the Shutdown script.

 

Hope this can help

Best regards

Arthur


Reply