简体   繁体   English

Python-如何不按字母顺序对pydoc输出进行排序

[英]Python - How not to sort pydoc output in alphabetic order

pydoc's output sorts the results (methods and their docstrings) based on the alphabetical order. pydoc的输出根据字母顺序对结果(方法及其文档字符串)进行排序。 This is good, but I want to stay on top of what is new. 很好,但是我想紧跟新事物。 When I add a new method in my module, I add it as the first method, so every time I add a new method it becomes the first method in the file. 当我在模块中添加新方法时,我将其添加为第一个方法,因此,每次添加新方法时,它将成为文件中的第一个方法。

I want the pydoc output to display in the same order as the methods are in the file. 我希望pydoc输出以与文件中方法相同的顺序显示。

Is this possible? 这可能吗?

Example: 例:

Here is my module, pydoc_test.py: 这是我的模块pydoc_test.py:

#!/usr/bin/python
def test_my_code():
    """
    Docstring for test_my_code()
    :return:
    """
    pass


def add_my_code():
    """
    Docstring for add_my_code()
    :return:
    """
    pass

Here is the output of "pydoc pydoc_test": 这是“ pydoc pydoc_test”的输出:

Help on module pydoc_test:

NAME
    pydoc_test

FILE
    /Users/myname/Documents/scripts/python_learning/pydoc_test.py

FUNCTIONS
    add_my_code()
        Docstring for add_my_code()
        :return:

    test_my_code()
        Docstring for test_my_code()
        :return:

pydoc displays "add_my_code" first and then "test_my_code", but I want the same order as in the file. pydoc首先显示“ add_my_code”,然后显示“ test_my_code”,但是我想要的顺序与文件中的顺序相同。

I recently ran into the same problem, and as far as I can tell from the documentation , pydoc itself doesn't give you this option. 最近,我遇到了同样的问题,据我从文档中得知,pydoc本身并没有为您提供此选项。

I think the only thing you can do is catch the output of pydoc: 我认为您唯一可以做的就是捕获pydoc的输出:

import pydoc
import pydoc_test
docstrings = pydoc.render_doc(pydoc_test)

And parse it to get the separate docstrings. 并解析它以获得单独的文档字符串。

Then read pydoc_test.py, get the function names by searching for 'def' and rearrange the pydoc docstrings according to the order in which they appear in pydoc_test.py. 然后阅读pydoc_test.py,通过搜索'def'获得函数名称,并根据它们在pydoc_test.py中的出现顺序重新排列pydoc docstring。

Very ugly, I agree. 我非常难看。 I guess this is such a roundabout way of getting what you want, that you might as well forget about pydoc all together and simply read your pydoc_test.py file directly and parse it to get the docstrings. 我猜想这是一种获取所需内容的round回方式,以至于您可能完全忘记了pydoc,而直接直接读取pydoc_test.py文件并对其进行解析以获取文档字符串。

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

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