Skip to main content
Solved

Scripted Parameter

  • December 5, 2018
  • 5 replies
  • 131 views

nielsgerrits
VIP
Forum|alt.badge.img+54

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

 

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

5 replies

david_r
Celebrity
  • 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+54
david_r wrote:

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+54
david_r wrote:

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
  • December 7, 2018
nielsgerrits wrote:

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+54
  • Author
  • December 18, 2018
david_r wrote:

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.


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