Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/tutorial/stdlib2.rst

    r2 r391  
    7272==========
    7373
    74 The :mod:`string` module includes a versatile :class:`Template` class with a
    75 simplified syntax suitable for editing by end-users.  This allows users to
    76 customize their applications without having to alter the application.
     74The :mod:`string` module includes a versatile :class:`~string.Template` class
     75with a simplified syntax suitable for editing by end-users.  This allows users
     76to customize their applications without having to alter the application.
    7777
    7878The format uses placeholder names formed by ``$`` with valid Python identifiers
     
    8686   'Nottinghamfolk send $10 to the ditch fund.'
    8787
    88 The :meth:`substitute` method raises a :exc:`KeyError` when a placeholder is not
    89 supplied in a dictionary or a keyword argument. For mail-merge style
    90 applications, user supplied data may be incomplete and the
    91 :meth:`safe_substitute` method may be more appropriate --- it will leave
    92 placeholders unchanged if data is missing::
     88The :meth:`~string.Template.substitute` method raises a :exc:`KeyError` when a
     89placeholder is not supplied in a dictionary or a keyword argument.  For
     90mail-merge style applications, user supplied data may be incomplete and the
     91:meth:`~string.Template.safe_substitute` method may be more appropriate ---
     92it will leave placeholders unchanged if data is missing::
    9393
    9494   >>> t = Template('Return the $item to $owner.')
     
    9696   >>> t.substitute(d)
    9797   Traceback (most recent call last):
    98      . . .
     98     ...
    9999   KeyError: 'owner'
    100100   >>> t.safe_substitute(d)
     
    133133=======================================
    134134
    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
     135The :mod:`struct` module provides :func:`~struct.pack` and
     136:func:`~struct.unpack` functions for working with variable length binary
     137record formats.  The following example shows
    137138how to loop through header information in a ZIP file without using the
    138139:mod:`zipfile` module.  Pack codes ``"H"`` and ``"I"`` represent two and four
     
    219220   logging.critical('Critical error -- shutting down')
    220221
    221 This produces the following output::
     222This produces the following output:
     223
     224.. code-block:: none
    222225
    223226   WARNING:root:Warning:config file server.conf not found
     
    228231is sent to standard error.  Other output options include routing messages
    229232through 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`.
     233different routing based on message priority: :const:`~logging.DEBUG`,
     234:const:`~logging.INFO`, :const:`~logging.WARNING`, :const:`~logging.ERROR`,
     235and :const:`~logging.CRITICAL`.
    232236
    233237The logging system can be configured directly from Python or can be loaded from
     
    256260   >>> class A:
    257261   ...     def __init__(self, value):
    258    ...             self.value = value
     262   ...         self.value = value
    259263   ...     def __repr__(self):
    260    ...             return str(self.value)
     264   ...         return str(self.value)
    261265   ...
    262266   >>> a = A(10)                   # create a reference
     
    286290performance trade-offs.
    287291
    288 The :mod:`array` module provides an :class:`array()` object that is like a list
    289 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::
     292The :mod:`array` module provides an :class:`~array.array()` object that is like
     293a list that stores only homogeneous data and stores it more compactly.  The
     294following example shows an array of numbers stored as two byte unsigned binary
     295numbers (typecode ``"H"``) rather than the usual 16 bytes per entry for regular
     296lists of Python int objects::
    293297
    294298   >>> from array import array
     
    299303   array('H', [10, 700])
    300304
    301 The :mod:`collections` module provides a :class:`deque()` object that is like a
    302 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::
     305The :mod:`collections` module provides a :class:`~collections.deque()` object
     306that is like a list with faster appends and pops from the left side but slower
     307lookups in the middle. These objects are well suited for implementing queues
     308and breadth first tree searches::
    305309
    306310   >>> from collections import deque
     
    309313   >>> print "Handling", d.popleft()
    310314   Handling task1
     315
     316::
    311317
    312318   unsearched = deque([starting_node])
     
    346352=================================
    347353
    348 The :mod:`decimal` module offers a :class:`Decimal` datatype for decimal
    349 floating point arithmetic.  Compared to the built-in :class:`float`
     354The :mod:`decimal` module offers a :class:`~decimal.Decimal` datatype for
     355decimal floating point arithmetic.  Compared to the built-in :class:`float`
    350356implementation of binary floating point, the class is especially helpful for
    351357
     
    363369
    364370   >>> from decimal import *
    365    >>> Decimal('0.70') * Decimal('1.05')
     371   >>> x = Decimal('0.70') * Decimal('1.05')
     372   >>> x
    366373   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
     379The :class:`~decimal.Decimal` result keeps a trailing zero, automatically
     380inferring four place significance from multiplicands with two place
     381significance.  Decimal reproduces mathematics as done by hand and avoids
     382issues that can arise when binary floating point cannot exactly represent
     383decimal quantities.
     384
     385Exact representation enables the :class:`~decimal.Decimal` class to perform
     386modulo calculations and equality tests that are unsuitable for binary floating
     387point::
    377388
    378389   >>> Decimal('1.00') % Decimal('.10')
Note: See TracChangeset for help on using the changeset viewer.