Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional

16.01.2014 Views

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

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.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!