Re: CGI input: Filter dict.update() unwanted variables

2011-08-23 Thread Chris Angelico
On Mon, Aug 22, 2011 at 4:39 PM, Miki Tebeka wrote: > You can check if there is a "non-allowed variable" and then return HTTP error. > if set(form) - set(allowedVariables): >    print('Status: 406\n\n') >    raise SystemExit() > I'd be disinclined to do this; ignore unrecognized query variables,

Re: CGI input: Filter dict.update() unwanted variables

2011-08-22 Thread Gnarlodious
On Aug 22, 9:39 am, Miki Tebeka wrote: > HTH Yes it helps, thank you! -- Gnarlie http://Gnarlodious.com -- http://mail.python.org/mailman/listinfo/python-list

Re: CGI input: Filter dict.update() unwanted variables

2011-08-22 Thread Miki Tebeka
> Is there an easy way to limit updates to > ONLY variables in the allowedVariables dict? allowedVariables = ['eeny', 'meeny', 'miny', 'mo'] form = cgi.FieldStorage() safe_input = dict((key, form.getvalue(key)) for key in allowedVariables) > And in addition, maybe return an error so the attacke

CGI input: Filter dict.update() unwanted variables

2011-08-22 Thread Gnarlodious
In my last post I learned of the necessity of filtering CGI input, so what I want to do is set a dict of allowable variable names: allowedVariables = {'eeny':None, 'meeny':None, 'miny':None, 'mo':None} # Set up a FieldStorage object: import cgi inputVariables = cgi.FieldStorage() for name, value

Re: dict.update

2008-09-02 Thread Tino Wildenhain
csv file that would have the form id var1 var2 123 4 3 157 2 0 i looks like the dict.update looks almost there but i can't get it to work properly, can anyone offer any advise? result=dict((key,(dict1.get(key,None), dict2.get(key,None))) for key i

Re: dict.update

2008-09-02 Thread Mike P
Thanks Raymond, That's a neat trick, i'll look into learning more about this Mike -- http://mail.python.org/mailman/listinfo/python-list

Re: dict.update

2008-09-02 Thread Mike P
Thanks Diez, This is almost perfect! Is there a way to ensure each list has two elements, even if one of them is blank? Mike -- http://mail.python.org/mailman/listinfo/python-list

Re: dict.update

2008-09-02 Thread Raymond Hettinger
gt; dict3 = {123:[4,3],157:[2,0],234:[5,5],456:[3,3],567:[2,0]} > > As later on i want to write a csv file that would have the form > > id      var1  var2 > 123     4      3 > 157     2      0 > > i looks like the dict.update looks almost there but i can't get it to > wor

Re: dict.update

2008-09-02 Thread Diez B. Roggisch
a csv file that would have the form id var1 var2 123 4 3 157 2 0 i looks like the dict.update looks almost there but i can't get it to work properly, can anyone offer any advise? res = {} for d in dict1, dict2: for key, value in d.iteritems(): res.setde

dict.update

2008-09-02 Thread Mike P
would have the form id var1 var2 123 4 3 157 2 0 i looks like the dict.update looks almost there but i can't get it to work properly, can anyone offer any advise? -- http://mail.python.org/mailman/listinfo/python-list

Re: dict.update() useful or not?

2008-08-13 Thread Aahz
In article <[EMAIL PROTECTED]>, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > >What do other people find? Do you find use for dict.update()? What other >idioms do you use for combining dictionaries? My company's code relies heavily on d.update() -- it's ext

Re: dict.update() useful or not?

2008-08-11 Thread Martin v. Löwis
>> def create_request(url, headers): >> headers.update(DEFAULT_HEADERS) >> req = urllib2.Request(url, None, headers) >> # ... >> return req >> >> but of course this second example does the Wrong Thing, replacing >> explicit headers with default values. > > There's a second code s

Re: dict.update() useful or not?

2008-08-11 Thread Duncan Booth
Steven D'Aprano <[EMAIL PROTECTED]> wrote: > dict1.update(dict2) is of course equivalent to this code: > > for key, value in dict2.iteritems(): > dict1[key] = value > > Note that it replaces values in dict1 with the value taken from dict2. I > don't know about other people, but I more often

dict.update() useful or not?

2008-08-11 Thread Steven D'Aprano
urllib2.Request(url, None, headers) # ... return req but of course this second example does the Wrong Thing, replacing explicit headers with default values. What do other people find? Do you find use for dict.update()? What other idioms do you use for combining dictionaries?

Re: Optimize function similiar to dict.update() but adds common values

2005-12-15 Thread Peter Otten
Christopher Subich wrote: > Peter Otten wrote: >> >> def add_freqs3(freq1, freq2): >> total = dict(freq1) >> for key, value in freq2.iteritems(): >> try: >> total[key] += value >> except KeyError: >> total[key] = value >> return total >> > > U

Re: Optimize function similiar to dict.update() but adds common values

2005-12-15 Thread bonono
Christopher Subich wrote: > Peter Otten wrote: > > > > def add_freqs3(freq1, freq2): > > total = dict(freq1) > > for key, value in freq2.iteritems(): > > try: > > total[key] += value > > except KeyError: > > total[key] = value > > return total >

Re: Optimize function similiar to dict.update() but adds common values

2005-12-15 Thread Christopher Subich
Peter Otten wrote: > > def add_freqs3(freq1, freq2): > total = dict(freq1) > for key, value in freq2.iteritems(): > try: > total[key] += value > except KeyError: > total[key] = value > return total > Untested, but replacing the try/except pair

Re: Optimize function similiar to dict.update() but adds common values

2005-12-14 Thread Gregory Piñero
OK, I ran Peter's add_freq3 and it ran four times on really large dictionaries in about 3000 seconds. So I'd say that at a minimum that's ten times faster than my original function since it ran all last night and didn't finish. Much obliged, Peter! -Greg On 12/14/05, Gregory Piñero <[EMAIL PRO

Re: Optimize function similiar to dict.update() but adds common values

2005-12-14 Thread Gregory Piñero
Yes, that makes sense. I can't wait to run it tonight. Sorry I can't give you the running time of my original function as it never finished :-( I'll report back the running time of the new function though, assuming it finishes ;-) Thanks again, -Greg On 12/14/05, Peter Otten <[EMAIL PROTECTE

Re: Optimize function similiar to dict.update() but adds common values

2005-12-14 Thread Peter Otten
Gregory Piñero wrote: > Here's a question about your functions. if I only look at the keys in > freq2 then won't I miss any keys that are in freq1 and not in freq2? No. As I start with a copy of freq1, all keys of freq1 are already there. There is probably a loop involved, but it in Python's un

Re: Optimize function similiar to dict.update() but adds common values

2005-12-14 Thread Steve Holden
Gregory Piñero wrote: [top-posting rearranged] > On 12/14/05, Peter Otten <[EMAIL PROTECTED]> wrote: > >>Gregory Piñero wrote: >> >> >>>def add_freqs(freq1,freq2): >>>"""Addtwowordfreqdicts""" >>>newfreq={} >>>forkey,valueinfreq1.items(): >>>newfreq[key]=value+freq2.get(key,0) >>>forkey,valueinfre

Re: Optimize function similiar to dict.update() but adds common values

2005-12-14 Thread Gregory Piñero
Thanks Peter, those are some really good ideas. I can't wait to try them out tonight. Here's a question about your functions. if I only look at the keys in freq2 then won't I miss any keys that are in freq1 and not in freq2? That's why I have the two loops in my original function. -Greg On 1

Re: Optimize function similiar to dict.update() but adds common values

2005-12-14 Thread Peter Otten
Gregory Piñero wrote: > def add_freqs(freq1,freq2): > """Add two word freq dicts""" > newfreq={} > for key,value in freq1.items(): > newfreq[key]=value+freq2.get(key,0) > for key,value in freq2.items(): > newfreq[key]=value+freq1.get(key,0) > return newfreq > A

Optimize function similiar to dict.update() but adds common values

2005-12-14 Thread Gregory Piñero
Hey guys, I thought I'd throw this out there since everyone loves optimization questions (it's true, check out the number of replies to those type of questions!) Right now I have a function shown below. I want to combine two dictionaries and add the values together where-ever there is overlap.