FAQ
I get a construct like this:

form=FieldStorage(None, None, [MiniFieldStorage('name1', 'Val1'),
MiniFieldStorage('name2', 'Val2'), MiniFieldStorage('name3', 'Val3')])

Now how would I assign every variable name* its value?

lI did try locals().update(form) however I get
name2
-> MiniFieldStorage('name2', 'Val2')

when I need to assign the variable name2 the value Val2

This is Py3

-- Gnarlie

Search Discussions

  • Gnarlodious at Aug 17, 2011 at 9:16 am
    I get a construct like this:

    form=FieldStorage(None, None, [MiniFieldStorage('name1', 'Val1'),
    MiniFieldStorage('name2', 'Val2'), MiniFieldStorage('name3', 'Val3')])

    Now how would I assign every variable name* its value?

    lI did try locals().update(form) however I get
    name2
    -> MiniFieldStorage('name2', 'Val2')

    when I need to assign the variable name2 the value Val2

    This is Py3

    -- Gnarlie
  • Gnarlodious at Aug 17, 2011 at 9:19 am
    I should add that this does what I want, but something a little more
    Pythonic?

    import cgi, os
    os.environ["QUERY_STRING"] = "name1=Val1&name2=Val2&name3=Val3"
    form=cgi.FieldStorage()

    form

    dict = {}
    for key in form.keys(): dict[ key ] = form[ key ].value

    dict
    locals().update(dict)
    name3

    -- Gnarlie
  • Chris Angelico at Aug 17, 2011 at 9:41 am

    On Wed, Aug 17, 2011 at 10:19 AM, Gnarlodious wrote:
    import cgi, os
    os.environ["QUERY_STRING"] = "name1=Val1&name2=Val2&name3=Val3"
    form=cgi.FieldStorage()

    form

    dict = {}
    for key in form.keys(): dict[ key ] = form[ key ].value
    You could probably use a list comp for this, but there's not a lot of
    difference. (By the way, you should be aware that you're shadowing the
    builtin 'dict' here. That's not a problem, but be aware.)

    dict.update(((key,form[key].value) for key in form))

    That's a generator that returns a (key,value) tuple for each form
    element, which update() will happily use.

    But the last line:
    locals().update(dict)
    is, IMHO, a very bad idea. Rename your dictionary to 'request' or
    'data' or 'form' or something, and then reference form['name3']
    instead; or, be explicit:
    name3=form['name3']

    Incidentally, you seem to be working at the module level, where
    locals() is globals() (and I mean that literally - 'locals() is
    globals()' is True). You may not be able to do this with locals() in a
    function:
    http://docs.python.org/library/functions.html#locals

    Chris Angelico
  • Chris Rebert at Aug 17, 2011 at 4:23 pm

    On Wed, Aug 17, 2011 at 2:19 AM, Gnarlodious wrote:
    I should add that this does what I want, but something a little more
    Pythonic?

    import cgi, os
    os.environ["QUERY_STRING"] = "name1=Val1&name2=Val2&name3=Val3"
    form=cgi.FieldStorage()

    form

    dict = {}
    for key in form.keys(): dict[ key ] = form[ key ].value

    dict
    locals().update(dict)
    name3
    Try it within a function. It will fail epic-ly in CPython and most
    other implementations.
    Read the note in the locals() docs:
    http://docs.python.org/library/functions.html#locals

    Cheers,
    Chris
  • Gnarlodious at Aug 17, 2011 at 9:20 am
    I should add that this does what I want, but something a little more
    Pythonic?

    import cgi, os
    os.environ["QUERY_STRING"] = "name1=Val1&name2=Val2&name3=Val3"
    form=cgi.FieldStorage()

    form

    dict = {}
    for key in form.keys(): dict[ key ] = form[ key ].value

    dict
    locals().update(dict)
    name3

    -- Gnarlie
  • Chris Angelico at Aug 17, 2011 at 9:25 am

    On Wed, Aug 17, 2011 at 10:06 AM, Gnarlodious wrote:
    I get a construct like this:

    form=FieldStorage(None, None, [MiniFieldStorage('name1', 'Val1'),
    MiniFieldStorage('name2', 'Val2'), MiniFieldStorage('name3', 'Val3')])

    when I need to assign the variable name2 the value Val2
    You can probably do this with some kind of list comprehension, but I
    recommend against it, if this has come from a web form. You do NOT
    want end users having the power to set variables. Keep it in a
    separate object (such as 'form') such that you must always be explicit
    about fetching form data. PHP has learned the risks; here's a decent
    summary:

    http://www.php.net/manual/en/security.globals.php

    Chris Angelico
  • Nobody at Aug 17, 2011 at 12:45 pm

    On Wed, 17 Aug 2011 02:06:31 -0700, Gnarlodious wrote:

    I get a construct like this:

    form=FieldStorage(None, None, [MiniFieldStorage('name1', 'Val1'),
    MiniFieldStorage('name2', 'Val2'), MiniFieldStorage('name3', 'Val3')])

    Now how would I assign every variable name* its value?
    Don't do this. It will allow the user to set any variable they wish,
    not just the ones you want them to, which is a major security flaw. PHP
    had this as a language feature (controlled by the register_globals
    directive), and it was rightly decried as a major security flaw.
  • Gnarlodious at Aug 18, 2011 at 3:52 am

    On Aug 17, 3:25?am, Chris Angelico wrote:
    You do NOT
    want end users having the power to set variables.
    Thanks for the warning, I can see I will need to quarantine the form
    input. And update() is out of the question.

    -- Gnarlie
    http://Gnarlodious.com/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 17, '11 at 9:06a
activeAug 18, '11 at 3:52a
posts9
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase