Newbie: Problems with File IO

Steve Holden sholden at holdenweb.com
Mon Feb 18 13:59:00 CET 2002


"Flavian Hardcastle" <deathtospam43423 at altavista.com> wrote in message
news:Xns91B9D83F1CFC1AusYourStandingInIt at 130.133.1.4...
> I cracked it!
>
Congratulations! Solving a problem like this always gives you more
confidence with the next one.

> It was a lot simpler than I thought. Immensely obvious infact!
>
> In the part of the program which processes the fieldstorage, I inserted
the
> lines...
>
>
> The_Form = cgi.FieldStorage()
>
> if (The_Form.has_key("handle")):
>     user = str(The_Form["handle"].value)
> elif not (The_Form.has_key("handle")):
>     user = " "
>
A quick note here: since the first test eliminates the case where your form
has the required key, by the law of the excluded middle (something is either
true or it is not), your second test isn't needed, and you could just write

if (The_Form.has_key("handle")):
    user = str(The_Form["handle"].value)
else:
    user = " "

Some people would shorten this still further by setting the default value,
and only changing it if the key is found:

user = " "
if (The_Form.has_key("handle")):
    user = str(The_Form["handle"].value)

That's a matter of taste. Not sure what a user name of a single space
implies in your code, of course.

> So the user gets to decide their handle, and handle will be stored in  the
> string "user".
>
> Then, in the actual html form itself, I inserted ....
>
> print '<input type=text NAME="handle" SIZE="30" MAXLENGTH="30" value =
> "'+user+'">'
>
> So whatever the user puts in the  input box "handle", will come back to
> them when the script re creates the html page.
>
> Simplicity itself heh? I can't imagine why I didn't spot it before!
Perhaps
> I did, but for some strange reason I thought the script would confuse
> everyone's handles.
>
Nope. Each interaction gets its own inputs, separate from those of any other
user.

> I've already had a chat buddy test it out with me, it works ok. The only
> problem is that the posts are stored in the file "log.txt", and the script
> reproduces the file in it's entirety every chat session. The result being,
> the longer you chat, the bigger and bigger web page gets, until it's a cow
> to download.
>
> For my next feat of mental contortion, I have to figure out a way to limit
> the number of posts in the "log.txt" file that are displayed. Ideally, I
> want the user to be able to specify that amount, so that (s)he can chose
> how many old posts (s)he wishes to view.
>
Well that's something for you to work on. You're being very adventurous
working with CGI so early in your programming career, and this will mean you
have quite a bit of learning about the nature of web systems as well as
Python. I guess we'll be seeing you again ;-)

regards
 Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Author, Python Web Programming: http://pydish.holdenweb.com/pwp/

"This is Python.  We don't care much about theory, except where it
intersects with useful practice."  Aahz Maruch on c.l.py







More information about the Python-list mailing list