Skip to main content
Solved

getting the value of a list attribute with python

  • December 29, 2016
  • 3 replies
  • 89 views

arthy
Contributor
Forum|alt.badge.img+8
  • Contributor

Hi,

I would like to write a python function to extract the value a list attribute knowing its e.g. Working with a dgn file for instance, I would like to extract the value igds_linkage{ind_entity}.key where ind_entity is a know index.

I already wrote a script to do that but it requires 3 transformers and I want to optimize my workspace.

Thanks

Best answer by takashi

Hi @arthy, a list element can be considered as an attribute whose name contains index number surrounded by curly brackets, so the point is how to construct the name using the index number. For example,  assuming that the attribute called "ind_entity" stores the index indicating the element to be extracted from the list, the PythonCaller with this script extracts the value of the element and save it to a new attribute called "key".

def processFeature(feature):
    index = feature.getAttribute('ind_entity')
    key = feature.getAttribute('igds_linkage{%s}.key' % index)
    feature.setAttribute('key', key)

Once you have understood the mechanism, you can also use the AttributeCreator.

0684Q00000ArJkpQAF.png

@Value(igds_linkage{@Value(ind_entity)}.key)
View original
Did this help you find an answer to your question?

3 replies

takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • Best Answer
  • December 29, 2016

Hi @arthy, a list element can be considered as an attribute whose name contains index number surrounded by curly brackets, so the point is how to construct the name using the index number. For example,  assuming that the attribute called "ind_entity" stores the index indicating the element to be extracted from the list, the PythonCaller with this script extracts the value of the element and save it to a new attribute called "key".

def processFeature(feature):
    index = feature.getAttribute('ind_entity')
    key = feature.getAttribute('igds_linkage{%s}.key' % index)
    feature.setAttribute('key', key)

Once you have understood the mechanism, you can also use the AttributeCreator.

0684Q00000ArJkpQAF.png

@Value(igds_linkage{@Value(ind_entity)}.key)

arthy
Contributor
Forum|alt.badge.img+8
  • Author
  • Contributor
  • December 29, 2016
takashi wrote:

Hi @arthy, a list element can be considered as an attribute whose name contains index number surrounded by curly brackets, so the point is how to construct the name using the index number. For example,  assuming that the attribute called "ind_entity" stores the index indicating the element to be extracted from the list, the PythonCaller with this script extracts the value of the element and save it to a new attribute called "key".

def processFeature(feature):
    index = feature.getAttribute('ind_entity')
    key = feature.getAttribute('igds_linkage{%s}.key' % index)
    feature.setAttribute('key', key)

Once you have understood the mechanism, you can also use the AttributeCreator.

0684Q00000ArJkpQAF.png

@Value(igds_linkage{@Value(ind_entity)}.key)
@Takashi,

 

 

Thank you very much.

 

Before posting my question, I wrote exactly this syntax using the attribute creator but at the end, I was getting it a constant equals to <@Value(igds_linkage{@Value(ind_entity)}.key)>

 

Could you tell me what I was doing wrong when defing the transformer?

 

 


takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • December 30, 2016
arthy wrote:
@Takashi,

 

 

Thank you very much.

 

Before posting my question, I wrote exactly this syntax using the attribute creator but at the end, I was getting it a constant equals to <@Value(igds_linkage{@Value(ind_entity)}.key)>

 

Could you tell me what I was doing wrong when defing the transformer?

 

 

@arthy, cannot specify the exact reason unless seeing the actual parameters setting, but a possible reason I can think of is that there could be a typo in the expression. Note that FME function names ('@Value' in this case) should be written in case-sensitive, and the expression will be treated as a constant string value if there was a typo including wrong case.

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