Skip to content

Instantly share code, notes, and snippets.

@knzm
Created May 7, 2012 20:16
Show Gist options
  • Select an option

  • Save knzm/2630124 to your computer and use it in GitHub Desktop.

Select an option

Save knzm/2630124 to your computer and use it in GitHub Desktop.
zope.interface memo

summary

  provide implement
descriptor
  • classProvides
  • moduleProvides
  • implements
  • implementsOnly
decorator
  • provider
  • implementer
  • implementer_only
imperative
  • directlyProvides
  • alsoProvides
  • noLongerProvides
  • classImplements
  • classImplementsOnly
predicate
  • providedBy
  • directlyProvidedBy
  • implementedBy
  • "X provides Y" means the object X can act as the interface Y.
  • "X implements Y" means an instance of class X can act as the interface Y.

example

>>> from zope.interface import *
>>> class IFoo(Interface): pass
...
>>> class IBar(Interface): pass
...
>>> class IFooFactory(Interface): pass
...
>>> class C1(object):
...   implements(IFoo)
...   classProvides(IFooFactory)
...
>>> @implementer(IFoo)
... @provider(IFooFactory)
... class C2(object):
...   pass
...
>>> class C3(object):
...   pass
...
>>> classImplements(C3, IFoo)
>>> directlyProvides(C3, IFooFactory)
>>> [i.getName() for i in implementedBy(C1)]
['IFoo']
>>> [i.getName() for i in providedBy(C1)]
['IFooFactory']
>>> [i.getName() for i in providedBy(C1())]
['IFoo']
>>> [i.getName() for i in directlyProvidedBy(C1)]
['IFooFactory']
>>> [i.getName() for i in directlyProvidedBy(C1())]
[]
>>> o = C1()
>>> directlyProvides(o, IBar)
>>> [i.getName() for i in providedBy(o)]
['IBar', 'IFoo']
>>> [i.getName() for i in directlyProvidedBy(o)]
['IBar']
>>> from zope.interface.registry import Components
>>> registry = Components()
>>> registry.registerUtility(C1)
>>> registry.registerUtility(o, IBar)
>>> registry.queryUtility(IFooFactory) is C1
True
>>> registry.queryUtility(IBar) is o
True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment