将Python 3转换为可由autodoc读取的"简单"python

Jam*_*mes 2 python python-sphinx autodoc

我在Python 3中编写了一个程序,并使用Sphinx来记录它.Sphinx的autodoc很棒,但它只适用于Python 2.有些模块在autodoc中工作正常,但模块却没有.一些例子:Python 2抱怨Python 3样式元类,以及一些在Python 2中不再存在的模块,如configparser.这很烦人,因为它无法从该文件导入文档字符串.

我不想在Python 2中重写整个程序,但我想使用autodoc.

我有一个想法是一个小程序,它读取每个Python文件并删除所有功能,但只是将基本函数和类与其文档字符串一起离开(因为autodoc导入每个模块并读取特定函数或类的文档字符串).

import configparser
import os

class TestClass:
    """
    I am a class docstring.
    """
    def method(self, argument):
        """
        I am a method docstring.
        """
        #Some code here
        print(os.getcwd())

def TestFunction():
    """
    I am a function docstring.
    """
    #Some more useless code here
    return os.path.join("foo", "bar")
Run Code Online (Sandbox Code Playgroud)

成...

class TestClass:
    """
    I am a class docstring.
    """
    def method(self, argument):
        """
        I am a method docstring.
        """
        pass

def TestFunction():
    """
    I am a function docstring.
    """
    pass
Run Code Online (Sandbox Code Playgroud)

通过这种方式,处理过的代码可以通过autodoc读取,但仍然具有我真正需要的文档字符串.这是解决这个问题的最好方法,并且有人建议如何编写转换代码的小程序.

我可以使用一些正则表达式轻松删除元类问题,但我正在努力解决其余问题.

m = re.search("\(metaclass=.*\)", file_content)
if m:
    file_content = "".join(file_content[:m.start()], file_content[m.end():])
Run Code Online (Sandbox Code Playgroud)

请问AST模块是有用的?

谢谢.

Flo*_*scu 5

你可以安装sphinx的开发版本,它支持python 3.

pip-3.2 install hg+https://bitbucket.org/birkenfeld/sphinx
Run Code Online (Sandbox Code Playgroud)

我在你的课上测试了autodoc功能,但它确实有效.