@@ -14,8 +14,8 @@ The :mod:`pprint` module provides a capability to "pretty-print" arbitrary
1414Python data structures in a form which can be used as input to the interpreter.
1515If the formatted structures include objects which are not fundamental Python
1616types, the representation may not be loadable. This may be the case if objects
17- such as files, sockets, classes, or instances are included, as well as many
18- other built-in objects which are not representable as Python constants .
17+ such as files, sockets or classes are included, as well as many other
18+ objects which are not representable as Python literals .
1919
2020The formatted representation keeps objects on a single line if it can, and
2121breaks them onto multiple lines if they don't fit within the allowed width.
@@ -65,7 +65,7 @@ The :mod:`pprint` module defines one class:
6565 ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
6666
6767
68- The :class: ` PrettyPrinter ` class supports several derivative functions:
68+ The :mod: ` pprint ` module also provides several shortcut functions:
6969
7070.. function :: pformat(object, indent=1, width=80, depth=None)
7171
@@ -193,101 +193,141 @@ Example
193193-------
194194
195195To demonstrate several uses of the :func: `pprint ` function and its parameters,
196- let's fetch information about a project from PyPI::
196+ let's fetch information about a project from ` PyPI < https://pypi.python.org >`_ ::
197197
198198 >>> import json
199199 >>> import pprint
200200 >>> from urllib.request import urlopen
201- >>> with urlopen('http://pypi.python.org/pypi/configparser /json') as url:
201+ >>> with urlopen('http://pypi.python.org/pypi/Twisted /json') as url:
202202 ... http_info = url.info()
203203 ... raw_data = url.read().decode(http_info.get_content_charset())
204204 >>> project_info = json.loads(raw_data)
205- >>> result = {'headers': http_info.items(), 'body': project_info}
206205
207206In its basic form, :func: `pprint ` shows the whole object::
208207
209- >>> pprint.pprint(result)
210- {'body': {'info': {'_pypi_hidden': False,
211- '_pypi_ordering': 12,
212- 'classifiers': ['Development Status :: 4 - Beta',
213- 'Intended Audience :: Developers',
214- 'License :: OSI Approved :: MIT License',
215- 'Natural Language :: English',
216- 'Operating System :: OS Independent',
217- 'Programming Language :: Python',
218- 'Programming Language :: Python :: 2',
219- 'Programming Language :: Python :: 2.6',
220- 'Programming Language :: Python :: 2.7',
221- 'Topic :: Software Development :: Libraries',
222- 'Topic :: Software Development :: Libraries :: Python Modules'],
223- 'download_url': 'UNKNOWN',
224- 'home_page': 'http://docs.python.org/py3k/library/configparser.html',
225- 'keywords': 'configparser ini parsing conf cfg configuration file',
226- 'license': 'MIT',
227- 'name': 'configparser',
228- 'package_url': 'http://pypi.python.org/pypi/configparser',
229- 'platform': 'any',
230- 'release_url': 'http://pypi.python.org/pypi/configparser/3.2.0r3',
231- 'requires_python': None,
232- 'stable_version': None,
233- 'summary': 'This library brings the updated configparser from Python 3.2+ to Python 2.6-2.7.',
234- 'version': '3.2.0r3'},
235- 'urls': [{'comment_text': '',
236- 'downloads': 47,
237- 'filename': 'configparser-3.2.0r3.tar.gz',
238- 'has_sig': False,
239- 'md5_digest': '8500fd87c61ac0de328fc996fce69b96',
240- 'packagetype': 'sdist',
241- 'python_version': 'source',
242- 'size': 32281,
243- 'upload_time': '2011-05-10T16:28:50',
244- 'url': 'http://pypi.python.org/packages/source/c/configparser/configparser-3.2.0r3.tar.gz'}]},
245- 'headers': [('Date', 'Sat, 14 May 2011 12:48:52 GMT'),
246- ('Server', 'Apache/2.2.16 (Debian)'),
247- ('Content-Disposition', 'inline'),
248- ('Connection', 'close'),
249- ('Transfer-Encoding', 'chunked'),
250- ('Content-Type', 'application/json; charset="UTF-8"')]}
208+ >>> pprint.pprint(project_info)
209+ {'info': {'_pypi_hidden': False,
210+ '_pypi_ordering': 125,
211+ 'author': 'Glyph Lefkowitz',
212+ 'author_email': '[email protected] ', 213+ 'bugtrack_url': '',
214+ 'cheesecake_code_kwalitee_id': None,
215+ 'cheesecake_documentation_id': None,
216+ 'cheesecake_installability_id': None,
217+ 'classifiers': ['Programming Language :: Python :: 2.6',
218+ 'Programming Language :: Python :: 2.7',
219+ 'Programming Language :: Python :: 2 :: Only'],
220+ 'description': 'An extensible framework for Python programming, '
221+ 'with special focus\r\n'
222+ 'on event-based network programming and '
223+ 'multiprotocol integration.',
224+ 'docs_url': '',
225+ 'download_url': 'UNKNOWN',
226+ 'home_page': 'http://twistedmatrix.com/',
227+ 'keywords': '',
228+ 'license': 'MIT',
229+ 'maintainer': '',
230+ 'maintainer_email': '',
231+ 'name': 'Twisted',
232+ 'package_url': 'http://pypi.python.org/pypi/Twisted',
233+ 'platform': 'UNKNOWN',
234+ 'release_url': 'http://pypi.python.org/pypi/Twisted/12.3.0',
235+ 'requires_python': None,
236+ 'stable_version': None,
237+ 'summary': 'An asynchronous networking framework written in Python',
238+ 'version': '12.3.0'},
239+ 'urls': [{'comment_text': '',
240+ 'downloads': 71844,
241+ 'filename': 'Twisted-12.3.0.tar.bz2',
242+ 'has_sig': False,
243+ 'md5_digest': '6e289825f3bf5591cfd670874cc0862d',
244+ 'packagetype': 'sdist',
245+ 'python_version': 'source',
246+ 'size': 2615733,
247+ 'upload_time': '2012-12-26T12:47:03',
248+ 'url': 'https://pypi.python.org/packages/source/T/Twisted/Twisted-12.3.0.tar.bz2'},
249+ {'comment_text': '',
250+ 'downloads': 5224,
251+ 'filename': 'Twisted-12.3.0.win32-py2.7.msi',
252+ 'has_sig': False,
253+ 'md5_digest': '6b778f5201b622a5519a2aca1a2fe512',
254+ 'packagetype': 'bdist_msi',
255+ 'python_version': '2.7',
256+ 'size': 2916352,
257+ 'upload_time': '2012-12-26T12:48:15',
258+ 'url': 'https://pypi.python.org/packages/2.7/T/Twisted/Twisted-12.3.0.win32-py2.7.msi'}]}
251259
252260The result can be limited to a certain *depth * (ellipsis is used for deeper
253261contents)::
254262
255- >>> pprint.pprint(result, depth=3)
256- {'body': {'info': {'_pypi_hidden': False,
257- '_pypi_ordering': 12,
258- 'classifiers': [...],
259- 'download_url': 'UNKNOWN',
260- 'home_page': 'http://docs.python.org/py3k/library/configparser.html',
261- 'keywords': 'configparser ini parsing conf cfg configuration file',
262- 'license': 'MIT',
263- 'name': 'configparser',
264- 'package_url': 'http://pypi.python.org/pypi/configparser',
265- 'platform': 'any',
266- 'release_url': 'http://pypi.python.org/pypi/configparser/3.2.0r3',
267- 'requires_python': None,
268- 'stable_version': None,
269- 'summary': 'This library brings the updated configparser from Python 3.2+ to Python 2.6-2.7.',
270- 'version': '3.2.0r3'},
271- 'urls': [{...}]},
272- 'headers': [('Date', 'Sat, 14 May 2011 12:48:52 GMT'),
273- ('Server', 'Apache/2.2.16 (Debian)'),
274- ('Content-Disposition', 'inline'),
275- ('Connection', 'close'),
276- ('Transfer-Encoding', 'chunked'),
277- ('Content-Type', 'application/json; charset="UTF-8"')]}
278-
279- Additionally, maximum *width * can be suggested. If a long object cannot be
280- split, the specified width will be exceeded::
281-
282- >>> pprint.pprint(result['headers'], width=30)
283- [('Date',
284- 'Sat, 14 May 2011 12:48:52 GMT'),
285- ('Server',
286- 'Apache/2.2.16 (Debian)'),
287- ('Content-Disposition',
288- 'inline'),
289- ('Connection', 'close'),
290- ('Transfer-Encoding',
291- 'chunked'),
292- ('Content-Type',
293- 'application/json; charset="UTF-8"')]
263+ >>> pprint.pprint(project_info, depth=2)
264+ {'info': {'_pypi_hidden': False,
265+ '_pypi_ordering': 125,
266+ 'author': 'Glyph Lefkowitz',
267+ 'author_email': '[email protected] ', 268+ 'bugtrack_url': '',
269+ 'cheesecake_code_kwalitee_id': None,
270+ 'cheesecake_documentation_id': None,
271+ 'cheesecake_installability_id': None,
272+ 'classifiers': [...],
273+ 'description': 'An extensible framework for Python programming, '
274+ 'with special focus\r\n'
275+ 'on event-based network programming and '
276+ 'multiprotocol integration.',
277+ 'docs_url': '',
278+ 'download_url': 'UNKNOWN',
279+ 'home_page': 'http://twistedmatrix.com/',
280+ 'keywords': '',
281+ 'license': 'MIT',
282+ 'maintainer': '',
283+ 'maintainer_email': '',
284+ 'name': 'Twisted',
285+ 'package_url': 'http://pypi.python.org/pypi/Twisted',
286+ 'platform': 'UNKNOWN',
287+ 'release_url': 'http://pypi.python.org/pypi/Twisted/12.3.0',
288+ 'requires_python': None,
289+ 'stable_version': None,
290+ 'summary': 'An asynchronous networking framework written in Python',
291+ 'version': '12.3.0'},
292+ 'urls': [{...}, {...}]}
293+
294+ Additionally, maximum character *width * can be suggested. If a long object
295+ cannot be split, the specified width will be exceeded::
296+
297+ >>> pprint.pprint(project_info, depth=2, width=50)
298+ {'info': {'_pypi_hidden': False,
299+ '_pypi_ordering': 125,
300+ 'author': 'Glyph Lefkowitz',
301+ 'author_email': '[email protected] ', 302+ 'bugtrack_url': '',
303+ 'cheesecake_code_kwalitee_id': None,
304+ 'cheesecake_documentation_id': None,
305+ 'cheesecake_installability_id': None,
306+ 'classifiers': [...],
307+ 'description': 'An extensible '
308+ 'framework for '
309+ 'Python programming, '
310+ 'with special '
311+ 'focus\r\n'
312+ 'on event-based '
313+ 'network programming '
314+ 'and multiprotocol '
315+ 'integration.',
316+ 'docs_url': '',
317+ 'download_url': 'UNKNOWN',
318+ 'home_page': 'http://twistedmatrix.com/',
319+ 'keywords': '',
320+ 'license': 'MIT',
321+ 'maintainer': '',
322+ 'maintainer_email': '',
323+ 'name': 'Twisted',
324+ 'package_url': 'http://pypi.python.org/pypi/Twisted',
325+ 'platform': 'UNKNOWN',
326+ 'release_url': 'http://pypi.python.org/pypi/Twisted/12.3.0',
327+ 'requires_python': None,
328+ 'stable_version': None,
329+ 'summary': 'An asynchronous '
330+ 'networking framework '
331+ 'written in Python',
332+ 'version': '12.3.0'},
333+ 'urls': [{...}, {...}]}
0 commit comments