Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Help a newbie revise my program

Reply
Thread Tools

Help a newbie revise my program

 
 
Sean Berry
Guest
Posts: n/a
 
      04-26-2004
My program is used by an applet to look up items in a mysql database.
It is a "smart program". By that I mean it returns different results based
on
the arguments from the URL. Like,
http://www.domain.com/cgi-bin/getMod...aped=5&sized=7

It works well enough. However, I would like to find out better ways to do
some of the things I am doing,
such as getting the variable values from the URL.

Here is my code:

#!/usr/local/bin/python

import MySQLdb, os
db=MySQLdb.connect(db="aquatics")
c=db.cursor()
d = ()
di = {}
x = []
li2 = []

def getVars():
if len(os.environ["QUERY_STRING"]) > 0:
val_list = os.environ["QUERY_STRING"].split("&")
for item in val_list:
templi = item.split("=")
li2.append(templi)
return li2
else:
return []

x = getVars()
if len(x) > 0:
di = dict(x)
sql = """SELECT code from models"""

if 'sized' and 'shaped' in di:
sql = sql + """ where size like '%s%%' and shape=%s order by code"""
%(di['sized'], di['shaped'])
elif di.has_key('shaped'):
sql = sql + """ where shape=%s order by code""" %di['shaped']
elif di.has_key('size'):
sizevalue = di['sized']
sql = sql + """ where size like '%s%%' order by code""" %di['sized']

c.execute("%s" %sql)
d=c.fetchall()
for type in d:
print "<option>" + type[0] "+</option>\n"

-------------------------------------------------------------------------

Thanks in advance for any help.



 
Reply With Quote
 
 
 
 
Paul McGuire
Guest
Posts: n/a
 
      04-26-2004
"Sean Berry" <(E-Mail Removed)> wrote in message
news:5SZic.98241$U83.18636@fed1read03...
> My program is used by an applet to look up items in a mysql database.
> It is a "smart program". By that I mean it returns different results

based
> on
> the arguments from the URL. Like,
> http://www.domain.com/cgi-bin/getMod...aped=5&sized=7
>
> It works well enough. However, I would like to find out better ways to do
> some of the things I am doing,
> such as getting the variable values from the URL.
>


I think you'll find some helpful stuff in the cgi module, especially
parse_qs(queryString), which in your example will return a dictionary as
follows:

>>> import cgi
>>> print cgi.parse_qs("shaped=5&sized=7")

{'sized': ['7'], 'shaped': ['5']}
>>>


I can't explain why the dictionary values are lists, though - I would have
expected scalar strings.

-- Paul


 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      04-26-2004
In <JR0jc.25858$(E-Mail Removed)>, Paul McGuire wrote:

>>>> import cgi
>>>> print cgi.parse_qs("shaped=5&sized=7")

> {'sized': ['7'], 'shaped': ['5']}
>>>>
>>>>

> I can't explain why the dictionary values are lists, though - I would have
> expected scalar strings.


It's allowed to use the same key in URLs twice or more:

>>> import cgi
>>> print cgi.parse_qs("shaped=5&sized=7&sized=42")

{'sized': ['7', '42'], 'shaped': ['5']}
>>>


Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: May have to revise my site's 404 page. OldGringo38 Computer Support 0 07-31-2010 08:49 PM
Need to revise my JNLP file Ramon F Herrera Java 11 07-13-2009 05:03 PM
revise this code matrix Java 0 11-04-2006 03:19 PM
revise my code matrix Java 2 11-04-2006 02:48 AM
Should I revise chaotic content? jcnews HTML 147 10-20-2004 08:58 PM



Advertisments