Some python syntax that I'm not getting

Discussion in 'Python' started by waltbrad, Dec 7, 2007.

  1. waltbrad

    waltbrad Guest

    Hello. Been studying Python for about a week now. I did a quick read
    of the tutorial in the manual and I'm reading Programming Python by
    Mark Lutz. I'm still getting used to the Python syntax, but I'm able
    to pretty much follow what is being said. But tonight Lutz was
    talking about implementing a database on a website and threw out this
    piece in his code:

    <tr><th>key<td><input type=text name=key value="%(key)s">

    That last bit is the bit that throws me: %(keys)s

    He explains this so:

    "The only feat of semimagic it relies on is using a record's attribute
    dictionary (__dict__) as the source of values when applying string
    formatting to the HTML reply template string in the last line of the
    script. Recall that a %(key)code replacement target fetches a value by
    key from a dictionary:

    >>> D = {'say': 5, 'get': 'shrubbery'}
    >>> D['say']

    5
    >>> S = '%(say)s => %(get)s' % D
    >>> S

    '5 => shrubbery' "

    Hmmmmm...

    I understand how D['say'] gets you 5, But I still don't understand
    the line after the 5.

    How is the character 's' some special code? And I don't get what is
    going on with the % character. I'm used to it's use in c-style
    formatting, but this just seems so bizarre. I can tell that the key
    is being replaced by it's value in the string, but I don't know how
    that is being done.

    TIA
    waltbrad, Dec 7, 2007
    #1
    1. Advertising

  2. waltbrad

    Chris Guest

    On Dec 7, 2:31 pm, waltbrad <> wrote:
    >
    > I understand how D['say'] gets you 5, But I still don't understand
    > the line after the 5.
    >
    > How is the character 's' some special code? And I don't get what is
    > going on with the % character. I'm used to it's use in c-style
    > formatting, but this just seems so bizarre. I can tell that the key
    > is being replaced by it's value in the string, but I don't know how
    > that is being done.
    >
    > TIA


    http://docs.python.org/lib/typesseq-strings.html

    The '%' invokes the formatter, the 's' specifies string type.
    Chris, Dec 7, 2007
    #2
    1. Advertising

  3. Chris a écrit :
    > On Dec 7, 2:31 pm, waltbrad <> wrote:
    >> I understand how D['say'] gets you 5, But I still don't understand
    >> the line after the 5.
    >>
    >> How is the character 's' some special code? And I don't get what is
    >> going on with the % character. I'm used to it's use in c-style
    >> formatting, but this just seems so bizarre. I can tell that the key
    >> is being replaced by it's value in the string, but I don't know how
    >> that is being done.
    >>
    >> TIA

    >
    > http://docs.python.org/lib/typesseq-strings.html
    >
    > The '%' invokes the formatter, the 's' specifies string type.


    And the (name) specify to find the value to format (following %s rules)
    in a dictionnary given as % operator parameter, under the name key.
    Laurent Pointal, Dec 7, 2007
    #3
  4. waltbrad

    Chris Mellon Guest

    On Dec 7, 2007 6:31 AM, waltbrad <> wrote:
    > Hello. Been studying Python for about a week now. I did a quick read
    > of the tutorial in the manual and I'm reading Programming Python by
    > Mark Lutz. I'm still getting used to the Python syntax, but I'm able
    > to pretty much follow what is being said. But tonight Lutz was
    > talking about implementing a database on a website and threw out this
    > piece in his code:
    >
    > <tr><th>key<td><input type=text name=key value="%(key)s">
    >
    > That last bit is the bit that throws me: %(keys)s
    >
    > He explains this so:
    >
    > "The only feat of semimagic it relies on is using a record's attribute
    > dictionary (__dict__) as the source of values when applying string
    > formatting to the HTML reply template string in the last line of the
    > script. Recall that a %(key)code replacement target fetches a value by
    > key from a dictionary:
    >
    > >>> D = {'say': 5, 'get': 'shrubbery'}
    > >>> D['say']

    > 5
    > >>> S = '%(say)s => %(get)s' % D
    > >>> S

    > '5 => shrubbery' "
    >
    > Hmmmmm...
    >
    > I understand how D['say'] gets you 5, But I still don't understand
    > the line after the 5.
    >
    > How is the character 's' some special code? And I don't get what is
    > going on with the % character. I'm used to it's use in c-style
    > formatting, but this just seems so bizarre. I can tell that the key
    > is being replaced by it's value in the string, but I don't know how
    > that is being done.
    >
    > TIA
    >


    Python string interpolation is based on the C printf style:
    "one two %s" % ("three",)

    is basically the same as
    printf("one two %s", "three")

    (% being overload for strings to do formatting).

    Used this way, as in C, each wildcard in the format string is replaced
    in order with the corresponding item in the format arguments, which is
    a sequence.

    However, you can also provide a dictionary (that is, a key/value
    mapping) to the string formatter, and use the %(key)s format. The name
    in the parenthesis is looked up in the provided dictionary, rather
    than by position.

    This is basically the same thing as the difference between positional
    and named arguments.
    Chris Mellon, Dec 7, 2007
    #4
  5. waltbrad

    Matt Guest

    Think of it as using a name instead of a position for your "%s".

    In addition to what others already said, I thought I'd add an example
    of where this is useful.
    One place where you don't just want to have a position is when doing
    internatiolization.
    When translating for example:
    "I'm going by %(transport)s to %(place)s" % ( {'transport' : 'car',
    'place' : 'mexico'} )

    In another language, the 'place' and 'transport' might not be in the
    same order in the sentence, so pretty much the only way to get it
    right is to use named parameters instead of positions.
    Matt, Dec 8, 2007
    #5
  6. On Sat, 8 Dec 2007 03:17:52 -0800 (PST), Matt
    <> declaimed the following in
    comp.lang.python:

    > One place where you don't just want to have a position is when doing
    > internatiolization.
    > When translating for example:
    > "I'm going by %(transport)s to %(place)s" % ( {'transport' : 'car',
    > 'place' : 'mexico'} )
    >

    I don't think the outer () are needed on the dictionary <G>

    > In another language, the 'place' and 'transport' might not be in the
    > same order in the sentence, so pretty much the only way to get it
    > right is to use named parameters instead of positions.


    Of course, not mentioned is the oldie "mail-merge" process...

    -=-=-=-=-=-

    TEMPLATE = """
    Spam, Inc.
    221b Parrot Lane
    Larch


    %(title)s %(given)s %(last)s
    %(street)s
    %(city)s, %(state)s %(postcode)s


    Dear %(title)s %(last)s:

    Spam, Inc. is happy to announce the availability
    of our direct home delivery service in %(city)s. Now,
    you too, %(given)s, can receive our full product line,
    delivered right to your door at %(street)s....
    """

    VICTIMS = [ {"title" : "Miss",
    "given" : "Little Red",
    "last" : "Riding-Hood",
    "street" : "Granny's House",
    "city" : "Forest",
    "state" : "Disbelief",
    "postcode" : "3.14159" } ]


    for v in VICTIMS:
    print TEMPLATE % v

    -=-=-=-=-=-=-


    Spam, Inc.
    221b Parrot Lane
    Larch


    Miss Little Red Riding-Hood
    Granny's House
    Forest, Disbelief 3.14159


    Dear Miss Riding-Hood:

    Spam, Inc. is happy to announce the availability
    of our direct home delivery service in Forest. Now,
    you too, Little Red, can receive our full product line,
    delivered right to your door at Granny's House....

    -=-=-=-=-=-=-=-

    Using the dictionary form allows values to be used multiple times
    without needing to duplicate them on a positional parameter list.
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Dec 8, 2007
    #6
    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. Binoy
    Replies:
    6
    Views:
    442
    Binoy
    May 13, 2004
  2. =?Utf-8?B?bWF2cmlja18xMDE=?=

    Forms Authentication Fails some times and not some times???

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    488
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 28, 2006
  3. Jeremy
    Replies:
    2
    Views:
    342
    Jeremy
    Nov 27, 2007
  4. MM User

    Spaces in some files some not

    MM User, Dec 11, 2008, in forum: ASP General
    Replies:
    3
    Views:
    90
    Adrienne Boswell
    Dec 13, 2008
  5. Good Night Moon
    Replies:
    9
    Views:
    280
    Rick DeNatale
    Jul 25, 2007
Loading...

Share This Page