Re: Loop through a dict changing keys

2011-10-17 Thread Gnarlodious
Steven: Thanks for those tips, I've implemented all of them. Also only allowing whitelisted variable names. Feeling much more confident. -- Gnarlie -- http://mail.python.org/mailman/listinfo/python-list

Re: Loop through a dict changing keys

2011-10-17 Thread Steven D'Aprano
On Sun, 16 Oct 2011 17:41:55 -0700, Gnarlodious wrote: > On Oct 16, 5:25 pm, Steven D'Aprano +comp.lang.pyt...@pearwood.info> wrote: > >> How do you sanitize user input? > Thanks for your concern. This is what I now have, which merely expands > each value into its usable type (unquotes them): >

Re: Loop through a dict changing keys

2011-10-17 Thread Gnarlodious
On Oct 16, 5:25 pm, Steven D'Aprano wrote: > How do you sanitize user input? Thanks for your concern. This is what I now have, which merely expands each value into its usable type (unquotes them): # filter each value try: var=int(var) except ValueError: if var in ('False', 'True'): v

Re: Loop through a dict changing keys

2011-10-17 Thread PoD
On Sun, 16 Oct 2011 00:18:40 -0700, Jon Clements wrote: > On Oct 16, 12:53 am, PoD wrote: >> On Sat, 15 Oct 2011 11:00:17 -0700, Gnarlodious wrote: >> > What is the best way (Python 3) to loop through dict keys, examine >> > the string, change them if needed, and save the changes to the same >> >

Re: Loop through a dict changing keys

2011-10-17 Thread Steven D'Aprano
On Sun, 16 Oct 2011 11:20:49 -0700, Gnarlodious wrote: > On Oct 15, 5:53 pm, PoD wrote: > >> data = { >>     'Mobile': 'string', >>     'context': '', >>     'order': '7', >>     'time': 'True'} >> types={'Mobile':str,'context':str,'order':int,'time':bool} >> >> for k,v in data.items(): >>     d

Re: Loop through a dict changing keys

2011-10-17 Thread Ian Kelly
On Mon, Oct 17, 2011 at 10:21 AM, 8 dihedral wrote: > Uh, sounds reasonable, if one loops over an index variable  that could be > altered during the loop execution then the loop may not end as expected. >From the docs: "Iterating views while adding or deleting entries in the dictionary may

Re: Loop through a dict changing keys

2011-10-17 Thread 88888 dihedral
Uh, sounds reasonable, if one loops over an index variable that could be altered during the loop execution then the loop may not end as expected. -- http://mail.python.org/mailman/listinfo/python-list

Re: Loop through a dict changing keys

2011-10-17 Thread Chris Angelico
On Mon, Oct 17, 2011 at 5:20 AM, Gnarlodious wrote: > On Oct 15, 5:53 pm, PoD wrote: > >> types={'Mobile':str,'context':str,'order':int,'time':bool} >> >> for k,v in data.items(): >>     data[k] = types[k](v) > > Thanks for the tip, I didn't know you could do that. I ended up > filtering the valu

Re: Loop through a dict changing keys

2011-10-17 Thread Gnarlodious
On Oct 15, 5:53 pm, PoD wrote: > data = { >     'Mobile': 'string', >     'context': '', >     'order': '7', >     'time': 'True'} > types={'Mobile':str,'context':str,'order':int,'time':bool} > > for k,v in data.items(): >     data[k] = types[k](v) Thanks for the tip, I didn't know you could do

Re: Loop through a dict changing keys

2011-10-16 Thread Jon Clements
On Oct 16, 12:53 am, PoD wrote: > On Sat, 15 Oct 2011 11:00:17 -0700, Gnarlodious wrote: > > What is the best way (Python 3) to loop through dict keys, examine the > > string, change them if needed, and save the changes to the same dict? > > > So for input like this: > > {'Mobile': 'string', 'cont

Re: Loop through a dict changing keys

2011-10-15 Thread PoD
On Sat, 15 Oct 2011 11:00:17 -0700, Gnarlodious wrote: > What is the best way (Python 3) to loop through dict keys, examine the > string, change them if needed, and save the changes to the same dict? > > So for input like this: > {'Mobile': 'string', 'context': '', 'order': '7', > 'time': 'True'}

Re: Loop through a dict changing keys

2011-10-15 Thread 88888 dihedral
Is there an FAQ available here? Please check the PYTHON official site and the active state PYTHON examples first, also check the PLEAC comparisons of a lot programming languages first! - Nothing is more thrilling to o

Re: Loop through a dict changing keys

2011-10-15 Thread Alexander Kapps
On 15.10.2011 20:00, Gnarlodious wrote: What is the best way (Python 3) to loop through dict keys, examine the string, change them if needed, and save the changes to the same dict? So for input like this: {'Mobile': 'string', 'context': '', 'order': '7', 'time': 'True'} I want to booleanize 'Tr

Re: Loop through a dict changing keys

2011-10-15 Thread MRAB
On 15/10/2011 19:00, Gnarlodious wrote: What is the best way (Python 3) to loop through dict keys, examine the string, change them if needed, and save the changes to the same dict? So for input like this: {'Mobile': 'string', 'context': '', 'order': '7', 'time': 'True'} I want to booleanize 'Tr

Loop through a dict changing keys

2011-10-15 Thread Gnarlodious
What is the best way (Python 3) to loop through dict keys, examine the string, change them if needed, and save the changes to the same dict? So for input like this: {'Mobile': 'string', 'context': '', 'order': '7', 'time': 'True'} I want to booleanize 'True', turn '7' into an integer, escape '',