gpt4 book ai didi

python - 为所有类方法打印 python 文档字符串

转载 作者:行者123 更新时间:2023-11-28 17:56:19 24 4
gpt4 key购买 nike

我想打印一个模块的文档,以包括每个类和类中每个方法的文档字符串,但只包括我编写的方法;不是默认方法。

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

我应该用什么来打印所有的方法文档字符串?

最佳答案

这是一个大量使用检查模块的解决方案。请注意,这只会深入 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)

关于python - 为所有类方法打印 python 文档字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58089209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com