我在Python 3中編寫了一個程序,並且正在使用Sphinx來記錄它。 Sphinx的autodoc很棒,但它只適用於Python 2.一些模塊在autodoc中工作正常,但是模塊不能。一些示例:Python 2抱怨Python 3風格的元類,以及Python 2中不再存在的某些模塊,如configparser。這很煩人,因爲它無法從該文件導入文檔。將Python 3轉換爲可以通過autodoc讀取的「簡單」python
我不想在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")
成...
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
這樣處理的碼可通過車博士讀,但仍然有文檔字符串這是我真正需要的。這是解決這個問題的最好方法,並且沒有人對如何編寫轉換代碼的小程序有任何建議。
我可以很容易地用一些正則表達式去除元類問題,但是我正在努力解決其餘問題。
m = re.search("\(metaclass=.*\)", file_content)
if m:
file_content = "".join(file_content[:m.start()], file_content[m.end():])
請問AST模塊是有用的?
謝謝。
哇,謝謝你。迄今爲止最好的解決方案。 – James 2011-04-11 21:02:34