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


Python components.proxyForInterface方法代码示例

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


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

示例1: test_decoratedProxyMethod

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def test_decoratedProxyMethod(self):
        """
        Methods of the class created from L{proxyForInterface} can be used with
        the decorator-helper L{functools.wraps}.
        """
        base = proxyForInterface(IProxiedInterface)
        class klass(base):
            @wraps(base.yay)
            def yay(self):
                self.original.yays += 1
                return base.yay(self)

        original = Yayable()
        yayable = klass(original)
        yayable.yay()
        self.assertEqual(2, original.yays) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:18,代码来源:test_components.py

示例2: test_subclassing

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def test_subclassing(self):
        """
        It is possible to subclass the result of L{proxyForInterface}.
        """

        class SpecializedProxy(proxyForInterface(IProxiedInterface)):
            """
            A specialized proxy which can decrement the number of yays.
            """
            def boo(self):
                """
                Decrement the number of yays.
                """
                self.original.yays -= 1

        yayable = Yayable()
        special = SpecializedProxy(yayable)
        self.assertEqual(yayable.yays, 0)
        special.boo()
        self.assertEqual(yayable.yays, -1) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:22,代码来源:test_components.py

示例3: test_proxyInheritance

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def test_proxyInheritance(self):
        """
        Subclasses of the class returned from L{proxyForInterface} should be
        able to upcall methods by reference to their superclass, as any normal
        Python class can.
        """
        class YayableWrapper(proxyForInterface(IProxiedInterface)):
            """
            This class does not override any functionality.
            """

        class EnhancedWrapper(YayableWrapper):
            """
            This class overrides the 'yay' method.
            """
            wrappedYays = 1
            def yay(self, *a, **k):
                self.wrappedYays += 1
                return YayableWrapper.yay(self, *a, **k) + 7

        yayable = Yayable()
        wrapper = EnhancedWrapper(yayable)
        self.assertEqual(wrapper.yay(3, 4, x=5, y=6), 8)
        self.assertEqual(yayable.yayArgs,
                          [((3, 4), dict(x=5, y=6))]) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:27,代码来源:test_components.py

示例4: test_attributeCustomization

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def test_attributeCustomization(self):
        """
        The original attribute name can be customized via the
        C{originalAttribute} argument of L{proxyForInterface}: the attribute
        should change, but the methods of the original object should still be
        callable, and the attributes still accessible.
        """
        yayable = Yayable()
        yayable.ifaceAttribute = object()
        proxy = proxyForInterface(
            IProxiedInterface, originalAttribute='foo')(yayable)
        self.assertIs(proxy.foo, yayable)

        # Check the behavior
        self.assertEqual(proxy.yay(), 1)
        self.assertIs(proxy.ifaceAttribute, yayable.ifaceAttribute)
        thingy = object()
        proxy.ifaceAttribute = thingy
        self.assertIs(yayable.ifaceAttribute, thingy)
        del proxy.ifaceAttribute
        self.assertFalse(hasattr(yayable, 'ifaceAttribute')) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:23,代码来源:test_components.py

示例5: _superlative

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def _superlative(interface, comparison_result):
    """
    Create a proxy type for ``interface`` which also overrides comparison to
    return a particular result.

    :param zope.interface.Interface interface: The interface for which to
        proxy.
    :param int comparison_result: A value to return from ``__cmp__`` as
        implemented on the proxy type.

    :return: The new proxy type.
    """
    class ComparisonProxy(proxyForInterface(interface, "_original")):
        def __cmp__(self, other):
            return comparison_result
    return ComparisonProxy 
开发者ID:ClusterHQ,项目名称:flocker,代码行数:18,代码来源:test_change.py

示例6: __init__

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def __init__(self, realmName, services):
        recordTypes = set()

        for service in services:
            if not IDirectoryService.implementedBy(service.__class__):
                raise ValueError(
                    "Not a directory service: {0}".format(service)
                )

            service = proxyForInterface(
                IDirectoryService, originalAttribute="_service"
            )(service)

            for recordType in service.recordTypes():
                if recordType in recordTypes:
                    raise DirectoryConfigurationError(
                        "Aggregated services may not vend "
                        "the same record type: {0}"
                        .format(recordType)
                    )
                recordTypes.add(recordType)

        BaseDirectoryService.__init__(self, realmName)

        self._services = tuple(services) 
开发者ID:apple,项目名称:ccs-twistedextensions,代码行数:27,代码来源:aggregate.py

示例7: __eq__

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def __eq__(self, other):
        """
        Determines if two requests are the same object.

        @param other: Another object whose identity will be compared
            to this instance's.

        @return: L{True} when the two are the same object and L{False}
            when not.
        @rtype: L{bool}
        """
        # When other is not an instance of request, return
        # NotImplemented so that Python uses other.__eq__ to perform
        # the comparison.  This ensures that a Request proxy generated
        # by proxyForInterface compares equal to an actual Request
        # instanceby turning request != proxy into proxy != request.
        if isinstance(other, Request):
            return self is other
        return NotImplemented 
开发者ID:wistbean,项目名称:learn_python3_spider,代码行数:21,代码来源:http.py

示例8: __ne__

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def __ne__(self, other):
        """
        Determines if two requests are not the same object.

        @param other: Another object whose identity will be compared
            to this instance's.

        @return: L{True} when the two are not the same object and
            L{False} when they are.
        @rtype: L{bool}
        """
        # When other is not an instance of request, return
        # NotImplemented so that Python uses other.__ne__ to perform
        # the comparison.  This ensures that a Request proxy generated
        # by proxyForInterface can compare equal to an actual Request
        # instance by turning request != proxy into proxy != request.
        if isinstance(other, Request):
            return self is not other
        return NotImplemented 
开发者ID:wistbean,项目名称:learn_python3_spider,代码行数:21,代码来源:http.py

示例9: test_subclassing

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def test_subclassing(self):
        """
        It is possible to subclass the result of L{proxyForInterface}.
        """

        class SpecializedProxy(proxyForInterface(IProxiedInterface)):
            """
            A specialized proxy which can decrement the number of yays.
            """
            def boo(self):
                """
                Decrement the number of yays.
                """
                self.original.yays -= 1

        yayable = Yayable()
        special = SpecializedProxy(yayable)
        self.assertEquals(yayable.yays, 0)
        special.boo()
        self.assertEquals(yayable.yays, -1) 
开发者ID:kuri65536,项目名称:python-for-android,代码行数:22,代码来源:test_components.py

示例10: test_proxyInheritance

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def test_proxyInheritance(self):
        """
        Subclasses of the class returned from L{proxyForInterface} should be
        able to upcall methods by reference to their superclass, as any normal
        Python class can.
        """
        class YayableWrapper(proxyForInterface(IProxiedInterface)):
            """
            This class does not override any functionality.
            """

        class EnhancedWrapper(YayableWrapper):
            """
            This class overrides the 'yay' method.
            """
            wrappedYays = 1
            def yay(self, *a, **k):
                self.wrappedYays += 1
                return YayableWrapper.yay(self, *a, **k) + 7

        yayable = Yayable()
        wrapper = EnhancedWrapper(yayable)
        self.assertEquals(wrapper.yay(3, 4, x=5, y=6), 8)
        self.assertEquals(yayable.yayArgs,
                          [((3, 4), dict(x=5, y=6))]) 
开发者ID:kuri65536,项目名称:python-for-android,代码行数:27,代码来源:test_components.py

示例11: _loginSucceeded

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def _loginSucceeded(self, args):
        """
        Handle login success by wrapping the resulting L{IResource} avatar
        so that the C{logout} callback will be invoked when rendering is
        complete.
        """
        interface, avatar, logout = args
        class ResourceWrapper(proxyForInterface(IResource, 'resource')):
            """
            Wrap an L{IResource} so that whenever it or a child of it
            completes rendering, the cred logout hook will be invoked.

            An assumption is made here that exactly one L{IResource} from
            among C{avatar} and all of its children will be rendered.  If
            more than one is rendered, C{logout} will be invoked multiple
            times and probably earlier than desired.
            """
            def getChildWithDefault(self, name, request):
                """
                Pass through the lookup to the wrapped resource, wrapping
                the result in L{ResourceWrapper} to ensure C{logout} is
                called when rendering of the child is complete.
                """
                return ResourceWrapper(self.resource.getChildWithDefault(name, request))

            def render(self, request):
                """
                Hook into response generation so that when rendering has
                finished completely (with or without error), C{logout} is
                called.
                """
                request.notifyFinish().addBoth(lambda ign: logout())
                return super(ResourceWrapper, self).render(request)

        return ResourceWrapper(avatar) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:37,代码来源:wrapper.py

示例12: test_original

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def test_original(self):
        """
        Proxy objects should have an C{original} attribute which refers to the
        original object passed to the constructor.
        """
        original = object()
        proxy = proxyForInterface(IProxiedInterface)(original)
        self.assertIs(proxy.original, original) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:10,代码来源:test_components.py

示例13: test_proxyMethod

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def test_proxyMethod(self):
        """
        The class created from L{proxyForInterface} passes methods on an
        interface to the object which is passed to its constructor.
        """
        klass = proxyForInterface(IProxiedInterface)
        yayable = Yayable()
        proxy = klass(yayable)
        proxy.yay()
        self.assertEqual(proxy.yay(), 2)
        self.assertEqual(yayable.yays, 2) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:13,代码来源:test_components.py

示例14: test_proxySetAttribute

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def test_proxySetAttribute(self):
        """
        The attributes that proxy objects proxy should be assignable and affect
        the original object.
        """
        yayable = Yayable()
        proxy = proxyForInterface(IProxiedInterface)(yayable)
        thingy = object()
        proxy.ifaceAttribute = thingy
        self.assertIs(yayable.ifaceAttribute, thingy) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:12,代码来源:test_components.py

示例15: test_proxyDeleteAttribute

# 需要导入模块: from twisted.python import components [as 别名]
# 或者: from twisted.python.components import proxyForInterface [as 别名]
def test_proxyDeleteAttribute(self):
        """
        The attributes that proxy objects proxy should be deletable and affect
        the original object.
        """
        yayable = Yayable()
        yayable.ifaceAttribute = None
        proxy = proxyForInterface(IProxiedInterface)(yayable)
        del proxy.ifaceAttribute
        self.assertFalse(hasattr(yayable, 'ifaceAttribute')) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:12,代码来源:test_components.py


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