Debian Codegraph @

Doc/tutorial/stdlib2.rst

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