04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

250 CHAPTER 10 ■ BATTERIES INCLUDED<br />

Listing 10-13. Some Template Definitions<br />

[name = 'Magnus Lie Hetland' ]<br />

[email = 'magnus@foo.bar' ]<br />

[language = 'python' ]<br />

Listing 10-14. A Template<br />

[import time]<br />

Dear [name],<br />

I would like to learn how to program. I hear you use<br />

the [language] language a lot -- is it something I<br />

should consider?<br />

And, by the way, is [email] your correct email address?<br />

Fooville, [time.asctime()]<br />

Oscar Frozzbozz<br />

The import time isn’t an assignment (which is the statement type I set out to handle), but because I’m not being<br />

picky and just use a simple try/except statement, my program supports any statement or expression that works<br />

with eval or exec. You can run the program like this (assuming a UNIX command line):<br />

$ python templates.py magnus.txt template.txt<br />

You should get some output similar to that in Listing 10-15.<br />

Listing 10-15. Sample Output from the Template System<br />

Dear Magnus Lie Hetland,<br />

I would like to learn how to program. I hear you use<br />

the python language a lot -- is it something I<br />

should consider?<br />

And, by the way, is magnus@foo.bar your correct email address?<br />

Fooville, Wed Apr 24 20:34:29 2004<br />

Oscar Frozzbozz

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

Saved successfully!

Ooh no, something went wrong!