16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

CHAPTER 10 ■ BATTERIES INCLUDED 215<br />

The Standard Library: A Few Favorites<br />

Chances are that you’re beginning <strong>to</strong> wonder what the title of this chapter means. The phrase<br />

was originally coined by Frank Stajano and refers <strong>to</strong> <strong>Python</strong>’s copious standard library. When<br />

you install <strong>Python</strong>, you get lots of useful modules (the batteries) for “free.” Because there are so<br />

many ways of getting more information about these modules (as explained in the first part of<br />

this chapter), I won’t include a full reference here (which would take up far <strong>to</strong>o much space<br />

anyway), but I’ll describe a few of my favorite standard modules <strong>to</strong> whet your appetite for<br />

exploration. You’ll encounter more standard modules in the project chapters (Chapter 20 and<br />

later). The module descriptions are not complete but highlight some of the interesting features<br />

of each module.<br />

sys<br />

This module gives you access <strong>to</strong> variables and functions that are closely linked <strong>to</strong> the <strong>Python</strong><br />

interpreter. Some of these are shown in Table 10-2.<br />

Table 10-2. Some Important Functions and Variables in the sys Module<br />

Function/Variable<br />

argv<br />

exit([arg])<br />

modules<br />

path<br />

platform<br />

stdin<br />

stdout<br />

stderr<br />

Description<br />

The command-line arguments, including the script name<br />

Exits the current program, optionally with a given return value or<br />

error message<br />

A dictionary mapping module names <strong>to</strong> loaded modules<br />

A list of direc<strong>to</strong>ry names where modules can be found<br />

Contains a platform identifier such as sunos5 or win32<br />

Standard input stream—a file-like object<br />

Standard output stream—a file-like object<br />

Standard error stream—a file-like object<br />

The variable sys.argv contains the arguments passed <strong>to</strong> the <strong>Python</strong> interpreter, including<br />

the script name.<br />

The function sys.exit exits the current program. (If called within a try/finally block, the<br />

finally clause is executed.) You can supply an integer <strong>to</strong> indicate whether the program succeeded<br />

or not—a UNIX convention. You’ll probably be fine in most cases if you rely on the default<br />

(which is zero, indicating success). Alternatively, you can supply a string, which is used as an<br />

error message and can be very useful for a user trying <strong>to</strong> figure out why the program halted;<br />

then, the program exits with that error message and a code indicating failure.<br />

The mapping sys.modules maps module names <strong>to</strong> actual modules. It only applies <strong>to</strong> currently<br />

imported modules.<br />

The module variable sys.path was discussed earlier in this chapter. It’s a list of strings, in<br />

which each string is the name of a direc<strong>to</strong>ry where the interpreter will look for modules when<br />

an import statement is executed.

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

Saved successfully!

Ooh no, something went wrong!