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

Discussion in 'Python' started by shaun, Sep 6, 2012.

  1. shaun

    shaun Guest

    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//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
     
    shaun, Sep 6, 2012
    #1
    1. Advertising

  2. Re: Need help fixing this error please:NameError: global name is notdefined

    On Thu, Sep 6, 2012 at 8:45 PM, shaun <> wrote:
    > ////CODE///////////////////////////////
    >
    > def databasebatchcall(self,tid, bid):
    > con=cx_Oracle.connect('user//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
     
    Chris Angelico, Sep 6, 2012
    #2
    1. Advertising

  3. shaun

    Dave Angel Guest

    Re: Need help fixing this error please:NameError: global name isnot defined

    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//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
     
    Dave Angel, Sep 6, 2012
    #3
  4. shaun

    Peter Otten Guest

    Re: Need help fixing this error please:NameError: global name is notdefined

    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//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.
     
    Peter Otten, Sep 6, 2012
    #4
  5. shaun

    shaun Guest

    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//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
     
    shaun, Sep 6, 2012
    #5
  6. shaun

    shaun Guest

    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
     
    shaun, Sep 6, 2012
    #6
  7. Re: Need help fixing this error please:NameError: global name is notdefined

    On Thu, Sep 6, 2012 at 9:37 PM, shaun <> wrote:
    > class StringCall:
    > results=[]
    > def databasebatchcall(self,termid, batchid):
    > con = cx_Oracle.connect('user//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
     
    Chris Angelico, Sep 6, 2012
    #7
  8. shaun

    shaun Guest

    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
     
    shaun, Sep 6, 2012
    #8
  9. shaun

    MRAB Guest

    Re: Need help fixing this error please:NameError: global name isnot defined

    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//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.

    >
    > def returnbatchheader(self):
    > return BatchHeaderPacket
    > def returnparameterpacket(self):
    > return ParameterPacket
    > def returntrailerpacket(self):
    > return TrailerPacket
    >
     
    MRAB, Sep 6, 2012
    #9
  10. Re: Need help fixing this error please:NameError: global name is notdefined

    On Thu, Sep 6, 2012 at 10:07 PM, shaun <> 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 :D


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

    ChrisA
     
    Chris Angelico, Sep 6, 2012
    #10
  11. Re: Need help fixing this error please:NameError: global name is notdefined

    On Thu, 6 Sep 2012 04:37:04 -0700 (PDT), shaun
    <> declaimed the following in
    gmane.comp.python.general:


    >
    > 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//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.
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Sep 6, 2012
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lalit
    Replies:
    1
    Views:
    790
    Diez B. Roggisch
    Apr 24, 2008
  2. Stef Mientki
    Replies:
    2
    Views:
    579
    Stef Mientki
    Dec 29, 2010
  3. Mohsen Pahlevanzadeh
    Replies:
    0
    Views:
    134
    Mohsen Pahlevanzadeh
    Sep 5, 2013
  4. Terry Reedy
    Replies:
    0
    Views:
    134
    Terry Reedy
    Sep 5, 2013
  5. Mohsen Pahlevanzadeh
    Replies:
    0
    Views:
    140
    Mohsen Pahlevanzadeh
    Sep 6, 2013
Loading...

Share This Page