Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Some python syntax that I'm not getting

Reply
Thread Tools

Some python syntax that I'm not getting

 
 
waltbrad
Guest
Posts: n/a
 
      12-07-2007
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

 
Reply With Quote
 
 
 
 
Chris
Guest
Posts: n/a
 
      12-07-2007
On Dec 7, 2:31 pm, waltbrad <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Laurent Pointal
Guest
Posts: n/a
 
      12-07-2007
Chris a écrit :
> On Dec 7, 2:31 pm, waltbrad <(E-Mail Removed)> 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.

 
Reply With Quote
 
Chris Mellon
Guest
Posts: n/a
 
      12-07-2007
On Dec 7, 2007 6:31 AM, waltbrad <(E-Mail Removed)> 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.
 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      12-08-2007

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.
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      12-08-2007
On Sat, 8 Dec 2007 03:17:52 -0800 (PST), Matt
<(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
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
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Syntax Checker that's better than the normal syntax checker Jacob Grover Ruby 5 07-18-2008 05:07 AM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM



Advertisments