Need help fixing this error please:NameError: global name is not defined

S

shaun

Hi all,

I have a class which I create an object from in a different script but when its run I get an error at the last part of this method:

////CODE///////////////////////////////

def databasebatchcall(self,tid, bid):
con=cx_Oracle.connect('user/[email protected]/ODB4TEST.COMPANY.COM')
cur = con.cursor()
cur.execute("SELECT * FROM name)
results = cur.fetchall()
//////////////////////////////////////////

From this last line I get the following error which I don't understand I'm very new to python and have no idea about this any help would be appreciated


//////////////////////////////////////////////

File "/home/dcroke/mdcFDACStringCall.py", line 21, in fetchbatchdata
results = cur.fetchall()
NameError: global name 'cur' is not defined

//////////////////////////////////////////////


Thanks all,
Shaun
 
C

Chris Angelico

////CODE///////////////////////////////

def databasebatchcall(self,tid, bid):
con=cx_Oracle.connect('user/[email protected]/ODB4TEST.COMPANY.COM')
cur = con.cursor()
cur.execute("SELECT * FROM name)
results = cur.fetchall()
//////////////////////////////////////////

From this last line I get the following error which I don't understand I'm very new to python and have no idea about this any help would be appreciated


//////////////////////////////////////////////

File "/home/dcroke/mdcFDACStringCall.py", line 21, in fetchbatchdata
results = cur.fetchall()
NameError: global name 'cur' is not defined

//////////////////////////////////////////////

Not quite, actually. The traceback names a different function. Look in
your code for a 'fetchbatchdata' function; it looks like you need to
pass the cursor from one function to the other.

To further assist, we'd need to see more of the code; but for a guess,
I would say that you either need to make cur a function argument, or
else (since this is class) an instance member, which you'd reference
as 'self.cur'.

ChrisA
 
D

Dave Angel

Hi all,

I have a class which I create an object from in a different script but when its run I get an error at the last part of this method:

////CODE///////////////////////////////

def databasebatchcall(self,tid, bid):
con=cx_Oracle.connect('user/[email protected]/ODB4TEST.COMPANY.COM')
cur = con.cursor()
cur.execute("SELECT * FROM name)
results = cur.fetchall()
//////////////////////////////////////////



//////////////////////////////////////////////

File "/home/dcroke/mdcFDACStringCall.py", line 21, in fetchbatchdata
results = cur.fetchall()
NameError: global name 'cur' is not defined

//////////////////////////////////////////////


Thanks all,
Shaun

is that really the function in line 21, or is it just a similar line?
Notice the error gives you filename & line number. I have no way to
check either of those, but you can and should. What really stands out
is the function name the error occurs in: fetchbatchdata()

You don't show us the source to that function.
 
P

Peter Otten

shaun said:
I have a class which I create an object from in a different script but
when its run I get an error at the last part of this method:
def databasebatchcall(self,tid, bid):
con=cx_Oracle.connect(
'user/[email protected]/ODB4TEST.COMPANY.COM')
cur = con.cursor()
cur.execute("SELECT * FROM name)
results = cur.fetchall()

This is not your real code. The above would give you a SyntaxError in the
line
cur.execute("SELECT * FROM name)
From this last line I get the following error which I don't understand I'm
very new to python and have no idea about this any help would be appreciated
File "/home/dcroke/mdcFDACStringCall.py", line 21, in fetchbatchdata
results = cur.fetchall()
NameError: global name 'cur' is not defined

The offending line may not be indented correctly:

def databasebatchcall(...):
...
cur = con.cursor()
...
results = cur.fetchall()

This may be obscured by mixing tabs and spaces. However, without the actual
code this is nothing but a guess.
 
S

shaun

Sorry guys here is the full code for the class:

#!/usr/bin/python
# Echo client program
import cx_Oracle
import socket
import pprint
from struct import *
import sys
from binascii import *
import time
import datetime


class StringCall:
results=[]
def databasebatchcall(self,termid, batchid):
con = cx_Oracle.connect('user/[email protected]/ODB4TEST.COMPANY.IE')
cur = con.cursor()
cur.execute("SELECT * from name)
results = cur.fetchall()


def fetchbatchdata(self,results):

for row in results:
mer = row[0].ljust(25, ' ')
mercity = row[1].ljust(13, ' ')
mertype = row[2]
merloc = row[3]
mercount = row[4]
mersec = row[5]
acq = row[6]
btime = row[7].strftime('%d%m')
bmerch = str(row[8]).rjust(12, '0')
termcur = row[9]
acqbank = str(row[10]).rjust(24, '0')
termtype = row[11]
termsoftver = row[12]
merbatch = str(row[13]).rjust(3, '0')
reccount = str(row[14]).rjust(9, '0')
amounttotal = str(row[15]).rjust(16, '0')
cashback = str(row[16]).rjust(16, '0')
deposit = str(row[17]).rjust(16, '0')

def createbatchstrings(self):
BatchHeaderPacket = "\x01000\x0251.520%s00000%s000006060001%s%s%s%s0003 \x03" % (btime, bmerch, termcur, acqbank, termtype, termsoftver);
ParameterPacket = "\x01001\x0251.5300000401%s%sIE%s%s%s00000%s%s0%s \x03" % (mer, mercity, mertype, merloc, termid, mercount, mersec, acq);
TrailerPacket = "\x01003\x0251.550%s00%s%s%s%s%s00000000000\x03" % (btime, merbatch, reccount, amounttotal, cashback, deposit);
cur.close()

def returnbatchheader(self):
return BatchHeaderPacket
def returnparameterpacket(self):
return ParameterPacket
def returntrailerpacket(self):
return TrailerPacket
 
S

shaun

This is the code in the script im calling:


batchObject=StringCall()
batchObject.databasebatchcall(termid, batchid)
batchObject.fetchbatchdata()
batchObject.createbatchstrings()

BatchHeaderPacket =batchObject.returnbatchheader()
ParameterPacket =batchObject.returnparameterpacket()
TrailerPacket =batchObject.returntrailerpacket()

print BatchHeaderPacket
print ParameterPacket
print TrailerPacket
 
C

Chris Angelico

class StringCall:
results=[]
def databasebatchcall(self,termid, batchid):
con = cx_Oracle.connect('user/[email protected]/ODB4TEST.COMPANY.IE')
cur = con.cursor()
cur.execute("SELECT * from name)
results = cur.fetchall()

This actually never sets 'results', which is a class variable. You
should probably be using 'self.results' here; Python does not include
class/instance members in scope automatically.

Try using 'self.' for everything that you need to be maintained as
state, and see if that solves your problem.

But this looks to me like a case where you may not really even need a
class at all.

ChrisA
 
S

shaun

Hi Chris,

I'm changing it into multiple classes because the script is going to get much larger its more for maintainability reasons rather than functionality reasons.

Thanks so much man it was the "self" fix you stated above. I woe you a pint of Guinness :D

Thanks again,
Shaun
 
M

MRAB

Sorry guys here is the full code for the class:

#!/usr/bin/python
# Echo client program
import cx_Oracle
import socket
import pprint
from struct import *
import sys
from binascii import *

Don't use "from something import *". It'll import a whole load of
names. Import only those names you wish to use.
import time
import datetime


class StringCall:
results=[]
def databasebatchcall(self,termid, batchid):
con = cx_Oracle.connect('user/[email protected]/ODB4TEST.COMPANY.IE')
cur = con.cursor()
cur.execute("SELECT * from name)

That line has an unterminated string literal (missing quote). That
means that this file won't compile.
results = cur.fetchall()

As you're binding to "results" in the method, Python will assume that
that name is local to the method. The results will be discarded as soon
as the method returns, which it does right after.
def fetchbatchdata(self,results):

for row in results:
mer = row[0].ljust(25, ' ')
mercity = row[1].ljust(13, ' ')
mertype = row[2]
merloc = row[3]
mercount = row[4]
mersec = row[5]
acq = row[6]
btime = row[7].strftime('%d%m')
bmerch = str(row[8]).rjust(12, '0')
termcur = row[9]
acqbank = str(row[10]).rjust(24, '0')
termtype = row[11]
termsoftver = row[12]
merbatch = str(row[13]).rjust(3, '0')
reccount = str(row[14]).rjust(9, '0')
amounttotal = str(row[15]).rjust(16, '0')
cashback = str(row[16]).rjust(16, '0')
deposit = str(row[17]).rjust(16, '0')

All of the names "mer", "mercity", etc, will be local to this method.
def createbatchstrings(self):
BatchHeaderPacket = "\x01000\x0251.520%s00000%s000006060001%s%s%s%s0003 \x03" % (btime, bmerch, termcur, acqbank, termtype, termsoftver);
ParameterPacket = "\x01001\x0251.5300000401%s%sIE%s%s%s00000%s%s0%s \x03" % (mer, mercity, mertype, merloc, termid, mercount, mersec, acq);
TrailerPacket = "\x01003\x0251.550%s00%s%s%s%s%s00000000000\x03" % (btime, merbatch, reccount, amounttotal, cashback, deposit);
cur.close()

Where do the names "btime", "bmerch", etc, come from? They are
certainly not the same as those in "fetchbatchdata" because this is a
separate method.
 
C

Chris Angelico

Hi Chris,

I'm changing it into multiple classes because the script is going to get much larger its more for maintainability reasons rather than functionality reasons.

Doesn't necessarily have to be multiple classes. Python gives you the
flexibility of dividing things in whatever way makes sense to your
project. Maybe a class is right - I can't say without seeing all your
code and knowing all your intentions - but if it's not, you don't have
to feel constrained by it. This isn't Java where all code goes into a
class!
Thanks so much man it was the "self" fix you stated above. I woe you a pint of Guinness :D

You're welcome! I'm a non-drinker, though, so you can drink it and
think of me. :)

ChrisA
 
D

Dennis Lee Bieber

class StringCall:
results=[]
def databasebatchcall(self,termid, batchid):

What are termid and batchid -- you pass them into the method, and
NEVER USE THEM!
con = cx_Oracle.connect('user/[email protected]/ODB4TEST.COMPANY.IE')
cur = con.cursor()
cur.execute("SELECT * from name)

con and cur are locals to this function/method
results = cur.fetchall()
results is also a local... All three disappear when the method
returns

I suspect you need to study the language reference regarding classes
and function/methods, argument passing, and the use of "self"
def fetchbatchdata(self,results):

for row in results:
mer = row[0].ljust(25, ' ')
mercity = row[1].ljust(13, ' ')
mertype = row[2]
merloc = row[3]
mercount = row[4]
mersec = row[5]
acq = row[6]
btime = row[7].strftime('%d%m')
bmerch = str(row[8]).rjust(12, '0')
termcur = row[9]
acqbank = str(row[10]).rjust(24, '0')
termtype = row[11]
termsoftver = row[12]
merbatch = str(row[13]).rjust(3, '0')
reccount = str(row[14]).rjust(9, '0')
amounttotal = str(row[15]).rjust(16, '0')
cashback = str(row[16]).rjust(16, '0')
deposit = str(row[17]).rjust(16, '0')
I'd reserve all the formatting for when you need to output the data;
that would permit using tuple unpacking

(mer, mercity, mertype, ...
cashback, deposit) = row

But note that after you've filled all those names with the first
record -- you throw the first record away with the data from the second
record, and repeat for each record... When the loop ends, all those
names only reference the last record's data...

AND then the method finishes and all those names are garbage
collected.
def createbatchstrings(self):
BatchHeaderPacket = "\x01000\x0251.520%s00000%s000006060001%s%s%s%s0003 \x03" % (btime, bmerch, termcur, acqbank, termtype, termsoftver);
ParameterPacket = "\x01001\x0251.5300000401%s%sIE%s%s%s00000%s%s0%s \x03" % (mer, mercity, mertype, merloc, termid, mercount, mersec, acq);
TrailerPacket = "\x01003\x0251.550%s00%s%s%s%s%s00000000000\x03" % (btime, merbatch, reccount, amounttotal, cashback, deposit);
cur.close()
All the xxxxPacket names are local and will be thrown away after the
method returns...

And cur, mer, mercity, etc. are UNDEFINED locals here...

def returnbatchheader(self):
return BatchHeaderPacket
def returnparameterpacket(self):
return ParameterPacket
def returntrailerpacket(self):
return TrailerPacket
None of these "return xxxx" will work, since the "xxxx" are all
undefined local names.

And these methods are superfluous, why not just have
"createBatchStrings" return all three as a tuple.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top