丰富的检查发现

Rich Inspect Discovery

我正在尝试进一步深入研究 Rich inpsect,以了解它是如何执行以下操作的。

如果我这样看一个对象 (result)。我可以看到它有以下属性和方法。

>>> vars(result)
{'name': 'hello_world'}
>>> print(dir(result))
[
    '__class__',
    '__class_getitem__',
    '__contains__',
    '__delattr__',
    '__delitem__',
    '__dict__',
    '__dir__',
    '__doc__',
    '__eq__',
    '__format__',
    '__ge__',
    '__getattribute__',
    '__getitem__',
    '__gt__',
    '__hash__',
    '__init__',
    '__init_subclass__',
    '__iter__',
    '__le__',
    '__len__',
    '__lt__',
    '__module__',
    '__ne__',
    '__new__',
    '__orig_bases__',
    '__parameters__',
    '__reduce__',
    '__reduce_ex__',
    '__repr__',
    '__reversed__',
    '__setattr__',
    '__setitem__',
    '__sizeof__',
    '__slots__',
    '__str__',
    '__subclasshook__',
    '__weakref__',
    '_is_protocol',
    'clear',
    'copy',
    'failed',
    'failed_hosts',
    'fromkeys',
    'get',
    'items',
    'keys',
    'name',
    'pop',
    'popitem',
    'raise_on_error',
    'setdefault',
    'update',
    'values'
]

如果我这样看 Rich 的输出:

>>> inspect(result)
╭─────────────────────────────────────────────────────────────────────────────── <class 'nornir.core.task.AggregatedResult'> ────────────────────────────────────────────────────────────────────────────────╮
│ It basically is a dict-like object that aggregates the results for all devices.                                                                                                                            │
│ You can access each individual result by doing ``my_aggr_result["hostname_of_device"]``.                                                                                                                   │
│                                                                                                                                                                                                            │
│ ╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│ │ AggregatedResult (hello_world): {'spine1-nxos': MultiResult: [Result: "hello_world", Result: "ABC1", Result: "ABC1a", Result: "ABC1b"], 'spine2-nxos': MultiResult: [Result: "hello_world", Result:    │ │
│ │ "ABC1", Result: "ABC1a", Result: "ABC1b"]}                                                                                                                                                             │ │
│ ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                                                                                                                                            │
│       failed = False                                                                                                                                                                                       │
│ failed_hosts = {}                                                                                                                                                                                          │
│         name = 'hello_world'                                                                                                                                                                               │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

我看到它只正确显示了感兴趣的属性和方法。 谁能告诉我 Rich 如何确定要打印的内容?

谢谢,

open-source 代码的好处在于您可以找到源代码并阅读它。对于这个问题,您会对 rich 源代码中的 this file 感兴趣。您最感兴趣的是 _render 方法,尽管有些东西是在 __init__.

中设置的

我们可以分解您看到的输出,以便正确理解它:

╭─────────────────────────────────────────────────────────────────────────────── <class 'nornir.core.task.AggregatedResult'> ────────────────────────────────────────────────────────────────────────────────╮

顶行包含被检查的 object 的“标题”。在这种情况下,它是 object 的 class 的 repr。

│ It basically is a dict-like object that aggregates the results for all devices.                                                                                                                            │
│ You can access each individual result by doing ``my_aggr_result["hostname_of_device"]``.                                                                                                                   │

这是 object 的文档字符串。

│ ╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│ │ AggregatedResult (hello_world): {'spine1-nxos': MultiResult: [Result: "hello_world", Result: "ABC1", Result: "ABC1a", Result: "ABC1b"], 'spine2-nxos': MultiResult: [Result: "hello_world", Result:    │ │
│ │ "ABC1", Result: "ABC1a", Result: "ABC1b"]}                                                                                                                                                             │ │
│ ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │

这是 object 的 pretty-printed“值”。它可能基于 object 自己的 repr,但我没有深入探讨 rich.pretty.Pretty 的工作原理,因此可能还有更多内容。

│       failed = False                                                                                                                                                                                       │
│ failed_hosts = {}                                                                                                                                                                                          │
│         name = 'hello_world'                                                                                                                                                                               │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

输出的其余部分是属性及其值的列表。默认情况下,只会列出 non-callable、非 _private、非 __dunder__ 属性,但您可以使用 inspect 的不同参数获得更完整的列表。