[Tutor] CGI and Better Programming Practice
Jason Drake
jdrake@jam.rr.com
Thu, 22 Feb 2001 21:23:36 -0600
This is a multi-part message in MIME format.
------=_NextPart_000_00DD_01C09D15.B78E3C40
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I've been rather silent lately because of other projects, but I'm taking =
some time again tonight to play around again in the Python/CGI arena. =
When last I sat here playing with the snake... err... Python that is, =
not my John Thomas, I managed to piece together a simple bit of code =
which used the Python CGI module to extract information passed by an =
HTML form using the post method and print it using basic HTML statements =
combined with Python to a fresh page. I then took a little time to =
pretty both the form and the output up just a little, and will probably =
do a little more of that tonight also. (Hey, we've gotta have balance, =
right?)=20
So I manage to get info from a user and print it back to him/her which =
is great and I then determine to forge ahead and, with a little =
prompting (competition is good) from Random, I set out to have the next =
incarnation spit out the circumference of a circle based on the radius =
as supplied by the user. Wow! Neat! I managed to get it to work... =
almost. It happily dropped decimal places on me so I was back to the =
research. After a few more moments of banging my head against the =
monitor and a few hints and suggestions alluding to the eventual =
conclusion I manage to figure out how to control the variables to have =
them output as floating point and no longer lose my decimals! Yay! =
Having done it once, I figure I need to test that bit of learning a =
little further and recode the whole thing over again, only this time I =
change up the equations and have the code output the area instead of the =
circumference.
Okay, so here is where the success story pauses and I look for more =
info. 1st off, although I know I can keep on cruising along as I am and =
end up making mammoth sized programs that work, I know for fact that I =
really need to start figuring out how to break my programs up into =
'modules' or 'snippets' which can be written once and used repeatedly. =
Also, the next step in my gameplan of learning to use Python as an =
effective programming language for CGI programming is learning to write =
to a file. Specifically what I am wanting to do (as a first measure) is =
to extract the data from the form, format the data as a comma delimited =
string with double-quote qualifiers and append the line to a text file =
such as testrun.csv. Why a .csv? Well, simply put, I've found .csv files =
to be astoundingly versatile and easy to use with most mainstream =
programs. It's very easy for me, for instance, to download a .csv file =
with a header row and import it into an Access table, work with the data =
and use it effectively in my day-to-day life for printing labels, =
sending form letters, etc. Also, it's a format I've used for so many =
years now for so many reasons that I'm simply all too familiar with it =
which allows a little extra comfort in my life. (Don't worry, I am =
working toward the eventuality of using a SQL database, the .csv thing =
is just a step.)
These things said and a cold Heineken at my side, I would post the code =
thus far, but would rather not stick so much bad code into one document =
:) Instead I'll post the offensive code at =
www.lowerstandard.com/frik/thecode.html by way of links to .txt =
documents for the curious to peruse as well as links to the actual =
working versions. Give me 5-10 minutes and it'll be there.
Jay
PS - I am documenting this learning experience as I go along and *WILL* =
at the 'end' be uploading what I hope will be a fairly good tutorial =
with practical examples of each of the experiences I have to =
lowerstandard.com when I get to a point that I feel that I can make a =
contribution that some poor schmoe like me would appreciate and =
understand ;)
------=_NextPart_000_00DD_01C09D15.B78E3C40
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.4611.1300" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV>
<DIV><FONT face=3DArial size=3D2>I've been rather silent lately because =
of other=20
projects, but I'm taking some time again tonight to play around again in =
the=20
Python/CGI arena. When last I sat here playing with the snake... err... =
Python=20
that is, not my John Thomas, I managed to piece together a simple bit of =
code=20
which used the Python CGI module to extract information passed by an =
HTML form=20
using the post method and print it using basic HTML statements combined =
with=20
Python to a fresh page. I then took a little time to pretty both the =
form and=20
the output up just a little, and will probably do a little more of that =
tonight=20
also. (Hey, we've gotta have balance, right?) </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>So I manage to get info from a user and =
print it=20
back to him/her which is great and I then determine to forge ahead and, =
with a=20
little prompting (competition is good) from Random, I set out to have =
the next=20
incarnation spit out the circumference of a circle based on the radius =
as=20
supplied by the user. Wow! Neat! I managed to get it to work... almost. =
It=20
happily dropped decimal places on me so I was back to the research. =
After a few=20
more moments of banging my head against the monitor and a few hints and=20
suggestions alluding to the eventual conclusion I manage to figure out =
how to=20
control the variables to have them output as floating point and no =
longer lose=20
my decimals! Yay! Having done it once, I figure I need to test that bit =
of=20
learning a little further and recode the whole thing over again, only =
this time=20
I change up the equations and have the code output the area instead of =
the=20
circumference.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Okay, so here is where the success =
story pauses and=20
I look for more info. 1st off, although I know I can keep on cruising =
along as I=20
am and end up making mammoth sized programs that work, I know for fact =
that I=20
really need to start figuring out how to break my programs up into =
'modules' or=20
'snippets' which can be written once and used repeatedly. Also, the next =
step in=20
my gameplan of learning to use Python as an effective programming =
language for=20
CGI programming is learning to write to a file. Specifically what I am =
wanting=20
to do (as a first measure) is to extract the data from the form, format =
the data=20
as a comma delimited string with double-quote qualifiers and append the =
line to=20
a text file such as testrun.csv. Why a .csv? Well, simply put, I've =
found .csv=20
files to be astoundingly versatile and easy to use with most mainstream=20
programs. It's very easy for me, for instance, to download a .csv file =
with a=20
header row and import it into an Access table, work with the data and =
use it=20
effectively in my day-to-day life for printing labels, sending form =
letters,=20
etc. Also, it's a format I've used for so many years now for so many =
reasons=20
that I'm simply all too familiar with it which allows a little extra =
comfort in=20
my life. (Don't worry, I am working toward the eventuality of using a =
SQL=20
database, the .csv thing is just a step.)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>These things said and a cold Heineken =
at my side, I=20
would post the code thus far, but would rather not stick so much bad =
code into=20
one document :) Instead I'll post the offensive code at <A=20
href=3D"http://www.lowerstandard.com/frik/thecode.html">www.lowerstandard=
.com/frik/thecode.html</A>=20
by way of links to .txt documents for the curious to peruse as well as =
links to=20
the actual working versions. Give me 5-10 minutes and it'll be=20
there.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Jay</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>PS - I am documenting this learning =
experience as I=20
go along and *WILL* at the 'end' be uploading what I hope will be a =
fairly good=20
tutorial with practical examples of each of the experiences I have to=20
lowerstandard.com when I get to a point that I feel that I can make a=20
contribution that some poor schmoe like me would appreciate and =
understand=20
;)</FONT></DIV></DIV></BODY></HTML>
------=_NextPart_000_00DD_01C09D15.B78E3C40--