• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python registry.Registry类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中reg.registry.Registry的典型用法代码示例。如果您正苦于以下问题:Python Registry类的具体用法?Python Registry怎么用?Python Registry使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



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

示例1: test_adapter_two_sources

def test_adapter_two_sources():
    reg = Registry()

    class Adapted(ITarget):
        def __init__(self, alpha, beta):
            self.alpha = alpha
            self.beta = beta

        def foo(self):
            pass

    reg.register(ITarget, [IAlpha, IBeta], Adapted)

    alpha = Alpha()
    beta = Beta()
    adapted = reg.adapt(ITarget, [alpha, beta])

    assert isinstance(adapted, Adapted)
    assert adapted.alpha is alpha
    assert adapted.beta is beta

    adapted = ITarget.adapt(alpha, beta, lookup=reg)
    assert isinstance(adapted, Adapted)
    assert adapted.alpha is alpha
    assert adapted.beta is beta
开发者ID:reinout,项目名称:reg,代码行数:25,代码来源:test_interface.py


示例2: test_register_dispatch_predicates_twice

def test_register_dispatch_predicates_twice():
    @dispatch_external_predicates()
    def foo(obj):
        pass

    def for_bar(obj):
        return "for bar"

    def for_qux(obj):
        return "for qux"

    class Bar(object):
        pass

    class Qux(object):
        pass

    registry = Registry()

    registry.register_dispatch_predicates(
        foo, [match_instance('obj',
                             lambda obj: obj)])
    assert foo in registry.initialized
    # second time has no effect
    registry.register_dispatch_predicates(
        foo, [match_instance('obj',
                             lambda obj: obj)])
开发者ID:jean,项目名称:reg,代码行数:27,代码来源:test_dispatch.py


示例3: test_component_inheritance_old_style_class

def test_component_inheritance_old_style_class():
    reg = Registry()
    foo = object()

    class Gamma:
        pass

    class Delta(Gamma):
        pass

    @generic
    def target():
        pass

    reg.register(target, [Gamma], foo)

    gamma = Gamma()
    delta = Delta()

    assert reg.component(target, [gamma]) is foo
    assert target.component(gamma, lookup=reg) is foo

    # inheritance case
    assert reg.component(target, [delta]) is foo
    assert target.component(delta, lookup=reg) is foo
开发者ID:waytai,项目名称:reg,代码行数:25,代码来源:test_generic.py


示例4: test_call_two_sources

def test_call_two_sources():
    reg = Registry()

    @generic
    def target(a, b):
        pass

    class Adapted(object):
        def __init__(self, alpha, beta):
            self.alpha = alpha
            self.beta = beta

        def foo(self):
            pass

    reg.register(target, [IAlpha, IBeta], Adapted)

    alpha = Alpha()
    beta = Beta()
    adapted = reg.call(target, [alpha, beta])

    assert isinstance(adapted, Adapted)
    assert adapted.alpha is alpha
    assert adapted.beta is beta

    adapted = target(alpha, beta, lookup=reg)
    assert isinstance(adapted, Adapted)
    assert adapted.alpha is alpha
    assert adapted.beta is beta
开发者ID:iapilgrim,项目名称:reg,代码行数:29,代码来源:test_generic.py


示例5: test_dispatch_match_instance

def test_dispatch_match_instance():
    @dispatch(match_instance('obj', lambda obj: obj))
    def foo(obj):
        pass

    def for_bar(obj):
        return obj.method()

    def for_qux(obj):
        return obj.method()

    class Bar(object):
        def method(self):
            return "bar's method"

    class Qux(object):
        def method(self):
            return "qux's method"

    registry = Registry()

    registry.register_function(foo, for_bar, obj=Bar)
    registry.register_function(foo, for_qux, obj=Qux)

    lookup = registry.lookup()
    assert foo(Bar(), lookup=lookup) == "bar's method"
    assert foo(Qux(), lookup=lookup) == "qux's method"
开发者ID:jean,项目名称:reg,代码行数:27,代码来源:test_dispatch.py


示例6: test_component_two_sources

def test_component_two_sources():
    reg = Registry()
    foo = object()
    reg.register(ITarget, (IAlpha, IBeta), foo)
    alpha = Alpha()
    beta = Beta()
    assert reg.component(ITarget, [alpha, beta]) is foo
    assert ITarget.component(alpha, beta, lookup=reg) is foo
开发者ID:reinout,项目名称:reg,代码行数:8,代码来源:test_interface.py


示例7: test_component_class_based_registration

def test_component_class_based_registration():
    reg = Registry()
    foo = object()
    reg.register(ITarget, (Alpha,), foo)

    alpha = Alpha()
    assert reg.component(ITarget, [alpha]) is foo
    assert ITarget.component(alpha, lookup=reg) is foo
开发者ID:reinout,项目名称:reg,代码行数:8,代码来源:test_interface.py


示例8: test_non_adapter_looked_up_as_adapter

def test_non_adapter_looked_up_as_adapter():
    reg = Registry()
    foo = object()
    reg.register(ITarget, [Alpha], foo)
    alpha = Alpha()

    with py.test.raises(TypeError):
        ITarget.adapt(alpha, lookup=reg)
开发者ID:reinout,项目名称:reg,代码行数:8,代码来源:test_interface.py


示例9: test_register_no_external_predicates_for_external

def test_register_no_external_predicates_for_external():
    @dispatch_external_predicates()
    def foo():
        pass

    r = Registry()
    with pytest.raises(RegistrationError):
        r.register_dispatch(foo)
开发者ID:jean,项目名称:reg,代码行数:8,代码来源:test_dispatch.py


示例10: test_adapter_returns_none

def test_adapter_returns_none():
    def adapt(obj):
        return None
    reg = Registry()
    reg.register(ITarget, [Alpha], adapt)
    alpha = Alpha()
    with py.test.raises(ComponentLookupError):
        ITarget.adapt(alpha, lookup=reg)
    assert ITarget.adapt(alpha, lookup=reg, default='default') == 'default'
开发者ID:reinout,项目名称:reg,代码行数:9,代码来源:test_interface.py


示例11: test_interface_component

def test_interface_component():
    class IFoo(Interface):
        pass

    registry = Registry()

    registry.register(IFoo, (), "test component")

    assert IFoo.component(lookup=registry) == 'test component'
开发者ID:reinout,项目名称:reg,代码行数:9,代码来源:test_interface.py


示例12: test_lookup_passed_along_fallback

def test_lookup_passed_along_fallback():
    @dispatch()
    def a(lookup):
        return "fallback"

    reg = Registry()
    reg.register_dispatch(a)

    assert a(lookup=reg.lookup()) == 'fallback'
开发者ID:jean,项目名称:reg,代码行数:9,代码来源:test_dispatch.py


示例13: test_dict_to_predicate_key_for_unknown_dispatch

def test_dict_to_predicate_key_for_unknown_dispatch():
    @dispatch()
    def foo():
        pass

    r = Registry()

    with pytest.raises(KeyError):
        r.key_dict_to_predicate_key(foo, {})
开发者ID:jean,项目名称:reg,代码行数:9,代码来源:test_dispatch.py


示例14: test_component

def test_component():
    @generic
    def foo():
        pass

    registry = Registry()

    registry.register(foo, (), "test component")

    assert foo.component(lookup=registry) == 'test component'
开发者ID:iapilgrim,项目名称:reg,代码行数:10,代码来源:test_generic.py


示例15: test_component_to_itself

def test_component_to_itself():
    reg = Registry()
    alpha = Alpha()

    foo = object()

    reg.register(IAlpha, [IAlpha], foo)

    assert reg.component(IAlpha, [alpha]) is foo
    assert IAlpha.component(alpha, lookup=reg) is foo
开发者ID:reinout,项目名称:reg,代码行数:10,代码来源:test_interface.py


示例16: test_implicit_component_lookup

def test_implicit_component_lookup():
    class ITarget(Interface):
        pass

    reg = Registry()

    reg.register(ITarget, (), 'test component')

    implicit.initialize(reg)
    assert ITarget.component() == 'test component'
开发者ID:reinout,项目名称:reg,代码行数:10,代码来源:test_implicit.py


示例17: test_extra_kw

def test_extra_kw():
    reg = Registry()
    foo = object()

    reg.register(ITarget, [Alpha], foo)
    alpha = Alpha()

    with py.test.raises(TypeError) as e:
        ITarget.component(alpha, lookup=reg, extra="illegal")
    assert str(e.value) == 'Illegal extra keyword arguments: extra'
开发者ID:reinout,项目名称:reg,代码行数:10,代码来源:test_interface.py


示例18: test_fallback_to_fallback

def test_fallback_to_fallback():

    def fallback(obj):
        return 'fallback!'

    @dispatch(match_instance('obj', lambda obj: obj,
                             fallback=fallback))
    def target(obj):
        return 'not the fallback we want'

    reg = Registry()

    def specific_target(obj):
        return 'specific'

    reg.register_dispatch(target)
    reg.register_function(target, specific_target, obj=Alpha)

    beta = Beta()
    assert target(beta, lookup=reg.lookup()) == 'fallback!'
    # this is *not* a registered fallback so won't be returned here
    assert target.fallback(beta, lookup=reg.lookup()) is fallback
    # we cannot find a fallback for alpha, as it doesn't hit the fallback
    assert target(Alpha(), lookup=reg.lookup()) == 'specific'
    assert target.fallback(Alpha(), lookup=reg.lookup()) is NOT_FOUND
开发者ID:jean,项目名称:reg,代码行数:25,代码来源:test_dispatch.py


示例19: test_registry_sources

def test_registry_sources():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    class LineCount(object):
        pass

    reg.register(LineCount, (Document,), 'document line count')
    reg.register(LineCount, (SpecialDocument,), 'special document line count')

    assert (reg.component(LineCount, (Document(),)) ==
            'document line count')

    assert (reg.component(LineCount, (SpecialDocument(),)) ==
            'special document line count')

    class AnotherDocument(Document):
        pass

    assert (reg.component(LineCount, (AnotherDocument(),)) ==
            'document line count')

    class Other(object):
        pass

    assert reg.component(LineCount, (Other(),), default=None) is None
开发者ID:reinout,项目名称:reg,代码行数:31,代码来源:test_registry.py


示例20: test_registry_target_find_specific

def test_registry_target_find_specific():
    reg = Registry()

    class Document(object):
        pass

    class LineCount(object):
        pass

    class SpecialLineCount(LineCount):
        pass

    class SpecialDocument(Document):
        pass

    reg.register(LineCount, (Document,), 'line count')
    reg.register(SpecialLineCount, (Document,), 'special line count')

    assert reg.component(LineCount, (Document(),)) == 'line count'
    assert (reg.component(SpecialLineCount, (Document(),)) ==
            'special line count')

    assert reg.component(LineCount, (SpecialDocument(),)) == 'line count'
    assert (reg.component(SpecialLineCount, (SpecialDocument(),)) ==
            'special line count')
开发者ID:reinout,项目名称:reg,代码行数:25,代码来源:test_registry.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python regex.compile函数代码示例发布时间:2022-05-26
下一篇:
Python refstack_client.parse_cli_args函数代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap