Question

Python Caller: Output list of attributes

  • 17 November 2022
  • 9 replies
  • 63 views

Badge

I have a Python caller that eventually makes a dictionary 

Input: {1:a, 2:b, 3:c}

Desired output:

ss 

A representation of the code is below. I am not using the class. Just a simple function which coverts input list to dictionary. I also expose the key and value attributes. This is only representation because the actual code is quite complex. The code does what I want but I am having an issue writing them out as attributes

import fme, fmeobjects
 
#Update connection properties
def processFeature(feature):
    
    # Initialize values
    list= feature.getAttribute('list')
 
    #Code to make list to dictionary
 
    #Output dictionary
    for key,value in dict.items():
        feature.setAttribute('key', key)
        feature.setAttribute('value',value)
    return

Any pointers would be extremely helpful!


9 replies

Userlevel 4

What do you mean by "writing them out"? Do you mean writing it in the FME log? Or somewhere else?

You can send any text to the FME log like this (inside your for-loop):

fmeobjects.FMELogFile().logMessageString('{} {}'.format(key, value), fmeobjects.FME_INFORM)

 See also https://docs.safe.com/fme/html/fmepython/api/fmeobjects/_log/fmeobjects.FMELogFile.logMessageString.html#fmeobjects.FMELogFile.logMessageString

Badge

What do you mean by "writing them out"? Do you mean writing it in the FME log? Or somewhere else?

You can send any text to the FME log like this (inside your for-loop):

fmeobjects.FMELogFile().logMessageString('{} {}'.format(key, value), fmeobjects.FME_INFORM)

 See also https://docs.safe.com/fme/html/fmepython/api/fmeobjects/_log/fmeobjects.FMELogFile.logMessageString.html#fmeobjects.FMELogFile.logMessageString

Thanks for the quick response. I meant the output from Python caller must be written out as attributes. Say my dictionary has 200 values, there should be 200 records of key and 200 records of value under the key and value attribute names respectively.

Userlevel 4

Thanks for the quick response. I meant the output from Python caller must be written out as attributes. Say my dictionary has 200 values, there should be 200 records of key and 200 records of value under the key and value attribute names respectively.

You'll need to use the class method, in that case. The for-loop would then look something like this:

#Output dictionary
    for key,value in dict.items():
        new_feature = feature.clone()
        new_feature.setAttribute('key', key)
        new_feature.setAttribute('value',value)
        self.pyoutput(new_feature)

This will create a new feature for each item in your dictionary.

Make sure that you remove the default "self.pyoutput(feature)" at the end.

Badge

Thanks for the quick response. I meant the output from Python caller must be written out as attributes. Say my dictionary has 200 values, there should be 200 records of key and 200 records of value under the key and value attribute names respectively.

I thought it could be done without a class. Is that not a possibility?

Userlevel 4

Thanks for the quick response. I meant the output from Python caller must be written out as attributes. Say my dictionary has 200 values, there should be 200 records of key and 200 records of value under the key and value attribute names respectively.

Not that I know of.

You can probably simply copy-paste the existing code into the input() method of the class template.

Badge

Thanks for the quick response. I meant the output from Python caller must be written out as attributes. Say my dictionary has 200 values, there should be 200 records of key and 200 records of value under the key and value attribute names respectively.

Yes, that works! Seems to be the only way then :|

Userlevel 1
Badge +10

Thanks for the quick response. I meant the output from Python caller must be written out as attributes. Say my dictionary has 200 values, there should be 200 records of key and 200 records of value under the key and value attribute names respectively.

Curious why you would want to avoid using the class?

Badge

Thanks for the quick response. I meant the output from Python caller must be written out as attributes. Say my dictionary has 200 values, there should be 200 records of key and 200 records of value under the key and value attribute names respectively.

For simple code, it seems to be an overkill. I have managed to get the python caller to work with the ability of getting the value written into an attribute (singular) before without the need for defining the class.

Userlevel 4

Thanks for the quick response. I meant the output from Python caller must be written out as attributes. Say my dictionary has 200 values, there should be 200 records of key and 200 records of value under the key and value attribute names respectively.

For what it's worth, I never use the method interface, regardless of complexity. Sure, the method interface works fine, but as you discovered, you'll reach its limits fairly quickly :-)

Reply