blob: 4be327517205e2bf5bb65d21986fe1a9b237b7bc [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. _tut-brieftourtwo:
2
3*********************************************
4Brief Tour of the Standard Library -- Part II
5*********************************************
6
7This second tour covers more advanced modules that support professional
8programming needs. These modules rarely occur in small scripts.
9
10
11.. _tut-output-formatting:
12
13Output Formatting
14=================
15
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +000016The :mod:`reprlib` module provides a version of :func:`repr` customized for
Georg Brandl116aa622007-08-15 14:28:22 +000017abbreviated displays of large or deeply nested containers::
18
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +000019 >>> import reprlib
20 >>> reprlib.repr(set('supercalifragilisticexpialidocious'))
Georg Brandl116aa622007-08-15 14:28:22 +000021 "set(['a', 'c', 'd', 'e', 'f', 'g', ...])"
22
23The :mod:`pprint` module offers more sophisticated control over printing both
24built-in and user defined objects in a way that is readable by the interpreter.
25When the result is longer than one line, the "pretty printer" adds line breaks
26and indentation to more clearly reveal data structure::
27
28 >>> import pprint
29 >>> t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta',
30 ... 'yellow'], 'blue']]]
31 ...
32 >>> pprint.pprint(t, width=30)
33 [[[['black', 'cyan'],
34 'white',
35 ['green', 'red']],
36 [['magenta', 'yellow'],
37 'blue']]]
38
39The :mod:`textwrap` module formats paragraphs of text to fit a given screen
40width::
41
42 >>> import textwrap
43 >>> doc = """The wrap() method is just like fill() except that it returns
44 ... a list of strings instead of one big string with newlines to separate
45 ... the wrapped lines."""
46 ...
Georg Brandl6911e3c2007-09-04 07:15:32 +000047 >>> print(textwrap.fill(doc, width=40))
Georg Brandl116aa622007-08-15 14:28:22 +000048 The wrap() method is just like fill()
49 except that it returns a list of strings
50 instead of one big string with newlines
51 to separate the wrapped lines.
52
53The :mod:`locale` module accesses a database of culture specific data formats.
54The grouping attribute of locale's format function provides a direct way of
55formatting numbers with group separators::
56
57 >>> import locale
58 >>> locale.setlocale(locale.LC_ALL, 'English_United States.1252')
59 'English_United States.1252'
60 >>> conv = locale.localeconv() # get a mapping of conventions
61 >>> x = 1234567.8
62 >>> locale.format("%d", x, grouping=True)
63 '1,234,567'
64 >>> locale.format("%s%.*f", (conv['currency_symbol'],
Georg Brandla1c6a1c2009-01-03 21:26:05 +000065 ... conv['frac_digits'], x), grouping=True)
Georg Brandl116aa622007-08-15 14:28:22 +000066 '$1,234,567.80'
67
68
69.. _tut-templating:
70
71Templating
72==========
73
74The :mod:`string` module includes a versatile :class:`Template` class with a
75simplified syntax suitable for editing by end-users. This allows users to
76customize their applications without having to alter the application.
77
78The format uses placeholder names formed by ``$`` with valid Python identifiers
79(alphanumeric characters and underscores). Surrounding the placeholder with
80braces allows it to be followed by more alphanumeric letters with no intervening
81spaces. Writing ``$$`` creates a single escaped ``$``::
82
83 >>> from string import Template
84 >>> t = Template('${village}folk send $$10 to $cause.')
85 >>> t.substitute(village='Nottingham', cause='the ditch fund')
86 'Nottinghamfolk send $10 to the ditch fund.'
87
88The :meth:`substitute` method raises a :exc:`KeyError` when a placeholder is not
89supplied in a dictionary or a keyword argument. For mail-merge style
90applications, user supplied data may be incomplete and the
91:meth:`safe_substitute` method may be more appropriate --- it will leave
92placeholders unchanged if data is missing::
93
94 >>> t = Template('Return the $item to $owner.')
95 >>> d = dict(item='unladen swallow')
96 >>> t.substitute(d)
97 Traceback (most recent call last):
98 . . .
99 KeyError: 'owner'
100 >>> t.safe_substitute(d)
101 'Return the unladen swallow to $owner.'
102
103Template subclasses can specify a custom delimiter. For example, a batch
104renaming utility for a photo browser may elect to use percent signs for
105placeholders such as the current date, image sequence number, or file format::
106
Georg Brandl8d5c3922007-12-02 22:48:17 +0000107 >>> import time, os.path
Georg Brandl116aa622007-08-15 14:28:22 +0000108 >>> photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']
109 >>> class BatchRename(Template):
110 ... delimiter = '%'
Georg Brandl8d5c3922007-12-02 22:48:17 +0000111 >>> fmt = input('Enter rename style (%d-date %n-seqnum %f-format): ')
Georg Brandl116aa622007-08-15 14:28:22 +0000112 Enter rename style (%d-date %n-seqnum %f-format): Ashley_%n%f
113
114 >>> t = BatchRename(fmt)
115 >>> date = time.strftime('%d%b%y')
116 >>> for i, filename in enumerate(photofiles):
117 ... base, ext = os.path.splitext(filename)
118 ... newname = t.substitute(d=date, n=i, f=ext)
Benjamin Petersone6f00632008-05-26 01:03:56 +0000119 ... print('{0} --> {1}'.format(filename, newname))
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121 img_1074.jpg --> Ashley_0.jpg
122 img_1076.jpg --> Ashley_1.jpg
123 img_1077.jpg --> Ashley_2.jpg
124
125Another application for templating is separating program logic from the details
126of multiple output formats. This makes it possible to substitute custom
127templates for XML files, plain text reports, and HTML web reports.
128
129
130.. _tut-binary-formats:
131
132Working with Binary Data Record Layouts
133=======================================
134
135The :mod:`struct` module provides :func:`pack` and :func:`unpack` functions for
136working with variable length binary record formats. The following example shows
Christian Heimese7a15bb2008-01-24 16:21:45 +0000137how to loop through header information in a ZIP file without using the
138:mod:`zipfile` module. Pack codes ``"H"`` and ``"I"`` represent two and four
139byte unsigned numbers respectively. The ``"<"`` indicates that they are
140standard size and in little-endian byte order::
Georg Brandl116aa622007-08-15 14:28:22 +0000141
142 import struct
143
144 data = open('myfile.zip', 'rb').read()
145 start = 0
146 for i in range(3): # show the first 3 file headers
147 start += 14
Christian Heimese7a15bb2008-01-24 16:21:45 +0000148 fields = struct.unpack('<IIIHH', data[start:start+16])
Georg Brandl116aa622007-08-15 14:28:22 +0000149 crc32, comp_size, uncomp_size, filenamesize, extra_size = fields
150
151 start += 16
152 filename = data[start:start+filenamesize]
153 start += filenamesize
154 extra = data[start:start+extra_size]
Georg Brandl6911e3c2007-09-04 07:15:32 +0000155 print(filename, hex(crc32), comp_size, uncomp_size)
Georg Brandl116aa622007-08-15 14:28:22 +0000156
157 start += extra_size + comp_size # skip to the next header
158
159
160.. _tut-multi-threading:
161
162Multi-threading
163===============
164
165Threading is a technique for decoupling tasks which are not sequentially
166dependent. Threads can be used to improve the responsiveness of applications
167that accept user input while other tasks run in the background. A related use
168case is running I/O in parallel with computations in another thread.
169
170The following code shows how the high level :mod:`threading` module can run
171tasks in background while the main program continues to run::
172
173 import threading, zipfile
174
175 class AsyncZip(threading.Thread):
176 def __init__(self, infile, outfile):
Georg Brandl48310cd2009-01-03 21:18:54 +0000177 threading.Thread.__init__(self)
Georg Brandl116aa622007-08-15 14:28:22 +0000178 self.infile = infile
179 self.outfile = outfile
180 def run(self):
181 f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
182 f.write(self.infile)
183 f.close()
Georg Brandle4ac7502007-09-03 07:10:24 +0000184 print('Finished background zip of:', self.infile)
Georg Brandl116aa622007-08-15 14:28:22 +0000185
186 background = AsyncZip('mydata.txt', 'myarchive.zip')
187 background.start()
Guido van Rossum0616b792007-08-31 03:25:11 +0000188 print('The main program continues to run in foreground.')
Georg Brandl116aa622007-08-15 14:28:22 +0000189
190 background.join() # Wait for the background task to finish
Guido van Rossum0616b792007-08-31 03:25:11 +0000191 print('Main program waited until background was done.')
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193The principal challenge of multi-threaded applications is coordinating threads
194that share data or other resources. To that end, the threading module provides
195a number of synchronization primitives including locks, events, condition
196variables, and semaphores.
197
198While those tools are powerful, minor design errors can result in problems that
199are difficult to reproduce. So, the preferred approach to task coordination is
200to concentrate all access to a resource in a single thread and then use the
Alexandre Vassalottif260e442008-05-11 19:59:59 +0000201:mod:`queue` module to feed that thread with requests from other threads.
Georg Brandl116aa622007-08-15 14:28:22 +0000202Applications using :class:`Queue` objects for inter-thread communication and
203coordination are easier to design, more readable, and more reliable.
204
205
206.. _tut-logging:
207
208Logging
209=======
210
211The :mod:`logging` module offers a full featured and flexible logging system.
212At its simplest, log messages are sent to a file or to ``sys.stderr``::
213
214 import logging
215 logging.debug('Debugging information')
216 logging.info('Informational message')
217 logging.warning('Warning:config file %s not found', 'server.conf')
218 logging.error('Error occurred')
219 logging.critical('Critical error -- shutting down')
220
221This produces the following output::
222
223 WARNING:root:Warning:config file server.conf not found
224 ERROR:root:Error occurred
225 CRITICAL:root:Critical error -- shutting down
226
227By default, informational and debugging messages are suppressed and the output
228is sent to standard error. Other output options include routing messages
229through email, datagrams, sockets, or to an HTTP Server. New filters can select
230different routing based on message priority: :const:`DEBUG`, :const:`INFO`,
231:const:`WARNING`, :const:`ERROR`, and :const:`CRITICAL`.
232
233The logging system can be configured directly from Python or can be loaded from
234a user editable configuration file for customized logging without altering the
235application.
236
237
238.. _tut-weak-references:
239
240Weak References
241===============
242
243Python does automatic memory management (reference counting for most objects and
Christian Heimesd8654cf2007-12-02 15:22:16 +0000244:term:`garbage collection` to eliminate cycles). The memory is freed shortly
245after the last reference to it has been eliminated.
Georg Brandl116aa622007-08-15 14:28:22 +0000246
247This approach works fine for most applications but occasionally there is a need
248to track objects only as long as they are being used by something else.
249Unfortunately, just tracking them creates a reference that makes them permanent.
250The :mod:`weakref` module provides tools for tracking objects without creating a
251reference. When the object is no longer needed, it is automatically removed
252from a weakref table and a callback is triggered for weakref objects. Typical
253applications include caching objects that are expensive to create::
254
255 >>> import weakref, gc
256 >>> class A:
257 ... def __init__(self, value):
258 ... self.value = value
259 ... def __repr__(self):
260 ... return str(self.value)
261 ...
262 >>> a = A(10) # create a reference
263 >>> d = weakref.WeakValueDictionary()
264 >>> d['primary'] = a # does not create a reference
265 >>> d['primary'] # fetch the object if it is still alive
266 10
267 >>> del a # remove the one reference
268 >>> gc.collect() # run garbage collection right away
269 0
270 >>> d['primary'] # entry was automatically removed
271 Traceback (most recent call last):
Christian Heimesc3f30c42008-02-22 16:37:40 +0000272 File "<stdin>", line 1, in <module>
Georg Brandl116aa622007-08-15 14:28:22 +0000273 d['primary'] # entry was automatically removed
Barry Warsaw97f005d2008-12-03 16:46:14 +0000274 File "C:/python31/lib/weakref.py", line 46, in __getitem__
Georg Brandl116aa622007-08-15 14:28:22 +0000275 o = self.data[key]()
276 KeyError: 'primary'
277
278
279.. _tut-list-tools:
280
281Tools for Working with Lists
282============================
283
284Many data structure needs can be met with the built-in list type. However,
285sometimes there is a need for alternative implementations with different
286performance trade-offs.
287
288The :mod:`array` module provides an :class:`array()` object that is like a list
Georg Brandl2ee470f2008-07-16 12:55:28 +0000289that stores only homogeneous data and stores it more compactly. The following
Georg Brandl116aa622007-08-15 14:28:22 +0000290example shows an array of numbers stored as two byte unsigned binary numbers
291(typecode ``"H"``) rather than the usual 16 bytes per entry for regular lists of
292python int objects::
293
294 >>> from array import array
295 >>> a = array('H', [4000, 10, 700, 22222])
296 >>> sum(a)
297 26932
298 >>> a[1:3]
299 array('H', [10, 700])
300
301The :mod:`collections` module provides a :class:`deque()` object that is like a
302list with faster appends and pops from the left side but slower lookups in the
303middle. These objects are well suited for implementing queues and breadth first
304tree searches::
305
306 >>> from collections import deque
307 >>> d = deque(["task1", "task2", "task3"])
308 >>> d.append("task4")
Guido van Rossum0616b792007-08-31 03:25:11 +0000309 >>> print("Handling", d.popleft())
Georg Brandl116aa622007-08-15 14:28:22 +0000310 Handling task1
311
312 unsearched = deque([starting_node])
313 def breadth_first_search(unsearched):
314 node = unsearched.popleft()
315 for m in gen_moves(node):
316 if is_goal(m):
317 return m
318 unsearched.append(m)
319
320In addition to alternative list implementations, the library also offers other
321tools such as the :mod:`bisect` module with functions for manipulating sorted
322lists::
323
324 >>> import bisect
325 >>> scores = [(100, 'perl'), (200, 'tcl'), (400, 'lua'), (500, 'python')]
326 >>> bisect.insort(scores, (300, 'ruby'))
327 >>> scores
328 [(100, 'perl'), (200, 'tcl'), (300, 'ruby'), (400, 'lua'), (500, 'python')]
329
330The :mod:`heapq` module provides functions for implementing heaps based on
331regular lists. The lowest valued entry is always kept at position zero. This
332is useful for applications which repeatedly access the smallest element but do
333not want to run a full list sort::
334
335 >>> from heapq import heapify, heappop, heappush
336 >>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
337 >>> heapify(data) # rearrange the list into heap order
338 >>> heappush(data, -5) # add a new entry
339 >>> [heappop(data) for i in range(3)] # fetch the three smallest entries
340 [-5, 0, 1]
341
342
343.. _tut-decimal-fp:
344
345Decimal Floating Point Arithmetic
346=================================
347
348The :mod:`decimal` module offers a :class:`Decimal` datatype for decimal
349floating point arithmetic. Compared to the built-in :class:`float`
350implementation of binary floating point, the new class is especially helpful for
351financial applications and other uses which require exact decimal
352representation, control over precision, control over rounding to meet legal or
353regulatory requirements, tracking of significant decimal places, or for
354applications where the user expects the results to match calculations done by
355hand.
356
357For example, calculating a 5% tax on a 70 cent phone charge gives different
358results in decimal floating point and binary floating point. The difference
359becomes significant if the results are rounded to the nearest cent::
360
Georg Brandl48310cd2009-01-03 21:18:54 +0000361 >>> from decimal import *
Georg Brandl116aa622007-08-15 14:28:22 +0000362 >>> Decimal('0.70') * Decimal('1.05')
363 Decimal("0.7350")
364 >>> .70 * 1.05
Georg Brandl48310cd2009-01-03 21:18:54 +0000365 0.73499999999999999
Georg Brandl116aa622007-08-15 14:28:22 +0000366
367The :class:`Decimal` result keeps a trailing zero, automatically inferring four
368place significance from multiplicands with two place significance. Decimal
369reproduces mathematics as done by hand and avoids issues that can arise when
370binary floating point cannot exactly represent decimal quantities.
371
372Exact representation enables the :class:`Decimal` class to perform modulo
373calculations and equality tests that are unsuitable for binary floating point::
374
375 >>> Decimal('1.00') % Decimal('.10')
376 Decimal("0.00")
377 >>> 1.00 % 0.10
378 0.09999999999999995
379
380 >>> sum([Decimal('0.1')]*10) == Decimal('1.0')
381 True
382 >>> sum([0.1]*10) == 1.0
Georg Brandl48310cd2009-01-03 21:18:54 +0000383 False
Georg Brandl116aa622007-08-15 14:28:22 +0000384
385The :mod:`decimal` module provides arithmetic with as much precision as needed::
386
387 >>> getcontext().prec = 36
388 >>> Decimal(1) / Decimal(7)
389 Decimal("0.142857142857142857142857142857142857")
390
391