Skip to main content
Solved

Modify Schema list attributes


saravanan
Contributor
Forum|alt.badge.img+14

I have a requirement to modify the list attribute generated on schema port.

Lets say my schema scanner have default attributes name, fme data type and native data type.

I want to look on name attribute and find any of its elements have OBJECTID string and remove if it is present.

I try to modify list attributes using python caller. It modifies the list elements when I overwrite using feature.setAttribute method it is not working and returns original schema attributes unmodified.

 

Any help using python would be highly appreciable.

 

Best answer by david_r

Try something like this in a PythonCaller:

# Generate a list containing a tuple for each attribute definition
names = feature.getAttribute('attribute{}.name')
fme_data_types = feature.getAttribute('attribute{}.fme_data_type')
native_data_types = feature.getAttribute('attribute{}.native_data_type')
input_schema = zip(names, fme_data_types, native_data_types)

# Filter out named attributes
attributes_to_delete = ('OBJECTID', 'MyAttribute1', 'MyAttribute2')
output_schema = [attr_def 
                 for attr_def 
                 in input_schema 
                 if attr_def[0] not in attributes_to_delete]

# Need to explicitely remove all existing list items first, since
# simply setting new values does not automatically remove
# existing items outside of the range of new items.
feature.removeAttribute('attribute{}.name')
feature.removeAttribute('attribute{}.fme_data_type')
feature.removeAttribute('attribute{}.native_data_type')

# Set the schema feature attribute definitions
feature.setAttribute('attribute{}.name', 
                     [attr_def[0] for attr_def in output_schema])
feature.setAttribute('attribute{}.fme_data_type', 
                     [attr_def[1] for attr_def in output_schema])
feature.setAttribute('attribute{}.native_data_type', 
                     [attr_def[2] for attr_def in output_schema])

Make sure to modify the contents of attributes_to_delete as needed.

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

4 replies

david_r
Celebrity
  • January 7, 2025

Have you checked on the writer, some of them have a setting to remove/ignore named attributes. That way you wouldn’t have to modify the schema feature.

 


saravanan
Contributor
Forum|alt.badge.img+14
  • Author
  • Contributor
  • January 7, 2025
david_r wrote:

Have you checked on the writer, some of them have a setting to remove/ignore named attributes. That way you wouldn’t have to modify the schema feature.

 

Yeah this one helps to remove when known attributes. My requirement is to remove unknown attributes that generates on the fly.  

 


david_r
Celebrity
  • Best Answer
  • January 7, 2025

Try something like this in a PythonCaller:

# Generate a list containing a tuple for each attribute definition
names = feature.getAttribute('attribute{}.name')
fme_data_types = feature.getAttribute('attribute{}.fme_data_type')
native_data_types = feature.getAttribute('attribute{}.native_data_type')
input_schema = zip(names, fme_data_types, native_data_types)

# Filter out named attributes
attributes_to_delete = ('OBJECTID', 'MyAttribute1', 'MyAttribute2')
output_schema = [attr_def 
                 for attr_def 
                 in input_schema 
                 if attr_def[0] not in attributes_to_delete]

# Need to explicitely remove all existing list items first, since
# simply setting new values does not automatically remove
# existing items outside of the range of new items.
feature.removeAttribute('attribute{}.name')
feature.removeAttribute('attribute{}.fme_data_type')
feature.removeAttribute('attribute{}.native_data_type')

# Set the schema feature attribute definitions
feature.setAttribute('attribute{}.name', 
                     [attr_def[0] for attr_def in output_schema])
feature.setAttribute('attribute{}.fme_data_type', 
                     [attr_def[1] for attr_def in output_schema])
feature.setAttribute('attribute{}.native_data_type', 
                     [attr_def[2] for attr_def in output_schema])

Make sure to modify the contents of attributes_to_delete as needed.


saravanan
Contributor
Forum|alt.badge.img+14
  • Author
  • Contributor
  • January 7, 2025
david_r wrote:

Try something like this in a PythonCaller:

# Generate a list containing a tuple for each attribute definition
names = feature.getAttribute('attribute{}.name')
fme_data_types = feature.getAttribute('attribute{}.fme_data_type')
native_data_types = feature.getAttribute('attribute{}.native_data_type')
input_schema = zip(names, fme_data_types, native_data_types)

# Filter out named attributes
attributes_to_delete = ('OBJECTID', 'MyAttribute1', 'MyAttribute2')
output_schema = [attr_def 
                 for attr_def 
                 in input_schema 
                 if attr_def[0] not in attributes_to_delete]

# Need to explicitely remove all existing list items first, since
# simply setting new values does not automatically remove
# existing items outside of the range of new items.
feature.removeAttribute('attribute{}.name')
feature.removeAttribute('attribute{}.fme_data_type')
feature.removeAttribute('attribute{}.native_data_type')

# Set the schema feature attribute definitions
feature.setAttribute('attribute{}.name', 
                     [attr_def[0] for attr_def in output_schema])
feature.setAttribute('attribute{}.fme_data_type', 
                     [attr_def[1] for attr_def in output_schema])
feature.setAttribute('attribute{}.native_data_type', 
                     [attr_def[2] for attr_def in output_schema])

Make sure to modify the contents of attributes_to_delete as needed.

Thank you ​@david_r This is what I exactly looking for. removeAttribute is mandate before setAttribute. 

 


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