我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用zope.interface.Attribute()。
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'])
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']), ])
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']), ])
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)
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))
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'])
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')
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')
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)
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'])
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)
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)
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)
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)
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)
def test___doc___element(self): class I(Interface): "xxx" self.assertEqual(I.__doc__, "xxx") self.assertEqual(list(I), []) class I(Interface): "xxx" __doc__ = Attribute('the doc') self.assertEqual(I.__doc__, "") self.assertEqual(list(I), ['__doc__'])
def test_invariant_as_decorator(): """Invaiants can be deined in line >>> class IRange(interface.Interface): ... min = interface.Attribute("Lower bound") ... max = interface.Attribute("Upper bound") ... ... @interface.invariant ... def range_invariant(ob): ... if ob.max < ob.min: ... raise Invalid('max < min') >>> class Range(object): ... interface.implements(IRange) ... ... def __init__(self, min, max): ... self.min, self.max = min, max >>> IRange.validateInvariants(Range(1,2)) >>> IRange.validateInvariants(Range(1,1)) >>> IRange.validateInvariants(Range(2,1)) Traceback (most recent call last): ... Invalid: max < min """
def testMethodForAttr(self): class IFoo(Interface): foo = Attribute("The foo Attribute") class Foo: implements(IFoo) def foo(self): pass verifyClass(IFoo, Foo)
def test_schema_wo_fields(self): from zope.interface import Interface from zope.interface import Attribute class INoFields(Interface): def method(): pass attr = Attribute('ignoreme') errors = self._callFUT(INoFields, object()) self.assertEqual(len(errors), 0)
def test_get_hit_from_base_wo__v_attrs(self): from zope.interface.interface import Attribute from zope.interface.interface import Interface class IFoo(Interface): foo = Attribute('foo') class IBar(Interface): bar = Attribute('bar') spec = self._makeOne([IFoo, IBar]) self.assertTrue(spec.get('foo') is IFoo.get('foo')) self.assertTrue(spec.get('bar') is IBar.get('bar'))
def test_ctor_w_attrs_attrib_methods(self): from zope.interface.interface import Attribute from zope.interface.interface import fromFunction def _bar(): """DOCSTRING""" ATTRS = {'foo': Attribute('Foo', ''), 'bar': fromFunction(_bar), } klass = self._getTargetClass() inst = klass('ITesting', attrs=ATTRS) self.assertEqual(inst.__name__, 'ITesting') self.assertEqual(inst.__doc__, '') self.assertEqual(inst.__bases__, ()) self.assertEqual(inst.names(), ATTRS.keys())
def test_names_w_all_False_ignores_bases(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.names(all=False)), ['baz'])
def test_names_w_all_True_no_bases(self): from zope.interface.interface import Attribute from zope.interface.interface import fromFunction def _bar(): """DOCSTRING""" ATTRS = {'foo': Attribute('Foo', ''), 'bar': fromFunction(_bar), } one = self._makeOne(attrs=ATTRS) self.assertEqual(sorted(one.names(all=True)), ['bar', 'foo'])
def test_names_w_all_True_w_bases_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.names(all=True)), ['bar', 'baz', 'foo'])
def test_namesAndDescriptions_w_all_False_ignores_bases(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=False)), [('baz', DERIVED_ATTRS['baz']), ])
def test_namesAndDescriptions_w_all_True_no_bases(self): from zope.interface.interface import Attribute from zope.interface.interface import fromFunction def _bar(): """DOCSTRING""" ATTRS = {'foo': Attribute('Foo', ''), 'bar': fromFunction(_bar), } one = self._makeOne(attrs=ATTRS) self.assertEqual(sorted(one.namesAndDescriptions(all=False)), [('bar', ATTRS['bar']), ('foo', ATTRS['foo']), ])
def test___getitem___hit(self): from zope.interface.interface import Attribute from zope.interface.interface import fromFunction def _bar(): """DOCSTRING""" ATTRS = {'foo': Attribute('Foo', ''), 'bar': fromFunction(_bar), } one = self._makeOne(attrs=ATTRS) self.assertEqual(one['foo'], ATTRS['foo']) self.assertEqual(one['bar'], ATTRS['bar'])
def test___contains___hit(self): from zope.interface.interface import Attribute from zope.interface.interface import fromFunction def _bar(): """DOCSTRING""" ATTRS = {'foo': Attribute('Foo', ''), 'bar': fromFunction(_bar), } one = self._makeOne(attrs=ATTRS) self.assertTrue('foo' in one) self.assertTrue('bar' in one)
def test_queryDescriptionFor_hit(self): from zope.interface import Attribute ATTRS = {'attr': Attribute('Title', 'Description')} iface = self._makeOne(attrs=ATTRS) self.assertEqual(iface.queryDescriptionFor('attr'), ATTRS['attr'])
def test_attributes_link_to_interface(self): from zope.interface import Interface from zope.interface import Attribute class I1(Interface): attr = Attribute("My attr") self.assertTrue(I1['attr'].interface is I1)
def test_names_simple(self): from zope.interface import Attribute from zope.interface import Interface class ISimple(Interface): attr = Attribute(u'My attr') def method(): pass self.assertEqual(sorted(ISimple.names()), ['attr', 'method'])
def test_getDescriptionFor_derived(self): from zope.interface import Attribute from zope.interface.interface import Method 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" a_desc = IDerived.getDescriptionFor('attr') self.assertTrue(isinstance(a_desc, Attribute)) self.assertEqual(a_desc.__name__, 'attr') self.assertEqual(a_desc.__doc__, 'My attr') m_desc = IDerived.getDescriptionFor('method') self.assertTrue(isinstance(m_desc, Method)) self.assertEqual(m_desc.__name__, 'method') self.assertEqual(m_desc.__doc__, 'My method, overridden') a2_desc = IDerived.getDescriptionFor('attr2') self.assertTrue(isinstance(a2_desc, Attribute)) self.assertEqual(a2_desc.__name__, 'attr2') self.assertEqual(a2_desc.__doc__, 'My attr2') m2_desc = IDerived.getDescriptionFor('method2') self.assertTrue(isinstance(m2_desc, Method)) self.assertEqual(m2_desc.__name__, 'method2') self.assertEqual(m2_desc.__doc__, 'My method2')