locals() and dictionaries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JerryB

    #1

    locals() and dictionaries

    Hi,
    I have a dictionary, a string, and I'm creating another string, like
    this:

    dict = {}
    dict[beatles] = "need"
    str = "love"

    mystr = """All you %(dict[beatles])s is %(str)s""" % locals()

    Why do I get
    keyerror: 'dict[one]'?

    Is there a way to reference the elements in a dictionary with locals()
    or do I need to create a temp variable, like

    need = dict[one]
    mystr = """All you %(need)s is %(str)s"""

  • Rocco Moretti

    #2
    Re: locals() and dictionaries

    JerryB wrote:[color=blue]
    > Hi,
    > I have a dictionary, a string, and I'm creating another string, like
    > this:
    >
    > dict = {}
    > dict[beatles] = "need"
    > str = "love"
    >
    > mystr = """All you %(dict[beatles])s is %(str)s""" % locals()
    >
    > Why do I get
    > keyerror: 'dict[one]'?
    >
    > Is there a way to reference the elements in a dictionary with locals()
    > or do I need to create a temp variable, like
    >
    > need = dict[one]
    > mystr = """All you %(need)s is %(str)s"""[/color]

    1) Avoid variable names like 'dict' and 'str'- they cover up the builtin
    names.

    2) When showing error, don't retype - cut and paste:
    [color=blue][color=green][color=darkred]
    >>> dict[beatles] = "need"[/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#6 >", line 1, in -toplevel-
    dict[beatles] = "need"
    NameError: name 'beatles' is not defined[color=blue][color=green][color=darkred]
    >>> dict['beatles'] = "need"
    >>>[/color][/color][/color]

    3) In string formating, the item in parenthesis, used as a string, is
    the key for the dictionary. That is:

    """All you %(dict[beatles])s is %(str)s""" % ld

    is the same as

    """All you %s is %s""" % (ld['dict[beatles]'],ld['str'])

    4) Your best bet is not to use locals(), but to create a new dictionary
    with the appropriate keys. E.g.:
    [color=blue][color=green][color=darkred]
    >>> d = {}
    >>> d['beatles'] = "need"
    >>> s = "love"
    >>> d2 = d.copy()
    >>> d2['str'] = s
    >>> d['str'][/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#2 4>", line 1, in -toplevel-
    d['str']
    KeyError: 'str'[color=blue][color=green][color=darkred]
    >>> d2['str'][/color][/color][/color]
    'love'[color=blue][color=green][color=darkred]
    >>> mystr = """All you %(beatles)s is %(str)s""" % d2
    >>> print mystr[/color][/color][/color]
    All you need is love

    Comment

    • JerryB

      #3
      Re: locals() and dictionaries

      Rocco:
      thanks for your response. The examples were just made up. I don't
      normally use 'dict' and 'str'.
      I know I can create a dictionary with the variables I want, etc. My
      question is not how to solve the problem, or how to come up with a
      work-around (I'm getting pretty good at this one :), so my question
      stands:

      is it possible to access the individual members of a dictionary using %
      locals() when creating a string?

      Thank you again for your suggestions.
      Jerry

      Comment

      • Alex Martelli

        #4
        Re: locals() and dictionaries

        JerryB <grblanco@gmail .com> wrote:
        ...[color=blue]
        > is it possible to access the individual members of a dictionary using %
        > locals() when creating a string?[/color]

        Not by using the built-in locals(); you'd have to override locals to
        mean someting different (not recommended).


        Alex

        Comment

        • Kent Johnson

          #5
          Re: locals() and dictionaries

          JerryB wrote:[color=blue]
          > Rocco:
          > thanks for your response. The examples were just made up. I don't
          > normally use 'dict' and 'str'.
          > I know I can create a dictionary with the variables I want, etc. My
          > question is not how to solve the problem, or how to come up with a
          > work-around (I'm getting pretty good at this one :), so my question
          > stands:
          >
          > is it possible to access the individual members of a dictionary using %
          > locals() when creating a string?[/color]

          You might want to use a more powerful templating engine, for example
          http://cheetahtemplate.org/ and probably many others.

          Kent

          Comment

          Working...