Simple Dictionary Question

David Bolen db3l at fitlinxx.com
Fri May 4 19:47:21 EDT 2001


Ben Ocean <zope at thewebsons.com> writes:

> At 01:51 PM 5/4/2001 -0700, you wrote:
> >You need to include more of your script (maybe the whole thing).
> >There is no clue about what v is supposed to be, or what i is
> >supposed to be, or what the script is trying to do.
> 
> Oops.
>  >>>
> for i in form.keys():
>     v = form[i].value
>     save.append(i + ": " + v + "\n")
>     if form.has_key("token"):
>       if i == "bladename":
>         bladename = v
>       if i == "bladephone":
>         bladephone = v
>       if i == "bladeemail":
>         bladeemail = v
>       if i == "token":
>         token = v
> <<<
> The *blade* things are keys: I need to extract their values. The variables 
> will be set only if *token* is set. Why can't I extract the value of the 
> variables? If you really want to see the whole script, here it is. Thanks 
> in advance for your help,

Hmm, well, you still haven't precisely said what the failure mode is -
what do you mean by "extract their values"?  Are you saying that you
did get a dictionary "form" that had a key "token" and one or more of
the other keys and your local variables didn't get set appropriately?

And if so, how did you check the problem - for example, I see where
you output the contents of the form() dictionary by using "save", but
looking through your script for references to bladename itself, I see:

>    print "Please tell us your name: <input type=text name=\"bladename\" 
> size=20><br>"

which gets it into the form submission:

>     blade = string.join(aaaa, bladename, bbbb, bladephone, cccc, 
> bladeemail, dddd)
>     line = string.join(blade, line)
>     import smtplib
>     server = smtplib.SMTP("localhost")
>     server.sendmail("beno at thewebsons.com", "beno at thewebsons.com", "Subject: 
> Blade Chevy Car Inquiry\n\n%s" % line)

So the only place I think you could verify that bladename had a value
was when you tried to use it in mail, but as you've seen in the
separate thread, nothing is going to show up because "line" is not a
well-formed RFC822 message format.  So I don't see where you could
ever expect to see the output value of bladename (either in the mail
or in any diagnostic HTML output).  So are you really sure the problem
is where you think it is?  :-)

The code itself looks like it'll probably do what you want, although
it does seem a bit convoluted to me.  Instead of retrieving keys
directly, you nest a series of comparisons over an iteration of the
keys.  And you check both for the token key (as well as then compare
against each possible key you are looking for) each pass through the
loop.  So IMHO, the code is making it a little difficult to easily
grasp what it is intended to do.

There's plenty of alternative approaches, but one that might be a bit
clearer could be:

token = form.get("token",None)
if token:
    bladename  = form.get("bladename",None)
    bladephone = form.get("bladephone",None)
    bladeemail = form.get("bladeemail",None)

which would result in token having a value or None if it wasn't
present, and if token had a value, the blade* variables having
appropriate values or None (adjust defaults as necessary).  Although
as you have more variables you'd probably just want to use your own
dictionary or other indexed data structure than manually writing
distinct variables.

You could still use a separate loop to create the "save" string if you
needed it beyond debugging.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list