Help a newbie revise my program

Discussion in 'Python' started by Sean Berry, Apr 26, 2004.

  1. Sean Berry

    Sean Berry Guest

    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,
    www.domain.com/cgi-bin/getModels?shaped=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.
    Sean Berry, Apr 26, 2004
    #1
    1. Advertising

  2. Sean Berry

    Paul McGuire Guest

    "Sean Berry" <> 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,
    > www.domain.com/cgi-bin/getModels?shaped=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
    Paul McGuire, Apr 26, 2004
    #2
    1. Advertising

  3. In <JR0jc.25858$>, 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
    Marc 'BlackJack' Rintsch, Apr 26, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jcnews
    Replies:
    147
    Views:
    2,504
    stoma
    Oct 20, 2004
  2. matrix

    revise my code

    matrix, Nov 3, 2006, in forum: Java
    Replies:
    2
    Views:
    370
    Andrew Thompson
    Nov 4, 2006
  3. matrix

    revise this code

    matrix, Nov 4, 2006, in forum: Java
    Replies:
    0
    Views:
    353
    matrix
    Nov 4, 2006
  4. Ramon F Herrera

    Need to revise my JNLP file

    Ramon F Herrera, Jul 11, 2009, in forum: Java
    Replies:
    11
    Views:
    1,176
    Andrew Thompson
    Jul 13, 2009
  5. Donald Welker

    Code fragment to revise CustomSD SDDL for Event Log

    Donald Welker, Sep 27, 2005, in forum: ASP .Net Security
    Replies:
    0
    Views:
    154
    Donald Welker
    Sep 27, 2005
Loading...

Share This Page