Skip to main content
Solved

Can anyone out there explain scripted python parameters to me?

  • February 13, 2013
  • 5 replies
  • 10 views

I've read everything out there on scripted parameters in FME (which is not much) and none of it has helped me.

 

 

I'm trying to have an AttributeRenamer, where the Attribute List is a scripted python parameter, a fairly simple one at that.  Say, my original field is "ONE_AAA". (but it could be "TWO_AAA", "THREE_AAA", etc).  I want it to rename it to just "AAA", so I have a this python script:

 

 

fld = FME_MacroValues['Test_Condition_1']

suffx = fld[(fld.find("_")+1):fld.__len__()]

return fld + ',' + suffx + ',""'

 

 

'Test_Condition_1' is simply a published parameter that the user enters to let FME know what field he wants renamed.  I've linked this python scripted parameter to the Attribute List and it does not work.  Can anyone tell me what I am doing wrong?

Best answer by david_r

I think your issue is more with the AttributeRenamer than with the Python scripted parameter here. In your place I would have dropped the scripted parameter and the AttributeRenamer altogether, replacing it with a PythonCaller with the following code:

 

 

import fmeobjects   def DynamicAttributeRenamer(feature):     tc1 = FME_MacroValues['Test_Condition_1']     old_name, new_name = tc1.split('_')     feature.setAttribute(new_name, feature.getAttribute(old_name))     feature.removeAttribute(old_name)

 

 

This will read a published parameter "Test_Condition_1" and, assuming a value of "ONE_AAA", will rename the attribute "ONE" to "AAA".

 

 

Hope this helps.

 

 

David
View original
Did this help you find an answer to your question?
This post is closed to further activity.
It may be a question with a best answer, an implemented idea, or just a post needing no comment.
If you have a follow-up or related question, 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
  • Best Answer
  • February 14, 2013
I think your issue is more with the AttributeRenamer than with the Python scripted parameter here. In your place I would have dropped the scripted parameter and the AttributeRenamer altogether, replacing it with a PythonCaller with the following code:

 

 

import fmeobjects   def DynamicAttributeRenamer(feature):     tc1 = FME_MacroValues['Test_Condition_1']     old_name, new_name = tc1.split('_')     feature.setAttribute(new_name, feature.getAttribute(old_name))     feature.removeAttribute(old_name)

 

 

This will read a published parameter "Test_Condition_1" and, assuming a value of "ONE_AAA", will rename the attribute "ONE" to "AAA".

 

 

Hope this helps.

 

 

David

  • Author
  • February 14, 2013

David,

 

 

I thought about PythonCaller, but again documentation was very inadequate, so I wasn't sure where to begin with it.  This might be the right way to go, but I still have some questions.

 

 

First of all, I think you misunderstood what I was trying to do.  The field is named "ONE_AAA" in the table and I want to rename it to "AAA" (I'm not going from "ONE" to "AAA").  Because some fields can have more underscores after the initial prefix, I chose not to use the split() function.

 

 

Anyway, questions what do I put in as the entry point?  Would that be just "DynamicAttributeRenamer", right?  And am I exposing any attributes?

 

 

Thanks,

 

John

  • Author
  • February 14, 2013
David,

 

 

I just got your script to run and it doesn't do what I want.  It's erasing my existing data and replacing it with "ONE" throughout the table.  Maybe I worded what I was trying to do incorrectly.  I do not want to change the data in the tables at all.  I just want to change the field names from "ONE_AAA" to "AAA" and copy the data over from a different bit of logic I have that works fine.

 

 

Sorry for the confusion,

 

John

  • Author
  • February 14, 2013
Ok, after a big of messing around with the data, I got it to work.  I had null values which was causing the script to crash my workspace and I had to handle it.  This works great.  Thanks David.

 

 

Though, I am still would love for someone to explain scripted parameters to me.

david_r
Celebrity
  • February 15, 2013
Hi Johnny,

 

 

good to hear that you got it to work.

 

 

Scripted parameters are (published or private) parameters with a value that is calculated by a script rather than given as-is on the command line. They really aren't much more complicated than that.

 

 

Here is an example use case for a scripted parameter:

 

http://fmepedia.safe.com/articles/How_To/Pass-a-List-of-Tables-or-Layers-to-an-FME-Reader-using-Python-Scripted-Parameters

 

 

Examples of other use cases:
  • reading settings from a configuration file
  • retrieving an authentication token from a webservice
  • calculating the output file name base on the input parameters
  • etc...

 

Hope this helps.

 

 

David

 

 


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