开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

source-code-analysis/python3.8.1

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
python3.8.1
/
Doc
/
library
/
pprint.rst
python3.8.1
/
Doc
/
library
/
pprint.rst
pprint.rst 15.63 KB
一键复制 编辑 原始数据 按行查看 历史
zhangweibo 提交于 2021年11月16日 09:46 +08:00 . git init

:mod:`pprint` --- Data pretty printer

.. module:: pprint
 :synopsis: Data pretty printer.

.. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>

Source code: :source:`Lib/pprint.py`


The :mod:`pprint` module provides a capability to "pretty-print" arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted structures include objects which are not fundamental Python types, the representation may not be loadable. This may be the case if objects such as files, sockets or classes are included, as well as many other objects which are not representable as Python literals.

The formatted representation keeps objects on a single line if it can, and breaks them onto multiple lines if they don't fit within the allowed width. Construct :class:`PrettyPrinter` objects explicitly if you need to adjust the width constraint.

Dictionaries are sorted by key before the display is computed.

The :mod:`pprint` module defines one class:

.. index:: single: ...; placeholder

The :mod:`pprint` module also provides several shortcut functions:

.. function:: pformat(object, indent=1, width=80, depth=None, *, \
 compact=False, sort_dicts=True)

 Return the formatted representation of *object* as a string. *indent*,
 *width*, *depth*, *compact* and *sort_dicts* will be passed to the
 :class:`PrettyPrinter` constructor as formatting parameters.

 .. versionchanged:: 3.4
 Added the *compact* parameter.

 .. versionchanged:: 3.8
 Added the *sort_dicts* parameter.


.. function:: pp(object, *args, sort_dicts=False, **kwargs)

 Prints the formatted representation of *object* followed by a newline.
 If *sort_dicts* is false (the default), dictionaries will be displayed with
 their keys in insertion order, otherwise the dict keys will be sorted.
 *args* and *kwargs* will be passed to :func:`pprint` as formatting
 parameters.

 .. versionadded:: 3.8


.. function:: pprint(object, stream=None, indent=1, width=80, depth=None, *, \
 compact=False, sort_dicts=True)

 Prints the formatted representation of *object* on *stream*, followed by a
 newline. If *stream* is ``None``, ``sys.stdout`` is used. This may be used
 in the interactive interpreter instead of the :func:`print` function for
 inspecting values (you can even reassign ``print = pprint.pprint`` for use
 within a scope). *indent*, *width*, *depth*, *compact* and *sort_dicts* will
 be passed to the :class:`PrettyPrinter` constructor as formatting parameters.

 .. versionchanged:: 3.4
 Added the *compact* parameter.

 .. versionchanged:: 3.8
 Added the *sort_dicts* parameter.

 >>> import pprint
 >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
 >>> stuff.insert(0, stuff)
 >>> pprint.pprint(stuff)
 [<Recursion on list with id=...>,
 'spam',
 'eggs',
 'lumberjack',
 'knights',
 'ni']


.. function:: isreadable(object)

 .. index:: builtin: eval

 Determine if the formatted representation of *object* is "readable," or can be
 used to reconstruct the value using :func:`eval`. This always returns ``False``
 for recursive objects.

 >>> pprint.isreadable(stuff)
 False


.. function:: isrecursive(object)

 Determine if *object* requires a recursive representation.


One more support function is also defined:

.. function:: saferepr(object)

 Return a string representation of *object*, protected against recursive data
 structures. If the representation of *object* exposes a recursive entry, the
 recursive reference will be represented as ``<Recursion on typename with
 id=number>``. The representation is not otherwise formatted.

 >>> pprint.saferepr(stuff)
 "[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"


PrettyPrinter Objects

:class:`PrettyPrinter` instances have the following methods:

.. method:: PrettyPrinter.pformat(object)

 Return the formatted representation of *object*. This takes into account the
 options passed to the :class:`PrettyPrinter` constructor.


.. method:: PrettyPrinter.pprint(object)

 Print the formatted representation of *object* on the configured stream,
 followed by a newline.

The following methods provide the implementations for the corresponding functions of the same names. Using these methods on an instance is slightly more efficient since new :class:`PrettyPrinter` objects don't need to be created.

.. method:: PrettyPrinter.isreadable(object)

 .. index:: builtin: eval

 Determine if the formatted representation of the object is "readable," or can be
 used to reconstruct the value using :func:`eval`. Note that this returns
 ``False`` for recursive objects. If the *depth* parameter of the
 :class:`PrettyPrinter` is set and the object is deeper than allowed, this
 returns ``False``.


.. method:: PrettyPrinter.isrecursive(object)

 Determine if the object requires a recursive representation.

This method is provided as a hook to allow subclasses to modify the way objects are converted to strings. The default implementation uses the internals of the :func:`saferepr` implementation.

.. method:: PrettyPrinter.format(object, context, maxlevels, level)

 Returns three values: the formatted version of *object* as a string, a flag
 indicating whether the result is readable, and a flag indicating whether
 recursion was detected. The first argument is the object to be presented. The
 second is a dictionary which contains the :func:`id` of objects that are part of
 the current presentation context (direct and indirect containers for *object*
 that are affecting the presentation) as the keys; if an object needs to be
 presented which is already represented in *context*, the third return value
 should be ``True``. Recursive calls to the :meth:`.format` method should add
 additional entries for containers to this dictionary. The third argument,
 *maxlevels*, gives the requested limit to recursion; this will be ``0`` if there
 is no requested limit. This argument should be passed unmodified to recursive
 calls. The fourth argument, *level*, gives the current level; recursive calls
 should be passed a value less than that of the current call.


Example

To demonstrate several uses of the :func:`pprint` function and its parameters, let's fetch information about a project from :func:`pprint` shows the whole object:

>>> pprint.pprint(project_info)
{'author': 'The Python Packaging Authority',
 'author_email': 'pypa-dev@googlegroups.com',
 'bugtrack_url': None,
 'classifiers': ['Development Status :: 3 - Alpha',
 'Intended Audience :: Developers',
 'License :: OSI Approved :: MIT License',
 'Programming Language :: Python :: 2',
 'Programming Language :: Python :: 2.6',
 'Programming Language :: Python :: 2.7',
 'Programming Language :: Python :: 3',
 'Programming Language :: Python :: 3.2',
 'Programming Language :: Python :: 3.3',
 'Programming Language :: Python :: 3.4',
 'Topic :: Software Development :: Build Tools'],
 'description': 'A sample Python project\n'
 '=======================\n'
 '\n'
 'This is the description file for the project.\n'
 '\n'
 'The file should use UTF-8 encoding and be written using '
 'ReStructured Text. It\n'
 'will be used to generate the project webpage on PyPI, and '
 'should be written for\n'
 'that purpose.\n'
 '\n'
 'Typical contents for this file would include an overview of '
 'the project, basic\n'
 'usage examples, etc. Generally, including the project '
 'changelog in here is not\n'
 'a good idea, although a simple "What\'s New" section for the '
 'most recent version\n'
 'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {'Download': 'UNKNOWN',
 'Homepage': 'https://github.com/pypa/sampleproject'},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}

The result can be limited to a certain depth (ellipsis is used for deeper contents):

>>> pprint.pprint(project_info, depth=1)
{'author': 'The Python Packaging Authority',
 'author_email': 'pypa-dev@googlegroups.com',
 'bugtrack_url': None,
 'classifiers': [...],
 'description': 'A sample Python project\n'
 '=======================\n'
 '\n'
 'This is the description file for the project.\n'
 '\n'
 'The file should use UTF-8 encoding and be written using '
 'ReStructured Text. It\n'
 'will be used to generate the project webpage on PyPI, and '
 'should be written for\n'
 'that purpose.\n'
 '\n'
 'Typical contents for this file would include an overview of '
 'the project, basic\n'
 'usage examples, etc. Generally, including the project '
 'changelog in here is not\n'
 'a good idea, although a simple "What\'s New" section for the '
 'most recent version\n'
 'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {...},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {...},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}

Additionally, maximum character width can be suggested. If a long object cannot be split, the specified width will be exceeded:

>>> pprint.pprint(project_info, depth=1, width=60)
{'author': 'The Python Packaging Authority',
 'author_email': 'pypa-dev@googlegroups.com',
 'bugtrack_url': None,
 'classifiers': [...],
 'description': 'A sample Python project\n'
 '=======================\n'
 '\n'
 'This is the description file for the '
 'project.\n'
 '\n'
 'The file should use UTF-8 encoding and be '
 'written using ReStructured Text. It\n'
 'will be used to generate the project '
 'webpage on PyPI, and should be written '
 'for\n'
 'that purpose.\n'
 '\n'
 'Typical contents for this file would '
 'include an overview of the project, '
 'basic\n'
 'usage examples, etc. Generally, including '
 'the project changelog in here is not\n'
 'a good idea, although a simple "What\'s '
 'New" section for the most recent version\n'
 'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {...},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {...},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python_sourcecode/python3.8.1.git
git@gitee.com:python_sourcecode/python3.8.1.git
python_sourcecode
python3.8.1
python3.8.1
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /