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

MRAB python at mrabarnett.plus.com
Thu Sep 6 08:08:35 EDT 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/user123 at 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
> 		




More information about the Python-list mailing list