Solved

PythonCaller User Parameters


Badge

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

icon

Best answer by david_r 21 March 2017, 16:25

View original

11 replies

Badge

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 

Badge

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!
Userlevel 5

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 :-)

Badge

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

 

Userlevel 5

 

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.
Badge +22

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

Userlevel 5

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?
Badge +22
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.

 

Badge

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

Badge

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

Userlevel 5

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