Python zope.interface.Attribute() Examples

The following are 30 code examples of zope.interface.Attribute(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module zope.interface , or try the search function .
Example #1
Source File: test_interface.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_namesAndDescriptions_simple(self):
        from zope.interface import Attribute
        from zope.interface.interface import Method
        from zope.interface import Interface
        

        class ISimple(Interface):
            attr = Attribute(u'My attr')

            def method():
                "My method"

        name_values = sorted(ISimple.namesAndDescriptions())

        self.assertEqual(len(name_values), 2)
        self.assertEqual(name_values[0][0], 'attr')
        self.assertTrue(isinstance(name_values[0][1], Attribute))
        self.assertEqual(name_values[0][1].__name__, 'attr')
        self.assertEqual(name_values[0][1].__doc__, 'My attr')
        self.assertEqual(name_values[1][0], 'method')
        self.assertTrue(isinstance(name_values[1][1], Method))
        self.assertEqual(name_values[1][1].__name__, 'method')
        self.assertEqual(name_values[1][1].__doc__, 'My method') 
Example #2
Source File: test_interface.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_direct_hit_local_miss_bases(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        def _foo():
            """DOCSTRING"""
        BASE_ATTRS = {'foo': Attribute('Foo', ''),
                      'bar': fromFunction(_bar),
                     }
        DERIVED_ATTRS = {'foo': fromFunction(_foo),
                         'baz': Attribute('Baz', ''),
                        }
        base = self._makeOne('IBase', attrs=BASE_ATTRS)
        derived = self._makeOne('IDerived', bases=(base,), attrs=DERIVED_ATTRS)
        self.assertEqual(derived.direct('foo'), DERIVED_ATTRS['foo'])
        self.assertEqual(derived.direct('baz'), DERIVED_ATTRS['baz'])
        self.assertEqual(derived.direct('bar'), None) 
Example #3
Source File: test_interface.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_namesAndDescriptions_simple(self):
        from zope.interface import Attribute
        from zope.interface.interface import Method
        from zope.interface import Interface


        class ISimple(Interface):
            attr = Attribute(u'My attr')

            def method():
                "My method"

        name_values = sorted(ISimple.namesAndDescriptions())

        self.assertEqual(len(name_values), 2)
        self.assertEqual(name_values[0][0], 'attr')
        self.assertTrue(isinstance(name_values[0][1], Attribute))
        self.assertEqual(name_values[0][1].__name__, 'attr')
        self.assertEqual(name_values[0][1].__doc__, 'My attr')
        self.assertEqual(name_values[1][0], 'method')
        self.assertTrue(isinstance(name_values[1][1], Method))
        self.assertEqual(name_values[1][1].__name__, 'method')
        self.assertEqual(name_values[1][1].__doc__, 'My method') 
Example #4
Source File: test_interface.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_namesAndDescriptions_w_all_True_bases_w_same_names(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        def _foo():
            """DOCSTRING"""
        BASE_ATTRS = {'foo': Attribute('Foo', ''),
                      'bar': fromFunction(_bar),
                     }
        DERIVED_ATTRS = {'foo': fromFunction(_foo),
                         'baz': Attribute('Baz', ''),
                        }
        base = self._makeOne('IBase', attrs=BASE_ATTRS)
        derived = self._makeOne('IDerived', bases=(base,), attrs=DERIVED_ATTRS)
        self.assertEqual(sorted(derived.namesAndDescriptions(all=True)),
                        [('bar', BASE_ATTRS['bar']),
                         ('baz', DERIVED_ATTRS['baz']),
                         ('foo', DERIVED_ATTRS['foo']),
                        ]) 
Example #5
Source File: test_interface.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_namesAndDescriptions_w_all_True_simple(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        BASE_ATTRS = {'foo': Attribute('Foo', ''),
                      'bar': fromFunction(_bar),
                     }
        DERIVED_ATTRS = {'baz': Attribute('Baz', ''),
                        }
        base = self._makeOne('IBase', attrs=BASE_ATTRS)
        derived = self._makeOne('IDerived', bases=(base,), attrs=DERIVED_ATTRS)
        self.assertEqual(sorted(derived.namesAndDescriptions(all=True)),
                        [('bar', BASE_ATTRS['bar']),
                         ('baz', DERIVED_ATTRS['baz']),
                         ('foo', BASE_ATTRS['foo']),
                        ]) 
Example #6
Source File: test_document.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_asReStructuredText_with_attribute_no_docstring(self):
        from zope.interface import Attribute
        from zope.interface import Interface
        EXPECTED = '\n\n'.join([
            "``IHasAttribute``",
            " This interface has an attribute.",
            " Attributes:",
            "  ``an_attribute`` -- no documentation",
            " Methods:",
            ""
        ])
        class IHasAttribute(Interface):
            """ This interface has an attribute.
            """
            an_attribute = Attribute('an_attribute')

        self.assertEqual(self._callFUT(IHasAttribute), EXPECTED) 
Example #7
Source File: test_interface.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test___getitem__simple(self):
        from zope.interface import Attribute
        from zope.interface.interface import Method
        from zope.interface import Interface


        class ISimple(Interface):
            attr = Attribute(u'My attr')

            def method():
                "My method"

        a_desc = ISimple['attr']
        self.assertTrue(isinstance(a_desc, Attribute))
        self.assertEqual(a_desc.__name__, 'attr')
        self.assertEqual(a_desc.__doc__, 'My attr')

        m_desc = ISimple['method']
        self.assertTrue(isinstance(m_desc, Method))
        self.assertEqual(m_desc.__name__, 'method')
        self.assertEqual(m_desc.__doc__, 'My method') 
Example #8
Source File: test_document.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_asReStructuredText_with_attribute_with_docstring(self):
        from zope.interface import Attribute
        from zope.interface import Interface
        EXPECTED = '\n\n'.join([
            "``IHasAttribute``",
            " This interface has an attribute.",
            " Attributes:",
            "  ``an_attribute`` -- This attribute is documented.",
            " Methods:",
            ""
        ])
        class IHasAttribute(Interface):
            """ This interface has an attribute.
            """
            an_attribute = Attribute('an_attribute',
                                     'This attribute is documented.')

        self.assertEqual(self._callFUT(IHasAttribute), EXPECTED) 
Example #9
Source File: test_interface.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test___iter__(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        def _foo():
            """DOCSTRING"""
        BASE_ATTRS = {'foo': Attribute('Foo', ''),
                      'bar': fromFunction(_bar),
                     }
        DERIVED_ATTRS = {'foo': fromFunction(_foo),
                         'baz': Attribute('Baz', ''),
                        }
        base = self._makeOne('IBase', attrs=BASE_ATTRS)
        derived = self._makeOne('IDerived', bases=(base,), attrs=DERIVED_ATTRS)
        self.assertEqual(sorted(derived), ['bar', 'baz', 'foo']) 
Example #10
Source File: test_interface.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test___contains__derived(self):
        from zope.interface import Attribute
        from zope.interface import Interface


        class IBase(Interface):
            attr = Attribute(u'My attr')

            def method():
                "My method"

        class IDerived(IBase):
            attr2 = Attribute(u'My attr2')

            def method():
                "My method, overridden"

            def method2():
                "My method2"

        self.assertTrue('attr' in IDerived)
        self.assertTrue('method' in IDerived)
        self.assertTrue('attr2' in IDerived)
        self.assertTrue('method2' in IDerived) 
Example #11
Source File: test_interface.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test___iter__derived(self):
        from zope.interface import Attribute
        from zope.interface import Interface


        class IBase(Interface):
            attr = Attribute(u'My attr')

            def method():
                "My method"

        class IDerived(IBase):
            attr2 = Attribute(u'My attr2')

            def method():
                "My method, overridden"

            def method2():
                "My method2"

        self.assertEqual(sorted(list(IDerived)),
                         ['attr', 'attr2', 'method', 'method2']) 
Example #12
Source File: test_interface.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_names_derived(self):
        from zope.interface import Attribute
        from zope.interface import Interface


        class IBase(Interface):
            attr = Attribute(u'My attr')

            def method():
                pass

        class IDerived(IBase):
            attr2 = Attribute(u'My attr2')

            def method():
                pass

            def method2():
                pass

        self.assertEqual(sorted(IDerived.names()),
                         ['attr2', 'method', 'method2'])
        self.assertEqual(sorted(IDerived.names(all=True)),
                         ['attr', 'attr2', 'method', 'method2']) 
Example #13
Source File: test_interface.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_verifyObject(self):
        from zope.interface import Attribute
        from zope.interface import Interface
        from zope.interface.verify import verifyObject
        

        class ICheckMe(Interface):
            attr = Attribute(u'My attr')

            def method():
                pass

        class CheckMe(object):
            __implemented__ = ICheckMe
            attr = 'value'

            def method(self):
                pass

        check_me = CheckMe()

        self.assertTrue(verifyObject(ICheckMe, check_me)) 
Example #14
Source File: test_interface.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_verifyObject(self):
        from zope.interface import Attribute
        from zope.interface import Interface
        from zope.interface.verify import verifyObject


        class ICheckMe(Interface):
            attr = Attribute(u'My attr')

            def method():
                "A method"

        class CheckMe(object):
            __implemented__ = ICheckMe
            attr = 'value'

            def method(self):
                raise NotImplementedError()

        check_me = CheckMe()

        self.assertTrue(verifyObject(ICheckMe, check_me)) 
Example #15
Source File: test_interface.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_names_derived(self):
        from zope.interface import Attribute
        from zope.interface import Interface
        

        class IBase(Interface):
            attr = Attribute(u'My attr')

            def method():
                pass

        class IDerived(IBase):
            attr2 = Attribute(u'My attr2')

            def method():
                pass

            def method2():
                pass

        self.assertEqual(sorted(IDerived.names()),
                         ['attr2', 'method', 'method2'])
        self.assertEqual(sorted(IDerived.names(all=True)),
                         ['attr', 'attr2', 'method', 'method2']) 
Example #16
Source File: test_document.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_asStructuredText_with_attribute_with_docstring(self):
        from zope.interface import Attribute
        from zope.interface import Interface
        EXPECTED = '\n\n'.join([
            "IHasAttribute",
            " This interface has an attribute.",
            " Attributes:",
            "  an_attribute -- This attribute is documented.",
            " Methods:",
            ""
        ])
        class IHasAttribute(Interface):
            """ This interface has an attribute.
            """
            an_attribute = Attribute('an_attribute',
                                     'This attribute is documented.')

        self.assertEqual(self._callFUT(IHasAttribute), EXPECTED) 
Example #17
Source File: test_interface.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test___getitem__simple(self):
        from zope.interface import Attribute
        from zope.interface.interface import Method
        from zope.interface import Interface
        

        class ISimple(Interface):
            attr = Attribute(u'My attr')

            def method():
                "My method"

        a_desc = ISimple['attr']
        self.assertTrue(isinstance(a_desc, Attribute))
        self.assertEqual(a_desc.__name__, 'attr')
        self.assertEqual(a_desc.__doc__, 'My attr')

        m_desc = ISimple['method']
        self.assertTrue(isinstance(m_desc, Method))
        self.assertEqual(m_desc.__name__, 'method')
        self.assertEqual(m_desc.__doc__, 'My method') 
Example #18
Source File: test_interface.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_direct_hit_local_miss_bases(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        def _foo():
            """DOCSTRING"""
        BASE_ATTRS = {'foo': Attribute('Foo', ''),
                      'bar': fromFunction(_bar),
                     }
        DERIVED_ATTRS = {'foo': fromFunction(_foo),
                         'baz': Attribute('Baz', ''),
                        }
        base = self._makeOne('IBase', attrs=BASE_ATTRS)
        derived = self._makeOne('IDerived', bases=(base,), attrs=DERIVED_ATTRS)
        self.assertEqual(derived.direct('foo'), DERIVED_ATTRS['foo'])
        self.assertEqual(derived.direct('baz'), DERIVED_ATTRS['baz'])
        self.assertEqual(derived.direct('bar'), None) 
Example #19
Source File: test_interface.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test___contains__derived(self):
        from zope.interface import Attribute
        from zope.interface import Interface
        

        class IBase(Interface):
            attr = Attribute(u'My attr')

            def method():
                "My method"

        class IDerived(IBase):
            attr2 = Attribute(u'My attr2')

            def method():
                "My method, overridden"

            def method2():
                "My method2"

        self.assertTrue('attr' in IDerived)
        self.assertTrue('method' in IDerived)
        self.assertTrue('attr2' in IDerived)
        self.assertTrue('method2' in IDerived) 
Example #20
Source File: test_interface.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test___iter__derived(self):
        from zope.interface import Attribute
        from zope.interface import Interface
        

        class IBase(Interface):
            attr = Attribute(u'My attr')

            def method():
                "My method"

        class IDerived(IBase):
            attr2 = Attribute(u'My attr2')

            def method():
                "My method, overridden"

            def method2():
                "My method2"

        self.assertEqual(sorted(list(IDerived)),
                         ['attr', 'attr2', 'method', 'method2']) 
Example #21
Source File: test_interface.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_namesAndDescriptions_w_all_True_bases_w_same_names(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        def _foo():
            """DOCSTRING"""
        BASE_ATTRS = {'foo': Attribute('Foo', ''),
                      'bar': fromFunction(_bar),
                     }
        DERIVED_ATTRS = {'foo': fromFunction(_foo),
                         'baz': Attribute('Baz', ''),
                        }
        base = self._makeOne('IBase', attrs=BASE_ATTRS)
        derived = self._makeOne('IDerived', bases=(base,), attrs=DERIVED_ATTRS)
        self.assertEqual(sorted(derived.namesAndDescriptions(all=True)),
                        [('bar', BASE_ATTRS['bar']),
                         ('baz', DERIVED_ATTRS['baz']),
                         ('foo', DERIVED_ATTRS['foo']),
                        ]) 
Example #22
Source File: test_interface.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_namesAndDescriptions_w_all_True_simple(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        BASE_ATTRS = {'foo': Attribute('Foo', ''),
                      'bar': fromFunction(_bar),
                     }
        DERIVED_ATTRS = {'baz': Attribute('Baz', ''),
                        }
        base = self._makeOne('IBase', attrs=BASE_ATTRS)
        derived = self._makeOne('IDerived', bases=(base,), attrs=DERIVED_ATTRS)
        self.assertEqual(sorted(derived.namesAndDescriptions(all=True)),
                        [('bar', BASE_ATTRS['bar']),
                         ('baz', DERIVED_ATTRS['baz']),
                         ('foo', BASE_ATTRS['foo']),
                        ]) 
Example #23
Source File: test_interface.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test___iter__(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        def _foo():
            """DOCSTRING"""
        BASE_ATTRS = {'foo': Attribute('Foo', ''),
                      'bar': fromFunction(_bar),
                     }
        DERIVED_ATTRS = {'foo': fromFunction(_foo),
                         'baz': Attribute('Baz', ''),
                        }
        base = self._makeOne('IBase', attrs=BASE_ATTRS)
        derived = self._makeOne('IDerived', bases=(base,), attrs=DERIVED_ATTRS)
        self.assertEqual(sorted(derived), ['bar', 'baz', 'foo']) 
Example #24
Source File: test_interface.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_description_cache_management(self):
        # See https://bugs.launchpad.net/zope.interface/+bug/185974
        # There was a bug where the cache used by Specification.get() was not
        # cleared when the bases were changed.
        from zope.interface import Interface
        from zope.interface import Attribute

        class I1(Interface):
            a = Attribute('a')

        class I2(I1):
            pass

        class I3(I2):
            pass

        self.assertTrue(I3.get('a') is I1.get('a'))

        I2.__bases__ = (Interface,)
        self.assertTrue(I3.get('a') is None) 
Example #25
Source File: test_verify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_class_misses_attribute_for_attribute(self):
        # This check *fails* for verifyObject
        from zope.interface import Attribute
        from zope.interface import Interface
        from zope.interface import implementer
        from zope.interface.exceptions import BrokenImplementation

        class ICurrent(Interface):
            attr = Attribute("The foo Attribute")

        @implementer(ICurrent)
        class Current:
            pass

        self.assertRaises(BrokenImplementation,
                          self._callFUT, ICurrent, Current) 
Example #26
Source File: _diffing.py    From flocker with Apache License 2.0 6 votes vote down vote up
def _get_or_add_proxy_child(parent_proxy, segment):
    """
    Returns a proxy wrapper around the ``_IEvolvable`` object corresponding to
    ``segment``. A new proxy is created if one does not already exist and it is
    added to ``parent_proxy._children``.

    :param _IParentProxy parent_proxy: The parent.
    :param unicode segment: The label in a ``path`` supplied to ``transform``.
    :returns:
    """
    child = parent_proxy._children.get(segment)
    if child is not None:
        return child
    child = _get(parent_proxy._original, segment, _sentinel)
    if child is _sentinel:
        raise KeyError(
            "Attribute or key '{}' not found in {}".format(
                segment, parent_proxy._original
            )
        )
    proxy_for_child = _proxy_for_evolvable_object(child)
    parent_proxy._children[segment] = proxy_for_child
    return proxy_for_child 
Example #27
Source File: test_document.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_asStructuredText_with_attribute_no_docstring(self):
        from zope.interface import Attribute
        from zope.interface import Interface
        EXPECTED = '\n\n'.join([
            "IHasAttribute",
            " This interface has an attribute.",
            " Attributes:",
            "  an_attribute -- no documentation",
            " Methods:",
            ""
        ])
        class IHasAttribute(Interface):
            """ This interface has an attribute.
            """
            an_attribute = Attribute('an_attribute')

        self.assertEqual(self._callFUT(IHasAttribute), EXPECTED) 
Example #28
Source File: test_document.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_asStructuredText_with_attribute_with_docstring(self):
        from zope.interface import Attribute
        from zope.interface import Interface
        EXPECTED = '\n\n'.join([
            "IHasAttribute",
            " This interface has an attribute.",
            " Attributes:",
            "  an_attribute -- This attribute is documented.",
            " Methods:",
            ""
        ])
        class IHasAttribute(Interface):
            """ This interface has an attribute.
            """
            an_attribute = Attribute('an_attribute',
                                     'This attribute is documented.')

        self.assertEqual(self._callFUT(IHasAttribute), EXPECTED) 
Example #29
Source File: test_document.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_asReStructuredText_with_attribute_no_docstring(self):
        from zope.interface import Attribute
        from zope.interface import Interface
        EXPECTED = '\n\n'.join([
            "``IHasAttribute``",
            " This interface has an attribute.",
            " Attributes:",
            "  ``an_attribute`` -- no documentation",
            " Methods:",
            ""
        ])
        class IHasAttribute(Interface):
            """ This interface has an attribute.
            """
            an_attribute = Attribute('an_attribute')

        self.assertEqual(self._callFUT(IHasAttribute), EXPECTED) 
Example #30
Source File: test_document.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_asReStructuredText_with_attribute_with_docstring(self):
        from zope.interface import Attribute
        from zope.interface import Interface
        EXPECTED = '\n\n'.join([
            "``IHasAttribute``",
            " This interface has an attribute.",
            " Attributes:",
            "  ``an_attribute`` -- This attribute is documented.",
            " Methods:",
            ""
        ])
        class IHasAttribute(Interface):
            """ This interface has an attribute.
            """
            an_attribute = Attribute('an_attribute',
                                     'This attribute is documented.')

        self.assertEqual(self._callFUT(IHasAttribute), EXPECTED)