@@ -36,7 +36,7 @@ Functions
3636---------
3737
3838.. function :: pp(object, stream=None, indent=1, width=80, depth=None, *, \
39- compact=False, sort_dicts=False, underscore_numbers=False)
39+ compact=False, sort_dicts=False, underscore_numbers=False, block_style=False )
4040
4141 Prints the formatted representation of *object *, followed by a newline.
4242 This function may be used in the interactive interpreter
@@ -85,6 +85,12 @@ Functions
8585 integers will be formatted with the ``_ `` character for a thousands separator,
8686 otherwise underscores are not displayed (the default).
8787
88+ :param bool block_style:
89+ If ``True ``,
90+ opening parentheses and brackets will be followed by a newline and the
91+ following content will be indented by one level, similar to block style
92+ JSON formatting. This option is not compatible with *compact *.
93+
8894 >>> import pprint
8995 >>> stuff = [' spam' , ' eggs' , ' lumberjack' , ' knights' , ' ni' ]
9096 >>> stuff.insert(0 , stuff)
@@ -100,18 +106,18 @@ Functions
100106
101107
102108.. function :: pprint(object, stream=None, indent=1, width=80, depth=None, *, \
103- compact=False, sort_dicts=True, underscore_numbers=False)
109+ compact=False, sort_dicts=True, underscore_numbers=False, block_style=False )
104110
105111 Alias for :func: `~pprint.pp ` with *sort_dicts * set to ``True `` by default,
106112 which would automatically sort the dictionaries' keys,
107113 you might want to use :func: `~pprint.pp ` instead where it is ``False `` by default.
108114
109115
110116.. function :: pformat(object, indent=1, width=80, depth=None, *, \
111- compact=False, sort_dicts=True, underscore_numbers=False)
117+ compact=False, sort_dicts=True, underscore_numbers=False, block_style=False )
112118
113119 Return the formatted representation of *object * as a string. *indent *,
114- *width *, *depth *, *compact *, *sort_dicts * and *underscore_numbers * are
120+ *width *, *depth *, *compact *, *sort_dicts *, * underscore_numbers * and *block_style * are
115121 passed to the :class: `PrettyPrinter ` constructor as formatting parameters
116122 and their meanings are as described in the documentation above.
117123
@@ -155,7 +161,7 @@ PrettyPrinter Objects
155161.. index :: single: ...; placeholder
156162
157163.. class :: PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, \
158- compact=False, sort_dicts=True, underscore_numbers=False)
164+ compact=False, sort_dicts=True, underscore_numbers=False, block_style=False )
159165
160166 Construct a :class: `PrettyPrinter ` instance.
161167
@@ -179,6 +185,22 @@ PrettyPrinter Objects
179185 'knights', 'ni'],
180186 'spam', 'eggs', 'lumberjack', 'knights',
181187 'ni']
188+ >>> pp = pprint.PrettyPrinter(width = 41 , block_style = 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+ ]
182204 >>> tup = (' spam' , (' eggs' , (' lumberjack' , (' knights' , (' ni' , (' dead' ,
183205 ... (' parrot' , (' fresh fruit' ,))))))))
184206 >>> pp = pprint.PrettyPrinter(depth = 6 )
@@ -198,6 +220,9 @@ PrettyPrinter Objects
198220 .. versionchanged :: 3.11
199221 No longer attempts to write to :data: `!sys.stdout ` if it is ``None ``.
200222
223+ .. versionchanged :: 3.13
224+ Added the *block_style * parameter.
225+
201226
202227:class: `PrettyPrinter ` instances have the following methods:
203228
@@ -420,3 +445,72 @@ cannot be split, the specified width will be exceeded::
420445 'requires_python': None,
421446 'summary': 'A sample Python project',
422447 'version': '1.2.0'}
448+
449+ Lastly, we can achieve block style formatting with the *block_style * parameter. Best results
450+ are achieved with a higher *indent * value::
451+
452+ >>> pprint.pp(project_info, indent=4, block_style=True)
453+ {
454+ 'author': 'The Python Packaging Authority',
455+ 'author_email': '[email protected] ', 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+ }
0 commit comments