Trees | Index | Help |
|
---|
Package zope :: Package interface :: Module declarations |
|
Implementation of interface declarations
There are three flavors of declarations:
Declarations are used to simply name declared interfaces.
ImplementsDeclarations are used to express the interfaces that a class implements (that instances of the class provides).
Implements specifications support inheriting interfaces.
ProvidesDeclarations are used to express interfaces directly provided by objects.
$Id: declarations.py,v 1.2 2005/06/24 16:52:37 nyergler Exp $
Classes | |
---|---|
ClassProvides |
Special descriptor for class __provides__ |
ClassProvidesBase |
|
ClassProvidesBasePy |
|
Declaration |
Interface declarations |
implementer |
|
Implements |
|
ObjectSpecificationDescriptor |
Implement the __providedBy__ attribute |
ObjectSpecificationDescriptorPy |
Implement the __providedBy__ attribute |
Function Summary | |
---|---|
_classProvides_advice(cls)
| |
_implements(name,
interfaces,
classImplements)
| |
_implements_advice(cls)
| |
Normalize declaration arguments | |
Declare interfaces declared directly for an object | |
Declare additional interfaces implemented for instances of a class | |
Declare the only interfaces implemented by instances of a class | |
Declare interfaces provided directly by a class | |
Return the interfaces directly provided by the given object | |
Declare interfaces declared directly for an object | |
getObjectSpecification(ob)
| |
Return the interfaces implemented for a class' instances | |
Return the interfaces implemented for a class' instances | |
Declare interfaces implemented by instances of a class | |
Declare the only interfaces implemented by instances of a class | |
Declare interfaces provided by a module | |
Provide object specifications | |
providedBy(ob)
| |
Cache instance declarations |
Variable Summary | |
---|---|
Declaration |
_empty = <zope.interface.declarations.Declaration object...
|
dict |
BuiltinImplementationSpecifications = {<type 'object'>: ...
|
tuple |
DescriptorAwareMetaClasses = (<type 'classobj'>, <type '...
|
WeakValueDictionary |
InstanceDeclarations = <WeakValueDictionary at -12099291...
|
ObjectSpecificationDescriptorPy |
objectSpecificationDescriptor = <zope.interface.declarat...
|
Function Details |
---|
_normalizeargs(sequence, output=None)Normalize declaration arguments Normalization arguments might contain Declarions, tuples, or single interfaces. Anything but individial interfaces or implements specs will be expanded. |
alsoProvides(object, *interfaces)Declare interfaces declared directly for an object The arguments after the object are one or more interfaces or interface specifications (IDeclaration objects). The interfaces given (including the interfaces in the specifications) are added to the interfaces previously declared for the object. Consider the following example: >>> from zope.interface import Interface >>> class I1(Interface): pass ... >>> class I2(Interface): pass ... >>> class IA1(Interface): pass ... >>> class IA2(Interface): pass ... >>> class IB(Interface): pass ... >>> class IC(Interface): pass ... >>> class A(object): implements(IA1, IA2) ... >>> class B(object): implements(IB) ... >>> class C(A, B): ... implements(IC) >>> ob = C() >>> directlyProvides(ob, I1) >>> int(I1 in providedBy(ob)) 1 >>> int(I2 in providedBy(ob)) 0 >>> int(IA1 in providedBy(ob)) 1 >>> int(IA2 in providedBy(ob)) 1 >>> int(IB in providedBy(ob)) 1 >>> int(IC in providedBy(ob)) 1 >>> alsoProvides(ob, I2) >>> int(I1 in providedBy(ob)) 1 >>> int(I2 in providedBy(ob)) 1 >>> int(IA1 in providedBy(ob)) 1 >>> int(IA2 in providedBy(ob)) 1 >>> int(IB in providedBy(ob)) 1 >>> int(IC in providedBy(ob)) 1 The object, ob provides I1, I2, and whatever interfaces instances have been declared for instances of C. Notice that the alsoProvides just extends the provided interfaces. |
classImplements(cls, *interfaces)Declare additional interfaces implemented for instances of a class The arguments after the class are one or more interfaces or interface specifications (IDeclaration objects). The interfaces given (including the interfaces in the specifications) are added to any interfaces previously declared. Consider the following example: for example: >>> from zope.interface import Interface >>> class I1(Interface): pass ... >>> class I2(Interface): pass ... >>> class I3(Interface): pass ... >>> class I4(Interface): pass ... >>> class I5(Interface): pass ... >>> class A(object): ... implements(I3) >>> class B(object): ... implements(I4) >>> class C(A, B): ... pass >>> classImplements(C, I1, I2) >>> [i.getName() for i in implementedBy(C)] ['I1', 'I2', 'I3', 'I4'] >>> classImplements(C, I5) >>> [i.getName() for i in implementedBy(C)] ['I1', 'I2', 'I5', 'I3', 'I4'] Instances of C provide I1, I2, I5, and whatever interfaces instances of A and B provide. |
classImplementsOnly(cls, *interfaces)Declare the only interfaces implemented by instances of a class The arguments after the class are one or more interfaces or interface specifications (IDeclaration objects). The interfaces given (including the interfaces in the specifications) replace any previous declarations. Consider the following example: >>> from zope.interface import Interface >>> class I1(Interface): pass ... >>> class I2(Interface): pass ... >>> class I3(Interface): pass ... >>> class I4(Interface): pass ... >>> class A(object): ... implements(I3) >>> class B(object): ... implements(I4) >>> class C(A, B): ... pass >>> classImplementsOnly(C, I1, I2) >>> [i.getName() for i in implementedBy(C)] ['I1', 'I2'] Instances of C provide only I1, I2, and regardless of whatever interfaces instances of A and B implement. |
classProvides(*interfaces)Declare interfaces provided directly by a class This function is called in a class definition. The arguments are one or more interfaces or interface specifications (IDeclaration objects). The given interfaces (including the interfaces in the specifications) are used to create the class's direct-object interface specification. An error will be raised if the module class has an direct interface specification. In other words, it is an error to call this function more than once in a class definition. Note that the given interfaces have nothing to do with the interfaces implemented by instances of the class. This function is provided for convenience. It provides a more convenient way to call directlyProvidedByProvides for a class. For example: classProvides(I1) is equivalent to calling: directlyProvides(theclass, I1) after the class has been created. For example: >>> from zope.interface import Interface >>> class IFoo(Interface): pass ... >>> class IFooFactory(Interface): pass ... >>> class C(object): ... implements(IFoo) ... classProvides(IFooFactory) >>> [i.getName() for i in C.__providedBy__] ['IFooFactory'] >>> [i.getName() for i in C().__providedBy__] ['IFoo'] if equivalent to: >>> from zope.interface import Interface >>> class IFoo(Interface): pass ... >>> class IFooFactory(Interface): pass ... >>> class C(object): ... implements(IFoo) >>> directlyProvides(C, IFooFactory) >>> [i.getName() for i in C.__providedBy__] ['IFooFactory'] >>> [i.getName() for i in C().__providedBy__] ['IFoo'] |
directlyProvidedBy(object)Return the interfaces directly provided by the given object The value returned is an IDeclaration. |
directlyProvides(object, *interfaces)Declare interfaces declared directly for an object The arguments after the object are one or more interfaces or interface specifications (IDeclaration objects). The interfaces given (including the interfaces in the specifications) replace interfaces previously declared for the object. Consider the following example: >>> from zope.interface import Interface >>> class I1(Interface): pass ... >>> class I2(Interface): pass ... >>> class IA1(Interface): pass ... >>> class IA2(Interface): pass ... >>> class IB(Interface): pass ... >>> class IC(Interface): pass ... >>> class A(object): implements(IA1, IA2) ... >>> class B(object): implements(IB) ... >>> class C(A, B): ... implements(IC) >>> ob = C() >>> directlyProvides(ob, I1, I2) >>> int(I1 in providedBy(ob)) 1 >>> int(I2 in providedBy(ob)) 1 >>> int(IA1 in providedBy(ob)) 1 >>> int(IA2 in providedBy(ob)) 1 >>> int(IB in providedBy(ob)) 1 >>> int(IC in providedBy(ob)) 1 The object, ob provides I1, I2, and whatever interfaces instances have been declared for instances of C. To remove directly provided interfaces, use directlyProvidedBy and subtract the unwanted interfaces. For example: >>> directlyProvides(ob, directlyProvidedBy(ob)-I2) >>> int(I1 in providedBy(ob)) 1 >>> int(I2 in providedBy(ob)) 0 removes I2 from the interfaces directly provided by ob. The object, ob no longer directly provides I2, although it might still provide I2 if it's class implements I2. To add directly provided interfaces, use directlyProvidedBy and include additional interfaces. For example: >>> int(I2 in providedBy(ob)) 0 >>> directlyProvides(ob, directlyProvidedBy(ob), I2) adds I2 to the interfaces directly provided by ob: >>> int(I2 in providedBy(ob)) 1 |
implementedBy(cls)Return the interfaces implemented for a class' instances The value returned is an IDeclaration. for example: >>> from zope.interface import Interface >>> class I1(Interface): pass ... >>> class I2(I1): pass ... >>> class I3(Interface): pass ... >>> class I4(I3): pass ... >>> class C1(object): ... implements(I2) >>> class C2(C1): ... implements(I3) >>> [i.getName() for i in implementedBy(C2)] ['I3', 'I2'] |
implementedByFallback(cls)Return the interfaces implemented for a class' instances The value returned is an IDeclaration. for example: >>> from zope.interface import Interface >>> class I1(Interface): pass ... >>> class I2(I1): pass ... >>> class I3(Interface): pass ... >>> class I4(I3): pass ... >>> class C1(object): ... implements(I2) >>> class C2(C1): ... implements(I3) >>> [i.getName() for i in implementedBy(C2)] ['I3', 'I2'] |
implements(*interfaces)Declare interfaces implemented by instances of a class This function is called in a class definition. The arguments are one or more interfaces or interface specifications (IDeclaration objects). The interfaces given (including the interfaces in the specifications) are added to any interfaces previously declared. Previous declarations include declarations for base classes unless implementsOnly was used. This function is provided for convenience. It provides a more convenient way to call classImplements. For example: implements(I1) is equivalent to calling: classImplements(C, I1) after the class has been created. Consider the following example: >>> from zope.interface import Interface >>> class IA1(Interface): pass ... >>> class IA2(Interface): pass ... >>> class IB(Interface): pass ... >>> class IC(Interface): pass ... >>> class A(object): implements(IA1, IA2) ... >>> class B(object): implements(IB) ... >>> class C(A, B): ... implements(IC) >>> ob = C() >>> int(IA1 in providedBy(ob)) 1 >>> int(IA2 in providedBy(ob)) 1 >>> int(IB in providedBy(ob)) 1 >>> int(IC in providedBy(ob)) 1 Instances of C implement I1, I2, and whatever interfaces instances of A and B implement. |
implementsOnly(*interfaces)Declare the only interfaces implemented by instances of a class This function is called in a class definition. The arguments are one or more interfaces or interface specifications (IDeclaration objects). Previous declarations including declarations for base classes are overridden. This function is provided for convenience. It provides a more convenient way to call classImplementsOnly. For example: implementsOnly(I1) is equivalent to calling: classImplementsOnly(I1) after the class has been created. Consider the following example: >>> from zope.interface import Interface >>> class IA1(Interface): pass ... >>> class IA2(Interface): pass ... >>> class IB(Interface): pass ... >>> class IC(Interface): pass ... >>> class A(object): implements(IA1, IA2) ... >>> class B(object): implements(IB) ... >>> class C(A, B): ... implementsOnly(IC) >>> ob = C() >>> int(IA1 in providedBy(ob)) 0 >>> int(IA2 in providedBy(ob)) 0 >>> int(IB in providedBy(ob)) 0 >>> int(IC in providedBy(ob)) 1 Instances of C implement IC, regardless of what instances of A and B implement. |
moduleProvides(*interfaces)Declare interfaces provided by a module This function is used in a module definition. The arguments are one or more interfaces or interface specifications (IDeclaration objects). The given interfaces (including the interfaces in the specifications) are used to create the module's direct-object interface specification. An error will be raised if the module already has an interface specification. In other words, it is an error to call this function more than once in a module definition. This function is provided for convenience. It provides a more convenient way to call directlyProvides. For example: moduleImplements(I1) is equivalent to: directlyProvides(sys.modules[__name__], I1) |
ObjectSpecification(direct, cls)Provide object specifications These combine information for the object and for it's classes. For example: >>> from zope.interface import Interface >>> class I1(Interface): pass ... >>> class I2(Interface): pass ... >>> class I3(Interface): pass ... >>> class I31(I3): pass ... >>> class I4(Interface): pass ... >>> class I5(Interface): pass ... >>> class A(object): implements(I1) ... >>> class B(object): __implemented__ = I2 ... >>> class C(A, B): implements(I31) ... >>> c = C() >>> directlyProvides(c, I4) >>> [i.getName() for i in providedBy(c)] ['I4', 'I31', 'I1', 'I2'] >>> [i.getName() for i in providedBy(c).flattened()] ['I4', 'I31', 'I3', 'I1', 'I2', 'Interface'] >>> int(I1 in providedBy(c)) 1 >>> int(I3 in providedBy(c)) 0 >>> int(providedBy(c).extends(I3)) 1 >>> int(providedBy(c).extends(I31)) 1 >>> int(providedBy(c).extends(I5)) 0 >>> class COnly(A, B): implementsOnly(I31) ... >>> class D(COnly): implements(I5) ... >>> c = D() >>> directlyProvides(c, I4) >>> [i.getName() for i in providedBy(c)] ['I4', 'I5', 'I31'] >>> [i.getName() for i in providedBy(c).flattened()] ['I4', 'I5', 'I31', 'I3', 'Interface'] >>> int(I1 in providedBy(c)) 0 >>> int(I3 in providedBy(c)) 0 >>> int(providedBy(c).extends(I3)) 1 >>> int(providedBy(c).extends(I1)) 0 >>> int(providedBy(c).extends(I31)) 1 >>> int(providedBy(c).extends(I5)) 1 nonzero: >>> from zope.interface import Interface >>> class I1(Interface): ... pass >>> class I2(Interface): ... pass >>> class C(object): ... implements(I1) >>> c = C() >>> int(bool(providedBy(c))) 1 >>> directlyProvides(c, I2) >>> int(bool(providedBy(c))) 1 >>> class C(object): ... pass >>> c = C() >>> int(bool(providedBy(c))) 0 >>> directlyProvides(c, I2) >>> int(bool(providedBy(c))) 1 |
Provides(*interfaces)Cache instance declarations Instance declarations are shared among instances that have the same declaration. The declarations are cached in a weak value dictionary.
) >>> collect() >>> before = len(InstanceDeclarations) >>> class C(object): ... pass >>> from zope.interface import Interface >>> class I(Interface): ... pass >>> c1 = C() >>> c2 = C() >>> len(InstanceDeclarations) == before 1 >>> directlyProvides(c1, I) >>> len(InstanceDeclarations) == before + 1 1 >>> directlyProvides(c2, I) >>> len(InstanceDeclarations) == before + 1 1 >>> del c1 >>> collect() >>> len(InstanceDeclarations) == before + 1 1 >>> del c2 >>> collect() >>> len(InstanceDeclarations) == before 1 |
Variable Details |
---|
_empty
|
DescriptorAwareMetaClasses
|
InstanceDeclarations
|
objectSpecificationDescriptor
|
Trees | Index | Help |
|
---|
Generated by Epydoc 2.1 on Fri Jun 24 12:01:21 2005 | http://epydoc.sf.net |