Skip to content

Commit 132abfd

Browse files
donbarbosstodoranStefanTodoranblurb-it[bot]hugovk
authored
gh-112632: Add optional keyword-only argument expand to pprint (#136964)
Co-authored-by: stodoran <stefan.todoran@uipath.com> Co-authored-by: StefanTodoran <stefan.alex4@gmail.com> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 3df0c4d commit 132abfd

File tree

5 files changed

+768
-86
lines changed

5 files changed

+768
-86
lines changed

Doc/library/pprint.rst

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ Functions
3131
---------
3232

3333
.. function:: pp(object, stream=None, indent=1, width=80, depth=None, *, \
34-
compact=False, sort_dicts=False, underscore_numbers=False)
34+
compact=False, expand=False, sort_dicts=False, \
35+
underscore_numbers=False)
3536

3637
Prints the formatted representation of *object*, followed by a newline.
3738
This function may be used in the interactive interpreter
@@ -69,6 +70,13 @@ Functions
6970
each item of a sequence will be formatted on a separate line,
7071
otherwise as many items as will fit within the *width*
7172
will be formatted on each output line.
73+
Incompatible with *expand*.
74+
75+
:param bool expand:
76+
If ``True``,
77+
opening parentheses and brackets will be followed by a newline and the
78+
following content will be indented by one level, similar to
79+
pretty-printed JSON. Incompatible with *compact*.
7280

7381
:param bool sort_dicts:
7482
If ``True``, dictionaries will be formatted with
@@ -95,18 +103,20 @@ Functions
95103

96104

97105
.. function:: pprint(object, stream=None, indent=1, width=80, depth=None, *, \
98-
compact=False, sort_dicts=True, underscore_numbers=False)
106+
compact=False, expand=False, sort_dicts=True, \
107+
underscore_numbers=False)
99108

100109
Alias for :func:`~pprint.pp` with *sort_dicts* set to ``True`` by default,
101110
which would automatically sort the dictionaries' keys,
102111
you might want to use :func:`~pprint.pp` instead where it is ``False`` by default.
103112

104113

105114
.. function:: pformat(object, indent=1, width=80, depth=None, *, \
106-
compact=False, sort_dicts=True, underscore_numbers=False)
115+
compact=False, expand=False, sort_dicts=True, \
116+
underscore_numbers=False)
107117

108118
Return the formatted representation of *object* as a string. *indent*,
109-
*width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* are
119+
*width*, *depth*, *compact*, *expand*, *sort_dicts* and *underscore_numbers* are
110120
passed to the :class:`PrettyPrinter` constructor as formatting parameters
111121
and their meanings are as described in the documentation above.
112122

@@ -150,7 +160,8 @@ PrettyPrinter Objects
150160
.. index:: single: ...; placeholder
151161

152162
.. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, \
153-
compact=False, sort_dicts=True, underscore_numbers=False)
163+
compact=False, expand=False, sort_dicts=True, \
164+
underscore_numbers=False)
154165

155166
Construct a :class:`PrettyPrinter` instance.
156167

@@ -174,6 +185,22 @@ PrettyPrinter Objects
174185
'knights', 'ni'],
175186
'spam', 'eggs', 'lumberjack', 'knights',
176187
'ni']
188+
>>> pp = pprint.PrettyPrinter(width=41, expand=True, indent=3)
189+
>>> pp.pprint(stuff)
190+
[
191+
[
192+
'spam',
193+
'eggs',
194+
'lumberjack',
195+
'knights',
196+
'ni',
197+
],
198+
'spam',
199+
'eggs',
200+
'lumberjack',
201+
'knights',
202+
'ni',
203+
]
177204
>>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
178205
... ('parrot', ('fresh fruit',))))))))
179206
>>> pp = pprint.PrettyPrinter(depth=6)
@@ -193,6 +220,9 @@ PrettyPrinter Objects
193220
.. versionchanged:: 3.11
194221
No longer attempts to write to :data:`!sys.stdout` if it is ``None``.
195222

223+
.. versionchanged:: next
224+
Added the *expand* parameter.
225+
196226

197227
:class:`PrettyPrinter` instances have the following methods:
198228

@@ -415,3 +445,72 @@ cannot be split, the specified width will be exceeded::
415445
'requires_python': None,
416446
'summary': 'A sample Python project',
417447
'version': '1.2.0'}
448+
449+
Lastly, we can format like pretty-printed JSON with the *expand* parameter.
450+
Best results are achieved with a higher *indent* value::
451+
452+
>>> pprint.pp(project_info, indent=4, expand=True)
453+
{
454+
'author': 'The Python Packaging Authority',
455+
'author_email': 'pypa-dev@googlegroups.com',
456+
'bugtrack_url': None,
457+
'classifiers': [
458+
'Development Status :: 3 - Alpha',
459+
'Intended Audience :: Developers',
460+
'License :: OSI Approved :: MIT License',
461+
'Programming Language :: Python :: 2',
462+
'Programming Language :: Python :: 2.6',
463+
'Programming Language :: Python :: 2.7',
464+
'Programming Language :: Python :: 3',
465+
'Programming Language :: Python :: 3.2',
466+
'Programming Language :: Python :: 3.3',
467+
'Programming Language :: Python :: 3.4',
468+
'Topic :: Software Development :: Build Tools',
469+
],
470+
'description': 'A sample Python project\n'
471+
'=======================\n'
472+
'\n'
473+
'This is the description file for the project.\n'
474+
'\n'
475+
'The file should use UTF-8 encoding and be written using ReStructured '
476+
'Text. It\n'
477+
'will be used to generate the project webpage on PyPI, and should be '
478+
'written for\n'
479+
'that purpose.\n'
480+
'\n'
481+
'Typical contents for this file would include an overview of the project, '
482+
'basic\n'
483+
'usage examples, etc. Generally, including the project changelog in here '
484+
'is not\n'
485+
'a good idea, although a simple "What\'s New" section for the most recent '
486+
'version\n'
487+
'may be appropriate.',
488+
'description_content_type': None,
489+
'docs_url': None,
490+
'download_url': 'UNKNOWN',
491+
'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
492+
'dynamic': None,
493+
'home_page': 'https://github.com/pypa/sampleproject',
494+
'keywords': 'sample setuptools development',
495+
'license': 'MIT',
496+
'license_expression': None,
497+
'license_files': None,
498+
'maintainer': None,
499+
'maintainer_email': None,
500+
'name': 'sampleproject',
501+
'package_url': 'https://pypi.org/project/sampleproject/',
502+
'platform': 'UNKNOWN',
503+
'project_url': 'https://pypi.org/project/sampleproject/',
504+
'project_urls': {
505+
'Download': 'UNKNOWN',
506+
'Homepage': 'https://github.com/pypa/sampleproject',
507+
},
508+
'provides_extra': None,
509+
'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
510+
'requires_dist': None,
511+
'requires_python': None,
512+
'summary': 'A sample Python project',
513+
'version': '1.2.0',
514+
'yanked': False,
515+
'yanked_reason': None,
516+
}

Doc/whatsnew/3.15.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,16 @@ platform
15071507
(Contributed by Alexey Makridenko in :gh:`133604`.)
15081508

15091509

1510+
pprint
1511+
------
1512+
1513+
* Add an *expand* keyword argument for :func:`pprint.pprint`,
1514+
:func:`pprint.pformat`, :func:`pprint.pp`. If true, the output will be
1515+
formatted similar to pretty-printed :func:`json.dumps` when
1516+
*indent* is supplied.
1517+
(Contributed by Stefan Todoran and Semyon Moroz in :gh:`112632`.)
1518+
1519+
15101520
sre_*
15111521
-----
15121522

0 commit comments

Comments
 (0)