13

python 中的任何命令行选项是否用于打印异常/错误类层次结构?

输出应该类似于http://docs.python.org/2/library/exceptions.html#exception-hierarchy

4

2 回答 2

31

检查模块可能会有所帮助,特别是getclasstree()函数:

将给定的类列表排列成嵌套列表的层次结构。在嵌套列表出现的地方,它包含从其条目紧接在列表之前的类派生的类。

inspect.getclasstree(inspect.getmro(Exception))

或者,您可以递归地遍历__subclasses__()继承树,如下所示:

def classtree(cls, indent=0):
    print '.' * indent, cls.__name__
    for subcls in cls.__subclasses__():
        classtree(subcls, indent + 3)

classtree(BaseException)

印刷:

 BaseException
... Exception
...... StandardError
......... TypeError
......... ImportError
............ ZipImportError
......... EnvironmentError
............ IOError
............... ItimerError
............ OSError
......... EOFError
......... RuntimeError
............ NotImplementedError
......... NameError
............ UnboundLocalError
......... AttributeError
......... SyntaxError
............ IndentationError
............... TabError
......... LookupError
............ IndexError
............ KeyError
............ CodecRegistryError
......... ValueError
............ UnicodeError
............... UnicodeEncodeError
............... UnicodeDecodeError
............... UnicodeTranslateError
......... AssertionError
......... ArithmeticError
............ FloatingPointError
............ OverflowError
............ ZeroDivisionError
......... SystemError
............ CodecRegistryError
......... ReferenceError
......... MemoryError
......... BufferError
...... StopIteration
...... Warning
......... UserWarning
......... DeprecationWarning
......... PendingDeprecationWarning
......... SyntaxWarning
......... RuntimeWarning
......... FutureWarning
......... ImportWarning
......... UnicodeWarning
......... BytesWarning
...... _OptionError
... GeneratorExit
... SystemExit
... KeyboardInterrupt
于 2013-08-18T07:20:07.693 回答
2

重用标准库中的代码,而不是自己滚动。

import inspect
import pydoc

def print_class_hierarchy(classes=()):
    td = pydoc.TextDoc()
    tree_list_of_lists = inspect.getclasstree(classes)
    print(td.formattree(tree_list_of_lists, 'NameSpaceName'))

要使用它,我们需要一个列表形式的类层次结构,这对我们传递函数是有意义的。我们可以通过递归搜索类.__subclasses__()方法结果来构建它,使用这个函数(我将保留这里的规范版本):

def get_subclasses(cls):
    """returns all subclasses of argument, cls"""
    if issubclass(cls, type): # not a bound method
        subclasses = cls.__subclasses__(cls)
    else:
        subclasses = cls.__subclasses__()
    for subclass in subclasses:
        subclasses.extend(get_subclasses(subclass))
    return subclasses

把这个放在一起:

list_of_classes = get_subclasses(int)
print_class_hierarchy(list_of_classes)

哪个打印(在 Python 3 中):

>>> print_class_hierarchy(classes)
builtins.int(builtins.object)
    builtins.bool
    enum.IntEnum(builtins.int, enum.Enum)
        inspect._ParameterKind
        signal.Handlers
        signal.Signals
    enum.IntFlag(builtins.int, enum.Flag)
        re.RegexFlag
    sre_constants._NamedIntConstant
    subprocess.Handle
enum.Enum(builtins.object)
    enum.IntEnum(builtins.int, enum.Enum)
        inspect._ParameterKind
        signal.Handlers
        signal.Signals
enum.Flag(enum.Enum)
    enum.IntFlag(builtins.int, enum.Flag)
        re.RegexFlag

这为我们提供了所有子类以及相关的多重继承类的树 - 并告诉我们它们所在的模块。

于 2017-11-01T17:02:13.850 回答