-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-112632 : Added an option for block formatting to pprint
#129274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b9baf94
f5fcbfc
31a4d1e
a92ef3d
d9a147f
ffef3b0
19e4e08
8ef085f
c446aa1
f9bd6ef
49b1108
62dd5e5
0b8fd2b
5f08960
ac71fc4
59ed5fd
cbc392d
d1af62c
283ed38
142b92b
2273be2
d8b5942
8c3ed5c
9c7afd5
4e7fbff
5abab85
5e8a624
5ffbd0d
fa4cdac
6bffc7e
c8ce4ff
879f1da
21c9fa6
815e1da
9d12a8a
a7c26dd
37312d9
e3b09fa
712b644
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -36,7 +36,8 @@ Functions | |||||||||
--------- | ||||||||||
|
||||||||||
.. function:: pp(object, stream=None, indent=1, width=80, depth=None, *, \ | ||||||||||
compact=False, sort_dicts=False, underscore_numbers=False) | ||||||||||
compact=False, sort_dicts=False, underscore_numbers=False, \ | ||||||||||
block_style=False) | ||||||||||
|
||||||||||
Prints the formatted representation of *object*, followed by a newline. | ||||||||||
This function may be used in the interactive interpreter | ||||||||||
|
@@ -85,6 +86,12 @@ Functions | |||||||||
integers will be formatted with the ``_`` character for a thousands separator, | ||||||||||
otherwise underscores are not displayed (the default). | ||||||||||
|
||||||||||
:param bool block_style: | ||||||||||
If ``True``, | ||||||||||
opening parentheses and brackets will be followed by a newline and the | ||||||||||
following content will be indented by one level, similar to block style | ||||||||||
JSON formatting. This option is not compatible with *compact*. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
A passing thought: Perhaps introduce a mode (str) argument, and deprecate compact in favour of it? It's not great to have two conflicting Booleans, though it may be the best option for now. |
||||||||||
|
||||||||||
>>> import pprint | ||||||||||
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] | ||||||||||
>>> stuff.insert(0, stuff) | ||||||||||
|
@@ -100,18 +107,20 @@ Functions | |||||||||
|
||||||||||
|
||||||||||
.. 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, block_style=False) | ||||||||||
Comment on lines
+110
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise
Suggested change
|
||||||||||
|
||||||||||
Alias for :func:`~pprint.pp` with *sort_dicts* set to ``True`` by default, | ||||||||||
which would automatically sort the dictionaries' keys, | ||||||||||
you might want to use :func:`~pprint.pp` instead where it is ``False`` by default. | ||||||||||
|
||||||||||
|
||||||||||
.. 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, block_style=False) | ||||||||||
Comment on lines
+119
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise
Suggested change
|
||||||||||
|
||||||||||
Return the formatted representation of *object* as a string. *indent*, | ||||||||||
*width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* are | ||||||||||
*width*, *depth*, *compact*, *sort_dicts*, *underscore_numbers* and *block_style* are | ||||||||||
passed to the :class:`PrettyPrinter` constructor as formatting parameters | ||||||||||
and their meanings are as described in the documentation above. | ||||||||||
|
||||||||||
|
@@ -155,7 +164,8 @@ PrettyPrinter Objects | |||||||||
.. index:: single: ...; placeholder | ||||||||||
|
||||||||||
.. 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, block_style=False) | ||||||||||
Comment on lines
+167
to
+168
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise
Suggested change
|
||||||||||
|
||||||||||
Construct a :class:`PrettyPrinter` instance. | ||||||||||
|
||||||||||
|
@@ -179,6 +189,22 @@ PrettyPrinter Objects | |||||||||
'knights', 'ni'], | ||||||||||
'spam', 'eggs', 'lumberjack', 'knights', | ||||||||||
'ni'] | ||||||||||
>>> pp = pprint.PrettyPrinter(width=41, block_style=True, indent=3) | ||||||||||
>>> 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) | ||||||||||
|
@@ -198,6 +224,9 @@ PrettyPrinter Objects | |||||||||
.. versionchanged:: 3.11 | ||||||||||
No longer attempts to write to :data:`!sys.stdout` if it is ``None``. | ||||||||||
|
||||||||||
.. versionchanged:: next | ||||||||||
Added the *block_style* parameter. | ||||||||||
|
||||||||||
|
||||||||||
:class:`PrettyPrinter` instances have the following methods: | ||||||||||
|
||||||||||
|
@@ -420,3 +449,72 @@ cannot be split, the specified width will be exceeded:: | |||||||||
'requires_python': None, | ||||||||||
'summary': 'A sample Python project', | ||||||||||
'version': '1.2.0'} | ||||||||||
|
||||||||||
Lastly, we can achieve block style formatting with the *block_style* parameter. | ||||||||||
Best results are achieved with a higher *indent* value:: | ||||||||||
|
||||||||||
>>> pprint.pp(project_info, indent=4, block_style=True) | ||||||||||
{ | ||||||||||
'author': 'The Python Packaging Authority', | ||||||||||
'author_email': 'pypa-dev@googlegroups.com', | ||||||||||
'bugtrack_url': None, | ||||||||||
'classifiers': [ | ||||||||||
'Development Status :: 3 - Alpha', | ||||||||||
'Intended Audience :: Developers', | ||||||||||
'License :: OSI Approved :: MIT License', | ||||||||||
'Programming Language :: Python :: 2', | ||||||||||
'Programming Language :: Python :: 2.6', | ||||||||||
'Programming Language :: Python :: 2.7', | ||||||||||
'Programming Language :: Python :: 3', | ||||||||||
'Programming Language :: Python :: 3.2', | ||||||||||
'Programming Language :: Python :: 3.3', | ||||||||||
'Programming Language :: Python :: 3.4', | ||||||||||
'Topic :: Software Development :: Build Tools' | ||||||||||
], | ||||||||||
'description': 'A sample Python project\n' | ||||||||||
'=======================\n' | ||||||||||
'\n' | ||||||||||
'This is the description file for the project.\n' | ||||||||||
'\n' | ||||||||||
'The file should use UTF-8 encoding and be written using ReStructured ' | ||||||||||
'Text. It\n' | ||||||||||
'will be used to generate the project webpage on PyPI, and should be ' | ||||||||||
'written for\n' | ||||||||||
'that purpose.\n' | ||||||||||
'\n' | ||||||||||
'Typical contents for this file would include an overview of the project, ' | ||||||||||
'basic\n' | ||||||||||
'usage examples, etc. Generally, including the project changelog in here ' | ||||||||||
'is not\n' | ||||||||||
'a good idea, although a simple "What\'s New" section for the most recent ' | ||||||||||
'version\n' | ||||||||||
'may be appropriate.', | ||||||||||
'description_content_type': None, | ||||||||||
'docs_url': None, | ||||||||||
'download_url': 'UNKNOWN', | ||||||||||
'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1}, | ||||||||||
'dynamic': None, | ||||||||||
'home_page': 'https://github.com/pypa/sampleproject', | ||||||||||
'keywords': 'sample setuptools development', | ||||||||||
'license': 'MIT', | ||||||||||
'license_expression': None, | ||||||||||
'license_files': None, | ||||||||||
'maintainer': None, | ||||||||||
'maintainer_email': None, | ||||||||||
'name': 'sampleproject', | ||||||||||
'package_url': 'https://pypi.org/project/sampleproject/', | ||||||||||
'platform': 'UNKNOWN', | ||||||||||
'project_url': 'https://pypi.org/project/sampleproject/', | ||||||||||
'project_urls': { | ||||||||||
'Download': 'UNKNOWN', | ||||||||||
'Homepage': 'https://github.com/pypa/sampleproject' | ||||||||||
}, | ||||||||||
'provides_extra': None, | ||||||||||
'release_url': 'https://pypi.org/project/sampleproject/1.2.0/', | ||||||||||
'requires_dist': None, | ||||||||||
'requires_python': None, | ||||||||||
'summary': 'A sample Python project', | ||||||||||
'version': '1.2.0', | ||||||||||
'yanked': False, | ||||||||||
'yanked_reason': None | ||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -888,6 +888,16 @@ platform | |||||
(Contributed by Bénédikt Tran in :gh:`122549`.) | ||||||
|
||||||
|
||||||
pprint | ||||||
------ | ||||||
|
||||||
* Add a *block_style* keyword argument for :func:`pprint.pprint`, | ||||||
:func:`pprint.pformat`, :func:`pprint.pp`. If true, the output will be | ||||||
formatted in a block style similar to pretty-printed :func:`json.dumps` when | ||||||
*indent* is supplied. | ||||||
(Contributed by Stefan Todoran in :gh:`129274`.) | ||||||
StefanTodoran marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
|
||||||
pydoc | ||||||
----- | ||||||
|
||||||
|
@@ -1200,7 +1210,7 @@ Deprecated | |||||
.. include:: ../deprecations/pending-removal-in-future.rst | ||||||
|
||||||
Removed | ||||||
======= | ||||||
======== | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
argparse | ||||||
-------- | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are keyword-only (after
*
), so we have the luxury of alphabetical sorting: