本文整理汇总了Python中zope.interface.registry.Components.handle方法的典型用法代码示例。如果您正苦于以下问题:Python Components.handle方法的具体用法?Python Components.handle怎么用?Python Components.handle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zope.interface.registry.Components
的用法示例。
在下文中一共展示了Components.handle方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_extendning
# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import handle [as 别名]
def test_extendning(self):
c1 = Components('1')
self.assertEqual(c1.__bases__, ())
c2 = Components('2', (c1, ))
self.assertTrue(c2.__bases__ == (c1, ))
test_object1 = U1(1)
test_object2 = U1(2)
test_object3 = U12(1)
test_object4 = U12(3)
self.assertEqual(len(list(c1.registeredUtilities())), 0)
self.assertEqual(len(list(c2.registeredUtilities())), 0)
c1.registerUtility(test_object1)
self.assertEqual(len(list(c1.registeredUtilities())), 1)
self.assertEqual(len(list(c2.registeredUtilities())), 0)
self.assertEqual(c1.queryUtility(I1), test_object1)
self.assertEqual(c2.queryUtility(I1), test_object1)
c1.registerUtility(test_object2)
self.assertEqual(len(list(c1.registeredUtilities())), 1)
self.assertEqual(len(list(c2.registeredUtilities())), 0)
self.assertEqual(c1.queryUtility(I1), test_object2)
self.assertEqual(c2.queryUtility(I1), test_object2)
c3 = Components('3', (c1, ))
c4 = Components('4', (c2, c3))
self.assertEqual(c4.queryUtility(I1), test_object2)
c1.registerUtility(test_object3, I2)
self.assertEqual(c4.queryUtility(I2), test_object3)
c3.registerUtility(test_object4, I2)
self.assertEqual(c4.queryUtility(I2), test_object4)
@adapter(I1)
def handle1(x):
self.assertEqual(x, test_object1)
def handle(*objects):
self.assertEqual(objects, (test_object1,))
@adapter(I1)
def handle3(x):
self.assertEqual(x, test_object1)
@adapter(I1)
def handle4(x):
self.assertEqual(x, test_object1)
c1.registerHandler(handle1, info=u'First handler')
c2.registerHandler(handle, required=[U])
c3.registerHandler(handle3)
c4.registerHandler(handle4)
c4.handle(test_object1)
示例2: TestHandler
# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import handle [as 别名]
class TestHandler(unittest.TestCase):
def setUp(self):
self.components = Components('comps')
def test_register_handler(self):
test_object1 = U1(1)
test_object2 = U12(2)
@adapter(I1)
def handle1(x):
self.assertEqual(x, test_object1)
self.components.registerHandler(handle1, info=u'First handler')
self.components.handle(test_object1)
@adapter(I1, I2)
def handle12(x, y):
self.assertEqual(x, test_object1)
self.assertEqual(y, test_object2)
self.components.registerHandler(handle12)
self.components.handle(test_object1, test_object2)
def test_register_noncompliant_handler(self):
handle_calls = []
def handle(*objects):
handle_calls.append(objects)
self.assertRaises(TypeError, self.components.registerHandler, handle)
self.components.registerHandler(
handle, required=[I1], info=u'a comment')
self.components.registerHandler(
handle, required=[U], info=u'handle a class')
test_object = U1(1)
self.components.handle(test_object)
self.assertEqual(len(handle_calls), 2)
map(self.assertEqual, handle_calls, [(test_object,), (test_object,)])
def test_list_handlers(self):
test_object1 = U1(1)
test_object2 = U12(2)
@adapter(I1)
def handle1(x):
self.assertEqual(x, test_object1)
@adapter(I1, I2)
def handle12(x, y):
self.assertEqual(x, test_object1)
self.assertEqual(y, test_object2)
handle_calls = []
def handle(*objects):
handle_calls.append(objects)
self.components.registerHandler(handle1, info=u'First handler')
self.components.registerHandler(handle12)
self.components.registerHandler(
handle, required=[I1], info=u'a comment')
self.components.registerHandler(
handle, required=[U], info=u'handle a class')
handlers = list(self.components.registeredHandlers())
handlers_required = map(lambda x: getattr(x, 'required'), handlers)
handlers_handler = map(lambda x: getattr(x, 'handler'), handlers)
handlers_info = map(lambda x: getattr(x, 'info'), handlers)
self.assertEqual(len(handlers), 4)
self.assertEqual(handlers_required,
[(I1,), (I1, I2), (I1,), (implementedBy(U),)])
self.assertEqual(handlers_handler,
[handle1, handle12, handle, handle])
self.assertEqual(
handlers_info,
[u'First handler', u'', u'a comment', u'handle a class'])
def test_unregister_handler(self):
test_object1 = U1(1)
test_object2 = U12(2)
@adapter(I1)
def handle1(x):
self.assertEqual(x, test_object1)
@adapter(I1, I2)
def handle12(x, y):
self.assertEqual(x, test_object1)
self.assertEqual(y, test_object2)
handle_calls = []
def handle(*objects):
handle_calls.append(objects)
self.components.registerHandler(handle1, info=u'First handler')
self.components.registerHandler(handle12)
self.components.registerHandler(
handle, required=[I1], info=u'a comment')
self.components.registerHandler(
#.........这里部分代码省略.........