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


Groups > comp.lang.python > #28642

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

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Need help fixing this error please:NameError: global name is not defined
Date 2012-09-06 17:16 -0400
Organization > Bestiaria Support Staff <
References <149e9472-ec31-4b74-9f20-d4945a9fb678@googlegroups.com> <42718e31-d77b-4c8a-ae48-1dae0a780585@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.329.1346966229.27098.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, 6 Sep 2012 04:37:04 -0700 (PDT), shaun
<shaun.wiseman91@gmail.com> 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/user123@odb4.dcc.company.ie/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
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Back to comp.lang.python | Previous | NextPrevious 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