Search code examples
pythoncommentsdoc

python doc for docstring


I want to have a tool which can generate documentation from the source code comments, I found a tool named pythondoc, it can generate HTML or XML from python source codes, but it doesn't support extracting the docstring, only supports extrats the '#'comments, is there a tool which can support the docstring extracting?


Solution

  • You can easily scan all your files and folders, find all python files, and get only docstrings out of them with some regex matching, something like this:

    import re
    import os
    import xmlutils # download it from: https://gist.github.com/huseyinyilmaz/1448723
    
    my_dir = '<absolute path to my dir>'
    
    doc_dict = dict()
    
    for root, dirs, files in os.walk(my_dir):
        for filename in files:
            if filename.endswith('.py'):
                with open(os.path.join(root, filename), 'r') as py_file:
                    basename = os.path.basename(filename)
                    doc_dict[basename] = [m.group('doc') for m in re.finditer(
                        re.compile(r'("|\'){3}(?P<doc>.*?)\1{3}', re.DOTALL),
                        py_file.read()
                    )]
    
    dict2xml(dict(root=doc_dict))