Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
84aacf89
Kaydet (Commit)
84aacf89
authored
May 14, 2011
tarafından
Kurt B. Kaiser
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Sade Fark
Merge heads
üst
dfbe1592
4ad78ab7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
99 additions
and
30 deletions
+99
-30
pprint.rst
Doc/library/pprint.rst
+99
-30
No files found.
Doc/library/pprint.rst
Dosyayı görüntüle @
84aacf89
...
@@ -188,36 +188,105 @@ are converted to strings. The default implementation uses the internals of the
...
@@ -188,36 +188,105 @@ are converted to strings. The default implementation uses the internals of the
.. _pprint-example:
.. _pprint-example:
pprint
Example
Example
-------
-------
-------
This example demonstrates several uses of the :func:`pprint` function and its parameters.
To demonstrate several uses of the :func:`pprint` function and its parameters,
let's fetch information about a package from PyPI::
>>> import json
>>> import pprint
>>> import pprint
>>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
>>> from urllib.request import urlopen
... ('parrot', ('fresh fruit',))))))))
>>> with urlopen('http://pypi.python.org/pypi/configparser/json') as url:
>>> stuff = ['a' * 10, tup, ['a' * 30, 'b' * 30], ['c' * 20, 'd' * 20]]
... http_info = url.info()
>>> pprint.pprint(stuff)
... raw_data = url.read().decode(http_info.get_content_charset())
['aaaaaaaaaa',
>>> package_data = json.loads(raw_data)
('spam',
>>> result = {'headers': http_info.items(), 'body': package_data}
('eggs',
('lumberjack',
In its basic form, :func:`pprint` shows the whole object::
('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
>>> pprint.pprint(result)
['cccccccccccccccccccc', 'dddddddddddddddddddd']]
{'body': {'info': {'_pypi_hidden': False,
>>> pprint.pprint(stuff, depth=3)
'_pypi_ordering': 12,
['aaaaaaaaaa',
'classifiers': ['Development Status :: 4 - Beta',
('spam', ('eggs', (...))),
'Intended Audience :: Developers',
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
'License :: OSI Approved :: MIT License',
['cccccccccccccccccccc', 'dddddddddddddddddddd']]
'Natural Language :: English',
>>> pprint.pprint(stuff, width=60)
'Operating System :: OS Independent',
['aaaaaaaaaa',
'Programming Language :: Python',
('spam',
'Programming Language :: Python :: 2',
('eggs',
'Programming Language :: Python :: 2.6',
('lumberjack',
'Programming Language :: Python :: 2.7',
('knights',
'Topic :: Software Development :: Libraries',
('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
'Topic :: Software Development :: Libraries :: Python Modules'],
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'download_url': 'UNKNOWN',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
'home_page': 'http://docs.python.org/py3k/library/configparser.html',
['cccccccccccccccccccc', 'dddddddddddddddddddd']]
'keywords': 'configparser ini parsing conf cfg configuration file',
'license': 'MIT',
'name': 'configparser',
'package_url': 'http://pypi.python.org/pypi/configparser',
'platform': 'any',
'release_url': 'http://pypi.python.org/pypi/configparser/3.2.0r3',
'requires_python': None,
'stable_version': None,
'summary': 'This library brings the updated configparser from Python 3.2+ to Python 2.6-2.7.',
'version': '3.2.0r3'},
'urls': [{'comment_text': '',
'downloads': 47,
'filename': 'configparser-3.2.0r3.tar.gz',
'has_sig': False,
'md5_digest': '8500fd87c61ac0de328fc996fce69b96',
'packagetype': 'sdist',
'python_version': 'source',
'size': 32281,
'upload_time': '2011-05-10T16:28:50',
'url': 'http://pypi.python.org/packages/source/c/configparser/configparser-3.2.0r3.tar.gz'}]},
'headers': [('Date', 'Sat, 14 May 2011 12:48:52 GMT'),
('Server', 'Apache/2.2.16 (Debian)'),
('Content-Disposition', 'inline'),
('Connection', 'close'),
('Transfer-Encoding', 'chunked'),
('Content-Type', 'application/json; charset="UTF-8"')]}
The result can be limited to a certain *depth* (ellipsis is used for deeper
contents)::
>>> pprint.pprint(result, depth=3)
{'body': {'info': {'_pypi_hidden': False,
'_pypi_ordering': 12,
'classifiers': [...],
'download_url': 'UNKNOWN',
'home_page': 'http://docs.python.org/py3k/library/configparser.html',
'keywords': 'configparser ini parsing conf cfg configuration file',
'license': 'MIT',
'name': 'configparser',
'package_url': 'http://pypi.python.org/pypi/configparser',
'platform': 'any',
'release_url': 'http://pypi.python.org/pypi/configparser/3.2.0r3',
'requires_python': None,
'stable_version': None,
'summary': 'This library brings the updated configparser from Python 3.2+ to Python 2.6-2.7.',
'version': '3.2.0r3'},
'urls': [{...}]},
'headers': [('Date', 'Sat, 14 May 2011 12:48:52 GMT'),
('Server', 'Apache/2.2.16 (Debian)'),
('Content-Disposition', 'inline'),
('Connection', 'close'),
('Transfer-Encoding', 'chunked'),
('Content-Type', 'application/json; charset="UTF-8"')]}
Additionally, maximum *width* can be suggested. If a long object cannot be
split, the specified width will be exceeded::
>>> pprint.pprint(result['headers'], width=30)
[('Date',
'Sat, 14 May 2011 12:48:52 GMT'),
('Server',
'Apache/2.2.16 (Debian)'),
('Content-Disposition',
'inline'),
('Connection', 'close'),
('Transfer-Encoding',
'chunked'),
('Content-Type',
'application/json; charset="UTF-8"')]
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment