[3.12] gh-115986 Improve pprint docs formatting (GH-117401) (GH-121099)

gh-115986 Improve pprint docs formatting (GH-117401)

* Move pprinter parameters description to the table

The change improves readability.
Suggested in the GHGH-116085 PR discussion.

* Make pprint doc with params markup

* Fix formatting
Indentation of code blocks made them nested
"Version changed" is better placed after the code block

* Fix formatting for tests

* fix code indentation for autotests

* Fix identation for autotests

* Remove duplication of the parameters' description

* Rearrange parameters description in a correct order

---------

(cherry picked from commit 0890ad7c02)

Co-authored-by: Kerim Kabirov <the.privat33r+gh@pm.me>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-06-28 18:21:56 +02:00 committed by GitHub
parent b31f7e2e90
commit 3a420de986
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -35,24 +35,66 @@ Dictionaries are sorted by key before the display is computed.
Functions Functions
--------- ---------
.. function:: pp(object, *args, sort_dicts=False, **kwargs) .. function:: pp(object, stream=None, indent=1, width=80, depth=None, *, \
compact=False, sort_dicts=False, underscore_numbers=False)
Prints the formatted representation of *object* followed by a newline. Prints the formatted representation of *object*, followed by a newline.
If *sort_dicts* is false (the default), dictionaries will be displayed with This function may be used in the interactive interpreter
their keys in insertion order, otherwise the dict keys will be sorted. instead of the :func:`print` function for inspecting values.
*args* and *kwargs* will be passed to :func:`~pprint.pprint` as formatting Tip: you can reassign ``print = pprint.pp`` for use within a scope.
parameters.
>>> import pprint :param object:
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] The object to be printed.
>>> stuff.insert(0, stuff)
>>> pprint.pp(stuff) :param stream:
[<Recursion on list with id=...>, A file-like object to which the output will be written
'spam', by calling its :meth:`!write` method.
'eggs', If ``None`` (the default), :data:`sys.stdout` is used.
'lumberjack', :type stream: :term:`file-like object` | None
'knights',
'ni'] :param int indent:
The amount of indentation added for each nesting level.
:param int width:
The desired maximum number of characters per line in the output.
If a structure cannot be formatted within the width constraint,
a best effort will be made.
:param depth:
The number of nesting levels which may be printed.
If the data structure being printed is too deep,
the next contained level is replaced by ``...``.
If ``None`` (the default), there is no constraint
on the depth of the objects being formatted.
:type depth: int | None
:param bool compact:
Control the way long :term:`sequences <sequence>` are formatted.
If ``False`` (the default),
each item of a sequence will be formatted on a separate line,
otherwise as many items as will fit within the *width*
will be formatted on each output line.
:param bool sort_dicts:
If ``True``, dictionaries will be formatted with
their keys sorted, otherwise
they will be displayed in insertion order (the default).
:param bool underscore_numbers:
If ``True``,
integers will be formatted with the ``_`` character for a thousands separator,
otherwise underscores are not displayed (the default).
>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff)
>>> pprint.pp(stuff)
[<Recursion on list with id=...>,
'spam',
'eggs',
'lumberjack',
'knights',
'ni']
.. versionadded:: 3.8 .. versionadded:: 3.8
@ -60,19 +102,10 @@ Functions
.. function:: pprint(object, stream=None, indent=1, width=80, depth=None, *, \ .. function:: pprint(object, stream=None, indent=1, width=80, depth=None, *, \
compact=False, sort_dicts=True, underscore_numbers=False) compact=False, sort_dicts=True, underscore_numbers=False)
Prints the formatted representation of *object* on *stream*, followed by a Alias for :func:`~pprint.pp` with *sort_dicts* set to ``True`` by default,
newline. If *stream* is ``None``, :data:`sys.stdout` is used. This may be used which would automatically sort the dictionaries' keys,
in the interactive interpreter instead of the :func:`print` function for you might want to use :func:`~pprint.pp` instead where it is ``False`` by default.
inspecting values (you can even reassign ``print = pprint.pprint`` for use
within a scope).
The configuration parameters *stream*, *indent*, *width*, *depth*,
*compact*, *sort_dicts* and *underscore_numbers* are passed to the
:class:`PrettyPrinter` constructor and their meanings are as
described in its documentation below.
Note that *sort_dicts* is ``True`` by default and you might want to use
:func:`~pprint.pp` instead where it is ``False`` by default.
.. function:: pformat(object, indent=1, width=80, depth=None, *, \ .. function:: pformat(object, indent=1, width=80, depth=None, *, \
compact=False, sort_dicts=True, underscore_numbers=False) compact=False, sort_dicts=True, underscore_numbers=False)
@ -80,7 +113,7 @@ Functions
Return the formatted representation of *object* as a string. *indent*, Return the formatted representation of *object* as a string. *indent*,
*width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* are *width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* are
passed to the :class:`PrettyPrinter` constructor as formatting parameters passed to the :class:`PrettyPrinter` constructor as formatting parameters
and their meanings are as described in its documentation below. and their meanings are as described in the documentation above.
.. function:: isreadable(object) .. function:: isreadable(object)
@ -119,51 +152,39 @@ Functions
PrettyPrinter Objects PrettyPrinter Objects
--------------------- ---------------------
This module defines one class:
.. First the implementation class:
.. index:: single: ...; placeholder .. index:: single: ...; placeholder
.. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, \ .. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, \
compact=False, sort_dicts=True, underscore_numbers=False) compact=False, sort_dicts=True, underscore_numbers=False)
Construct a :class:`PrettyPrinter` instance. This constructor understands Construct a :class:`PrettyPrinter` instance.
several keyword parameters.
*stream* (default :data:`!sys.stdout`) is a :term:`file-like object` to Arguments have the same meaning as for :func:`~pprint.pp`.
which the output will be written by calling its :meth:`!write` method. Note that they are in a different order, and that *sort_dicts* defaults to ``True``.
If both *stream* and :data:`!sys.stdout` are ``None``, then
:meth:`~PrettyPrinter.pprint` silently returns.
Other values configure the manner in which nesting of complex data >>> import pprint
structures is displayed. >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff[:])
>>> pp = pprint.PrettyPrinter(indent=4)
>>> pp.pprint(stuff)
[ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
'spam',
'eggs',
'lumberjack',
'knights',
'ni']
>>> pp = pprint.PrettyPrinter(width=41, compact=True)
>>> pp.pprint(stuff)
[['spam', 'eggs', 'lumberjack',
'knights', 'ni'],
'spam', 'eggs', 'lumberjack', 'knights',
'ni']
>>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
... ('parrot', ('fresh fruit',))))))))
>>> pp = pprint.PrettyPrinter(depth=6)
>>> pp.pprint(tup)
('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
*indent* (default 1) specifies the amount of indentation added for
each nesting level.
*depth* controls the number of nesting levels which may be printed; if
the data structure being printed is too deep, the next contained level
is replaced by ``...``. By default, there is no constraint on the
depth of the objects being formatted.
*width* (default 80) specifies the desired maximum number of characters per
line in the output. If a structure cannot be formatted within the width
constraint, a best effort will be made.
*compact* impacts the way that long sequences (lists, tuples, sets, etc)
are formatted. If *compact* is false (the default) then each item of a
sequence will be formatted on a separate line. If *compact* is true, as
many items as will fit within the *width* will be formatted on each output
line.
If *sort_dicts* is true (the default), dictionaries will be formatted with
their keys sorted, otherwise they will display in insertion order.
If *underscore_numbers* is true, integers will be formatted with the
``_`` character for a thousands separator, otherwise underscores are not
displayed (the default).
.. versionchanged:: 3.4 .. versionchanged:: 3.4
Added the *compact* parameter. Added the *compact* parameter.
@ -177,29 +198,6 @@ This module defines one class:
.. versionchanged:: 3.11 .. versionchanged:: 3.11
No longer attempts to write to :data:`!sys.stdout` if it is ``None``. No longer attempts to write to :data:`!sys.stdout` if it is ``None``.
>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff[:])
>>> pp = pprint.PrettyPrinter(indent=4)
>>> pp.pprint(stuff)
[ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
'spam',
'eggs',
'lumberjack',
'knights',
'ni']
>>> pp = pprint.PrettyPrinter(width=41, compact=True)
>>> pp.pprint(stuff)
[['spam', 'eggs', 'lumberjack',
'knights', 'ni'],
'spam', 'eggs', 'lumberjack', 'knights',
'ni']
>>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
... ('parrot', ('fresh fruit',))))))))
>>> pp = pprint.PrettyPrinter(depth=6)
>>> pp.pprint(tup)
('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
:class:`PrettyPrinter` instances have the following methods: :class:`PrettyPrinter` instances have the following methods: