473,804 Members | 3,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

List comprehension and FieldStorage

Given this FieldStorage object:

FieldStorage(No ne, None, [MiniFieldStorag e('Checkbox1', 'on'),
MiniFieldStorag e('Checkbox2', 'on')])

I am trying to cgi.urlencode the FieldStorage object to POST to another
cgi script. The documentation for urlencode,
http://docs.python.org/lib/module-urllib.html , says that urlencode
takes a mapping object or a list of double tuples. I see that I could
use cgi.parse_qs and cgi.parse_qsl to create these object types. Should
I just use that or should I continue using FieldStorage? Also how would
I create an object of either type from a FieldStorage object? Here is
my attempt at creating the double tuple list (It sucks....I know):

print [(key, value) for key, value in form.keys() and form[key].value]

Thanks for the help,
Derek Basch

Jul 19 '05 #1
4 2122
bump

Jul 19 '05 #2
Derek Basch wrote:
bump


If "bump" is supposed to be some kind of nudge to get people to reply,
please have more patience. You posted only sometime late yesterday, and
many people take up to a few days to receive the posts from this
newsgroup, and even those who don't shouldn't be expected to reply the
same instant they read your question. Also, it's quite possible you
won't get any reply (though this is pretty rare) in which case "bump" is
going to be seen as nothing more than rudeness, given that if an answer
was forthcoming it would probably already have been posted. In such a
case, you're better off rereading your request and considering whether
you phrased it adequately or can supply additional information to help
potential responders.

(In this case, your question looks fairly clear, though it doesn't look
like you've spent much time at the interactive prompt experimenting and
trying to find an answer on your own.)

-Peter
Jul 19 '05 #3
Sorry Peter. I will refrain from nudging in the future. I did spend
time at the interactive prompt and got nothing. Maybe I will have
better luck next time.

Jul 19 '05 #4
Derek Basch wrote:
print [(key, value) for key, value in form.keys() and form[key].value]
and
bump


The tutorial is always willing to make time for you :-)
import cgi
fs = cgi.FieldStorag e(environ=dict( QUERY_STRING="a lpha=1&beta=2") )
[(key, fs[key].value) for key in fs.keys()] [('alpha', '1'), ('beta', '2')]

So you might have come up yourself with the above in the mean time.
However, it breaks if there are multiple occurrences of the same key:
fs = cgi.FieldStorag e(environ=dict( QUERY_STRING="a lpha=1&beta=2&a lpha=3")) [(key, fs[key].value) for key in fs.keys()] Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'list' object has no attribute 'value'

A quick glance in the source suggests that
[(mfs.name, mfs.value) for mfs in fs.list]

[('alpha', '1'), ('beta', '2'), ('alpha', '3')]

is a bit more robust. But perhaps you can get hold of the original unparsed
query string and avoid the detour via FieldStorage altogether?

Peter

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
40656
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or* just created a new pointer to the original value.... For example I wanted to initialize a list of empty lists.... a=, , , , ] I thought there has to be a *really* easy way of doing it - after a
35
3000
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you could do: result = for element in list: if element == 'blah':
7
2280
by: Chris P. | last post by:
Hi. I've made a program that logs onto a telnet server, enters a command, and then creates a list of useful information out of the information that is dumped to the screen as a result of the command. Here's a generic version of the code in question: ##### # Prior code opens telnet connection "tn" and logs in. tn.read_until('> ') tn.write('THE COMMAND IS HERE\n')
6
1990
by: jena | last post by:
hello, when i create list of lambdas: l=] then l() returns 'C', i think, it should be 'A' my workaround is to define helper class with __call__ method: class X: def __init__(self,s): self.s=s def __call__(self): return self.s.upper() l=]
18
460
by: a | last post by:
can someone tell me how to use them thanks
4
1815
by: Gregory Guthrie | last post by:
Sorry for a simple question- but I don't understand how to parse this use of a list comprehension. The "or" clauses are odd to me. It also seems like it is being overly clever (?) in using a lc expression as a for loop to drive the recursion. Thanks for any insight! Gregory
4
1571
by: bullockbefriending bard | last post by:
Given: class Z(object): various defs, etc. class ZList(list): various defs, etc. i would like to be able to replace
10
15036
by: Debajit Adhikary | last post by:
I have two lists: a = b = What I'd like to do is append all of the elements of b at the end of a, so that a looks like: a =
4
3059
by: beginner | last post by:
Hi All, If I have a list comprehension: ab= c = "ABC" print c
0
9572
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10070
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.