Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > expanding a variable to a dict

Reply
Thread Tools

expanding a variable to a dict

 
 
idle
Guest
Posts: n/a
 
      04-03-2008
I've got a variable in a loop that I'm trying to expand/translate/
readdress as an existing dict so as to add some keys into it..

eg; I have a set of existing dicts: dictFoo, dictBar, dictFrotz (names
changed to protect the innocent)

now I'd like to check them all for the existence of certain default
keys; ie, if the dicts don't contain the keys, add them in with
default values.

so, I've got:

for a in ['dictFoo','dictBar','dictFrotz']:
if hasattr(a,'srcdir') == False:
a['srcdir']='/usr/src'

the error I get (which I expect) is 'str' object doesn't support item
assignment.

what incantation do I cast on 'a' to make the interpreter parse it as
'dictFoo' on the first iteration, 'dictBar' on the second, and so
forth?

and/or less importantly, what is such a transformation called, to help
me target my searching?

thanks
 
Reply With Quote
 
 
 
 
Arnaud Delobelle
Guest
Posts: n/a
 
      04-03-2008
On Apr 3, 10:56*pm, idle <spiro.har...@gmail.com> wrote:
> I've got a variable in a loop that I'm trying to expand/translate/
> readdress as an existing dict so as to add some keys into it..
>
> eg; I have a set of existing dicts: dictFoo, dictBar, dictFrotz (names
> changed to protect the innocent)
>
> now I'd like to check them all for the existence of certain default
> keys; ie, if the dicts don't contain the keys, add them in with
> default values.
>
> so, I've got:
>
> for a in ['dictFoo','dictBar','dictFrotz']:
> * * if hasattr(a,'srcdir') == False:
> * * * * a['srcdir']='/usr/src'
>
> the error I get (which I expect) is 'str' object doesn't support item
> assignment.
>
> what incantation do I cast on 'a' to make the interpreter parse it as
> 'dictFoo' on the first iteration, 'dictBar' on the second, and so
> forth?
>
> and/or less importantly, what is such a transformation called, to help
> me target my searching?
>
> thanks


You want a to iterate through the dictionary *objects*, not *names*,
so write

for a in [dictFoo, dictBar, dictFrotz]:
...

BTW, hasattr() is not what you want as it check the existence of an
attribute, i.e. a.hasattr('x') means that a.x exists; you could write
the whole thing as:

for a in dictFoo, dictBar, dictFrotz:
if 'srcdir' not in a:
a['srcdir'] = '/usr/src'

Or more concisely:

for a in ... :
a.setdefault('srcdir', '/usr/src')


For more information, help(dict) is your friend

HTH

--
Arnaud

 
Reply With Quote
 
 
 
 
Gary Herron
Guest
Posts: n/a
 
      04-03-2008
idle wrote:
> I've got a variable in a loop that I'm trying to expand/translate/
> readdress as an existing dict so as to add some keys into it..
>
> eg; I have a set of existing dicts: dictFoo, dictBar, dictFrotz (names
> changed to protect the innocent)
>
> now I'd like to check them all for the existence of certain default
> keys; ie, if the dicts don't contain the keys, add them in with
> default values.
>
> so, I've got:
>
> for a in ['dictFoo','dictBar','dictFrotz']:
> if hasattr(a,'srcdir') == False:
> a['srcdir']='/usr/src'
>
> the error I get (which I expect) is 'str' object doesn't support item
> assignment.
>
> what incantation do I cast on 'a' to make the interpreter parse it as
> 'dictFoo' on the first iteration, 'dictBar' on the second, and so
> forth?
>
> and/or less importantly, what is such a transformation called, to help
> me target my searching?
>
> thanks
>


Try this:

for a in [dictFoo, dictBar, dictFrotz]:
if 'srcdir' in a:
a['srcdir']='/usr/src'

Gary Herron




 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      04-03-2008
On Apr 4, 8:56 am, idle <spiro.har...@gmail.com> wrote:
> I've got a variable in a loop that I'm trying to expand/translate/
> readdress as an existing dict so as to add some keys into it..
>
> eg; I have a set of existing dicts: dictFoo, dictBar, dictFrotz (names
> changed to protect the innocent)
>
> now I'd like to check them all for the existence of certain default
> keys; ie, if the dicts don't contain the keys, add them in with
> default values.
>
> so, I've got:
>
> for a in ['dictFoo','dictBar','dictFrotz']:
> if hasattr(a,'srcdir') == False:
> a['srcdir']='/usr/src'
>
> the error I get (which I expect) is 'str' object doesn't support item
> assignment.
>
> what incantation do I cast on 'a' to make the interpreter parse it as
> 'dictFoo' on the first iteration, 'dictBar' on the second, and so
> forth?
>
> and/or less importantly, what is such a transformation called, to help
> me target my searching?
>


It's called "deleting extraneous apostrophes from source code".

Happy googling!
 
Reply With Quote
 
idle
Guest
Posts: n/a
 
      04-03-2008
brilliant.

thanks to both of you.
 
Reply With Quote
 
Gary Herron
Guest
Posts: n/a
 
      04-03-2008
idle wrote:
> brilliant.
>

Hardly. Any perceived brilliance is in the design of Python, not our
simple and hopefully effective use of it.

Gary Herron


> thanks to both of you.
>


 
Reply With Quote
 
Max M
Guest
Posts: n/a
 
      04-03-2008
idle skrev:

> now I'd like to check them all for the existence of certain default
> keys; ie, if the dicts don't contain the keys, add them in with
> default values.
>
> so, I've got:
>
> for a in ['dictFoo','dictBar','dictFrotz']:
> if hasattr(a,'srcdir') == False:
> a['srcdir']='/usr/src'


There are a few ways to do it.

for a in ['dictFoo','dictBar','dictFrotz']:
if not a.has_key('srcdir'):
a['srcdir'] = '/usr/src'

for a in ['dictFoo','dictBar','dictFrotz']:
if not 'srcdir' in a:
a['srcdir'] = '/usr/src'

for a in ['dictFoo','dictBar','dictFrotz']:
a.setdefault('srcdir') = '/usr/src'


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      04-03-2008
On Apr 4, 9:44 am, Max M <m...@mxm.dk> wrote:
> idle skrev:
>
> > now I'd like to check them all for the existence of certain default
> > keys; ie, if the dicts don't contain the keys, add them in with
> > default values.

>
> > so, I've got:

>
> > for a in ['dictFoo','dictBar','dictFrotz']:
> > if hasattr(a,'srcdir') == False:
> > a['srcdir']='/usr/src'

>
> There are a few ways to do it.
>
> for a in ['dictFoo','dictBar','dictFrotz']:


Ummm ... excessive apostrophes plus bonus gross syntax error, dood.
Did you try running any of these snippets???

> if not a.has_key('srcdir'):


a is a str object. Bang.


> a['srcdir'] = '/usr/src'
>
> for a in ['dictFoo','dictBar','dictFrotz']:
> if not 'srcdir' in a:
> a['srcdir'] = '/usr/src'


a is a str object. Bang.
>
> for a in ['dictFoo','dictBar','dictFrotz']:
> a.setdefault('srcdir') = '/usr/src'


SyntaxError: can't assign to function call

>
> --
>
> hilsen/regards Max M, Denmark
>
> http://www.mxm.dk/
> IT's Mad Science


Sure is.
 
Reply With Quote
 
Max M
Guest
Posts: n/a
 
      04-09-2008
John Machin skrev:
> On Apr 4, 9:44 am, Max M <m...@mxm.dk> wrote:



> Ummm ... excessive apostrophes plus bonus gross syntax error, dood.
> Did you try running any of these snippets???


No I just wanted to quickly show different ways to do it.

The dicts in the original question wasn't dicts either. So I asumed I
could answer in the same vein.



>> http://www.mxm.dk/
>> IT's Mad Science

>
> Sure is.


Oh yes. That will motivate further answers.


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dict.items() vs dict.iteritems and similar questions Drew Python 19 03-15-2007 09:23 PM
dict.has_key(x) versus 'x in dict' Paul Melis Python 48 12-15-2006 05:55 PM
Inconsistency in dictionary behaviour: dict(dict) not calling __setitem__ Almad Python 8 12-14-2006 07:37 PM
dict!ident as equivalent of dict["ident"] Alexander Kozlovsky Python 5 05-22-2006 08:06 AM
Re: dict->XML->dict? Or, passing small hashes through text? Skip Montanaro Python 0 08-15-2003 03:46 PM



Advertisments