Some python syntax that I'm not getting


W

waltbrad

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
 
Ad

Advertisements

C

Chris

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.
 
L

Laurent Pointal

Chris a écrit :
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.
 
C

Chris Mellon

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.
 
M

Matt

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.
 
Ad

Advertisements

D

Dennis Lee Bieber

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 said:
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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
Ad

Advertisements


Top