[Catalog-sig] Assistance with Python Classes

Graeme Matthew graeme@sofcom.com.au
Wed, 18 Jul 2001 11:32:22 +1000


This is a multi-part message in MIME format.

------=_NextPart_000_002B_01C10F7D.4F7FF8C0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi there can someone please assist me

I am a perl programmer learning python. I keep on getting an exception =
error:

NameError: global name 'cgi' is not defined

I have a class called WebManager below

class WebManager:
=20
     import os
     import cgi

     def __init__(self):

          cgiObj =3D cgi.cgi()
          self.formdata =3D cgiObj.FieldStorage()
          self.operatingSys =3D os.name

     def getParam(self,name):  =20
 =20
           if self.formdata.has_key(name):
           value =3D self.formdata[name].value
      else:
           value =3D none
           return value
=20
      def getGenericInfo(self):
          return self.operatingSys

Im calling it from a standard script as seen below (the formatting ie =
indentation was lost when pasting)

import WebManager

obj =3D WebManager.WebManager()

fname =3D obj.getParam("fname")
lname =3D obj.getParam("lname")
address =3D obj.getParam("address")
tel =3D obj.getParam("tel")
mob =3D obj.getParam("mob")

if (lname !=3D "Not Supplied") and (fname !=3D "Not Supplied"):
 message =3D "Thanks " + fname + " " + lname + " your data has been =
submitted"
else:
 message =3D "As a minimum you must supply your first and last names"

print "Content-Type: text/html"
print

print "<HTML><BODY>"
print "<script language=3Djavascript>"
print "function openRecordForPrinting() {"
print "var hwnd =3D =
window.open(\"/cgi-bin/start.py\",\"printRecord\",\"status=3Dno\");}"
print "</script>"
print message
print "<table width=3D\"40%\">"
print "<FORM NAME=3DTEST METHOD=3DPOST ACTION=3D/cgi-bin/start.py =
target=3D\"test\">"
print "<tr><td>First Name:</td><td><INPUT TYPE=3DTEXT NAME=3Dfname =
VALUE=3D\"" + fname + "\"></td></tr>"
print "<tr><td>Last Name:</td><td><INPUT TYPE=3DTEXT NAME=3Dlname =
VALUE=3D\"" + lname + "\"></td></tr>"
print "<tr><td>Address:</td><td><INPUT TYPE=3DTEXT NAME=3Daddress =
VALUE=3D\"" + address + "\"></td></tr>"
print "<tr><td>Telephone Number:</td><td><INPUT TYPE=3DTEXT NAME=3Dtel =
VALUE=3D\"" + tel + "\"></td></tr>"
print "<tr><td>Mobile Number:</td><td><INPUT TYPE=3DTEXT NAME=3Dmob =
VALUE=3D\"" + mob + "\"></td></tr>"
print "<tr><td>&nbsp</td><td><INPUT TYPE=3DSUBMIT NAME=3DSUBMIT =
VALUE=3DSEND></td></tr>"
print "<tr><td><a HREF=3D\"javascript:openRecordForPrinting()\">Click =
Here</a></td></tr>" =20

print """
</FORM>
</table>
</BODY>
</HTML>
"""

Thanks a million !!! =20

------=_NextPart_000_002B_01C10F7D.4F7FF8C0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4522.1800" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DTahoma size=3D2>Hi there can someone please assist =
me</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>I am a perl programmer learning =
python. I keep on=20
getting an exception error:</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>NameError: global name 'cgi' is not=20
defined</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>I have a class called WebManager=20
below</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma color=3D#0000ff size=3D2>class=20
WebManager:<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp; import=20
os<BR>&nbsp;&nbsp;&nbsp;&nbsp; import cgi</FONT></DIV>
<DIV><FONT face=3DTahoma color=3D#0000ff size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma color=3D#0000ff =
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp; def=20
__init__(self):</FONT></DIV>
<DIV><FONT face=3DTahoma color=3D#0000ff size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma color=3D#0000ff =
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; cgiObj =3D =
cgi.cgi()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; self.formdata =3D=20
cgiObj.FieldStorage()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;=20
self.operatingSys =3D os.name</FONT></DIV>
<DIV><FONT face=3DTahoma color=3D#0000ff size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2><FONT =
color=3D#0000ff>&nbsp;&nbsp;&nbsp;&nbsp; def=20
getParam(self,name):&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp;&nbsp; if=20
self.formdata.has_key(name):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; value =3D=20
self.formdata[name].value<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; value =
=3D=20
none<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
return=20
value<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def=20
getGenericInfo(self):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp;=20
return self.operatingSys</FONT></FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>Im calling it from a standard script =
as seen below=20
(the formatting ie indentation was lost when pasting)</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>import WebManager</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>obj =3D =
WebManager.WebManager()</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>fname =3D =
obj.getParam("fname")<BR>lname =3D=20
obj.getParam("lname")<BR>address =3D obj.getParam("address")<BR>tel =3D=20
obj.getParam("tel")<BR>mob =3D obj.getParam("mob")</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>if (lname !=3D "Not Supplied") and =
(fname !=3D "Not=20
Supplied"):<BR>&nbsp;message =3D "Thanks " + fname + " " + lname + " =
your data has=20
been submitted"<BR>else:<BR>&nbsp;message =3D "As a minimum you must =
supply your=20
first and last names"</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>print "Content-Type:=20
text/html"<BR>print</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>print =
"&lt;HTML&gt;&lt;BODY&gt;"<BR>print=20
"&lt;script language=3Djavascript&gt;"<BR>print "function =
openRecordForPrinting()=20
{"<BR>print "var hwnd =3D=20
window.open(\"/cgi-bin/start.py\",\"printRecord\",\"status=3Dno\");}"<BR>=
print=20
"&lt;/script&gt;"<BR>print message<BR>print "&lt;table=20
width=3D\"40%\"&gt;"<BR>print "&lt;FORM NAME=3DTEST METHOD=3DPOST=20
ACTION=3D/cgi-bin/start.py target=3D\"test\"&gt;"<BR>print=20
"&lt;tr&gt;&lt;td&gt;First Name:&lt;/td&gt;&lt;td&gt;&lt;INPUT =
TYPE=3DTEXT=20
NAME=3Dfname VALUE=3D\"" + fname + =
"\"&gt;&lt;/td&gt;&lt;/tr&gt;"<BR>print=20
"&lt;tr&gt;&lt;td&gt;Last Name:&lt;/td&gt;&lt;td&gt;&lt;INPUT =
TYPE=3DTEXT=20
NAME=3Dlname VALUE=3D\"" + lname + =
"\"&gt;&lt;/td&gt;&lt;/tr&gt;"<BR>print=20
"&lt;tr&gt;&lt;td&gt;Address:&lt;/td&gt;&lt;td&gt;&lt;INPUT TYPE=3DTEXT=20
NAME=3Daddress VALUE=3D\"" + address + =
"\"&gt;&lt;/td&gt;&lt;/tr&gt;"<BR>print=20
"&lt;tr&gt;&lt;td&gt;Telephone Number:&lt;/td&gt;&lt;td&gt;&lt;INPUT =
TYPE=3DTEXT=20
NAME=3Dtel VALUE=3D\"" + tel + "\"&gt;&lt;/td&gt;&lt;/tr&gt;"<BR>print=20
"&lt;tr&gt;&lt;td&gt;Mobile Number:&lt;/td&gt;&lt;td&gt;&lt;INPUT =
TYPE=3DTEXT=20
NAME=3Dmob VALUE=3D\"" + mob + "\"&gt;&lt;/td&gt;&lt;/tr&gt;"<BR>print=20
"&lt;tr&gt;&lt;td&gt;&amp;nbsp&lt;/td&gt;&lt;td&gt;&lt;INPUT =
TYPE=3DSUBMIT=20
NAME=3DSUBMIT VALUE=3DSEND&gt;&lt;/td&gt;&lt;/tr&gt;"<BR>print=20
"&lt;tr&gt;&lt;td&gt;&lt;a =
HREF=3D\"javascript:openRecordForPrinting()\"&gt;Click=20
Here&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;"&nbsp; </FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>print=20
"""<BR>&lt;/FORM&gt;<BR>&lt;/table&gt;<BR>&lt;/BODY&gt;<BR>&lt;/HTML&gt;<=
BR>"""</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>Thanks a million=20
!!!&nbsp;&nbsp;</FONT></DIV></BODY></HTML>

------=_NextPart_000_002B_01C10F7D.4F7FF8C0--