跳转到主要内容

将文档字符串格式化为遵循PEP 257

项目描述

代码

BLACK ISORT

文档字符串

SELF NUMPSTYLE

GitHub

CI CONTRIBUTORS COMMIT PRE

PyPi

VERSION LICENSE PYVERS PYMAT DD

格式化文档字符串以遵循PEP 257

特性

docformatter 会自动将文档字符串格式化为遵循PEP 257约定的一个子集。以下是从PEP 257中引用的相关项目。

  • 为了保持一致性,始终使用三重双引号包围文档字符串。

  • 即使字符串可以放在一行内,也使用三引号。

  • 多行文档字符串与单行文档字符串类似,包含一个总结行,接着是一个空行,然后是一个更详细的描述。

  • 除非整个文档字符串可以放在一行,否则将结束引号放在单独的一行上。

docformatter 还处理了一些 PEP 8 的约定。

  • 不要编写依赖于显著尾随空白的字符串字面量。这种尾随空白在视觉上无法区分,并且一些编辑器(或更近期的 reindent.py)会将其删除。

docformatter 当传递 --black 选项时,格式化与 black 兼容的文档字符串。

docformatter 格式化使用 Epytext 或 Sphinx 风格的字段列表。

请参阅完整的文档,尤其是在 read-the-docs 中的 需求 部分,以更详细地讨论 PEP 257 和其他要求。

安装

从 pip

$ pip install --upgrade docformatter

或者,如果您想使用 pyproject.toml 配置 docformatter 并且您使用的是 Python < 3.11

$ pip install --upgrade docformatter[tomli]

使用 Python >=3.11 时,使用标准库中的 tomllib

或者,如果您想使用候选版本(或任何其他标签)

$ pip install git+https://github.com/PyCQA/docformatter.git@<RC_TAG>

其中 <RC_TAG> 是您想安装的候选版本标签。候选版本标签的格式为 v1.6.0-rc1 候选版本也将作为 Github 发布版提供。

示例

在运行

$ docformatter --in-place example.py

此代码

"""   Here are some examples.

    This module docstring should be dedented."""


def launch_rocket():
    """Launch
the
rocket. Go colonize space."""


def factorial(x):
    '''

    Return x factorial.

    This uses math.factorial.

    '''
    import math
    return math.factorial(x)


def print_factorial(x):
    """Print x factorial"""
    print(factorial(x))


def main():
    """Main
    function"""
    print_factorial(5)
    if factorial(10):
        launch_rocket()

格式化成这样

"""Here are some examples.

This module docstring should be dedented.
"""


def launch_rocket():
    """Launch the rocket.

    Go colonize space.
    """


def factorial(x):
    """Return x factorial.

    This uses math.factorial.
    """
    import math
    return math.factorial(x)


def print_factorial(x):
    """Print x factorial."""
    print(factorial(x))


def main():
    """Main function."""
    print_factorial(5)
    if factorial(10):
        launch_rocket()

营销

您使用 docformatter 吗?您使用哪种风格的文档字符串?在您项目的 README 中添加一些徽章,让每个人都知道。

SELF

.. image:: https://img.shields.io/badge/%20formatter-docformatter-fedcba.svg
    :target: https://github.com/PyCQA/docformatter

SPHINXSTYLE

.. image:: https://img.shields.io/badge/%20style-sphinx-0a507a.svg
    :target: https://sphinx-doc.cn/en/master/usage/index.html

NUMPSTYLE

.. image:: https://img.shields.io/badge/%20style-numpy-459db9.svg
    :target: https://numpydoc.readthedocs.io/en/latest/format.html

GOOGSTYLE

.. image:: https://img.shields.io/badge/%20style-google-3666d6.svg
    :target: https://google.github.io/styleguide/pyguide.html#s3.8-comments-and-docstrings

问题

可以在 GitHub 页面 上报告错误和补丁。

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪一个,请了解更多关于 安装包 的信息。

源分布

docformatter-1.7.5.tar.gz (25.9 kB 查看哈希值)

上传时间 源代码

构建分发版

docformatter-1.7.5-py3-none-any.whl (32.8 kB 查看哈希值)

上传时间 Python 3

由以下支持

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误日志StatusPageStatusPage状态页面