-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Open
StefanTodoran
wants to merge
39
commits into
python:main
Choose a base branch
from
StefanTodoran:fix-issue-112632
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
b9baf94
Added an option for block formatting to pprint
stodoran f5fcbfc
Small bug fix for indent=1, added to unit test
StefanTodoran 31a4d1e
Merge branch 'main' into fix-issue-112632
StefanTodoran a92ef3d
Remove set from block style unit test (since order kept changing)
StefanTodoran d9a147f
📜🤖 Added by blurb_it.
blurb-it[bot] ffef3b0
Merge branch 'main' into fix-issue-112632
StefanTodoran 19e4e08
Fix params in news entry
StefanTodoran 8ef085f
Fix line length for news entry
StefanTodoran c446aa1
Shorten news entry
StefanTodoran f9bd6ef
Merge branch 'main' into fix-issue-112632
StefanTodoran 49b1108
Address documentation related PR comments
StefanTodoran 62dd5e5
Merge branch 'main' into fix-issue-112632
StefanTodoran 0b8fd2b
Fix length of title underline :/
StefanTodoran 5f08960
Fix test case, remove unnecessary recursion on dataclass (we aren't t…
StefanTodoran ac71fc4
Merge branch 'main' into fix-issue-112632
StefanTodoran 59ed5fd
Update Doc/library/pprint.rst
StefanTodoran cbc392d
Update Doc/library/pprint.rst
StefanTodoran d1af62c
Update Doc/library/pprint.rst
StefanTodoran 283ed38
Update Doc/library/pprint.rst
StefanTodoran 142b92b
Update Doc/library/pprint.rst
StefanTodoran 2273be2
Update Doc/whatsnew/3.14.rst
StefanTodoran d8b5942
Update Lib/pprint.py
StefanTodoran 8c3ed5c
Update Lib/pprint.py
StefanTodoran 9c7afd5
Update Lib/pprint.py
StefanTodoran 4e7fbff
Update Lib/pprint.py
StefanTodoran 5abab85
Update Lib/pprint.py
StefanTodoran 5e8a624
Update Lib/pprint.py
StefanTodoran 5ffbd0d
Update Lib/test/test_pprint.py
StefanTodoran fa4cdac
Update Lib/test/test_pprint.py
StefanTodoran 6bffc7e
Update Lib/test/test_pprint.py
StefanTodoran c8ce4ff
Update Lib/test/test_pprint.py
StefanTodoran 879f1da
Update Lib/test/test_pprint.py
StefanTodoran 21c9fa6
Update Lib/test/test_pprint.py
StefanTodoran 815e1da
Update Lib/test/test_pprint.py
StefanTodoran 9d12a8a
Merge branch 'main' into fix-issue-112632
StefanTodoran a7c26dd
Apply suggestions from code review
StefanTodoran 37312d9
Split large test case into multiple smaller ones, update news entry
StefanTodoran e3b09fa
Merge branch 'main' into fix-issue-112632
StefanTodoran 712b644
Merge changes
StefanTodoran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ 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 +85,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 +106,18 @@ 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) | ||
StefanTodoran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) | ||
StefanTodoran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 +161,7 @@ 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) | ||
StefanTodoran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Construct a :class:`PrettyPrinter` instance. | ||
|
||
|
@@ -179,6 +185,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 +220,9 @@ PrettyPrinter Objects | |
.. versionchanged:: 3.11 | ||
No longer attempts to write to :data:`!sys.stdout` if it is ``None``. | ||
|
||
.. versionchanged:: 3.13 | ||
StefanTodoran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Added the *block_style* parameter. | ||
|
||
|
||
:class:`PrettyPrinter` instances have the following methods: | ||
|
||
|
@@ -420,3 +445,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:: | ||
StefanTodoran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
>>> 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 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.