Skip to main content
Solved

Scripted Parameter

  • December 5, 2018
  • 5 replies
  • 175 views

nielsgerrits
VIP
Forum|alt.badge.img+60

I'm looking for a way to set a datasource based on where the workspace is run.

My current setup:

  • Private Parameters PathDev, PathTest and PathProd.
  • AttributeCreator creating Path. Value is conditional:
    • if $(FME_ENGINE) has no value then PathDev
    • else if $(FME_ENGINE) contains test then PathTest
    • else PathProd
  • FeatureReader reads from Path / FeatureWriter writes to Path.

This works well but it has a disadvantage when writing. I can assign Path in the beginning of the workflow and have to keep the Path attribute through the entire stream. Or I can assign just before the FeatureWriter but then it has to run the conditional for all features, which is useless expensive.

Can someone give me a pointer how to do this? My Python is nonexisting...

I hoped something like this:

if FME_MacroValues['FME_ENGINE'].str == "":
    return FME_MacroValues['PathDev']
if FME_MacroValues['FME_ENGINE'].str like "test":
    return FME_MacroValues['PathTest']
else:
    return FME_MacroValues['PathProd']

Thanks in advance.

Best answer by david_r

You probably just need to fix the syntax a bit:

if FME_MacroValues.get('FME_ENGINE', '') == '':
    return FME_MacroValues.get('PathDev')
elif FME_MacroValues.get('FME_ENGINE') == 'test':
    return FME_MacroValues.get('PathTest')
else:
    return FME_MacroValues.get('PathProd')

The difference is that if you use the .get() method on the FME_MacroValues dictionary you either get None or the second argument back if the key does not exist. If you use the other syntax, like FME_MacroValues['key'] then you'll get an exception if the key doesn't exist.

There are several nice tutorials on how to use dicts out there, here's one: https://www.pythonforbeginners.com/dictionary/how-to-use-dictionaries-in-python

 

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

5 replies

david_r
Celebrity
  • 8391 replies
  • Best Answer
  • December 5, 2018

You probably just need to fix the syntax a bit:

if FME_MacroValues.get('FME_ENGINE', '') == '':
    return FME_MacroValues.get('PathDev')
elif FME_MacroValues.get('FME_ENGINE') == 'test':
    return FME_MacroValues.get('PathTest')
else:
    return FME_MacroValues.get('PathProd')

The difference is that if you use the .get() method on the FME_MacroValues dictionary you either get None or the second argument back if the key does not exist. If you use the other syntax, like FME_MacroValues['key'] then you'll get an exception if the key doesn't exist.

There are several nice tutorials on how to use dicts out there, here's one: https://www.pythonforbeginners.com/dictionary/how-to-use-dictionaries-in-python

 


nielsgerrits
VIP
Forum|alt.badge.img+60
  • Author
  • 2937 replies
  • December 5, 2018

You probably just need to fix the syntax a bit:

if FME_MacroValues.get('FME_ENGINE', '') == '':
    return FME_MacroValues.get('PathDev')
elif FME_MacroValues.get('FME_ENGINE') == 'test':
    return FME_MacroValues.get('PathTest')
else:
    return FME_MacroValues.get('PathProd')

The difference is that if you use the .get() method on the FME_MacroValues dictionary you either get None or the second argument back if the key does not exist. If you use the other syntax, like FME_MacroValues['key'] then you'll get an exception if the key doesn't exist.

There are several nice tutorials on how to use dicts out there, here's one: https://www.pythonforbeginners.com/dictionary/how-to-use-dictionaries-in-python

 

Thanks David!


nielsgerrits
VIP
Forum|alt.badge.img+60
  • Author
  • 2937 replies
  • December 7, 2018

You probably just need to fix the syntax a bit:

if FME_MacroValues.get('FME_ENGINE', '') == '':
    return FME_MacroValues.get('PathDev')
elif FME_MacroValues.get('FME_ENGINE') == 'test':
    return FME_MacroValues.get('PathTest')
else:
    return FME_MacroValues.get('PathProd')

The difference is that if you use the .get() method on the FME_MacroValues dictionary you either get None or the second argument back if the key does not exist. If you use the other syntax, like FME_MacroValues['key'] then you'll get an exception if the key doesn't exist.

There are several nice tutorials on how to use dicts out there, here's one: https://www.pythonforbeginners.com/dictionary/how-to-use-dictionaries-in-python

 

I got it working after adding a colon at the end of line 4. Also changed the comparison a bit.

if FME_MacroValues.get('FME_ENGINE', '') == '':    return FME_MacroValues.get('PathDev')elif 'test' in FME_MacroValues.get('FME_ENGINE'):    return FME_MacroValues.get('PathTest')else:    return FME_MacroValues.get('PathProd')

Attached workspace as there never can be too much working examples.

scriptedparameter.fmwt

Thanks again.


david_r
Celebrity
  • 8391 replies
  • December 7, 2018

I got it working after adding a colon at the end of line 4. Also changed the comparison a bit.

if FME_MacroValues.get('FME_ENGINE', '') == '':    return FME_MacroValues.get('PathDev')elif 'test' in FME_MacroValues.get('FME_ENGINE'):    return FME_MacroValues.get('PathTest')else:    return FME_MacroValues.get('PathProd')

Attached workspace as there never can be too much working examples.

scriptedparameter.fmwt

Thanks again.

Ooops, sorry about that. Fixed the missing colon.


nielsgerrits
VIP
Forum|alt.badge.img+60
  • Author
  • 2937 replies
  • December 18, 2018

You probably just need to fix the syntax a bit:

if FME_MacroValues.get('FME_ENGINE', '') == '':
    return FME_MacroValues.get('PathDev')
elif FME_MacroValues.get('FME_ENGINE') == 'test':
    return FME_MacroValues.get('PathTest')
else:
    return FME_MacroValues.get('PathProd')

The difference is that if you use the .get() method on the FME_MacroValues dictionary you either get None or the second argument back if the key does not exist. If you use the other syntax, like FME_MacroValues['key'] then you'll get an exception if the key doesn't exist.

There are several nice tutorials on how to use dicts out there, here's one: https://www.pythonforbeginners.com/dictionary/how-to-use-dictionaries-in-python

 

Tripped over something when implementing this: 

The order of the Parameters in the Navigator is important. Parameters used in the Scripted Parameter must be placed above the Scripted Parameter. This is because the order in the Navigator determines the order of reading.

0684Q00000ArMbEQAV.png

Took me some iterations before realizing this must be the cause.