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/library/string.rst

    r2 r391  
    77
    88.. index:: module: re
     9
     10**Source code:** :source:`Lib/string.py`
     11
     12--------------
    913
    1014The :mod:`string` module contains a number of useful constants and
     
    1822string functions based on regular expressions.
    1923
    20 
    2124String constants
    2225----------------
     
    106109-----------------
    107110
    108 Starting in Python 2.6, the built-in str and unicode classes provide the ability
     111.. versionadded:: 2.6
     112
     113The built-in str and unicode classes provide the ability
    109114to do complex variable substitutions and value formatting via the
    110115:meth:`str.format` method described in :pep:`3101`.  The :class:`Formatter`
     
    117122   The :class:`Formatter` class has the following public methods:
    118123
    119    .. method:: format(format_string, *args, *kwargs)
    120 
    121       :meth:`format` is the primary API method.  It takes a format template
    122       string, and an arbitrary set of positional and keyword argument.
     124   .. method:: format(format_string, *args, **kwargs)
     125
     126      :meth:`format` is the primary API method.  It takes a format string and
     127      an arbitrary set of positional and keyword arguments.
    123128      :meth:`format` is just a wrapper that calls :meth:`vformat`.
    124129
     
    128133      separate function for cases where you want to pass in a predefined
    129134      dictionary of arguments, rather than unpacking and repacking the
    130       dictionary as individual arguments using the ``*args`` and ``**kwds``
    131       syntax.  :meth:`vformat` does the work of breaking up the format template
    132       string into character data and replacement fields.  It calls the various
     135      dictionary as individual arguments using the ``*args`` and ``**kwargs``
     136      syntax.  :meth:`vformat` does the work of breaking up the format string
     137      into character data and replacement fields.  It calls the various
    133138      methods described below.
    134139
     
    140145      Loop over the format_string and return an iterable of tuples
    141146      (*literal_text*, *field_name*, *format_spec*, *conversion*).  This is used
    142       by :meth:`vformat` to break the string in to either literal text, or
     147      by :meth:`vformat` to break the string into either literal text, or
    143148      replacement fields.
    144149
     
    189194      named arguments), and a reference to the *args* and *kwargs* that was
    190195      passed to vformat.  The set of unused args can be calculated from these
    191       parameters.  :meth:`check_unused_args` is assumed to throw an exception if
     196      parameters.  :meth:`check_unused_args` is assumed to raise an exception if
    192197      the check fails.
    193198
     
    200205
    201206      Converts the value (returned by :meth:`get_field`) given a conversion type
    202       (as in the tuple returned by the :meth:`parse` method.)  The default
    203       version understands 'r' (repr) and 's' (str) conversion types.
     207      (as in the tuple returned by the :meth:`parse` method).  The default
     208      version understands 's' (str), 'r' (repr) and 'a' (ascii) conversion
     209      types.
    204210
    205211
     
    211217The :meth:`str.format` method and the :class:`Formatter` class share the same
    212218syntax for format strings (although in the case of :class:`Formatter`,
    213 subclasses can define their own format string syntax.)
     219subclasses can define their own format string syntax).
    214220
    215221Format strings contain "replacement fields" surrounded by curly braces ``{}``.
     
    221227
    222228   .. productionlist:: sf
    223       replacement_field: "{" `field_name` ["!" `conversion`] [":" `format_spec`] "}"
    224       field_name: (`identifier` | `integer`) ("." `attribute_name` | "[" `element_index` "]")*
     229      replacement_field: "{" [`field_name`] ["!" `conversion`] [":" `format_spec`] "}"
     230      field_name: arg_name ("." `attribute_name` | "[" `element_index` "]")*
     231      arg_name: [`identifier` | `integer`]
    225232      attribute_name: `identifier`
    226233      element_index: `integer` | `index_string`
     
    229236      format_spec: <described in the next section>
    230237
    231 In less formal terms, the replacement field starts with a *field_name*, which
    232 can either be a number (for a positional argument), or an identifier (for
    233 keyword arguments).  Following this is an optional *conversion* field, which is
     238In less formal terms, the replacement field can start with a *field_name* that specifies
     239the object whose value is to be formatted and inserted
     240into the output instead of the replacement field.
     241The *field_name* is optionally followed by a  *conversion* field, which is
    234242preceded by an exclamation point ``'!'``, and a *format_spec*, which is preceded
    235 by a colon ``':'``.
    236 
    237 The *field_name* itself begins with either a number or a keyword.  If it's a
    238 number, it refers to a positional argument, and if it's a keyword it refers to a
    239 named keyword argument.  This can be followed by any number of index or
     243by a colon ``':'``.  These specify a non-default format for the replacement value.
     244
     245See also the :ref:`formatspec` section.
     246
     247The *field_name* itself begins with an *arg_name* that is either a number or a
     248keyword.  If it's a number, it refers to a positional argument, and if it's a keyword,
     249it refers to a named keyword argument.  If the numerical arg_names in a format string
     250are 0, 1, 2, ... in sequence, they can all be omitted (not just some)
     251and the numbers 0, 1, 2, ... will be automatically inserted in that order.
     252Because *arg_name* is not quote-delimited, it is not possible to specify arbitrary
     253dictionary keys (e.g., the strings ``'10'`` or ``':-]'``) within a format string.
     254The *arg_name* can be followed by any number of index or
    240255attribute expressions. An expression of the form ``'.name'`` selects the named
    241256attribute using :func:`getattr`, while an expression of the form ``'[index]'``
    242257does an index lookup using :func:`__getitem__`.
    243258
     259.. versionchanged:: 2.7
     260   The positional argument specifiers can be omitted, so ``'{} {}'`` is
     261   equivalent to ``'{0} {1}'``.
     262
    244263Some simple format string examples::
    245264
    246265   "First, thou shalt count to {0}" # References first positional argument
     266   "Bring me a {}"                  # Implicitly references the first positional argument
     267   "From {} to {}"                  # Same as "From {0} to {1}"
    247268   "My quest is {name}"             # References keyword argument 'name'
    248269   "Weight in tons {0.weight}"      # 'weight' attribute of first positional arg
     
    278299This allows the formatting of a value to be dynamically specified.
    279300
    280 For example, suppose you wanted to have a replacement field whose field width is
    281 determined by another variable::
    282 
    283    "A man with two {0:{1}}".format("noses", 10)
    284 
    285 This would first evaluate the inner replacement field, making the format string
    286 effectively::
    287 
    288    "A man with two {0:10}"
    289 
    290 Then the outer replacement field would be evaluated, producing::
    291 
    292    "noses     "
    293 
    294 Which is substituted into the string, yielding::
    295 
    296    "A man with two noses     "
    297 
    298 (The extra space is because we specified a field width of 10, and because left
    299 alignment is the default for strings.)
     301See the :ref:`formatexamples` section for some examples.
    300302
    301303
     
    307309"Format specifications" are used within replacement fields contained within a
    308310format string to define how individual values are presented (see
    309 :ref:`formatstrings`.)  They can also be passed directly to the built-in
     311:ref:`formatstrings`).  They can also be passed directly to the built-in
    310312:func:`format` function.  Each formattable type may define how the format
    311313specification is to be interpreted.
     
    321323
    322324.. productionlist:: sf
    323    format_spec: [[`fill`]`align`][`sign`][#][0][`width`][.`precision`][`type`]
    324    fill: <a character other than '}'>
     325   format_spec: [[`fill`]`align`][`sign`][#][0][`width`][,][.`precision`][`type`]
     326   fill: <any character>
    325327   align: "<" | ">" | "=" | "^"
    326328   sign: "+" | "-" | " "
     
    329331   type: "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
    330332
    331 The *fill* character can be any character other than '}' (which signifies the
    332 end of the field).  The presence of a fill character is signaled by the *next*
    333 character, which must be one of the alignment options. If the second character
    334 of *format_spec* is not a valid alignment option, then it is assumed that both
    335 the fill character and the alignment option are absent.
     333If a valid *align* value is specified, it can be preceeded by a *fill*
     334character that can be any character and defaults to a space if omitted.
     335Note that it is not possible to use ``{`` and ``}`` as *fill* char while
     336using the :meth:`str.format` method; this limitation however doesn't
     337affect the :func:`format` function.
    336338
    337339The meaning of the various alignment options is as follows:
     
    341343   +=========+==========================================================+
    342344   | ``'<'`` | Forces the field to be left-aligned within the available |
    343    |         | space (This is the default.)                             |
     345   |         | space (this is the default for most objects).            |
    344346   +---------+----------------------------------------------------------+
    345347   | ``'>'`` | Forces the field to be right-aligned within the          |
    346    |         | available space.                                         |
     348   |         | available space (this is the default for numbers).       |
    347349   +---------+----------------------------------------------------------+
    348350   | ``'='`` | Forces the padding to be placed after the sign (if any)  |
     
    379381by ``'0b'``, ``'0o'``, or ``'0x'``, respectively.
    380382
     383The ``','`` option signals the use of a comma for a thousands separator.
     384For a locale aware separator, use the ``'n'`` integer presentation type
     385instead.
     386
     387.. versionchanged:: 2.7
     388   Added the ``','`` option (see also :pep:`378`).
     389
    381390*width* is a decimal integer defining the minimum field width.  If not
    382391specified, then the field width will be determined by the content.
    383392
    384 If the *width* field is preceded by a zero (``'0'``) character, this enables
    385 zero-padding.  This is equivalent to an *alignment* type of ``'='`` and a *fill*
    386 character of ``'0'``.
     393Preceding the *width* field by a zero (``'0'``) character enables
     394sign-aware zero-padding for numeric types.  This is equivalent to a *fill*
     395character of ``'0'`` with an *alignment* type of ``'='``.
    387396
    388397The *precision* is a decimal number indicating how many digits should be
     
    445454   | ``'e'`` | Exponent notation. Prints the number in scientific       |
    446455   |         | notation using the letter 'e' to indicate the exponent.  |
     456   |         | The default precision is ``6``.                          |
    447457   +---------+----------------------------------------------------------+
    448458   | ``'E'`` | Exponent notation. Same as ``'e'`` except it uses an     |
     
    450460   +---------+----------------------------------------------------------+
    451461   | ``'f'`` | Fixed point. Displays the number as a fixed-point        |
    452    |         | number.                                                  |
     462   |         | number.  The default precision is ``6``.                 |
    453463   +---------+----------------------------------------------------------+
    454464   | ``'F'`` | Fixed point. Same as ``'f'``.                            |
     
    470480   |         | removed if there are no remaining digits following it.   |
    471481   |         |                                                          |
    472    |         | Postive and negative infinity, positive and negative     |
     482   |         | Positive and negative infinity, positive and negative    |
    473483   |         | zero, and nans, are formatted as ``inf``, ``-inf``,      |
    474484   |         | ``0``, ``-0`` and ``nan`` respectively, regardless of    |
     
    476486   |         |                                                          |
    477487   |         | A precision of ``0`` is treated as equivalent to a       |
    478    |         | precision of ``1``.                                      |
     488   |         | precision of ``1``.  The default precision is ``6``.     |
    479489   +---------+----------------------------------------------------------+
    480490   | ``'G'`` | General format. Same as ``'g'`` except switches to       |
     
    493503
    494504
     505
     506.. _formatexamples:
     507
     508Format examples
     509^^^^^^^^^^^^^^^
     510
     511This section contains examples of the new format syntax and comparison with
     512the old ``%``-formatting.
     513
     514In most of the cases the syntax is similar to the old ``%``-formatting, with the
     515addition of the ``{}`` and with ``:`` used instead of ``%``.
     516For example, ``'%03.2f'`` can be translated to ``'{:03.2f}'``.
     517
     518The new format syntax also supports new and different options, shown in the
     519follow examples.
     520
     521Accessing arguments by position::
     522
     523   >>> '{0}, {1}, {2}'.format('a', 'b', 'c')
     524   'a, b, c'
     525   >>> '{}, {}, {}'.format('a', 'b', 'c')  # 2.7+ only
     526   'a, b, c'
     527   >>> '{2}, {1}, {0}'.format('a', 'b', 'c')
     528   'c, b, a'
     529   >>> '{2}, {1}, {0}'.format(*'abc')      # unpacking argument sequence
     530   'c, b, a'
     531   >>> '{0}{1}{0}'.format('abra', 'cad')   # arguments' indices can be repeated
     532   'abracadabra'
     533
     534Accessing arguments by name::
     535
     536   >>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
     537   'Coordinates: 37.24N, -115.81W'
     538   >>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
     539   >>> 'Coordinates: {latitude}, {longitude}'.format(**coord)
     540   'Coordinates: 37.24N, -115.81W'
     541
     542Accessing arguments' attributes::
     543
     544   >>> c = 3-5j
     545   >>> ('The complex number {0} is formed from the real part {0.real} '
     546   ...  'and the imaginary part {0.imag}.').format(c)
     547   'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'
     548   >>> class Point(object):
     549   ...     def __init__(self, x, y):
     550   ...         self.x, self.y = x, y
     551   ...     def __str__(self):
     552   ...         return 'Point({self.x}, {self.y})'.format(self=self)
     553   ...
     554   >>> str(Point(4, 2))
     555   'Point(4, 2)'
     556
     557
     558Accessing arguments' items::
     559
     560   >>> coord = (3, 5)
     561   >>> 'X: {0[0]};  Y: {0[1]}'.format(coord)
     562   'X: 3;  Y: 5'
     563
     564Replacing ``%s`` and ``%r``::
     565
     566   >>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')
     567   "repr() shows quotes: 'test1'; str() doesn't: test2"
     568
     569Aligning the text and specifying a width::
     570
     571   >>> '{:<30}'.format('left aligned')
     572   'left aligned                  '
     573   >>> '{:>30}'.format('right aligned')
     574   '                 right aligned'
     575   >>> '{:^30}'.format('centered')
     576   '           centered           '
     577   >>> '{:*^30}'.format('centered')  # use '*' as a fill char
     578   '***********centered***********'
     579
     580Replacing ``%+f``, ``%-f``, and ``% f`` and specifying a sign::
     581
     582   >>> '{:+f}; {:+f}'.format(3.14, -3.14)  # show it always
     583   '+3.140000; -3.140000'
     584   >>> '{: f}; {: f}'.format(3.14, -3.14)  # show a space for positive numbers
     585   ' 3.140000; -3.140000'
     586   >>> '{:-f}; {:-f}'.format(3.14, -3.14)  # show only the minus -- same as '{:f}; {:f}'
     587   '3.140000; -3.140000'
     588
     589Replacing ``%x`` and ``%o`` and converting the value to different bases::
     590
     591   >>> # format also supports binary numbers
     592   >>> "int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42)
     593   'int: 42;  hex: 2a;  oct: 52;  bin: 101010'
     594   >>> # with 0x, 0o, or 0b as prefix:
     595   >>> "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)
     596   'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010'
     597
     598Using the comma as a thousands separator::
     599
     600   >>> '{:,}'.format(1234567890)
     601   '1,234,567,890'
     602
     603Expressing a percentage::
     604
     605   >>> points = 19.5
     606   >>> total = 22
     607   >>> 'Correct answers: {:.2%}'.format(points/total)
     608   'Correct answers: 88.64%'
     609
     610Using type-specific formatting::
     611
     612   >>> import datetime
     613   >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
     614   >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
     615   '2010-07-04 12:15:58'
     616
     617Nesting arguments and more complex examples::
     618
     619   >>> for align, text in zip('<^>', ['left', 'center', 'right']):
     620   ...     '{0:{fill}{align}16}'.format(text, fill=align, align=align)
     621   ...
     622   'left<<<<<<<<<<<<'
     623   '^^^^^center^^^^^'
     624   '>>>>>>>>>>>right'
     625   >>>
     626   >>> octets = [192, 168, 0, 1]
     627   >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)
     628   'C0A80001'
     629   >>> int(_, 16)
     630   3232235521
     631   >>>
     632   >>> width = 5
     633   >>> for num in range(5,12):
     634   ...     for base in 'dXob':
     635   ...         print '{0:{width}{base}}'.format(num, base=base, width=width),
     636   ...     print
     637   ...
     638       5     5     5   101
     639       6     6     6   110
     640       7     7     7   111
     641       8     8    10  1000
     642       9     9    11  1001
     643      10     A    12  1010
     644      11     B    13  1011
     645
     646
     647
    495648Template strings
    496649----------------
     650
     651.. versionadded:: 2.4
    497652
    498653Templates provide simpler string substitutions as described in :pep:`292`.
     
    513668Any other appearance of ``$`` in the string will result in a :exc:`ValueError`
    514669being raised.
    515 
    516 .. versionadded:: 2.4
    517670
    518671The :mod:`string` module provides a :class:`Template` class that implements
     
    549702      placeholders that are not valid Python identifiers.
    550703
    551 :class:`Template` instances also provide one public data attribute:
    552 
    553 
    554 .. attribute:: string.template
    555 
    556    This is the object passed to the constructor's *template* argument.  In general,
    557    you shouldn't change it, but read-only access is not enforced.
    558 
    559 Here is an example of how to use a Template:
     704   :class:`Template` instances also provide one public data attribute:
     705
     706   .. attribute:: template
     707
     708      This is the object passed to the constructor's *template* argument.  In
     709      general, you shouldn't change it, but read-only access is not enforced.
     710
     711Here is an example of how to use a Template::
    560712
    561713   >>> from string import Template
     
    566718   >>> Template('Give $who $100').substitute(d)
    567719   Traceback (most recent call last):
    568    [...]
    569    ValueError: Invalid placeholder in string: line 1, col 10
     720   ...
     721   ValueError: Invalid placeholder in string: line 1, col 11
    570722   >>> Template('$who likes $what').substitute(d)
    571723   Traceback (most recent call last):
    572    [...]
     724   ...
    573725   KeyError: 'what'
    574726   >>> Template('$who likes $what').safe_substitute(d)
     
    580732
    581733* *delimiter* -- This is the literal string describing a placeholder introducing
    582   delimiter.  The default value ``$``.  Note that this should *not* be a regular
    583   expression, as the implementation will call :meth:`re.escape` on this string as
    584   needed.
     734  delimiter.  The default value is ``$``.  Note that this should *not* be a
     735  regular expression, as the implementation will call :meth:`re.escape` on this
     736  string as needed.
    585737
    586738* *idpattern* -- This is the regular expression describing the pattern for
     
    644796Unicode objects; see section :ref:`string-methods` for more information on
    645797those.  You should consider these functions as deprecated, although they will
    646 not be removed until Python 3.0.  The functions defined in this module are:
     798not be removed until Python 3.  The functions defined in this module are:
    647799
    648800
     
    756908   Return a list of the words of the string *s*.  If the optional second argument
    757909   *sep* is absent or ``None``, the words are separated by arbitrary strings of
    758    whitespace characters (space, tab,  newline, return, formfeed).  If the second
     910   whitespace characters (space, tab, newline, return, formfeed).  If the second
    759911   argument *sep* is present and not ``None``, it specifies a string to be used as
    760912   the  word separator.  The returned list will then have one more item than the
    761    number of non-overlapping occurrences of the separator in the string.  The
    762    optional third argument *maxsplit* defaults to 0.  If it is nonzero, at most
    763    *maxsplit* number of splits occur, and the remainder of the string is returned
    764    as the final element of the list (thus, the list will have at most
    765    ``maxsplit+1`` elements).
     913   number of non-overlapping occurrences of the separator in the string.
     914   If *maxsplit* is given, at most *maxsplit* number of splits occur, and the
     915   remainder of the string is returned as the final element of the list (thus,
     916   the list will have at most ``maxsplit+1`` elements).  If *maxsplit* is not
     917   specified or ``-1``, then there is no limit on the number of splits (all
     918   possible splits are made).
    766919
    767920   The behavior of split on an empty string depends on the value of *sep*. If *sep*
     
    776929   intents and purposes, the resulting list of words is the same as returned by
    777930   :func:`split`, except when the optional third argument *maxsplit* is explicitly
    778    specified and nonzero.  When *maxsplit* is nonzero, at most *maxsplit* number of
     931   specified and nonzero.  If *maxsplit* is given, at most *maxsplit* number of
    779932   splits -- the *rightmost* ones -- occur, and the remainder of the string is
    780933   returned as the first element of the list (thus, the list will have at most
     
    8741027.. function:: zfill(s, width)
    8751028
    876    Pad a numeric string on the left with zero digits until the given width is
    877    reached.  Strings starting with a sign are handled correctly.
    878 
    879 
    880 .. function:: replace(str, old, new[, maxreplace])
    881 
    882    Return a copy of string *str* with all occurrences of substring *old* replaced
     1029   Pad a numeric string *s* on the left with zero digits until the
     1030   given *width* is reached.  Strings starting with a sign are handled
     1031   correctly.
     1032
     1033
     1034.. function:: replace(s, old, new[, maxreplace])
     1035
     1036   Return a copy of string *s* with all occurrences of substring *old* replaced
    8831037   by *new*.  If the optional argument *maxreplace* is given, the first
    8841038   *maxreplace* occurrences are replaced.
Note: See TracChangeset for help on using the changeset viewer.