Solved

Python 3.6 from 2.7, PythonCaller help

  • 4 October 2018
  • 4 replies
  • 1 view

Hi all,

 

I had a script working on 2.7 (our previous default) and have now switched to 3.6 to keep up to date.

I am getting a syntax error on line 18, but I can't see why, it is probably something obvious I am missing...

 

Also any feedback on the script would be cool, I am still new to python so would be appreciated!

 

Thanks in advance
#import libraries that we will potentially need
import difflib
import fme
import fmeobjects


#create the class for execution, as per the FME spec:
#https://docs.safe.com/fme/html/FME_Desktop_Documentation/FME_Transformers/Transformers/pythoncaller.htm
class addressMatch(object):
    def __init__(self):
        self.dataD = []
        self.pread = ''
        self.outputIndex = 0
        self.tmp = 0
        self.matchRate = 0
        
    def input(self,feature):
        if self.pread <> feature.getAttribute('CURRENT_ADDRESS') and self.pread <> '':
#Error here
            for index, item in enumerate(self.dataD):
                self.tmp = difflib.SequenceMatcher(None,item.getAttribute('CURRENT_ADDRESS'),item.getAttribute('full_address')).quick_ratio()
                if self.tmp > self.matchRate:
                    self.matchRate = self.tmp
                    self.outputIndex = index
                if self.tmp >= 0.95:
                    break
            self.dataD[self.outputIndex].setAttribute("matchRating",self.matchRate)
            self.pyoutput(self.dataD[self.outputIndex])
            self.dataD = []  
        self.dataD.append(feature)     
        self.pread = feature.getAttribute('CURRENT_ADDRESS')
        
    def close(self):
        self.outputIndex = 0
        self.tmp = 0
        self.matchRate = 0
        for idx, item in enumerate(self.dataD):
            self.tmp = difflib.SequenceMatcher(None,item.getAttribute('CURRENT_ADDRESS'),item.getAttribute('full_address')).quick_ratio()
            if self.tmp > self.matchRate:
                self.matchRate = self.tmp
                self.outputIndex = idx
            if self.tmp >= 0.95:
                break
        self.dataD[self.outputIndex].setAttribute("matchRating",self.matchRate)
        self.pyoutput(self.dataD[self.outputIndex])
icon

Best answer by david_r 4 October 2018, 17:01

View original

4 replies

Userlevel 4

Try replacing the line:

if self.pread <> feature.getAttribute('CURRENT_ADDRESS') and self.pread <> '':

with:

if self.pread != feature.getAttribute('CURRENT_ADDRESS') and self.pread != '':

In short, the comparison operator "<>" has been replaced with "!=" in Python 3.

Userlevel 4

Also, I highly recommend using the tool 2to3 for automatic conversion from Python 2 to 3:

https://docs.python.org/2/library/2to3.html

It can be a big time saver.

Also, I highly recommend using the tool 2to3 for automatic conversion from Python 2 to 3:

https://docs.python.org/2/library/2to3.html

It can be a big time saver.

 

Thanks for this, I have pinned it for future use!

Try replacing the line:

if self.pread <> feature.getAttribute('CURRENT_ADDRESS') and self.pread <> '':

with:

if self.pread != feature.getAttribute('CURRENT_ADDRESS') and self.pread != '':

In short, the comparison operator "<>" has been replaced with "!=" in Python 3.

Ahhh! Thought it would be something like that... Here is me searching for changes to the functions..

 

Thanks for your help :)

 

 

Reply