@@ -189,37 +189,105 @@ are converted to strings. The default implementation uses the internals of the
189189
190190.. _pprint-example :
191191
192- pprint Example
193- --------------
192+ Example
193+ -------
194194
195- This example demonstrates several uses of the :func: `pprint ` function and its
196- parameters.
195+ To demonstrate several uses of the :func: `pprint ` function and its parameters,
196+ let's fetch information about a package from PyPI::
197197
198+ >>> import json
198199 >>> import pprint
199- >>> tup = (' spam' , (' eggs' , (' lumberjack' , (' knights' , (' ni' , (' dead' ,
200- ... (' parrot' , (' fresh fruit' ,))))))))
201- >>> stuff = [' a' * 10 , tup, [' a' * 30 , ' b' * 30 ], [' c' * 20 , ' d' * 20 ]]
202- >>> pprint.pprint(stuff)
203- ['aaaaaaaaaa',
204- ('spam',
205- ('eggs',
206- ('lumberjack',
207- ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
208- ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
209- ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
210- >>> pprint.pprint(stuff, depth = 3 )
211- ['aaaaaaaaaa',
212- ('spam', ('eggs', (...))),
213- ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
214- ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
215- >>> pprint.pprint(stuff, width = 60 )
216- ['aaaaaaaaaa',
217- ('spam',
218- ('eggs',
219- ('lumberjack',
220- ('knights',
221- ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
222- ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
223- 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
224- ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
225-
200+ >>> from urllib.request import urlopen
201+ >>> with urlopen('http://pypi.python.org/pypi/configparser/json') as url:
202+ ... http_info = url.info()
203+ ... raw_data = url.read().decode(http_info.get_content_charset())
204+ >>> package_data = json.loads(raw_data)
205+ >>> result = {'headers': http_info.items(), 'body': package_data}
206+
207+ In its basic form, :func: `pprint ` shows the whole object::
208+
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"')]}
251+
252+ The result can be limited to a certain *depth * (ellipsis is used for deeper
253+ contents)::
254+
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"')]
0 commit comments