Вы находитесь на странице: 1из 14

==========

==========
.. contents::
- ()
"".
:
-
-
- - ,

.

.
.

=======================
class::
>>> import zope.interface
>>> class IFoo(zope.interface.Interface):
...
"""Foo blah blah"""
...
...
x = zope.interface.Attribute("""X blah blah""")
...
...
def bar(q, r=None):
...
"""bar blah blah"""
`IFoo`.
`zope.interface.Interface`,
, `object` -
[ create]_. ,
, `InterfaceClass`::
>>> type(IFoo)
<class zope.interface.interface.InterfaceClass>
::
>>> IFoo.__doc__
Foo blah blah
::
>>> IFoo.__name__
IFoo
::
>>> IFoo.__module__
__main__

:
`x`
.
. .
`bar`
. . -
,
.
, `self` `bar`.
.
`self`, `self`
. `self`

Python.
.

.
.

::
>>> x = IFoo[x]
>>> type(x)
<class zope.interface.interface.Attribute>
>>> x.__name__
x
>>> x.__doc__
X blah blah
>>> IFoo.get(x).__name__
x
>>> IFoo.get(y)
`in`
::
>>> x in IFoo
True

::
>>> names = list(IFoo)
>>> names.sort()
>>> names
[bar, x]
, .
::
>>> IFoo.x
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: InterfaceClass object has no attribute x

::
>>> bar = IFoo[bar]
>>> bar.getSignatureString()
(q, r=None)

======================
, .
:


,  .
, .
,
.

 .
,
.
[ factory]_. (
.)
,
.
.
-
, .

API .

--------------------------------- -
implements ::
>>> class Foo:
...
zope.interface.implements(IFoo)
...
...
def __init__(self, x=None):
...
self.x = x
...
...
def bar(self, q, r=None):
...
return q, r, self.x
...
...
def __repr__(self):
...
return "Foo(s)"  self.x
, `Foo` `IFoo`. ,
`Foo` `IFoo`.
. -
::
>>> IFoo.implementedBy(Foo)
True

::
>>> foo = Foo()
>>> IFoo.providedBy(foo)
True
`Foo` `IFoo`, ::
>>> IFoo.providedBy(Foo)
False
::
>>> list(zope.interface.implementedBy(Foo))
[<InterfaceClass __main__.IFoo>]
::
>>> IFoo.implementedBy(foo)
Traceback (most recent call last):
...
TypeError: (ImplementedBy called for non-factory, Foo(None))
>>> list(zope.interface.implementedBy(foo))
Traceback (most recent call last):
...
TypeError: (ImplementedBy called for non-factory, Foo(None))
::
>>> list(zope.interface.providedBy(foo))
[<InterfaceClass __main__.IFoo>]
>>> list(zope.interface.providedBy(Foo))
[]
( ).
`implementer` ( Python .).
Python . ::
>>> def yfoo(y):
...
foo = Foo()
...
foo.y = y
...
return foo
>>> yfoo = zope.interface.implementer(IFoo)(yfoo)
>>> list(zope.interface.implementedBy(yfoo))
[<InterfaceClass __main__.IFoo>]
, implementer .
,
.
XXX: Double check and update these version numbers, and translate to russian:
In zope.interface .5.1 and lower, the implementor decorator can not
be used for classes, but in .5. and higher it can:
>>> Foo = zope.interface.implementer(IFoo)(Foo)
>>> list(zope.interface.providedBy(Foo()))

[<InterfaceClass __main__.IFoo>]
Note that class decorators using the @implementor(IFoo) syntax are only
supported in Python .6 and later.

------------------------------------- .
`__init__` `Foo`.
 `IFoo`. `__init__`
Foo. `__init__` `__call__`
`Foo`::
>>> class IFooFactory(zope.interface.Interface):
...
"""Create foos"""
...
...
def __call__(x=None):
...
"""Create a foo
...
...
The argument provides the initial value for x ...
...
"""
,
::
>>> zope.interface.directlyProvides(Foo, IFooFactory)
, Foo ::
>>> list(zope.interface.providedBy(Foo))
[<InterfaceClass __main__.IFooFactory>]
>>> IFooFactory.providedBy(Foo)
True

`classProvides`,
::
>>> class Foo:
...
zope.interface.implements(IFoo)
...
zope.interface.classProvides(IFooFactory)
...
...
def __init__(self, x=None):
...
self.x = x
...
...
def bar(self, q, r=None):
...
return q, r, self.x
...
...
def __repr__(self):
...
return "Foo(s)"  self.x
>>> list(zope.interface.providedBy(Foo))
[<InterfaceClass __main__.IFooFactory>]
>>> IFooFactory.providedBy(Foo)
True
`moduleProvides`
.

`moduleProvides` `zope.interface.__init__`, ,
`zope.interface` `IInterfaceDeclaration`.
,
. ,
`ISpecial`::
>>> class ISpecial(zope.interface.Interface):
...
reason = zope.interface.Attribute("Reason why were special")
...
def brag():
...
"Brag about being special"
foo
`reason` `brag`::
>>> foo.reason = I just am
>>> def brag():
...
return "Im special"
>>> foo.brag = brag
>>> foo.reason
I just am
>>> foo.brag()
"Im special"
::
>>> zope.interface.directlyProvides(foo, ISpecial)
::
>>> ISpecial.providedBy(foo)
True
>>> list(zope.interface.providedBy(foo))
[<InterfaceClass __main__.ISpecial>, <InterfaceClass __main__.IFoo>]
,
::
>>> list(zope.interface.directlyProvidedBy(foo))
[<InterfaceClass __main__.ISpecial>]
>>> newfoo = Foo()
>>> list(zope.interface.directlyProvidedBy(newfoo))
[]

--------------------- ::
>>> class SpecialFoo(Foo):
...
zope.interface.implements(ISpecial)
...
reason = I just am
...
def brag(self):
...
return "Im special because s"  self.reason
>>> list(zope.interface.implementedBy(SpecialFoo))
[<InterfaceClass __main__.ISpecial>, <InterfaceClass __main__.IFoo>]
>>> list(zope.interface.providedBy(SpecialFoo()))

[<InterfaceClass __main__.ISpecial>, <InterfaceClass __main__.IFoo>]


.
`implementsOnly` `implements`::
>>> class Special(Foo):
...
zope.interface.implementsOnly(ISpecial)
...
reason = I just am
...
def brag(self):
...
return "Im special because s"  self.reason
>>> list(zope.interface.implementedBy(Special))
[<InterfaceClass __main__.ISpecial>]
>>> list(zope.interface.providedBy(Special()))
[<InterfaceClass __main__.ISpecial>]

----------------- .
. ,
.
`classImplements`::
>>> class C:
...
pass
>>> zope.interface.classImplements(C, IFoo)
>>> list(zope.interface.implementedBy(C))
[<InterfaceClass __main__.IFoo>]
`classImplementsOnly`
::
>>> class C(Foo):
...
pass
>>> zope.interface.classImplementsOnly(C, ISpecial)
>>> list(zope.interface.implementedBy(C))
[<InterfaceClass __main__.ISpecial>]

----------------- .
::
>>> type(zope.interface.implementedBy(Special))
<class zope.interface.declarations.Implements>
.
. ,
.
::
>>> class Special(Foo):
...
zope.interface.implementsOnly(
...
zope.interface.implementedBy(Foo),
...
ISpecial,

...
...
...
...

)
reason = I just am
def brag(self):
return "Im special because s"  self.reason


``zope.interface.implements(ISpecial)``,
::
>>> list(zope.interface.implementedBy(Special))
[<InterfaceClass __main__.IFoo>, <InterfaceClass __main__.ISpecial>]

========================
.
::
>>> class IBlat(zope.interface.Interface):
...
"""Blat blah blah"""
...
...
y = zope.interface.Attribute("y blah blah")
...
def eek():
...
"""eek blah blah"""
>>> IBlat.__bases__
(<InterfaceClass zope.interface.Interface>,)
>>> class IBaz(IFoo, IBlat):
...
"""Baz blah"""
...
def eek(a=1):
...
"""eek in baz blah"""
...
>>> IBaz.__bases__
(<InterfaceClass __main__.IFoo>, <InterfaceClass __main__.IBlat>)
>>> names = list(IBaz)
>>> names.sort()
>>> names
[bar, eek, x, y]
, `IBaz` eek::
>>> IBlat[eek].__doc__
eek blah blah
>>> IBaz[eek].__doc__
eek in baz blah
eek .
, [ compat]_
.
::
>>> IBaz.extends(IFoo)
True
>>> IBlat.extends(IFoo)
False

, ::
>>> IBaz.extends(IBaz)
False
,
`isOrExtends`::
>>> IBaz.isOrExtends(IBaz)
True
>>> IBaz.isOrExtends(IFoo)
True
>>> IFoo.isOrExtends(IBaz)
False

.
 .
`names`::
>>> list(IBaz.names())
[eek]

------------------------------------------- .

. ::
>>> class IBase(zope.interface.Interface):
...
...
def foo():
...
"base foo doc"
>>> class IBase1(IBase):
...
pass
>>> class IBase(IBase):
...
...
def foo():
...
"base foo doc"
>>> class ISub(IBase1, IBase):
...
pass
ISub foo IBase .. IBase
IBase::
>>> ISub[foo].__doc__
base foo doc
, .
, .
direct ::
>>> IBase.direct(foo).__doc__
base foo doc

>>> ISub.direct(foo)

----------- - .

.
::
>>> class Baz(object):
...
zope.interface.implements(IBaz)
>>> baz_implements = zope.interface.implementedBy(Baz)
>>> baz_implements.__bases__
(<InterfaceClass __main__.IBaz>, <implementedBy ...object>)
>>> baz_implements.extends(IFoo)
True
>>> baz_implements.isOrExtends(IFoo)
True
>>> baz_implements.isOrExtends(baz_implements)
True
( ) `__sro__`
::
>>> baz_implements.__sro__
(<implementedBy __main__.Baz>,
<InterfaceClass __main__.IBaz>,
<InterfaceClass __main__.IFoo>,
<InterfaceClass __main__.IBlat>,
<InterfaceClass zope.interface.Interface>,
<implementedBy ...object>)

===================

UML " "
::
>>> IFoo.setTaggedValue(date-modified, --1)
>>> IFoo.setTaggedValue(author, Jim Fulton)
>>> IFoo.getTaggedValue(date-modified)
--1
>>> IFoo.queryTaggedValue(date-modified)
--1
>>> IFoo.queryTaggedValue(datemodified)
>>> tags = list(IFoo.getTaggedValueTags())
>>> tags.sort()
>>> tags
[author, date-modified]

::
>>> class IBazFactory(zope.interface.Interface):
...
def __call__():

...
...

"create one"
__call__.return_type = IBaz

>>> IBazFactory[__call__].getTaggedValue(return_type)
<InterfaceClass __main__.IBaz>

::
>>> class IWithTaggedValues(zope.interface.Interface):
...
zope.interface.taggedValue(squish, squash)
>>> IWithTaggedValues.getTaggedValue(squish)
squash

==========

.
. -
.
`Invalid` . ::
>>> class RangeError(zope.interface.Invalid):
...
"""A range has invalid limits"""
...
def __repr__(self):
...
return "RangeError(r)"  self.args
>>> def range_invariant(ob):
...
if ob.max < ob.min:
...
raise RangeError(ob)
::
>>> class IRange(zope.interface.Interface):
...
min = zope.interface.Attribute("Lower bound")
...
max = zope.interface.Attribute("Upper bound")
...
...
zope.interface.invariant(range_invariant)
::
>>> class Range(object):
...
zope.interface.implements(IRange)
...
...
def __init__(self, min, max):
...
self.min, self.max = min, max
...
...
def __repr__(self):
...
return "Range(s, s)"  (self.min, self.max)
>>> IRange.validateInvariants(Range(1,))
>>> IRange.validateInvariants(Range(1,1))
>>> IRange.validateInvariants(Range(,1))
Traceback (most recent call last):
...
RangeError: Range(, 1)

. `validateInvariants`

`Invalid`
::
>>> from zope.interface.exceptions import Invalid
>>> errors = []
>>> try:
...
IRange.validateInvariants(Range(,1), errors)
... except Invalid, e:
...
str(e)
[RangeError(Range(, 1))]
::
>>> errors
[RangeError(Range(, 1))]
>>> del errors[:]

=========
.
adapt PEP 6.
TypeError::
>>> class I(zope.interface.Interface):
...
pass
>>> I()
Traceback (most recent call last):
...
TypeError: (Could not adapt, , <InterfaceClass __main__.I>)
::
>>> I(, bob)
bob
::
>>> class C(object):
...
zope.interface.implements(I)
>>> obj = C()
>>> I(obj) is obj
True
__conform__, ::
>>> class C(object):
...
zope.interface.implements(I)
...
def __conform__(self, proto):
...
return 
>>> I(C())

(. __adapt__)
::

>>> from zope.interface.interface import adapter_hooks


>>> def adapt__to_(iface, obj):
...
if obj == :
...
return 
>>> adapter_hooks.append(adapt__to_)
>>> I()

>>> adapter_hooks.remove(adapt__to_)
>>> I()
Traceback (most recent call last):
...
TypeError: (Could not adapt, , <InterfaceClass __main__.I>)
__adapt__
-------->>> class I(zope.interface.Interface):
...
pass
__adapt__ PEP 6.
. PEP 6
__call__ .
. None::
>>> I.__adapt__()
::
>>> class C(object):
...
zope.interface.implements(I)
>>> obj = C()
>>> I.__adapt__(obj) is obj
True
( )
" ".
 .
adapter_hooks::
>>> from zope.interface.interface import adapter_hooks
>>> def adapt__to_(iface, obj):
...
if obj == :
...
return 
>>> adapter_hooks.append(adapt__to_)
>>> I.__adapt__()

, None .
::
>>> adapter_hooks.remove(adapt__to_)
>>> I.__adapt__()
.. [ create] `Interface` -

, class
, .

. , (
)
`Interface`.
.
.. [ factory] - .
.
,

.
.. [ compat] - .

.
IBaz
IBlat.
.
- .