Changeset 391 for python/trunk/Doc/tutorial/stdlib2.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/tutorial/stdlib2.rst
r2 r391 72 72 ========== 73 73 74 The :mod:`string` module includes a versatile :class:` Template` class with a75 simplified syntax suitable for editing by end-users. This allows users to 76 customize their applications without having to alter the application.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 77 78 78 The format uses placeholder names formed by ``$`` with valid Python identifiers … … 86 86 'Nottinghamfolk send $10 to the ditch fund.' 87 87 88 The :meth:` substitute` method raises a :exc:`KeyError` when a placeholder is not89 supplied in a dictionary or a keyword argument. For mail-merge style 90 applications, user supplied data may be incomplete and the91 :meth:` safe_substitute` method may be more appropriate --- it will leave92 placeholders unchanged if data is missing::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 93 94 94 >>> t = Template('Return the $item to $owner.') … … 96 96 >>> t.substitute(d) 97 97 Traceback (most recent call last): 98 . ..98 ... 99 99 KeyError: 'owner' 100 100 >>> t.safe_substitute(d) … … 133 133 ======================================= 134 134 135 The :mod:`struct` module provides :func:`pack` and :func:`unpack` functions for 136 working with variable length binary record formats. The following example shows 135 The :mod:`struct` module provides :func:`~struct.pack` and 136 :func:`~struct.unpack` functions for working with variable length binary 137 record formats. The following example shows 137 138 how to loop through header information in a ZIP file without using the 138 139 :mod:`zipfile` module. Pack codes ``"H"`` and ``"I"`` represent two and four … … 219 220 logging.critical('Critical error -- shutting down') 220 221 221 This produces the following output:: 222 This produces the following output: 223 224 .. code-block:: none 222 225 223 226 WARNING:root:Warning:config file server.conf not found … … 228 231 is sent to standard error. Other output options include routing messages 229 232 through email, datagrams, sockets, or to an HTTP Server. New filters can select 230 different routing based on message priority: :const:`DEBUG`, :const:`INFO`, 231 :const:`WARNING`, :const:`ERROR`, and :const:`CRITICAL`. 233 different routing based on message priority: :const:`~logging.DEBUG`, 234 :const:`~logging.INFO`, :const:`~logging.WARNING`, :const:`~logging.ERROR`, 235 and :const:`~logging.CRITICAL`. 232 236 233 237 The logging system can be configured directly from Python or can be loaded from … … 256 260 >>> class A: 257 261 ... def __init__(self, value): 258 ... 262 ... self.value = value 259 263 ... def __repr__(self): 260 ... 264 ... return str(self.value) 261 265 ... 262 266 >>> a = A(10) # create a reference … … 286 290 performance trade-offs. 287 291 288 The :mod:`array` module provides an :class:` array()` object that is like a list289 that stores only homogeneous data and stores it more compactly. The following 290 example 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 292 Python int objects::292 The :mod:`array` module provides an :class:`~array.array()` object that is like 293 a list that stores only homogeneous data and stores it more compactly. The 294 following example shows an array of numbers stored as two byte unsigned binary 295 numbers (typecode ``"H"``) rather than the usual 16 bytes per entry for regular 296 lists of Python int objects:: 293 297 294 298 >>> from array import array … … 299 303 array('H', [10, 700]) 300 304 301 The :mod:`collections` module provides a :class:` deque()` object that is like a302 list with faster appends and pops from the left side but slower lookups in the 303 middle. These objects are well suited for implementing queues and breadth first 304 tree searches::305 The :mod:`collections` module provides a :class:`~collections.deque()` object 306 that is like a list with faster appends and pops from the left side but slower 307 lookups in the middle. These objects are well suited for implementing queues 308 and breadth first tree searches:: 305 309 306 310 >>> from collections import deque … … 309 313 >>> print "Handling", d.popleft() 310 314 Handling task1 315 316 :: 311 317 312 318 unsearched = deque([starting_node]) … … 346 352 ================================= 347 353 348 The :mod:`decimal` module offers a :class:` Decimal` datatype for decimal349 floating point arithmetic. Compared to the built-in :class:`float`354 The :mod:`decimal` module offers a :class:`~decimal.Decimal` datatype for 355 decimal floating point arithmetic. Compared to the built-in :class:`float` 350 356 implementation of binary floating point, the class is especially helpful for 351 357 … … 363 369 364 370 >>> from decimal import * 365 >>> Decimal('0.70') * Decimal('1.05') 371 >>> x = Decimal('0.70') * Decimal('1.05') 372 >>> x 366 373 Decimal('0.7350') 367 >>> .70 * 1.05 368 0.73499999999999999 369 370 The :class:`Decimal` result keeps a trailing zero, automatically inferring four 371 place significance from multiplicands with two place significance. Decimal 372 reproduces mathematics as done by hand and avoids issues that can arise when 373 binary floating point cannot exactly represent decimal quantities. 374 375 Exact representation enables the :class:`Decimal` class to perform modulo 376 calculations and equality tests that are unsuitable for binary floating point:: 374 >>> x.quantize(Decimal('0.01')) # round to nearest cent 375 Decimal('0.74') 376 >>> round(.70 * 1.05, 2) # same calculation with floats 377 0.73 378 379 The :class:`~decimal.Decimal` result keeps a trailing zero, automatically 380 inferring four place significance from multiplicands with two place 381 significance. Decimal reproduces mathematics as done by hand and avoids 382 issues that can arise when binary floating point cannot exactly represent 383 decimal quantities. 384 385 Exact representation enables the :class:`~decimal.Decimal` class to perform 386 modulo calculations and equality tests that are unsuitable for binary floating 387 point:: 377 388 378 389 >>> Decimal('1.00') % Decimal('.10')
Note:
See TracChangeset
for help on using the changeset viewer.