斯芬克斯“拿破仑”扩建。

sphinxcontrib-napoleon的Python项目详细描述


注意

从狮身人面像1.3开始,拿破仑分机将与 狮身人面像在sphinx.ext.napoleon下面。扩展名sphinxcontrib.napoleon 将继续使用sphinx<;=1.2。

您厌倦了编写这样的docstring吗:

:param path: The path of the file to wrap
:type path: str
:param field_storage: The :class:`FileStorage` instance to wrap
:type field_storage: FileStorage
:param temporary: Whether or not to delete the file when the File
   instance is destructed
:type temporary: bool
:returns: A buffered writable file descriptor
:rtype: BufferedFileStorage

ReStructuredText很好,但它创建了视觉密集、难以阅读的 docstrings。把上面的混乱和重写的一样东西比较一下 根据Google Python Style Guide

Args:
    path (str): The path of the file to wrap
    field_storage (FileStorage): The :class:`FileStorage` instance to wrap
    temporary (bool): Whether or not to delete the file when the File
       instance is destructed

Returns:
    BufferedFileStorage: A buffered writable file descriptor

更清晰,不是吗?

拿破仑是一个Sphinx extension使狮身人面像能够同时解析这两个NumPy 以及Google样式的docstrings-由Khan Academy推荐的样式。

napoleon是一个预处理器,它解析NumPyGoogle样式 在Sphinx尝试 分析它们。这发生在斯芬克斯正在处理的中间步骤中 文档,因此它不会修改实际 源代码文件。

开始

  1. 安装拿破仑加长件:

    $ pip install sphinxcontrib-napoleon
    
  2. setting up Sphinx构建文档之后,在 斯芬克斯conf.py文件:

    # conf.py
    
    # Add napoleon to the extensions list
    extensions = ['sphinxcontrib.napoleon']
    
  1. 使用sphinx-apidoc构建您的api文档:

    $ sphinx-apidoc -f -o docs/source projectdir
    

文档字符串

拿破仑解释了Sphinx autodoc能找到的每个文档字符串, 包括文档字符串:modulesclassesattributesmethodsfunctionsvariables。在每个docstring中, 特殊格式的Sections被解析并转换为 重组文本。

所有标准的structuredText格式仍按预期工作。

文档字符串节

支持以下所有节标题:

  • ^{tt7}$ (alias of Parameters)
  • ^{tt8}$ (alias of Parameters)
  • ^{tt9}$
  • ^{tt10}$
  • ^{tt11}$
  • ^{tt12}$ (alias of Keyword Arguments)
  • ^{tt13}$
  • ^{tt14}$
  • ^{tt15}$
  • ^{tt16}$
  • ^{tt17}$
  • ^{tt18}$
  • ^{tt19}$ (alias of Returns)
  • ^{tt20}$
  • ^{tt21}$
  • ^{tt22}$
  • ^{tt23}$
  • ^{tt24}$
  • ^{tt25}$ (alias of Warning)
  • ^{tt26}$
  • ^{tt27}$ (alias of Yields)
  • ^{tt28}$

谷歌vs Numpy

napoleon支持两种类型的docstring:GoogleNumPy。这个 这两种风格的主要区别在于google使用缩进 单独的部分,而numpy使用下划线。

谷歌风格:

def func(arg1, arg2):
    """Summary line.

    Extended description of function.

    Args:
        arg1 (int): Description of arg1
        arg2 (str): Description of arg2

    Returns:
        bool: Description of return value

    """
    return True

纽米风格:

def func(arg1, arg2):
    """Summary line.

    Extended description of function.

    Parameters
    ----------
    arg1 : int
        Description of arg1
    arg2 : str
        Description of arg2

    Returns
    -------
    bool
        Description of return value

    """
    return True

numpy风格倾向于需要更多的垂直空间,而google风格 倾向于使用更多的水平空间。谷歌风格往往更容易 阅读简短的文档字符串,而numpy风格更容易 阅读长而深入的文档字符串。

Khan Academy建议使用google风格。

在风格之间的选择很大程度上是审美的,但这两种风格应该 不要混为一谈。为你的项目选择一种风格并与之保持一致。

有关完整文档,请参见https://sphinxcontrib-napoleon.readthedocs.io

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
实现接口方法时不允许java@Override   使用BuffereImage加载映像时java高ram使用率   java For循环混乱,为什么不是循环?   java Android网格视图字符串对齐问题   java如何将方法与比较类型的附加功能进行比较?   在Java Swing中放置JSepator后的间隙大小   java如何避免并发访问我的网站中的支付链接   java如何从现有的Unix服务器连接到FTP服务器?   Spring中的java用户相关bean定义   带有scribesjava库的wordpress Woocommerce REST API返回消费者密钥参数缺失错误消息   java我可以自动检测特定设备连接的串行端口吗?   Javafx棋盘游戏   java使用JTextPane显示HTML,支持SVG吗?   SpringBoot如何在java中将映射转换为实体对象?   如何使用java代码对xls文件进行密码保护   Java JPA(EclipseLink)如何在持久化实际实体之前接收下一个生成的值?   Javaservlet启动外部进程