Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 10 ■ BATTERIES INCLUDED 249 ■Note It is much more efficient to put the lines into a list and then join them at the end than to do something like this: # Don't do this: text = '' for line in fileinput.input(): text += line Although this looks elegant, each assignment has to create a new string, which is the old string with the new one appended. This leads to a terrible waste of resources and makes your program slow. Don’t do this. If you want a more elegant way to read in all the text of a file, take a peek at Chapter 11. So, I have just created a really powerful template system in only 15 lines of code (not counting whitespace and comments). I hope you’re starting to see how powerful Python becomes when you use the standard libraries. Let’s finish this example by testing the template system. Try running it on the simple file shown in Listing 10-12. Listing 10-12. A Simple Template Example [x = 2] [y = 3] The sum of [x] and [y] is [x + y]. You should see this: The sum of 2 and 3 is 5. ■Note It may not be obvious, but there are three empty lines in the preceding output—two above and one below the text. Although the first two fields have been replaced by empty strings, the newlines following them are still there. Also, the print statement adds a newline, which accounts for the empty line at the end. But wait, it gets better! Because I have used fileinput, I can process several files in turn. That means that I can use one file to define values for some variables, and then another file as a template where these values are inserted. For example, I might have one file with definitions as in Listing 10-13, named magnus.txt, and a template file as in Listing 10-14, named template.txt.
250 CHAPTER 10 ■ BATTERIES INCLUDED Listing 10-13. Some Template Definitions [name = 'Magnus Lie Hetland' ] [email = 'magnus@foo.bar' ] [language = 'python' ] Listing 10-14. A Template [import time] Dear [name], I would like to learn how to program. I hear you use the [language] language a lot -- is it something I should consider? And, by the way, is [email] your correct email address? Fooville, [time.asctime()] Oscar Frozzbozz The import time isn’t an assignment (which is the statement type I set out to handle), but because I’m not being picky and just use a simple try/except statement, my program supports any statement or expression that works with eval or exec. You can run the program like this (assuming a UNIX command line): $ python templates.py magnus.txt template.txt You should get some output similar to that in Listing 10-15. Listing 10-15. Sample Output from the Template System Dear Magnus Lie Hetland, I would like to learn how to program. I hear you use the python language a lot -- is it something I should consider? And, by the way, is magnus@foo.bar your correct email address? Fooville, Wed Apr 24 20:34:29 2004 Oscar Frozzbozz
- Page 230 and 231: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 232 and 233: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 234 and 235: CHAPTER 10 ■ ■ ■ Batteries In
- Page 236 and 237: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 238 and 239: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 240 and 241: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 242 and 243: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 244 and 245: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 246 and 247: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 248 and 249: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 250 and 251: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 252 and 253: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 254 and 255: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 256 and 257: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 258 and 259: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 260 and 261: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 262 and 263: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 264 and 265: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 266 and 267: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 268 and 269: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 270 and 271: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 272 and 273: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 274 and 275: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 276 and 277: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 278 and 279: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 282 and 283: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 284: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 287 and 288: 256 CHAPTER 11 ■ FILES AND STUFF
- Page 289 and 290: 258 CHAPTER 11 ■ FILES AND STUFF
- Page 291 and 292: 260 CHAPTER 11 ■ FILES AND STUFF
- Page 293 and 294: 262 CHAPTER 11 ■ FILES AND STUFF
- Page 295 and 296: 264 CHAPTER 11 ■ FILES AND STUFF
- Page 297 and 298: 266 CHAPTER 11 ■ FILES AND STUFF
- Page 299 and 300: 268 CHAPTER 11 ■ FILES AND STUFF
- Page 301 and 302: 270 CHAPTER 12 ■ GRAPHICAL USER I
- Page 303 and 304: 272 CHAPTER 12 ■ GRAPHICAL USER I
- Page 305 and 306: 274 CHAPTER 12 ■ GRAPHICAL USER I
- Page 307 and 308: 276 CHAPTER 12 ■ GRAPHICAL USER I
- Page 309 and 310: 278 CHAPTER 12 ■ GRAPHICAL USER I
- Page 311 and 312: 280 CHAPTER 12 ■ GRAPHICAL USER I
- Page 313 and 314: 282 CHAPTER 12 ■ GRAPHICAL USER I
- Page 316 and 317: CHAPTER 13 ■ ■ ■ Database Sup
- Page 318 and 319: CHAPTER 13 ■ DATABASE SUPPORT 287
- Page 320 and 321: CHAPTER 13 ■ DATABASE SUPPORT 289
- Page 322 and 323: CHAPTER 13 ■ DATABASE SUPPORT 291
- Page 324 and 325: CHAPTER 13 ■ DATABASE SUPPORT 293
- Page 326: CHAPTER 13 ■ DATABASE SUPPORT 295
- Page 329 and 330: 298 CHAPTER 14 ■ NETWORK PROGRAMM
CHAPTER 10 ■ BATTERIES INCLUDED 249<br />
■Note It is much more efficient <strong>to</strong> put the lines in<strong>to</strong> a list and then join them at the end than <strong>to</strong> do something<br />
like this:<br />
# Don't do this:<br />
text = ''<br />
for line in fileinput.input():<br />
text += line<br />
Although this looks elegant, each assignment has <strong>to</strong> create a new string, which is the old string with the new<br />
one appended. This leads <strong>to</strong> a terrible waste of resources and makes your program slow. Don’t do this. If you<br />
want a more elegant way <strong>to</strong> read in all the text of a file, take a peek at Chapter 11.<br />
So, I have just created a really powerful template system in only 15 lines of code (not counting whitespace and comments).<br />
I hope you’re starting <strong>to</strong> see how powerful <strong>Python</strong> becomes when you use the standard libraries. Let’s finish this<br />
example by testing the template system. Try running it on the simple file shown in Listing 10-12.<br />
Listing 10-12. A Simple Template Example<br />
[x = 2]<br />
[y = 3]<br />
The sum of [x] and [y] is [x + y].<br />
You should see this:<br />
The sum of 2 and 3 is 5.<br />
■Note It may not be obvious, but there are three empty lines in the preceding output—two above and one<br />
below the text. Although the first two fields have been replaced by empty strings, the newlines following them<br />
are still there. Also, the print statement adds a newline, which accounts for the empty line at the end.<br />
But wait, it gets better! Because I have used fileinput, I can process several files in turn. That means that I can<br />
use one file <strong>to</strong> define values for some variables, and then another file as a template where these values are inserted.<br />
For example, I might have one file with definitions as in Listing 10-13, named magnus.txt, and a template file as<br />
in Listing 10-14, named template.txt.