04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

252 CHAPTER 10 ■ BATTERIES INCLUDED<br />

itertools. Here you have lots of tools for creating and combining iterators (or other iterable<br />

objects). There are functions for chaining iterables, for creating iterators that return consecutive<br />

integers forever (similar to range, but without an upper limit), to cycle through an iterable<br />

repeatedly, and several other useful stuff.<br />

logging. Simply using print statements to figure out what’s going on in your program can<br />

be useful. If you want to keep track of things even without having lots of debugging output,<br />

you might write this information to a log file. This module gives you a standard set of tools<br />

for managing one or more central logs, with several levels of priority for your log messages,<br />

among other things.<br />

getopt and optparse. In UNIX, command-line programs are often run with various options<br />

or switches. (The Python interpreter is a typical example.) These will all be found in sys.argv,<br />

but handling these correctly yourself is far from easy. The getopt library is a tried and true<br />

solution to this problem, while optparse is newer, more powerful, and much easier to use.<br />

cmd. This module enables you to write a command-line interpreter, somewhat like the<br />

Python interactive interpreter. You can define your own commands that the user can<br />

execute at the prompt. Perhaps you could use this as the user interface to one of your<br />

programs?<br />

A Quick Summary<br />

In this chapter, you’ve learned about modules: how to create them, how to explore them, and<br />

how to use some of those included in the standard Python libraries.<br />

Modules. A module is basically a subprogram whose main function is to define things,<br />

such as functions, classes, and variables. If a module contains any test code, it should be<br />

placed in an if statement that checks whether __name__=='__main__'. Modules can be<br />

imported if they are in the PYTHONPATH. You import a module stored in the file foo.py with<br />

the statement import foo.<br />

Packages. A package is just a module that contains other modules. Packages are implemented<br />

as directories that contain a file named __init__.py.<br />

Exploring modules. After you have imported a module into the interactive interpreter, you<br />

can explore it in many ways. Among them are using dir, examining the __all__ variable,<br />

and using the help function. The documentation and the source code can also be excellent<br />

sources of information and insight.<br />

The standard library. Python comes with several modules included, collectively called the<br />

standard library. Some of these were reviewed in this chapter:<br />

• sys: A module that gives you access to several variables and functions that are tightly<br />

linked with the Python interpreter.<br />

• os: A module that gives you access to several variables and functions that are tightly<br />

linked with the operating system.<br />

• fileinput: A module that makes it easy to iterate over the lines of several files or streams.

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

Saved successfully!

Ooh no, something went wrong!