Python: I Now Understand What's Going On With My String Formatting Question

  • From: "Homme, James" <james.homme@xxxxxxxxxxxx>
  • To: "programmingblind@xxxxxxxxxxxxx" <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 3 Aug 2011 15:12:34 -0400

Hi,
Now that I've read a few more things, I have put this together in my head. I'll 
try to explain. Here goes.

The string formatting operation I cited looks differently than I was reading 
about before, because I was looking at dictionary formatting. Before, I was 
looking at formatting just strings. When you use the % sign followed by 
something in parentheses, followed by the letter s, you are saying "Go get me 
the value of this dictionary key." When you think about what ConfigParcer does, 
this makes perfect sense.

ConfigParser is a built in module that works with ini style files. You can 
think of a section of one of those files as a dictionary. Dictionaries have 
key/value pairs. That means you can think of the section name as the name of 
the dictionary name and each line of the section as a key value pair. The key 
points to the value.
Here's the part of my file before the string formatting:

[bug_tracker]
protocol = http
server = localhost
port = 8080

Here's the format string from that same section:

url = %(protocol)s://%(server)s:%(port)s/bugs/

The words in parentheses: server, protocol and port match up with the values 
right above that line.

The other stuff in that string is literally what it looks like. You don't need 
quotes around the whole thing, because it's coming from the ini style file.

If I write Python code that gets those values, the output will be the values 
the keys in the parentheses point to. It will substitute http for protocol, 
localhost for server, and 8080 for port.

# Here's the line of code that gets what we need into the program that reads 
the ini file:
from ConfigParser import SafeConfigParser
# Make an instance of the ConfigParcer class.
parser = SafeConfigParser()

# Now we read our little file into the program all at once.

parser.read('interpolation.ini') # assumes that this file exists.

# Now, tell the program to get a key/value pair from our little section.
print 'Original value : ', parser.get('bug_tracker', 'url')

# Remember, url was that formatted thing that did the magic.

Configuration files can also have a special section called default. I'm not 
quite sure exactly how that works yet. I have to play with it a little more. 
And when you create an instance of ConfigParser, you can hard code a dictionary 
of defaults to use.

This next example reveals how it really works if you were to do it all yourself.

Here's a little program that makes a dictionary of three key/value pairs and 
uses the format specifier to print each of the values from the dictionary. Note 
that to the right of each of the print lines you see the exact same word, which 
is the name of the dictionary.

# fmt.py
# Create a dictionary.
date= {"month" : "August",
                "day" : 3,
                "year" : 2011 }

# Print out each of the dictionary values.
print "%(month)s" % date
print "%(day)s" % date
print "%(year)s" % date

Here is what it looks like when I get a command prompt and run this program.

Z:\temp>python fmt.py
August
3
2011
Z:\temp>

Hope this helps someone else.

Jim

Jim Homme,
Usability Services,
Phone: 412-544-1810.


________________________________
This e-mail and any attachments to it are confidential and are intended solely 
for use of the individual or entity to whom they are addressed. If you have 
received this e-mail in error, please notify the sender immediately and then 
delete it. If you are not the intended recipient, you must not keep, use, 
disclose, copy or distribute this e-mail without the author's prior permission. 
The views expressed in this e-mail message do not necessarily represent the 
views of Highmark Inc., its subsidiaries, or affiliates.

Other related posts:

  • » Python: I Now Understand What's Going On With My String Formatting Question - Homme, James