Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28581

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

Date 2012-09-06 13:08 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Need help fixing this error please:NameError: global name is not defined
References <149e9472-ec31-4b74-9f20-d4945a9fb678@googlegroups.com> <42718e31-d77b-4c8a-ae48-1dae0a780585@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.296.1346933317.27098.python-list@python.org> (permalink)

Show all headers | View raw


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/user123@odb4.dcc.company.ie/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
> 		

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Need help fixing this error please:NameError: global name is not defined shaun <shaun.wiseman91@gmail.com> - 2012-09-06 03:45 -0700
  Re: Need help fixing this error please:NameError: global name is not defined Chris Angelico <rosuav@gmail.com> - 2012-09-06 21:05 +1000
  Re: Need help fixing this error please:NameError: global name is not defined Dave Angel <d@davea.name> - 2012-09-06 07:05 -0400
  Re: Need help fixing this error please:NameError: global name is not defined Peter Otten <__peter__@web.de> - 2012-09-06 13:07 +0200
  Re: Need help fixing this error please:NameError: global name is not defined Chris Angelico <rosuav@gmail.com> - 2012-09-06 21:53 +1000
  Re: Need help fixing this error please:NameError: global name is not defined shaun <shaun.wiseman91@gmail.com> - 2012-09-06 05:07 -0700
    Re: Need help fixing this error please:NameError: global name is not defined Chris Angelico <rosuav@gmail.com> - 2012-09-06 23:16 +1000
  Re: Need help fixing this error please:NameError: global name is not defined MRAB <python@mrabarnett.plus.com> - 2012-09-06 13:08 +0100
  Re: Need help fixing this error please:NameError: global name is not defined Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-06 17:16 -0400

csiph-web