@@ -188,36 +188,105 @@ are converted to strings. The default implementation uses the internals of the
188188
189189.. _pprint-example :
190190
191- pprint Example
192- --------------
191+ Example
192+ -------
193193
194- This example demonstrates several uses of the :func: `pprint ` function and its parameters.
194+ To demonstrate several uses of the :func: `pprint ` function and its parameters,
195+ let's fetch information about a package from PyPI::
195196
197+ >>> import json
196198 >>> import pprint
197- >>> tup = (' spam' , (' eggs' , (' lumberjack' , (' knights' , (' ni' , (' dead' ,
198- ... (' parrot' , (' fresh fruit' ,))))))))
199- >>> stuff = [' a' * 10 , tup, [' a' * 30 , ' b' * 30 ], [' c' * 20 , ' d' * 20 ]]
200- >>> pprint.pprint(stuff)
201- ['aaaaaaaaaa',
202- ('spam',
203- ('eggs',
204- ('lumberjack',
205- ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
206- ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
207- ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
208- >>> pprint.pprint(stuff, depth = 3 )
209- ['aaaaaaaaaa',
210- ('spam', ('eggs', (...))),
211- ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
212- ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
213- >>> pprint.pprint(stuff, width = 60 )
214- ['aaaaaaaaaa',
215- ('spam',
216- ('eggs',
217- ('lumberjack',
218- ('knights',
219- ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
220- ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
221- 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
222- ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
223-
199+ >>> from urllib.request import urlopen
200+ >>> with urlopen('http://pypi.python.org/pypi/configparser/json') as url:
201+ ... http_info = url.info()
202+ ... raw_data = url.read().decode(http_info.get_content_charset())
203+ >>> package_data = json.loads(raw_data)
204+ >>> result = {'headers': http_info.items(), 'body': package_data}
205+
206+ In its basic form, :func: `pprint ` shows the whole object::
207+
208+ >>> pprint.pprint(result)
209+ {'body': {'info': {'_pypi_hidden': False,
210+ '_pypi_ordering': 12,
211+ 'classifiers': ['Development Status :: 4 - Beta',
212+ 'Intended Audience :: Developers',
213+ 'License :: OSI Approved :: MIT License',
214+ 'Natural Language :: English',
215+ 'Operating System :: OS Independent',
216+ 'Programming Language :: Python',
217+ 'Programming Language :: Python :: 2',
218+ 'Programming Language :: Python :: 2.6',
219+ 'Programming Language :: Python :: 2.7',
220+ 'Topic :: Software Development :: Libraries',
221+ 'Topic :: Software Development :: Libraries :: Python Modules'],
222+ 'download_url': 'UNKNOWN',
223+ 'home_page': 'http://docs.python.org/py3k/library/configparser.html',
224+ 'keywords': 'configparser ini parsing conf cfg configuration file',
225+ 'license': 'MIT',
226+ 'name': 'configparser',
227+ 'package_url': 'http://pypi.python.org/pypi/configparser',
228+ 'platform': 'any',
229+ 'release_url': 'http://pypi.python.org/pypi/configparser/3.2.0r3',
230+ 'requires_python': None,
231+ 'stable_version': None,
232+ 'summary': 'This library brings the updated configparser from Python 3.2+ to Python 2.6-2.7.',
233+ 'version': '3.2.0r3'},
234+ 'urls': [{'comment_text': '',
235+ 'downloads': 47,
236+ 'filename': 'configparser-3.2.0r3.tar.gz',
237+ 'has_sig': False,
238+ 'md5_digest': '8500fd87c61ac0de328fc996fce69b96',
239+ 'packagetype': 'sdist',
240+ 'python_version': 'source',
241+ 'size': 32281,
242+ 'upload_time': '2011-05-10T16:28:50',
243+ 'url': 'http://pypi.python.org/packages/source/c/configparser/configparser-3.2.0r3.tar.gz'}]},
244+ 'headers': [('Date', 'Sat, 14 May 2011 12:48:52 GMT'),
245+ ('Server', 'Apache/2.2.16 (Debian)'),
246+ ('Content-Disposition', 'inline'),
247+ ('Connection', 'close'),
248+ ('Transfer-Encoding', 'chunked'),
249+ ('Content-Type', 'application/json; charset="UTF-8"')]}
250+
251+ The result can be limited to a certain *depth * (ellipsis is used for deeper
252+ contents)::
253+
254+ >>> pprint.pprint(result, depth=3)
255+ {'body': {'info': {'_pypi_hidden': False,
256+ '_pypi_ordering': 12,
257+ 'classifiers': [...],
258+ 'download_url': 'UNKNOWN',
259+ 'home_page': 'http://docs.python.org/py3k/library/configparser.html',
260+ 'keywords': 'configparser ini parsing conf cfg configuration file',
261+ 'license': 'MIT',
262+ 'name': 'configparser',
263+ 'package_url': 'http://pypi.python.org/pypi/configparser',
264+ 'platform': 'any',
265+ 'release_url': 'http://pypi.python.org/pypi/configparser/3.2.0r3',
266+ 'requires_python': None,
267+ 'stable_version': None,
268+ 'summary': 'This library brings the updated configparser from Python 3.2+ to Python 2.6-2.7.',
269+ 'version': '3.2.0r3'},
270+ 'urls': [{...}]},
271+ 'headers': [('Date', 'Sat, 14 May 2011 12:48:52 GMT'),
272+ ('Server', 'Apache/2.2.16 (Debian)'),
273+ ('Content-Disposition', 'inline'),
274+ ('Connection', 'close'),
275+ ('Transfer-Encoding', 'chunked'),
276+ ('Content-Type', 'application/json; charset="UTF-8"')]}
277+
278+ Additionally, maximum *width * can be suggested. If a long object cannot be
279+ split, the specified width will be exceeded::
280+
281+ >>> pprint.pprint(result['headers'], width=30)
282+ [('Date',
283+ 'Sat, 14 May 2011 12:48:52 GMT'),
284+ ('Server',
285+ 'Apache/2.2.16 (Debian)'),
286+ ('Content-Disposition',
287+ 'inline'),
288+ ('Connection', 'close'),
289+ ('Transfer-Encoding',
290+ 'chunked'),
291+ ('Content-Type',
292+ 'application/json; charset="UTF-8"')]
0 commit comments