我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用zope.interface.implementedBy()。
def testClassImplements(self): self.assert_(IC.implementedBy(C)) self.assert_(I1.implementedBy(A)) self.assert_(I1.implementedBy(B)) self.assert_(not I1.implementedBy(C)) self.assert_(I1.implementedBy(D)) self.assert_(I1.implementedBy(E)) self.assert_(not I2.implementedBy(A)) self.assert_(I2.implementedBy(B)) self.assert_(not I2.implementedBy(C)) # No longer after interfacegeddon # self.assert_(not I2.implementedBy(D)) self.assert_(not I2.implementedBy(E))
def test_classImplements_base_not_derived(self): from zope.interface import Interface from zope.interface import implementedBy from zope.interface import providedBy class IBase(Interface): def method(): pass class IDerived(IBase): pass class Current(): __implemented__ = IBase def method(self): pass current = Current() self.assertTrue(IBase.implementedBy(Current)) self.assertFalse(IDerived.implementedBy(Current)) self.assertTrue(IBase in implementedBy(Current)) self.assertFalse(IDerived in implementedBy(Current)) self.assertTrue(IBase in providedBy(current)) self.assertFalse(IDerived in providedBy(current))
def test_classImplements(self): @implementer(I3) class A(Odd): pass @implementer(I4) class B(Odd): pass class C(A, B): pass classImplements(C, I1, I2) self.assertEqual([i.getName() for i in implementedBy(C)], ['I1', 'I2', 'I3', 'I4']) classImplements(C, I5) self.assertEqual([i.getName() for i in implementedBy(C)], ['I1', 'I2', 'I5', 'I3', 'I4'])
def testUtil(self): self.assert_(IC in implementedBy(C)) self.assert_(I1 in implementedBy(A)) self.assert_(not I1 in implementedBy(C)) self.assert_(I2 in implementedBy(B)) self.assert_(not I2 in implementedBy(C)) self.assert_(IC in providedBy(C())) self.assert_(I1 in providedBy(A())) self.assert_(not I1 in providedBy(C())) self.assert_(I2 in providedBy(B())) self.assert_(not I2 in providedBy(C()))
def test_classImplements(self): class A(Odd): implements(I3) class B(Odd): implements(I4) class C(A, B): pass classImplements(C, I1, I2) self.assertEqual([i.getName() for i in implementedBy(C)], ['I1', 'I2', 'I3', 'I4']) classImplements(C, I5) self.assertEqual([i.getName() for i in implementedBy(C)], ['I1', 'I2', 'I5', 'I3', 'I4'])
def test_classImplementsOnly(self): class A(Odd): implements(I3) class B(Odd): implements(I4) class C(A, B): pass classImplementsOnly(C, I1, I2) self.assertEqual([i.__name__ for i in implementedBy(C)], ['I1', 'I2'])
def test_implementedBy(self): class I2(I1): pass class C1(Odd): implements(I2) class C2(C1): implements(I3) self.assertEqual([i.getName() for i in implementedBy(C2)], ['I3', 'I2'])
def get_interfaces(self): if self._interfaces is not None: spec = Implements(*self._interfaces) spec.__name__ = getattr(self._callable, '__name__', '[callable]') return spec return implementedBy(self._callable)
def utility(_context, provides=None, component=None, factory=None, name=''): if factory and component: raise TypeError("Can't specify factory and component.") if provides is None: if factory: provides = list(implementedBy(factory)) else: provides = list(providedBy(component)) if len(provides) == 1: provides = provides[0] else: raise TypeError("Missing 'provides' attribute") if name == '': if factory: name = getName(factory) else: name = getName(component) _context.action( discriminator=('utility', provides, name), callable=handler, args=('registerUtility', component, provides, name), kw=dict(factory=factory)) _context.action( discriminator=None, callable=provide_interface, args=('', provides))
def __provides__(self): return providedBy(self.field) + implementedBy(FieldReadAccessor)
def test___provides___w_field_no_provides(self): from zope.interface import implementedBy from zope.interface import providedBy wrapped = self._makeOne(object()) self.assertEqual(list(providedBy(wrapped)), list(implementedBy(self._getTargetClass())))
def test_implementedBy_miss(self): from zope.interface import interface from zope.interface.declarations import _empty sb = self._makeOne() def _implementedBy(obj): return _empty with _Monkey(interface, implementedBy=_implementedBy): self.assertFalse(sb.implementedBy(object()))
def test_implementedBy_hit(self): from zope.interface import interface sb = self._makeOne() class _Decl(object): _implied = {sb: {},} def _implementedBy(obj): return _Decl() with _Monkey(interface, implementedBy=_implementedBy): self.assertTrue(sb.implementedBy(object()))
def test_classImplements_simple(self): from zope.interface import Interface from zope.interface import implementedBy from zope.interface import providedBy class ICurrent(Interface): def method1(a, b): pass def method2(a, b): pass class IOther(Interface): pass class Current(object): __implemented__ = ICurrent def method1(self, a, b): return 1 def method2(self, a, b): return 2 current = Current() self.assertTrue(ICurrent.implementedBy(Current)) self.assertFalse(IOther.implementedBy(Current)) self.assertTrue(ICurrent in implementedBy(Current)) self.assertFalse(IOther in implementedBy(Current)) self.assertTrue(ICurrent in providedBy(current)) self.assertFalse(IOther in providedBy(current))
def test_classImplements_base_and_derived(self): from zope.interface import Interface from zope.interface import implementedBy from zope.interface import providedBy class IBase(Interface): def method(): pass class IDerived(IBase): pass class Current(object): __implemented__ = IDerived def method(self): pass current = Current() self.assertTrue(IBase.implementedBy(Current)) self.assertTrue(IDerived.implementedBy(Current)) self.assertFalse(IBase in implementedBy(Current)) self.assertTrue(IBase in implementedBy(Current).flattened()) self.assertTrue(IDerived in implementedBy(Current)) self.assertFalse(IBase in providedBy(current)) self.assertTrue(IBase in providedBy(current).flattened()) self.assertTrue(IDerived in providedBy(current))
def test_classImplementsOnly(self): @implementer(I3) class A(Odd): pass @implementer(I4) class B(Odd): pass class C(A, B): pass classImplementsOnly(C, I1, I2) self.assertEqual([i.__name__ for i in implementedBy(C)], ['I1', 'I2'])
def test_implementedBy(self): class I2(I1): pass class C1(Odd): pass classImplements(C1, I2) class C2(C1): pass classImplements(C2, I3) self.assertEqual([i.getName() for i in implementedBy(C2)], ['I3', 'I2'])
def adapter_for_class(class_, adapter_interface): gsm = getGlobalSiteManager() adapter = gsm.adapters.registered((implementedBy(class_),), adapter_interface) if adapter: return adapter # recurse up the inheritence tree to find an adapter, if we don't have one # registered for this class directly. for base_class in class_.__bases__: return adapter_for_class(base_class, adapter_interface)
def __call__(self): result = {'type': self.field_type} for schema in implementedBy(self.field.__class__).flattened(): self.field_attributes.update(getFields(schema)) for attribute_name in sorted(self.field_attributes.keys()): attribute_field = self.field_attributes[attribute_name] if attribute_name in self.filtered_attributes: continue element_name = attribute_field.__name__ attribute_field = attribute_field.bind(self.field) force = (element_name in self.forced_fields) value = attribute_field.get(self.field) # For 'default', 'missing_value' etc, we want to validate against # the imported field type itself, not the field type of the # attribute if element_name in self.field_type_attributes or \ element_name in self.non_validated_field_type_attributes: attribute_field = self.field text = None if isinstance(value, bytes): text = value.decode('utf-8') elif isinstance(value, str): text = value elif IField.providedBy(value): serializer = getMultiAdapter( (value, self.field, self.request), ISchemaFieldSerializeToJson) text = serializer() elif value is not None and (force or value != self.field.missing_value): text = IValueToJson(value) # handle i18n # if isinstance(value, Message): # child.set(ns('domain', I18N_NAMESPACE), value.domain) # if not value.default: # child.set(ns('translate', I18N_NAMESPACE), '') # else: # child.set(ns('translate', I18N_NAMESPACE), child.text) # child.text = converter.toUnicode(value.default) if text: if attribute_name == 'value_type': attribute_name = 'items' result[attribute_name] = text if result['type'] == 'object': if isinstance(self.field.schema, dict): result['properties'] = self.field.schema else: schema_serializer = getMultiAdapter((self.field.schema, self.request), ISchemaSerializeToJson) result['properties'] = schema_serializer() return result
def adapter(_context, factory, provides=None, for_=None, name=''): if for_ is None: if len(factory) == 1: for_ = adaptedBy(factory[0]) if for_ is None: raise TypeError("No for attribute was provided and can't " "determine what the factory adapts.") for_ = tuple(for_) if provides is None: if len(factory) == 1: p = list(implementedBy(factory[0])) if len(p) == 1: provides = p[0] if provides is None: raise TypeError("Missing 'provides' attribute") if name == '': if len(factory) == 1: name = getName(factory[0]) # Generate a single factory from multiple factories: factories = factory if len(factories) == 1: factory = factories[0] elif len(factories) < 1: raise ComponentConfigurationError("No factory specified") elif len(factories) > 1 and len(for_) != 1: raise ComponentConfigurationError( "Can't use multiple factories and multiple for") else: factory = _rolledUpFactory(factories) _context.action( discriminator=('adapter', for_, provides, name), callable=handler, args=('registerAdapter', factory, for_, provides, name)) _context.action( discriminator=None, callable=provide_interface, args=('', provides)) if for_: for iface in for_: if iface is not None: _context.action( discriminator=None, callable=provide_interface, args=('', iface))
def test_classImplements_multiple_w_explict_implements(self): from zope.interface import Interface from zope.interface import implementedBy from zope.interface import providedBy class ILeft(Interface): def method(): pass class IRight(ILeft): pass class IOther(Interface): pass class Left(): __implemented__ = ILeft def method(self): pass class Right(object): __implemented__ = IRight class Other(object): __implemented__ = IOther class Mixed(Left, Right): __implemented__ = Left.__implemented__, Other.__implemented__ mixed = Mixed() self.assertTrue(ILeft.implementedBy(Mixed)) self.assertFalse(IRight.implementedBy(Mixed)) self.assertTrue(IOther.implementedBy(Mixed)) self.assertTrue(ILeft in implementedBy(Mixed)) self.assertFalse(IRight in implementedBy(Mixed)) self.assertTrue(IOther in implementedBy(Mixed)) self.assertTrue(ILeft in providedBy(mixed)) self.assertFalse(IRight in providedBy(mixed)) self.assertTrue(IOther in providedBy(mixed))