blob: a4749756a409dbbcf0b027e55f00450403afa6fc [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. _tut-brieftour:
2
3**********************************
4Brief Tour of the Standard Library
5**********************************
6
7
8.. _tut-os-interface:
9
10Operating System Interface
11==========================
12
13The :mod:`os` module provides dozens of functions for interacting with the
14operating system::
15
16 >>> import os
17 >>> os.system('time 0:02')
18 0
19 >>> os.getcwd() # Return the current working directory
20 'C:\\Python30'
21 >>> os.chdir('/server/accesslogs')
22
23Be sure to use the ``import os`` style instead of ``from os import *``. This
24will keep :func:`os.open` from shadowing the builtin :func:`open` function which
25operates much differently.
26
27.. index:: builtin: help
28
29The builtin :func:`dir` and :func:`help` functions are useful as interactive
30aids for working with large modules like :mod:`os`::
31
32 >>> import os
33 >>> dir(os)
34 <returns a list of all module functions>
35 >>> help(os)
36 <returns an extensive manual page created from the module's docstrings>
37
38For daily file and directory management tasks, the :mod:`shutil` module provides
39a higher level interface that is easier to use::
40
41 >>> import shutil
42 >>> shutil.copyfile('data.db', 'archive.db')
43 >>> shutil.move('/build/executables', 'installdir')
44
45
46.. _tut-file-wildcards:
47
48File Wildcards
49==============
50
51The :mod:`glob` module provides a function for making file lists from directory
52wildcard searches::
53
54 >>> import glob
55 >>> glob.glob('*.py')
56 ['primes.py', 'random.py', 'quote.py']
57
58
59.. _tut-command-line-arguments:
60
61Command Line Arguments
62======================
63
64Common utility scripts often need to process command line arguments. These
65arguments are stored in the :mod:`sys` module's *argv* attribute as a list. For
66instance the following output results from running ``python demo.py one two
67three`` at the command line::
68
69 >>> import sys
Guido van Rossum0616b792007-08-31 03:25:11 +000070 >>> print(sys.argv)
Georg Brandl116aa622007-08-15 14:28:22 +000071 ['demo.py', 'one', 'two', 'three']
72
73The :mod:`getopt` module processes *sys.argv* using the conventions of the Unix
74:func:`getopt` function. More powerful and flexible command line processing is
75provided by the :mod:`optparse` module.
76
77
78.. _tut-stderr:
79
80Error Output Redirection and Program Termination
81================================================
82
83The :mod:`sys` module also has attributes for *stdin*, *stdout*, and *stderr*.
84The latter is useful for emitting warnings and error messages to make them
85visible even when *stdout* has been redirected::
86
87 >>> sys.stderr.write('Warning, log file not found starting a new one\n')
88 Warning, log file not found starting a new one
89
90The most direct way to terminate a script is to use ``sys.exit()``.
91
92
93.. _tut-string-pattern-matching:
94
95String Pattern Matching
96=======================
97
98The :mod:`re` module provides regular expression tools for advanced string
99processing. For complex matching and manipulation, regular expressions offer
100succinct, optimized solutions::
101
102 >>> import re
103 >>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
104 ['foot', 'fell', 'fastest']
105 >>> re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')
106 'cat in the hat'
107
108When only simple capabilities are needed, string methods are preferred because
109they are easier to read and debug::
110
111 >>> 'tea for too'.replace('too', 'two')
112 'tea for two'
113
114
115.. _tut-mathematics:
116
117Mathematics
118===========
119
120The :mod:`math` module gives access to the underlying C library functions for
121floating point math::
122
123 >>> import math
124 >>> math.cos(math.pi / 4.0)
125 0.70710678118654757
126 >>> math.log(1024, 2)
127 10.0
128
129The :mod:`random` module provides tools for making random selections::
130
131 >>> import random
132 >>> random.choice(['apple', 'pear', 'banana'])
133 'apple'
134 >>> random.sample(range(100), 10) # sampling without replacement
135 [30, 83, 16, 4, 8, 81, 41, 50, 18, 33]
136 >>> random.random() # random float
137 0.17970987693706186
138 >>> random.randrange(6) # random integer chosen from range(6)
139 4
140
Guido van Rossum0616b792007-08-31 03:25:11 +0000141The SciPy project <http://scipy.org> has many other modules for numerical
142computations.
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144.. _tut-internet-access:
145
146Internet Access
147===============
148
149There are a number of modules for accessing the internet and processing internet
150protocols. Two of the simplest are :mod:`urllib2` for retrieving data from urls
151and :mod:`smtplib` for sending mail::
152
153 >>> import urllib2
154 >>> for line in urllib2.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):
155 ... if 'EST' in line or 'EDT' in line: # look for Eastern Time
Guido van Rossum0616b792007-08-31 03:25:11 +0000156 ... print(line)
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158 <BR>Nov. 25, 09:43:32 PM EST
159
160 >>> import smtplib
161 >>> server = smtplib.SMTP('localhost')
162 >>> server.sendmail('soothsayer@example.org', 'jcaesar@example.org',
Guido van Rossumf10aa982007-08-17 18:30:38 +0000163 ... """To: jcaesar@example.org
164 ... From: soothsayer@example.org
165 ...
166 ... Beware the Ides of March.
167 ... """)
Georg Brandl116aa622007-08-15 14:28:22 +0000168 >>> server.quit()
169
Guido van Rossumf10aa982007-08-17 18:30:38 +0000170(Note that the second example needs a mailserver running on localhost.)
171
Georg Brandl116aa622007-08-15 14:28:22 +0000172
173.. _tut-dates-and-times:
174
175Dates and Times
176===============
177
178The :mod:`datetime` module supplies classes for manipulating dates and times in
179both simple and complex ways. While date and time arithmetic is supported, the
180focus of the implementation is on efficient member extraction for output
181formatting and manipulation. The module also supports objects that are timezone
182aware. ::
183
184 # dates are easily constructed and formatted
185 >>> from datetime import date
186 >>> now = date.today()
187 >>> now
188 datetime.date(2003, 12, 2)
189 >>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
190 '12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'
191
192 # dates support calendar arithmetic
193 >>> birthday = date(1964, 7, 31)
194 >>> age = now - birthday
195 >>> age.days
196 14368
197
198
199.. _tut-data-compression:
200
201Data Compression
202================
203
204Common data archiving and compression formats are directly supported by modules
205including: :mod:`zlib`, :mod:`gzip`, :mod:`bz2`, :mod:`zipfile` and
206:mod:`tarfile`. ::
207
208 >>> import zlib
209 >>> s = 'witch which has which witches wrist watch'
210 >>> len(s)
211 41
212 >>> t = zlib.compress(s)
213 >>> len(t)
214 37
215 >>> zlib.decompress(t)
216 'witch which has which witches wrist watch'
217 >>> zlib.crc32(s)
218 226805979
219
220
221.. _tut-performance-measurement:
222
223Performance Measurement
224=======================
225
226Some Python users develop a deep interest in knowing the relative performance of
227different approaches to the same problem. Python provides a measurement tool
228that answers those questions immediately.
229
230For example, it may be tempting to use the tuple packing and unpacking feature
231instead of the traditional approach to swapping arguments. The :mod:`timeit`
232module quickly demonstrates a modest performance advantage::
233
234 >>> from timeit import Timer
235 >>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
236 0.57535828626024577
237 >>> Timer('a,b = b,a', 'a=1; b=2').timeit()
238 0.54962537085770791
239
240In contrast to :mod:`timeit`'s fine level of granularity, the :mod:`profile` and
241:mod:`pstats` modules provide tools for identifying time critical sections in
242larger blocks of code.
243
244
245.. _tut-quality-control:
246
247Quality Control
248===============
249
250One approach for developing high quality software is to write tests for each
251function as it is developed and to run those tests frequently during the
252development process.
253
254The :mod:`doctest` module provides a tool for scanning a module and validating
255tests embedded in a program's docstrings. Test construction is as simple as
256cutting-and-pasting a typical call along with its results into the docstring.
257This improves the documentation by providing the user with an example and it
258allows the doctest module to make sure the code remains true to the
259documentation::
260
261 def average(values):
262 """Computes the arithmetic mean of a list of numbers.
263
Guido van Rossum0616b792007-08-31 03:25:11 +0000264 >>> print(average([20, 30, 70]))
Georg Brandl116aa622007-08-15 14:28:22 +0000265 40.0
266 """
267 return sum(values, 0.0) / len(values)
268
269 import doctest
270 doctest.testmod() # automatically validate the embedded tests
271
272The :mod:`unittest` module is not as effortless as the :mod:`doctest` module,
273but it allows a more comprehensive set of tests to be maintained in a separate
274file::
275
276 import unittest
277
278 class TestStatisticalFunctions(unittest.TestCase):
279
280 def test_average(self):
281 self.assertEqual(average([20, 30, 70]), 40.0)
282 self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
283 self.assertRaises(ZeroDivisionError, average, [])
284 self.assertRaises(TypeError, average, 20, 30, 70)
285
286 unittest.main() # Calling from the command line invokes all tests
287
288
289.. _tut-batteries-included:
290
291Batteries Included
292==================
293
294Python has a "batteries included" philosophy. This is best seen through the
295sophisticated and robust capabilities of its larger packages. For example:
296
297* The :mod:`xmlrpclib` and :mod:`SimpleXMLRPCServer` modules make implementing
298 remote procedure calls into an almost trivial task. Despite the modules
299 names, no direct knowledge or handling of XML is needed.
300
301* The :mod:`email` package is a library for managing email messages, including
302 MIME and other RFC 2822-based message documents. Unlike :mod:`smtplib` and
303 :mod:`poplib` which actually send and receive messages, the email package has
304 a complete toolset for building or decoding complex message structures
305 (including attachments) and for implementing internet encoding and header
306 protocols.
307
308* The :mod:`xml.dom` and :mod:`xml.sax` packages provide robust support for
309 parsing this popular data interchange format. Likewise, the :mod:`csv` module
310 supports direct reads and writes in a common database format. Together, these
311 modules and packages greatly simplify data interchange between python
312 applications and other tools.
313
314* Internationalization is supported by a number of modules including
315 :mod:`gettext`, :mod:`locale`, and the :mod:`codecs` package.
316
317