Skip to main content
Solved

PythonCaller User Parameters


Forum|alt.badge.img

Hi,

is this possible to pass User Parameter into function argument as a second argument inside PythonCaller ?

Putting it like that, does not work:

def processFeature(feature, FME_MacroValues['SomeUserParameter']):

Thanks,

Submi

Best answer by david_r

While you can definitely do what @larry suggests, I'm not sure I see the point since "SomeUserParameter" will always be a constant (parameters cannot change value after the workspace is launched). In fact, I think it's just more likely to confuse those reading your code later.

I'd rather do something like

import fme
import fmeobjects

def processFeature(feature):
    p = FME_MacroValues['SomeUserParameter']
    print p  

At least then everybody should be able to understand your code  without wondering if there's a trick involved :-)

View original
Did this help you find an answer to your question?

11 replies

Forum|alt.badge.img
  • March 21, 2017

The macro value is the value of your parameter.

To achieve what you want, you have to name the parameter and give it the macro value as the default value

import fme
import fmeobjects

def processFeature(feature, p = FME_MacroValues['SomeUserParameter']):
    print p 


Forum|alt.badge.img
  • Author
  • March 21, 2017
larry wrote:

The macro value is the value of your parameter.

To achieve what you want, you have to name the parameter and give it the macro value as the default value

import fme
import fmeobjects

def processFeature(feature, p = FME_MacroValues['SomeUserParameter']):
    print p 

 

Thanks, it works!

david_r
Evangelist
  • Best Answer
  • March 21, 2017

While you can definitely do what @larry suggests, I'm not sure I see the point since "SomeUserParameter" will always be a constant (parameters cannot change value after the workspace is launched). In fact, I think it's just more likely to confuse those reading your code later.

I'd rather do something like

import fme
import fmeobjects

def processFeature(feature):
    p = FME_MacroValues['SomeUserParameter']
    print p  

At least then everybody should be able to understand your code  without wondering if there's a trick involved :-)


Forum|alt.badge.img
  • Author
  • March 21, 2017
david_r wrote:

While you can definitely do what @larry suggests, I'm not sure I see the point since "SomeUserParameter" will always be a constant (parameters cannot change value after the workspace is launched). In fact, I think it's just more likely to confuse those reading your code later.

I'd rather do something like

import fme
import fmeobjects

def processFeature(feature):
    p = FME_MacroValues['SomeUserParameter']
    print p  

At least then everybody should be able to understand your code  without wondering if there's a trick involved :-)

 

I want to be able to change this User Parameter in each workspace launching and withing each lunch it should be constant. Your solution also works for me and for readability point of view it maybe be better.

 

Thanks

 


david_r
Evangelist
  • March 21, 2017
submi wrote:

 

I want to be able to change this User Parameter in each workspace launching and withing each lunch it should be constant. Your solution also works for me and for readability point of view it maybe be better.

 

Thanks

 

Yes, yours is a very common use case and the code above would work in thoses cases too.

jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • March 21, 2017

One caveat to david_r's answer. If you put your pythonCaller inside a custom transformer, the parameter name changes to {customTransformerName}_SomeUserParameter.


david_r
Evangelist
  • March 21, 2017
jdh wrote:

One caveat to david_r's answer. If you put your pythonCaller inside a custom transformer, the parameter name changes to {customTransformerName}_SomeUserParameter.

Yes, that's true and it's a general nuisance in FME. But I suspect that would be true for @larry's answer too, no?

jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • March 21, 2017
david_r wrote:
Yes, that's true and it's a general nuisance in FME. But I suspect that would be true for @larry's answer too, no?
Almost certainly, but I've never attempted anything like @larry answer.

 


Forum|alt.badge.img
  • August 29, 2019
jdh wrote:

One caveat to david_r's answer. If you put your pythonCaller inside a custom transformer, the parameter name changes to {customTransformerName}_SomeUserParameter.

param = ctParam.split("}_")[1]

I believe something like this could done to clean in

[Update] I tried passing as a user param as through a custom transformer and does not appear to work with larry's method. Will try the other

[Update2] I tried david_r's answer to, does not work within custom transformer (ct_).

I'm trying to pass a value to the ct_ as a user param but does not work just fails and reports

Python Exception <KeyError>: 'Filepath'

Error encountered while calling function `fileExists'

f_6(PythonFactory): PythonFactory failed to process feature

A fatal error has occurred. Check the logfile above for details


Forum|alt.badge.img
  • August 29, 2019
jdh wrote:

One caveat to david_r's answer. If you put your pythonCaller inside a custom transformer, the parameter name changes to {customTransformerName}_SomeUserParameter.

To clarify, when you say {customTransformerName}_SomeUserParameter to you mean

This: FME_MacroValues['SomeUserParameter']

Becomes: FME_MacroValues['{customTransformerName}_SomeUserParameter']

? I tried doing this but could not get it to work


david_r
Evangelist
  • August 30, 2019
tforward wrote:

To clarify, when you say {customTransformerName}_SomeUserParameter to you mean

This: FME_MacroValues['SomeUserParameter']

Becomes: FME_MacroValues['{customTransformerName}_SomeUserParameter']

? I tried doing this but could not get it to work

If you have a PythonCaller inside a custom transformer and you need to access a published parameter local to the transformer, you should use the ParameterFetcher to "convert" the parameter to an attribute first. Example:

  1. ParameterFetcher (retrieve "SomeUserParameter" into a feature attribute)
  2. PythonCaller (work with feature attribute)
  3. AttributeRemover (to remove the temporary feature attribute)

In other word, do not reference locally published parameters from a PythonCaller inside a custom transformer.


Reply


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