Python syntax doesn't work after upgrade to newest version


#1


(Not a codeacademy exercise)


An SPSS syntax file calls the python program, and it returns an "Error Computing Case" line for each case.


Replace this line with your code. Do not remove the backticks that are above or beneath this line.

import spss
import sys

def getNameMatchVariables(lnam1="LNAM_A",lnam2="LNAM",fnam1="FNAM_A",fnam2="FNAM"):
    if "R:\python" in sys.path:
	print "R:\python Already A Recognized Directory"
    else:
	sys.path.append("R:\python")
	print "R:\Python added as a Directory"
    import soundex
    import levin
    import spss
    varnames = []
    varcount = spss.GetVariableCount()
    for i in xrange(varcount):
        varnames.append(spss.GetVariableName(i))
    lnam1index = varnames.index(lnam1)
    lnam2index = varnames.index(lnam2)
    fnam1index = varnames.index(fnam1)
    fnam2index = varnames.index(fnam2)
    datacur = spss.Cursor(accessType = 'w')
    datacur.SetVarNameAndType(['JAWAL','DICEL','LENL','JAWAF','DICEF','LENF','L1SOUND','L2SOUND','F1SOUND','F2SOUND'],[0,0,0,0,0,0,4,4,4,4])
    datacur.CommitDictionary()
    for i in range(datacur.GetCaseCount()):
        casecursor = datacur.fetchone()
        try:
            str1 = levin.cleanString(casecursor[lnam1index])
            str2 = levin.cleanString(casecursor[lnam2index])
            str3 = levin.cleanString(casecursor[fnam1index])
            str4 = levin.cleanString(casecursor[fnam2index])
            datacur.SetValueNumeric('JAWAL',100*levin.getJaWasteve(str1,str2))
            datacur.SetValueNumeric('DICEL',100*levin.getDice(str1,str2))
            datacur.SetValueNumeric('LENL',100*levin.getLenWt(str1,str2))
            datacur.SetValueNumeric('JAWAF',100*levin.getJaWasteve(str3,str4))
            datacur.SetValueNumeric('DICEF',100*levin.getDice(str3,str4))
            datacur.SetValueNumeric('LENF',100*levin.getLenWt(str3,str4))
            datacur.SetValueChar('F1SOUND',soundex.get_soundex(casecursor[fnam2index]))
            datacur.SetValueChar('L1SOUND',soundex.get_soundex(casecursor[lnam2index]))
            datacur.SetValueChar('F2SOUND',soundex.get_soundex(casecursor[fnam1index]))
            datacur.SetValueChar('L2SOUND',soundex.get_soundex(casecursor[lnam1index]))
            datacur.CommitCase()
        except:
            print "Error Computing Case"
            try:
                print casecursor[varnames.index("CASE")]
            except:
                print "CASE variable not in file"
    datacur.close()

def getMasterNYSID():
    if "R:\python" in sys.path:
    	print "R:\python Already A Recognized Directory"
    else:
	sys.path.append("R:\python")
	print "R:\Python added as a Directory"
    varnames = []
    varcount = spss.GetVariableCount()
    for i in xrange(varcount):
        varnames.append(spss.GetVariableName(i))
    datacur = spss.Cursor(accessType = 'w')
    datacur.SetVarNameAndType(['NYSIDM'],[8])
    datacur.CommitDictionary()
    nysidlist = ['NYSIDR','NYSIDN','NYSIDN2','NYSIDN3','NYSIDN4','NYSIDN5']
    nysiddict = {}
    exclude = ['',' ','  ','       ','        ',None,'0000000','0000000 ']
    for n in nysidlist:
        nysiddict[n]=varnames.index(n)
    x = 0
    for i in range(datacur.GetCaseCount()):
        casecursor = datacur.fetchone()
        x=x+1
        try:
            nysids = []
            for n in nysiddict:
                if casecursor[nysiddict[n]] not in exclude:
                    nysids.append(casecursor[nysiddict[n]])
                nysids.sort()
                datacur.SetValueChar('NYSIDM',nysids[0])
                datacur.CommitCase()
        except:
            print "ERROR " , nysids, n,x
    datacur.close()


#2

And I've already tried updating the print commands to the current version's specifications. Still doesn't work...


#3

There are numerous differences between Python 2 and 3.

I don't know what else to say... :slightly_smiling:


#4

I've tried to incorporate all of them, including commands at the top of the program like 'Import Future' and 'Import Modernize' which are supposed to make old syntax readable


#5

I'm sorry but I cannot help that much as I specialize in Python 2. :slightly_smiling:


#6

Is there a reason for not running it with the Python version it's written for?


#7

I've tried that as well, still no dice.