Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Need help fixing this error please:NameError: global name is not defined

Reply
Thread Tools

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

 
 
shaun
Guest
Posts: n/a
 
      09-06-2012
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/(E-Mail Removed)/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
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      09-06-2012
On Thu, Sep 6, 2012 at 8:45 PM, shaun <(E-Mail Removed)> wrote:
> ////CODE///////////////////////////////
>
> def databasebatchcall(self,tid, bid):
> con=cx_Oracle.connect('user/(E-Mail Removed)/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
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      09-06-2012
On 09/06/2012 06:45 AM, shaun wrote:
> 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/(E-Mail Removed)/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


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.

--

DaveA

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      09-06-2012
shaun wrote:

> 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/(E-Mail Removed)/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.

 
Reply With Quote
 
shaun
Guest
Posts: n/a
 
      09-06-2012
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/(E-Mail Removed)/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%s000 3 \x03" % (btime, bmerch, termcur, acqbank, termtype, termsoftver);
ParameterPacket = "\x01001\x0251.5300000401%s%sIE%s%s%s00000%s%s 0%s \x03" % (mer, mercity, mertype, merloc, termid, mercount, mersec, acq);
TrailerPacket = "\x01003\x0251.550%s00%s%s%s%s%s00000000000\x0 3" % (btime, merbatch, reccount, amounttotal, cashback, deposit);
cur.close()

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



 
Reply With Quote
 
shaun
Guest
Posts: n/a
 
      09-06-2012
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
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      09-06-2012
On Thu, Sep 6, 2012 at 9:37 PM, shaun <(E-Mail Removed)> wrote:
> class StringCall:
> results=[]
> def databasebatchcall(self,termid, batchid):
> con = cx_Oracle.connect('user/(E-Mail Removed)/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
 
Reply With Quote
 
shaun
Guest
Posts: n/a
 
      09-06-2012
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

Thanks again,
Shaun
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      09-06-2012
On 06/09/2012 12:37, shaun wrote:
> 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/(E-Mail Removed)/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%s000 3 \x03" % (btime, bmerch, termcur, acqbank, termtype, termsoftver);
> ParameterPacket = "\x01001\x0251.5300000401%s%sIE%s%s%s00000%s%s 0%s \x03" % (mer, mercity, mertype, merloc, termid, mercount, mersec, acq);
> TrailerPacket = "\x01003\x0251.550%s00%s%s%s%s%s00000000000\x0 3" % (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.

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


 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      09-06-2012
On Thu, Sep 6, 2012 at 10:07 PM, shaun <(E-Mail Removed)> wrote:
> 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


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

ChrisA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
Re: global name is not defined - error Marco Wahl Python 1 06-28-2006 11:25 AM
global name is not defined - error a Python 1 06-28-2006 10:23 AM
global name is not defined - error - but for array a Python 2 06-28-2006 08:23 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments