提供访问对象引用(子对象)的API。
sw.objectinspection的Python项目详细描述
软件对象检查
objectinspection是一个工具,它为您提供了一个要检查的通用api
对象的属性及其子对象。它有两个
基本检查员(BasicAttributesInspector
和BasicChildrenInspector
)
可以很容易地用你自己的检查员代替。
设置
创建一些稍后检查的对象:
>>> class Child1(object): ... pass >>> class Child2(object): ... pass >>> class Child3(object): ... pass >>> class Child4(object): ... misc = Child1() >>> class ToInspect(object): ... some_var = ["some tupel", (Child2(), Child3())] ... other_var = {"foo": "bar"} ... _private_var = 10 ... desc = Child4() ... ... def some_method(self): ... pass
检查属性
通过适应IAttributesInspector
,获得AttributesInspector
:
>>> from sw.objectinspection import IAttributesInspector >>> inspector = IAttributesInspector(ToInspect())
因为注册的ToInspect
对象没有专门的检查器,
使用BasicAttributesInspector
:
>>> inspector <sw.objectinspection.BasicAttributesInspector object at 0x11f69d0 used for ToInspect object at 0x11f6910>
要开始检查,只需致电检查员:
>>> inspector() ['desc', 'other_var', 'some_var']
儿童检查
检查子项的工作方式与检查属性的工作方式相同:
>>> from sw.objectinspection import IChildrenInspector >>> inspector = IChildrenInspector(ToInspect())>>> inspector <sw.objectinspection.BasicChildrenInspector object at 0x11f6a10 used for ToInspect object at 0x11f6910>>>> sorted(inspector()) [<Child2 object at 0x119cc10>, <Child3 object at 0x11f5790>, <Child4 object at 0x11f5830>]
编写自己的检查员
您可以编写自己的检查器并将其注册为比 此软件包附带的基本功能:
>>> from sw.objectinspection import BasicInspector >>> import zope.component >>> import zope.interface>>> class DummyAttributesInspector(BasicInspector): ... zope.component.adapts(ToInspect) ... zope.interface.implements(IAttributesInspector) ... ... def __call__(self): ... # The object, which is to be inspected, can be accessed ... # with self._inspecting ... return ['foo', 'bar']>>> gsm = zope.component.getGlobalSiteManager() >>> gsm.registerAdapter(DummyAttributesInspector)
现在,当检查属性时,我们得到
DummyAttributesInspector
:
>>> inspector = IAttributesInspector(ToInspect()) >>> inspector <DummyAttributesInspector object at 0x11f69d0 used for ToInspect object at 0x11f6910> >>> inspector() ['foo', 'bar']
更改
1.0.1(2009-08-13)
- 修复运行Windows的系统的内存地址检查程序。
1.0(2009-07-29)
- 初次发布。