Skip to main content
Solved

Create Multiple Parameters in Scripted Python Parameters


I have my parameter values in a SQLite table. I am able to access each value though a Python scripted parameter that opens the table and retrieves the value. However, this means that I have to open and close the SQL table one time for each parameter. Is there a way to make multiple parameters that can be read by the readers and transformers, perhaps in the Python startup script?

Best answer by takashi

Hack. You can define the dictionary as a global variable which can be accessed while FME is configuring user parameters. e.g.

First scripted (Python) parameter:

global a
a = {'ap':'apple','ba':'banana','ch':'cherry'}
return a['ap']

Second scripted (Python) parameter:

return a['ba']

Third scripted (Python) parameter:

return a['ch']

Note: A global variable defined in a scripted (Python) parameter can be used only in scripted (Python) parameters. It cannot be used in the Startup/Shutdown Python script, PythonCreator, and PythonCaller in the workspace.

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

7 replies

Forum|alt.badge.img
  • February 2, 2018

Hi @jimo

you could create one helper Python scripted parameter that would carry e.g. comma separated values for all your parameters. Then every parameter would be a scripted parameter as well and would retrieve its part from the comma separated helper parameter value. Please find a very simplified example attached.


  • February 2, 2018

I get the concept. Now instead of returning a string from the helper scripted parameter, I can return a dictionary that I create from my SQLite query: 

a = {'ap':'apple','ba':'banana','ch':'cherry'return a

Then in the second scripted parameter I do an eval() on what is returned  by the first scripted parameter to turn it back into a dictionary, then extract the key:

dictParam = eval(fme.macroValues['PARAM3']) 

return dictParam["ap"]

The value of PARAM4 is therefore "apple"!


Forum|alt.badge.img
  • February 2, 2018
jimo wrote:

I get the concept. Now instead of returning a string from the helper scripted parameter, I can return a dictionary that I create from my SQLite query: 

a = {'ap':'apple','ba':'banana','ch':'cherry'return a

Then in the second scripted parameter I do an eval() on what is returned  by the first scripted parameter to turn it back into a dictionary, then extract the key:

dictParam = eval(fme.macroValues['PARAM3']) 

return dictParam["ap"]

The value of PARAM4 is therefore "apple"!

Does this work? I didn't know you can return a dictionary as a param value - this is awesome! Thank you for posting your reply!

 

 


  • February 2, 2018
jimo wrote:

I get the concept. Now instead of returning a string from the helper scripted parameter, I can return a dictionary that I create from my SQLite query: 

a = {'ap':'apple','ba':'banana','ch':'cherry'return a

Then in the second scripted parameter I do an eval() on what is returned  by the first scripted parameter to turn it back into a dictionary, then extract the key:

dictParam = eval(fme.macroValues['PARAM3']) 

return dictParam["ap"]

The value of PARAM4 is therefore "apple"!

Yes, you can return a dictionary, but the next scripted parameter receives it as a string. That's why you have to do an eval() on it in PARAM4, to turn it back into a dictionary.

 

 


takashi
Influencer
  • Best Answer
  • February 2, 2018

Hack. You can define the dictionary as a global variable which can be accessed while FME is configuring user parameters. e.g.

First scripted (Python) parameter:

global a
a = {'ap':'apple','ba':'banana','ch':'cherry'}
return a['ap']

Second scripted (Python) parameter:

return a['ba']

Third scripted (Python) parameter:

return a['ch']

Note: A global variable defined in a scripted (Python) parameter can be used only in scripted (Python) parameters. It cannot be used in the Startup/Shutdown Python script, PythonCreator, and PythonCaller in the workspace.


  • February 3, 2018
takashi wrote:

Hack. You can define the dictionary as a global variable which can be accessed while FME is configuring user parameters. e.g.

First scripted (Python) parameter:

global a
a = {'ap':'apple','ba':'banana','ch':'cherry'}
return a['ap']

Second scripted (Python) parameter:

return a['ba']

Third scripted (Python) parameter:

return a['ch']

Note: A global variable defined in a scripted (Python) parameter can be used only in scripted (Python) parameters. It cannot be used in the Startup/Shutdown Python script, PythonCreator, and PythonCaller in the workspace.

Even easier, @takashi!

 


david_r
Celebrity
  • February 5, 2018
takashi wrote:

Hack. You can define the dictionary as a global variable which can be accessed while FME is configuring user parameters. e.g.

First scripted (Python) parameter:

global a
a = {'ap':'apple','ba':'banana','ch':'cherry'}
return a['ap']

Second scripted (Python) parameter:

return a['ba']

Third scripted (Python) parameter:

return a['ch']

Note: A global variable defined in a scripted (Python) parameter can be used only in scripted (Python) parameters. It cannot be used in the Startup/Shutdown Python script, PythonCreator, and PythonCaller in the workspace.

Agreed, this is a good trick and I do something similar pretty much all the time.

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