473,789 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1287
On Dec 7, 2:31 pm, waltbrad <waltb...@hotma il.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...@hotma il.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
3904
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 "base" syntax. Seems like we could put the @ symbol to good use in these situations. Examples: print @(separator = None) x, y, z @x,y:x*x+y*y -- anonymous function
14
1709
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 concatenation operator like in mathematics: ° such that: a(b(c(d))) <=> a°b°c(d)
3
1780
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 the Python tar file using gtar and I tried using gmake just to make sure there wasn't anything weird going on there (I know that using BSD tar when building Zope used to cause problems, so I first thought I might be having a similar problem. I'm...
19
1682
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 I'm ignorant still, but it's also positive because I can still see details that later probably I'll start to ignore. Some of the functions of IPython seem good and simple, and I think some of them can be integrated into the main Python line. ...
22
2334
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 :-). Few more notes on the language. I don't know if I can really suggest improvements to the language... but I hope to learn something :-) I think some things are better in Delphi/Pascal (like the := for assignments instead of = and = for...
1
1261
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 too much in favour of java and I have personally worked with both and find that python is heaven in syntax and makes it very easy to do complex things. on speed? I think experts on this list can give better answer, but efficiency and maintainance...
206
8376
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 footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
26
1862
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 in Python def foo(x,y): ... assigns the name foo to a function object. Is this pythonic? Why not use the = operator like most other assignments?
6
2564
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 to import it: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python25\lib\site-packages\Rpyc\__init__.py", line 7, in <module>
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.