Solved

Create Multiple Parameters in Scripted Python Parameters

  • 2 February 2018
  • 7 replies
  • 8 views

Badge +1
  • Participant
  • 126 replies

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?

icon

Best answer by takashi 3 February 2018, 00:50

View original

7 replies

Badge

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.

Badge +1

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"!

Badge

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!

 

 

Badge +1

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.

 

 

Userlevel 3
Badge +17

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.

Badge +1

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!

 

Userlevel 5

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.

Reply