Solved

Scripted Parameter

  • 5 December 2018
  • 5 replies
  • 43 views

Userlevel 6
Badge +32

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.

icon

Best answer by david_r 5 December 2018, 15:30

View original

5 replies

Userlevel 4

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

 

Userlevel 6
Badge +32

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!

Userlevel 6
Badge +32

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.

Userlevel 4

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.

Userlevel 6
Badge +32

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.

Reply