Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 10 ■ BATTERIES INCLUDED 251 Even though this template system is capable of some quite powerful substitutions, it still has some flaws. For example, it would be nice if you could write the definition file in a more flexible manner. If it were executed with execfile, you could simply use normal Python syntax. That would also fix the problem of getting lots of blank lines at the top of the output. Can you think of other ways of improving it? Can you think of other uses for the concepts used in this program? The best way (in my opinion) to become really proficient in any programming language is to play with it—test its limitations and discover its strengths. See if you can rewrite this program so it works better and suits your needs. ■Note There is, in fact, a perfectly good template system available in the standard libraries, in the string module. Just take a look at the Template class, for example. Other Interesting Standard Modules Even though this chapter has covered a lot of material, I have barely scratched the surface of the standard libraries. To tempt you to dive in, I’ll quickly mention a few more cool libraries: difflib. This library enables you to compute how similar two sequences are. It also enables you to find the sequences (from a list of possibilities) that are “most similar” to an original sequence you provide. difflib could be used to create a simple searching program, for example. md5 and sha. These modules can compute small “signatures” (numbers) from strings; and if you compute the signatures for two different strings, you can be almost certain that the two signatures will be different. You can use this on large text files. These modules have several uses in cryptography and security. csv. CSV is short for comma-separated values, a simple format used by many applications (for example, many spreadsheets and database programs) to store tabular data. It is mainly used when exchanging data between different programs. The csv module lets you read and write CSV files easily, and it handles some of the more tricky parts of the format quite transparently. timeit, profile, and trace. The timeit module (with its accompanying command-line script) is a tool for measuring the time a piece of code takes to run. It has some tricks up its sleeve, and you probably ought to use it rather than the time module for performance measurements. The profile module (along with its companion module, pstats) can be used for a more comprehensive analysis of the efficiency of a piece of code. The trace module (and program) can give you a coverage analysis (that is, which parts of your code are executed and which are not). This can be useful when writing test code, for example. datetime. If the time module isn’t enough for your time-tracking needs, it’s quite possible that datetime will be. It has support for special date and time objects, and allows you to construct and combine these in various ways. The interface is in many ways a bit more intuitive than that of the time module.
252 CHAPTER 10 ■ BATTERIES INCLUDED itertools. Here you have lots of tools for creating and combining iterators (or other iterable objects). There are functions for chaining iterables, for creating iterators that return consecutive integers forever (similar to range, but without an upper limit), to cycle through an iterable repeatedly, and several other useful stuff. logging. Simply using print statements to figure out what’s going on in your program can be useful. If you want to keep track of things even without having lots of debugging output, you might write this information to a log file. This module gives you a standard set of tools for managing one or more central logs, with several levels of priority for your log messages, among other things. getopt and optparse. In UNIX, command-line programs are often run with various options or switches. (The Python interpreter is a typical example.) These will all be found in sys.argv, but handling these correctly yourself is far from easy. The getopt library is a tried and true solution to this problem, while optparse is newer, more powerful, and much easier to use. cmd. This module enables you to write a command-line interpreter, somewhat like the Python interactive interpreter. You can define your own commands that the user can execute at the prompt. Perhaps you could use this as the user interface to one of your programs? A Quick Summary In this chapter, you’ve learned about modules: how to create them, how to explore them, and how to use some of those included in the standard Python libraries. Modules. A module is basically a subprogram whose main function is to define things, such as functions, classes, and variables. If a module contains any test code, it should be placed in an if statement that checks whether __name__=='__main__'. Modules can be imported if they are in the PYTHONPATH. You import a module stored in the file foo.py with the statement import foo. Packages. A package is just a module that contains other modules. Packages are implemented as directories that contain a file named __init__.py. Exploring modules. After you have imported a module into the interactive interpreter, you can explore it in many ways. Among them are using dir, examining the __all__ variable, and using the help function. The documentation and the source code can also be excellent sources of information and insight. The standard library. Python comes with several modules included, collectively called the standard library. Some of these were reviewed in this chapter: • sys: A module that gives you access to several variables and functions that are tightly linked with the Python interpreter. • os: A module that gives you access to several variables and functions that are tightly linked with the operating system. • fileinput: A module that makes it easy to iterate over the lines of several files or streams.
- 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 280 and 281: 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
- Page 331 and 332: 300 CHAPTER 14 ■ NETWORK PROGRAMM
CHAPTER 10 ■ BATTERIES INCLUDED 251<br />
Even though this template system is capable of some quite powerful substitutions, it still has some flaws. For<br />
example, it would be nice if you could write the definition file in a more flexible manner. If it were executed with<br />
execfile, you could simply use normal <strong>Python</strong> syntax. That would also fix the problem of getting lots of blank lines<br />
at the <strong>to</strong>p of the output.<br />
Can you think of other ways of improving it? Can you think of other uses for the concepts used in this program? The<br />
best way (in my opinion) <strong>to</strong> become really proficient in any programming language is <strong>to</strong> play with it—test its limitations<br />
and discover its strengths. See if you can rewrite this program so it works better and suits your needs.<br />
■Note There is, in fact, a perfectly good template system available in the standard libraries, in the string<br />
module. Just take a look at the Template class, for example.<br />
Other Interesting Standard Modules<br />
Even though this chapter has covered a lot of material, I have barely scratched the surface of<br />
the standard libraries. To tempt you <strong>to</strong> dive in, I’ll quickly mention a few more cool libraries:<br />
difflib. This library enables you <strong>to</strong> compute how similar two sequences are. It also enables<br />
you <strong>to</strong> find the sequences (from a list of possibilities) that are “most similar” <strong>to</strong> an original<br />
sequence you provide. difflib could be used <strong>to</strong> create a simple searching program, for<br />
example.<br />
md5 and sha. These modules can compute small “signatures” (numbers) from strings;<br />
and if you compute the signatures for two different strings, you can be almost certain that<br />
the two signatures will be different. You can use this on large text files. These modules have<br />
several uses in cryp<strong>to</strong>graphy and security.<br />
csv. CSV is short for comma-separated values, a simple format used by many applications<br />
(for example, many spreadsheets and database programs) <strong>to</strong> s<strong>to</strong>re tabular data. It is mainly<br />
used when exchanging data between different programs. The csv module lets you read<br />
and write CSV files easily, and it handles some of the more tricky parts of the format quite<br />
transparently.<br />
timeit, profile, and trace. The timeit module (with its accompanying command-line<br />
script) is a <strong>to</strong>ol for measuring the time a piece of code takes <strong>to</strong> run. It has some tricks up its<br />
sleeve, and you probably ought <strong>to</strong> use it rather than the time module for performance<br />
measurements. The profile module (along with its companion module, pstats) can be<br />
used for a more comprehensive analysis of the efficiency of a piece of code. The trace<br />
module (and program) can give you a coverage analysis (that is, which parts of your code<br />
are executed and which are not). This can be useful when writing test code, for example.<br />
datetime. If the time module isn’t enough for your time-tracking needs, it’s quite possible<br />
that datetime will be. It has support for special date and time objects, and allows you <strong>to</strong><br />
construct and combine these in various ways. The interface is in many ways a bit more<br />
intuitive than that of the time module.