expanding a variable to a dict

Discussion in 'Python' started by idle, Apr 3, 2008.

  1. idle

    idle Guest

    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
     
    idle, Apr 3, 2008
    #1
    1. Advertising

  2. On Apr 3, 10:56 pm, 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


    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
     
    Arnaud Delobelle, Apr 3, 2008
    #2
    1. Advertising

  3. idle

    Gary Herron Guest

    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
     
    Gary Herron, Apr 3, 2008
    #3
  4. idle

    John Machin Guest

    On Apr 4, 8:56 am, 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?
    >


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

    Happy googling!
     
    John Machin, Apr 3, 2008
    #4
  5. idle

    idle Guest

    brilliant.

    thanks to both of you.
     
    idle, Apr 3, 2008
    #5
  6. idle

    Gary Herron Guest

    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.
    >
     
    Gary Herron, Apr 3, 2008
    #6
  7. idle

    Max M Guest

    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
     
    Max M, Apr 3, 2008
    #7
  8. idle

    John Machin Guest

    On Apr 4, 9:44 am, Max M <> 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.
     
    John Machin, Apr 4, 2008
    #8
  9. idle

    Max M Guest

    John Machin skrev:
    > On Apr 4, 9:44 am, Max M <> 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
     
    Max M, Apr 9, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skip Montanaro
    Replies:
    0
    Views:
    428
    Skip Montanaro
    Aug 15, 2003
  2. Alexander Kozlovsky

    dict!ident as equivalent of dict["ident"]

    Alexander Kozlovsky, May 21, 2006, in forum: Python
    Replies:
    5
    Views:
    380
    Alexander Kozlovsky
    May 22, 2006
  3. Paul Melis

    dict.has_key(x) versus 'x in dict'

    Paul Melis, Dec 6, 2006, in forum: Python
    Replies:
    48
    Views:
    1,354
    Kent Johnson
    Dec 15, 2006
  4. Almad
    Replies:
    8
    Views:
    426
    Terry Reedy
    Dec 14, 2006
  5. Drew
    Replies:
    19
    Views:
    1,369
    Duncan Booth
    Mar 15, 2007
Loading...

Share This Page