Kaydet (Commit) 00f19eff authored tarafından Martin Panter's avatar Martin Panter

Issue #25179: Preparatory cleanup of existing docs on string formatting

* There was a link pointing to the section on the string.Formatter class (and
  multiple links in Python 3), when the section on the common format string
  syntax is probably more appropriate
* Fix references to various format() functions and methods
* Nested replacement fields may contain conversions and format specifiers,
  and this is tested in Python 3; see Issue #19729 for instance
üst 9c61f2e4
...@@ -558,7 +558,7 @@ Instance methods: ...@@ -558,7 +558,7 @@ Instance methods:
.. method:: date.__format__(format) .. method:: date.__format__(format)
Same as :meth:`.date.strftime`. This makes it possible to specify format Same as :meth:`.date.strftime`. This makes it possible to specify a format
string for a :class:`.date` object when using :meth:`str.format`. string for a :class:`.date` object when using :meth:`str.format`.
See section :ref:`strftime-strptime-behavior`. See section :ref:`strftime-strptime-behavior`.
...@@ -1058,7 +1058,7 @@ Instance methods: ...@@ -1058,7 +1058,7 @@ Instance methods:
.. method:: datetime.__format__(format) .. method:: datetime.__format__(format)
Same as :meth:`.datetime.strftime`. This makes it possible to specify format Same as :meth:`.datetime.strftime`. This makes it possible to specify a format
string for a :class:`.datetime` object when using :meth:`str.format`. string for a :class:`.datetime` object when using :meth:`str.format`.
See section :ref:`strftime-strptime-behavior`. See section :ref:`strftime-strptime-behavior`.
...@@ -1292,7 +1292,7 @@ Instance methods: ...@@ -1292,7 +1292,7 @@ Instance methods:
.. method:: time.__format__(format) .. method:: time.__format__(format)
Same as :meth:`.time.strftime`. This makes it possible to specify format string Same as :meth:`.time.strftime`. This makes it possible to specify a format string
for a :class:`.time` object when using :meth:`str.format`. for a :class:`.time` object when using :meth:`str.format`.
See section :ref:`strftime-strptime-behavior`. See section :ref:`strftime-strptime-behavior`.
......
...@@ -190,7 +190,7 @@ are converted to strings. The default implementation uses the internals of the ...@@ -190,7 +190,7 @@ are converted to strings. The default implementation uses the internals of the
the current presentation context (direct and indirect containers for *object* the current presentation context (direct and indirect containers for *object*
that are affecting the presentation) as the keys; if an object needs to be that are affecting the presentation) as the keys; if an object needs to be
presented which is already represented in *context*, the third return value presented which is already represented in *context*, the third return value
should be ``True``. Recursive calls to the :meth:`format` method should add should be ``True``. Recursive calls to the :meth:`.format` method should add
additional entries for containers to this dictionary. The third argument, additional entries for containers to this dictionary. The third argument,
*maxlevels*, gives the requested limit to recursion; this will be ``0`` if there *maxlevels*, gives the requested limit to recursion; this will be ``0`` if there
is no requested limit. This argument should be passed unmodified to recursive is no requested limit. This argument should be passed unmodified to recursive
......
...@@ -105,8 +105,8 @@ The constants defined in this module are: ...@@ -105,8 +105,8 @@ The constants defined in this module are:
.. _new-string-formatting: .. _new-string-formatting:
String Formatting Custom String Formatting
----------------- ------------------------
.. versionadded:: 2.6 .. versionadded:: 2.6
...@@ -115,7 +115,7 @@ to do complex variable substitutions and value formatting via the ...@@ -115,7 +115,7 @@ to do complex variable substitutions and value formatting via the
:meth:`str.format` method described in :pep:`3101`. The :class:`Formatter` :meth:`str.format` method described in :pep:`3101`. The :class:`Formatter`
class in the :mod:`string` module allows you to create and customize your own class in the :mod:`string` module allows you to create and customize your own
string formatting behaviors using the same implementation as the built-in string formatting behaviors using the same implementation as the built-in
:meth:`format` method. :meth:`~str.format` method.
.. class:: Formatter .. class:: Formatter
...@@ -123,9 +123,9 @@ string formatting behaviors using the same implementation as the built-in ...@@ -123,9 +123,9 @@ string formatting behaviors using the same implementation as the built-in
.. method:: format(format_string, *args, **kwargs) .. method:: format(format_string, *args, **kwargs)
:meth:`format` is the primary API method. It takes a format string and The primary API method. It takes a format string and
an arbitrary set of positional and keyword arguments. an arbitrary set of positional and keyword arguments.
:meth:`format` is just a wrapper that calls :meth:`vformat`. It is just a wrapper that calls :meth:`vformat`.
.. method:: vformat(format_string, args, kwargs) .. method:: vformat(format_string, args, kwargs)
...@@ -293,8 +293,9 @@ Most built-in types support a common formatting mini-language, which is ...@@ -293,8 +293,9 @@ Most built-in types support a common formatting mini-language, which is
described in the next section. described in the next section.
A *format_spec* field can also include nested replacement fields within it. A *format_spec* field can also include nested replacement fields within it.
These nested replacement fields can contain only a field name; conversion flags These nested replacement fields may contain a field name, conversion flag
and format specifications are not allowed. The replacement fields within the and format specification, but deeper nesting is
not allowed. The replacement fields within the
format_spec are substituted before the *format_spec* string is interpreted. format_spec are substituted before the *format_spec* string is interpreted.
This allows the formatting of a value to be dynamically specified. This allows the formatting of a value to be dynamically specified.
...@@ -332,8 +333,10 @@ The general form of a *standard format specifier* is: ...@@ -332,8 +333,10 @@ The general form of a *standard format specifier* is:
If a valid *align* value is specified, it can be preceded by a *fill* If a valid *align* value is specified, it can be preceded by a *fill*
character that can be any character and defaults to a space if omitted. character that can be any character and defaults to a space if omitted.
Note that it is not possible to use ``{`` and ``}`` as *fill* char while It is not possible to use a literal curly brace ("``{``" or "``}``") as
using the :meth:`str.format` method; this limitation however doesn't the *fill* character when using the :meth:`str.format`
method. However, it is possible to insert a curly brace
with a nested replacement field. This limitation doesn't
affect the :func:`format` function. affect the :func:`format` function.
The meaning of the various alignment options is as follows: The meaning of the various alignment options is as follows:
...@@ -508,8 +511,8 @@ The available presentation types for floating point and decimal values are: ...@@ -508,8 +511,8 @@ The available presentation types for floating point and decimal values are:
Format examples Format examples
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
This section contains examples of the new format syntax and comparison with This section contains examples of the :meth:`str.format` syntax and
the old ``%``-formatting. comparison with the old ``%``-formatting.
In most of the cases the syntax is similar to the old ``%``-formatting, with the In most of the cases the syntax is similar to the old ``%``-formatting, with the
addition of the ``{}`` and with ``:`` used instead of ``%``. addition of the ``{}`` and with ``:`` used instead of ``%``.
......
...@@ -46,7 +46,7 @@ howto/pyporting,,::,Programming Language :: Python :: 2 ...@@ -46,7 +46,7 @@ howto/pyporting,,::,Programming Language :: Python :: 2
howto/pyporting,,::,Programming Language :: Python :: 3 howto/pyporting,,::,Programming Language :: Python :: 3
howto/regex,,::, howto/regex,,::,
howto/regex,,:foo,(?:foo) howto/regex,,:foo,(?:foo)
howto/urllib2,,:example,"for example ""joe@password:example.com""" howto/urllib2,,:password,"for example ""joe:password@example.com"""
library/audioop,,:ipos,"# factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)]," library/audioop,,:ipos,"# factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],"
library/bisect,,:hi,all(val >= x for val in a[i:hi]) library/bisect,,:hi,all(val >= x for val in a[i:hi])
library/bisect,,:hi,all(val > x for val in a[i:hi]) library/bisect,,:hi,all(val > x for val in a[i:hi])
......
...@@ -357,9 +357,8 @@ The built-in function :func:`len` returns the length of a string:: ...@@ -357,9 +357,8 @@ The built-in function :func:`len` returns the length of a string::
Both strings and Unicode strings support a large number of methods for Both strings and Unicode strings support a large number of methods for
basic transformations and searching. basic transformations and searching.
:ref:`new-string-formatting` :ref:`formatstrings`
Information about string formatting with :meth:`str.format` is described Information about string formatting with :meth:`str.format`.
here.
:ref:`string-formatting` :ref:`string-formatting`
The old formatting operations invoked when strings and Unicode strings are The old formatting operations invoked when strings and Unicode strings are
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment