Skip to main content
Solved

PythonCaller input loop


Forum|alt.badge.img

I have a PythonCaller with an input function as usual.

 

In this function, I have a list to update.
myList = [toto, tata] if cond1: 	myList.remove(toto)
	logger1 	myList.append(momo)
	logger2 if cond2:
	myList.remove(tata)
	logger3
	myList.append(mama)	
	logger4

So, I want to obtain at last, myList = [momo, mama]

 

But, if I check the loggers : 

 

logger1 : [tata] (all right)

 

logger2 : [tata, momo] (still all right)

 

logger3 : [toto] (as if the list reseted)

 

logger4 : [toto, mama] (so not quite good)

I think the problem come from the fact that I have two objects coming in the FME project. Is that right?

How can I save the first change?

Am I missing something?

 

Best answer by david_r

I suspect this question would be more suitable in a Python forum.

Anyway, it's difficult to help unless you post the whole code and tell us precisely what you want to achieve.

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

7 replies

david_r
Evangelist
  • Best Answer
  • August 29, 2016

I suspect this question would be more suitable in a Python forum.

Anyway, it's difficult to help unless you post the whole code and tell us precisely what you want to achieve.


verdoodtdries
Supporter
Forum|alt.badge.img+18

I think you are mixing up local and global variables, but please post your whole code so we can see what's going wrong.


Forum|alt.badge.img
  • Author
  • August 29, 2016
david_r wrote:

I suspect this question would be more suitable in a Python forum.

Anyway, it's difficult to help unless you post the whole code and tell us precisely what you want to achieve.

def input(self,feature):

	...

	myAnswerField = [('Echelle1'None), ('Echelle2'None)] 	if '01' in feature.getAttribute('id_reseau'):
            scale1 = feature.getAttribute('echellesortieplanprincipal')
            myAnswerFields.remove(('Echelle1'None))
            myAnswerFields.append(('Echelle1', scale1))
        if '02' in feature.getAttribute('id_reseau'):
            scale2 = feature.getAttribute('echellesortieplanprincipal')
            myAnswerFields.remove(('Echelle2'None))
            myAnswerFields.append(('Echelle2', scale2))

 
with this database : 

 

0684Q00000ArMXTQA3.png


Forum|alt.badge.img
  • Author
  • August 29, 2016
david_r wrote:

I suspect this question would be more suitable in a Python forum.

Anyway, it's difficult to help unless you post the whole code and tell us precisely what you want to achieve.

I thought it was a PythonCaller problem actually. Maybe I misunderstood.

 


david_r
Evangelist
  • August 29, 2016
slerendu wrote:
I thought it was a PythonCaller problem actually. Maybe I misunderstood.

 

The way the PythonCaller works is pretty simple and it is coherent with the way Python (and most OOP languagues) work in this type of pattern:

 

 

  • Before the first feature arrives: call __init__() once
  • For each feature: call input() once per feature
  • After the last feature has passed: call close() once
You may want to read up a bit on Python objects and particulary on scope, e.g. here. I agree with @verdoodtdries that it looks like a scoping error.

 

 

I do not quite understand what you really want to achieve, but I suspect you want to make myAnswerField an object instance variable (rather than a local variable, as it is now). You can do this by initalizing myAnswerField in __init__() and prefixing it with "self." whenever you reference it.

 

 

As it is you are effectively resetting (redefining) myAnswerField for every incoming feature, that may not be what you want?

Forum|alt.badge.img
  • Author
  • August 29, 2016
david_r wrote:
The way the PythonCaller works is pretty simple and it is coherent with the way Python (and most OOP languagues) work in this type of pattern:

 

 

  • Before the first feature arrives: call __init__() once
  • For each feature: call input() once per feature
  • After the last feature has passed: call close() once
You may want to read up a bit on Python objects and particulary on scope, e.g. here. I agree with @verdoodtdries that it looks like a scoping error.

 

 

I do not quite understand what you really want to achieve, but I suspect you want to make myAnswerField an object instance variable (rather than a local variable, as it is now). You can do this by initalizing myAnswerField in __init__() and prefixing it with "self." whenever you reference it.

 

 

As it is you are effectively resetting (redefining) myAnswerField for every incoming feature, that may not be what you want?
Ok, I understand. And you both are right for intention in this context. I'll try. Thanks.

 

 


Forum|alt.badge.img
  • Author
  • August 31, 2016
david_r wrote:
The way the PythonCaller works is pretty simple and it is coherent with the way Python (and most OOP languagues) work in this type of pattern:

 

 

  • Before the first feature arrives: call __init__() once
  • For each feature: call input() once per feature
  • After the last feature has passed: call close() once
You may want to read up a bit on Python objects and particulary on scope, e.g. here. I agree with @verdoodtdries that it looks like a scoping error.

 

 

I do not quite understand what you really want to achieve, but I suspect you want to make myAnswerField an object instance variable (rather than a local variable, as it is now). You can do this by initalizing myAnswerField in __init__() and prefixing it with "self." whenever you reference it.

 

 

As it is you are effectively resetting (redefining) myAnswerField for every incoming feature, that may not be what you want?
Ok, it's seems that I should have just one feature in the input instead of 2. A colleague of mine helped me on that. So problem solved.

 

Thanks anyway. I have a better understanding of the PythonCller thanks to you.

 

 


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