简体   繁体   English

使用 Python Sphinx 向模块文档字符串添加参数

[英]Adding parameters to module docstrings with Python Sphinx

I have a docstring in the beginning of every module describing its usage and functionality.我在每个模块的开头都有一个文档字符串来描述它的用法和功能。 Here I also want to add the most relevant parameters - like settings in a parameter file or via command line arguments.在这里,我还想添加最相关的参数 - 如参数文件中的设置或通过命令行参数。 It's no classical function parameters since the module might be called as stand-alone as well (via if __name__ == '__main__' capture).它不是经典的函数参数,因为模块也可能被称为独立的(通过if __name__ == '__main__'捕获)。 But since the ordinary parameter formatting of Sphinx is neat I want to just re-use it.但是由于 Sphinx 的普通参数格式很整洁,我只想重新使用它。

However, Sphinx seems to handle the "Parameter" section differently when in a module compared to when in a function.但是,与在函数中相比,Sphinx 在模块中处理“参数”部分的方式似乎有所不同。

This is how they are formatted differently:这是它们的不同格式:

Parameters in function docstring:函数文档字符串中的参数:

函数文档字符串中的参数

Parameters in module docstring:模块文档字符串中的参数:

模块文档字符串中的参数

You see the difference.你看到了区别。 In functions the keyword "Parameters" is added and then we have a nice bullet list.在函数中添加了关键字“Parameters” ,然后我们就有了一个不错的项目符号列表。 In modules no title is created, no list, the type is not set in braces but on an additional line etc.在模块中没有创建标题,没有列表,类型没有设置在大括号中,而是在附加行等。

Docstring format is the same (numpydoc): Docstring 格式相同(numpydoc):

Parameters
----------
pars : dict
    Parameter dictionary.
key : str
    Parameter name.

vs.对比

Parameters
----------
num_axial_segments : int
    The number of axial rotor segments.
magnet_segmentation_method : int
    The method of magnet segmentation.
    0: Uniform segmentation (all magnets same amount of segments).

Does anyone have an idea why it's handled like this?有谁知道为什么要这样处理? And what I can do about it?我能做些什么呢?

I would like the parameters in modules output in the same way as in functions.我希望模块中的参数以与函数中相同的方式输出。

The style used to render the docstring sections is dependent on the HTML theme you are using with Sphinx.用于呈现文档字符串部分的样式取决于您在 Sphinx 中使用的HTML 主题

Does anyone have an idea why it's handled like this?有谁知道为什么要这样处理?

The reason the styles are different for module and function docstrings, is because it's customary to use a command-line syntax style to document scripts that is different from function signature syntax style .模块和函数文档字符串的样式不同的原因是,习惯上使用命令行语法样式来记录与函数签名语法样式不同的脚本。 Notice the style you showed for the module docstring is similar to a list of command-line arguments.请注意,您为模块文档字符串显示的样式类似于命令行参数列表。

I would like the parameters in modules output in the same way as in functions.我希望模块中的参数以与函数中相同的方式输出。

Different themes may render module docstrings similarly or differently from function and classe docstrings.不同的主题可能会以与函数和类文档字符串类似或不同的方式呈现模块文档字符串。 You would have to choose a different theme or customize the theme's CSS by copying the style used for functions to the style used for module docstrings.您必须选择不同的主题或通过将用于函数的样式复制到用于模块文档字符串的样式来自定义主题的 CSS。

the type is not set in braces but on an additional line etc.类型未设置在大括号中,而是设置在附加行等中。

This is noteworthy because you would expect the napoleon-sphinx extension to not render type and name on different lines as if it were using classic reST syntax instead of Google style or Numpy.这是值得注意的,因为您会期望napoleon-sphinx 扩展不会在不同的行上呈现类型和名称,就好像它使用经典的reST 语法而不是Google 样式或Numpy 一样。

I would recommend trying a different HTML theme or setting the napoleon_use_param , napoleon_use_ivar and napoleon_use_rtype explicitly to see if there's a difference.我建议尝试不同的 HTML 主题或明确设置napoleon_use_paramnapoleon_use_ivarnapoleon_use_rtype以查看是否有区别。

And what I can do about it?我能做些什么呢?

The examples given for Google style or Numpy style suggest using docstring sections but that's somewhat oversimplified because command-line style syntax is better illustrated and automated by the way argparse implements it.给出的Google 风格Numpy 风格的示例建议使用 docstring 部分,但这有点过于简化了,因为命令行风格的语法通过argparse实现方式得到了更好的说明和自动化。 There are a few extensions meant to ease and automate the process of documenting scripts.一些扩展旨在简化和自动化记录脚本的过程。

As for the difference in style I wouldn't worry about it (admittedly in the HTML theme you are currently using it doesn't look very good).至于风格上的差异,我不会担心(诚然,在您当前使用的 HTML 主题中,它看起来不太好)。 A command-line invocation of a script or a run-time call of a function are different and accordingly themes may and will render those docstrings sections with visual differences.脚本的命令行调用或函数的运行时调用是不同的,因此主题可能并将呈现具有视觉差异的文档字符串部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM