1

我试图进一步深入研究 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 如何确定要打印的内容?

谢谢,

4

1 回答 1

2

开源代码的好处是您可以找到源代码并阅读它。对于这个问题,你会对源代码中的这个文件感兴趣。rich尽管有些东西是_render__init__.

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

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

顶行包含被检查对象的“标题”。在这种情况下,它是对象类的 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"]``.                                                                                                                   │

这是对象的文档字符串。

│ ╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│ │ 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"]}                                                                                                                                                             │ │
│ ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │

这是对象的漂亮打印“值”。它可能是基于对象自己的repr,但我并没有深入研究它是如何rich.pretty.Pretty工作的,所以可能还有更多。

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

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

于 2022-02-13T21:14:02.107 回答