當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。