我想打印一个模块的文档,以包含每个类和该类中每个方法的文档字符串,但只包括我编写的方法;不是默认方法。
这是my_module.py
:
import json
import pandas as pd
class my_class1:
"""My class 1 docs."""
def my_method1(self):
"""My class 1 method 1 docs."""
return None
class my_class2:
"""My class 1 docs."""
def my_method1(self):
"""My class 2 method 2 docs."""
return None
所需的输出是这样
my_class1
My class 1 docs.
my_method1:
My class 1 method 1 docs.
my_class2
My class 2 docs.
my_method2:
My class 2 method 2 docs.
此脚本成功打印了我想要的所有类方法:
import my_module as mm
imports = ['json','pd'] # Pre-defined modules within my_module that I don't wish to document
for i in [c for c in dir(mm) if not c.startswith('__') and not c in imports]:
print(getattr(mm, i).__name__)
print(getattr(mm, i).__doc__)
输出:
my_class1
My class 1 docs.
my_class2
My class 1 docs.
但是我已经尝试过将该脚本包含方法,但是它只是多次记录每个类并排除了方法。
import my_module as mm
imports = ['json','pd']
for i in [c for c in dir(mm) if not c.startswith('__') and not c in imports]:
print(getattr(mm, i).__name__)
print(getattr(mm, i).__doc__)
for j in [c for c in dir(i) if c == '__init__' or not c.startswith('__')]:
print(getattr(mm, i,j).__name__)
print(getattr(mm, i,j).__doc__)
输出:
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class1
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
my_class2
My class 1 docs.
我应该使用什么来打印所有方法文档字符串?
答案 0 :(得分:1)
这是一个大量使用检查模块的解决方案。请注意,此深度仅2层。它不会捕获嵌套在其他类方法内的方法。我在相当多的随附软件包中对其进行了测试,并取得了良好的效果。
import inspect
def is_relevant(obj):
"""Filter for the inspector to filter out non user defined functions/classes"""
if hasattr(obj, '__name__') and obj.__name__ == 'type':
return False
if inspect.isfunction(obj) or inspect.isclass(obj) or inspect.ismethod(obj):
return True
def print_docs(module):
default = 'No doc string provided' # Default if there is no docstring, can be removed if you want
flag = True
for child in inspect.getmembers(module, is_relevant):
if not flag: print('\n\n\n')
flag = False # To avoid the newlines at top of output
doc = inspect.getdoc(child[1])
if not doc:
doc = default
print(child[0], doc, sep = '\n')
if inspect.isclass(child[1]):
for grandchild in inspect.getmembers(child[1], is_relevant):
doc = inspect.getdoc(grandchild[1])
if doc:
doc = doc.replace('\n', '\n ')
else:
doc = default
print('\n ' + grandchild[0], doc, sep = '\n ')
import your_module
print_docs(your_module)