当前位置: 首页>>代码示例>>Python>>正文


Python Components.queryUtility方法代码示例

本文整理汇总了Python中zope.interface.registry.Components.queryUtility方法的典型用法代码示例。如果您正苦于以下问题:Python Components.queryUtility方法的具体用法?Python Components.queryUtility怎么用?Python Components.queryUtility使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在zope.interface.registry.Components的用法示例。


在下文中一共展示了Components.queryUtility方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_extendning

# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import queryUtility [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)
开发者ID:ChimmyTee,项目名称:oh-mainline,代码行数:61,代码来源:test_registry.py

示例2: main

# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import queryUtility [as 别名]
def main():
    registry = Components()
    bootstrap(registry)

    session_factory = registry.queryUtility(IDBSessionFactory, "master")
    session = session_factory()
    a = MainTable()
    a.name = "test"
    session.add(a)
    print(session.get_bind())
    session.commit()

    session_factory = registry.queryUtility(IDBSessionFactory, "other")
    session = session_factory()
    a = OtherTable()
    a.name_other = "test"
    session.add(a)
    print(session.get_bind())
    session.commit()
开发者ID:TakesxiSximada,项目名称:sqlalchemy-example,代码行数:21,代码来源:zca.py

示例3: main

# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import queryUtility [as 别名]
def main(argv=sys.argv[1:]):
    reg = Components()
    reg.registerUtility(GoogleVisionAPIFaceDetection(),
                        IDetection, 'gcp')
    reg.registerUtility(MSProjectoxfordDetection(get_ms_param()['API_TOKEN']),
                        IDetection, 'ms')
    reg.registerUtility(AkamaiCrop(),
                        ICrop, 'akamai')

    parser = argparse.ArgumentParser()
    parser.add_argument('mode', help='gcp or ms')
    parser.add_argument('target', help='url or path')
    parser.add_argument('--crop', default='akamai')
    args = parser.parse_args(argv)

    detect = reg.queryUtility(IDetection, args.mode)
    result = detect(args.target)
    crop = AkamaiCrop()
    url = crop(result)
    print(url)
开发者ID:TakesxiSximada,项目名称:facedetection,代码行数:22,代码来源:__init__.py

示例4: TestUtility

# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import queryUtility [as 别名]
class TestUtility(unittest.TestCase):

    def setUp(self):
        self.components = Components('comps')

    def test_register_utility(self):
        test_object = U1(1)
        self.components.registerUtility(test_object)
        self.assertEqual(self.components.getUtility(I1), test_object)

    def test_register_utility_with_factory(self):
        test_object = U1(1)
        def factory():
           return test_object
        self.components.registerUtility(factory=factory)
        self.assertEqual(self.components.getUtility(I1), test_object)
        self.assertTrue(self.components.unregisterUtility(factory=factory))

    def test_register_utility_with_component_and_factory(self):
        def factory():
            return U1(1)
        self.assertRaises(
            TypeError,
            self.components.registerUtility, U1(1), factory=factory)

    def test_unregister_utility_with_and_without_component_and_factory(self):
        def factory():
            return U1(1)
        self.assertRaises(
            TypeError,
            self.components.unregisterUtility, U1(1), factory=factory)
        self.assertRaises(TypeError, self.components.unregisterUtility)

    def test_register_utility_with_no_interfaces(self):
        self.assertRaises(TypeError, self.components.registerUtility, A)

    def test_register_utility_with_two_interfaces(self):
        self.assertRaises(TypeError, self.components.registerUtility, U12(1))

    def test_register_utility_with_arguments(self):
        test_object1 = U12(1)
        test_object2 = U12(2)
        self.components.registerUtility(test_object1, I2)
        self.components.registerUtility(test_object2, I2, 'name')
        self.assertEqual(self.components.getUtility(I2), test_object1)
        self.assertEqual(self.components.getUtility(I2, 'name'), test_object2)

    def test_get_none_existing_utility(self):
        from zope.interface.interfaces import ComponentLookupError
        self.assertRaises(ComponentLookupError, self.components.getUtility, I3)

    def test_query_none_existing_utility(self):
        self.assertTrue(self.components.queryUtility(I3) is None)
        self.assertEqual(self.components.queryUtility(I3, default=42), 42)

    def test_registered_utilities_and_sorting(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object3, I2, u'name')
        self.components.registerUtility(test_object2, I2)

        sorted_utilities = sorted(self.components.registeredUtilities())
        sorted_utilities_name = map(lambda x: getattr(x, 'name'),
                                    sorted_utilities)
        sorted_utilities_component = map(lambda x: getattr(x, 'component'),
                                         sorted_utilities)
        sorted_utilities_provided = map(lambda x: getattr(x, 'provided'),
                                        sorted_utilities)

        self.assertEqual(len(sorted_utilities), 3)
        self.assertEqual(sorted_utilities_name, [u'', u'', u'name'])
        self.assertEqual(
            sorted_utilities_component,
            [test_object1, test_object2, test_object3])
        self.assertEqual(sorted_utilities_provided, [I1, I2, I2])

    def test_duplicate_utility(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        test_object4 = U1(4)
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object2, I2)
        self.components.registerUtility(test_object3, I2, u'name')
        self.assertEqual(self.components.getUtility(I1), test_object1)

        self.components.registerUtility(test_object4, info=u'use 4 now')
        self.assertEqual(self.components.getUtility(I1), test_object4)

    def test_unregister_utility(self):
        test_object = U1(1)
        self.components.registerUtility(test_object)
        self.assertEqual(self.components.getUtility(I1), test_object)
        self.assertTrue(self.components.unregisterUtility(provided=I1))
        self.assertFalse(self.components.unregisterUtility(provided=I1))

    def test_unregister_utility_extended(self):
        test_object = U1(1)
#.........这里部分代码省略.........
开发者ID:ChimmyTee,项目名称:oh-mainline,代码行数:103,代码来源:test_registry.py


注:本文中的zope.interface.registry.Components.queryUtility方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。