Prev: TypeError
Next: QDoubleValidator
From: MRAB on 8 Jan 2010 14:21 Victor Subervi wrote: > On Fri, Jan 8, 2010 at 1:26 PM, Steve Holden <steve(a)holdenweb.com > <mailto:steve(a)holdenweb.com>> wrote: > > MRAB wrote: > > Victor Subervi wrote: > > [snip] > >> > >> Code snippet: > >> > >> def cgiFieldStorageToDict(fieldStorage): > ^^^^^^^^^^^^ > Further hint ... > > >> params = {} > >> for key in fieldStorage.keys(): > >> params[key] = cgi.FieldStorage[key].value > > ^^^^^^^^^^^^^^^^^^^^^ > > This is your problem. > > > The problem is that I don't understand this code that I exactly copied > from a Web page tutorial. Can you folks point me to tutorials where I > can learn to comprehend this code? Specifically, the line in question. > How is it that one can code "params[key]" (what does that mean?) and the > other side, what does that mean > If you got it from: Recipe 81547: Using a simple dictionary for CGI parameters http://code.activestate.com/recipes/81547/ then no, it wasn't "exactly copied". If you'd copy-and-pasted it then it would've been correct, as well as much quicker to do...
From: MRAB on 8 Jan 2010 14:37
Victor Subervi wrote: > On Fri, Jan 8, 2010 at 3:09 PM, Victor Subervi <victorsubervi(a)gmail.com > <mailto:victorsubervi(a)gmail.com>> wrote: > > > > On Fri, Jan 8, 2010 at 2:52 PM, Jean-Michel Pichavant > <jeanmichel(a)sequans.com <mailto:jeanmichel(a)sequans.com>> wrote: > > Victor Subervi wrote: > > On Fri, Jan 8, 2010 at 1:26 PM, Steve Holden > <steve(a)holdenweb.com <mailto:steve(a)holdenweb.com> > <mailto:steve(a)holdenweb.com <mailto:steve(a)holdenweb.com>>> > wrote: > > MRAB wrote: > > Victor Subervi wrote: > > [snip] > >> > >> Code snippet: > >> > >> def cgiFieldStorageToDict(fieldStorage): > ^^^^^^^^^^^^ > Further hint ... > > >> params = {} > >> for key in fieldStorage.keys(): > >> params[key] = cgi.FieldStorage[key].value > > ^^^^^^^^^^^^^^^^^^^^^ > > This is your problem. > > > The problem is that I don't understand this code that I > exactly copied from a Web page tutorial. Can you folks point > me to tutorials where I can learn to comprehend this code? > Specifically, the line in question. How is it that one can > code "params[key]" (what does that mean?) and the other > side, what does that mean > > I think you are gathering more fans Victor :) > > http://docs.python.org/tutorial/datastructures.html#dictionaries > > > This still isn't telling me what I need to know. Perhaps I'm missing > the point, as in the recent case with putting the "print cookie" > statement in the header. I am assuming (translation: making an a$$ > out of you and me) that "params[key] automatically assigns the > fieldStorage key to the newly created params key in the dict of the > same, and assigning the value from the cgi.FieldStorage of that key > to the params value. Is that correct? > TIA, > beno > > > I may have answered my own question. I have this: > > def cgiFieldStorageToDict(fieldStorage): > params = {} > for key in fieldStorage.keys(): > params[key] = fieldStorage[key].value > return params > Which is what Recipe 81547 actually says! > dict = cgiFieldStorageToDict(cgi.FieldStorage()) > print dict > > which gave me this: > > {'store': 'products', 'cat': 'prodCat1'} > > which looks about right. I would have written the code like this: > > keys = [] > values = [] > for key, value in fieldStorage.iteritems(): > keys.append(key) > values.append(value) > params = dict(zip(keys, values)) > > which obviously isn't as elegant. But that's what I knew. Learned > another trick, I guess ;) > If that works then so should this: params = dict(fieldStorage.iteritems()) |