简体   繁体   中英

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. 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.

Is this possible?

Example:

Here is my module, 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":

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.

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.

I think the only thing you can do is catch the output of 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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