473,320 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Some python syntax that I'm not getting

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

Dec 7 '07 #1
2 1266
On Dec 7, 2:31 pm, waltbrad <waltb...@hotmail.comwrote:
>
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.
Dec 7 '07 #2
Chris a écrit :
On Dec 7, 2:31 pm, waltbrad <waltb...@hotmail.comwrote:
>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.

Dec 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

75
by: David MacQuigg | last post by:
Seems like we need a simple way to extend Python syntax that doesn't break existing syntax or clash with any other syntax in Python, is easy to type, easy to read, and is clearly distinct from the...
14
by: Uwe Mayer | last post by:
Hi, I know the python community is not very receptive towards extending the python syntax. Nevertheless I'd like to make a suggestion and hear your pro and cons. I want so suggest a...
3
by: Noah | last post by:
I'm getting configure warnings and make errors when I try to build Python 2.3.4 under OpenBSD 3.5. I don't see anything in the README under "Platform specific note" for OpenBSD. I tried opening...
19
by: bearophile | last post by:
This is my first post here, I hope this is the right place to talk about such things. I have few comments and notes on the Python language. I've just started to learn it; this is negative because...
22
by: bearophile | last post by:
Ville Vainio: >It's highly typical for the newbies to suggest improvements to the >language. They will usually learn that they are wrong, but the >discussion that ensues can be fruitfull anyway...
1
by: krishnakant Mane | last post by:
hello all. actually I have been recently appointed as a technology consulltent at a huge company. and I have couple more such projects in the pypeline. unfortunately the officials out here are...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
26
by: Frank Samuelson | last post by:
I love Python, and it is one of my 2 favorite languages. I would suggest that Python steal some aspects of the S language. ------------------------------------------------------- 1. Currently...
6
by: Daniel | last post by:
I hope this question is OK for this list. I've downloaded Rpyc and placed it in my site packages dir. On some machines it works fine, on others not so much. Here is one error I get when I try...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.