Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install zope.annotation

How to install zope.annotation

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install zope.annotation
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
4.2.0 Available View build log
3.5.0 Available View build log
Windows (64-bit)
4.2.0 Available View build log
3.5.0 Available View build log
Mac OS X (10.5+)
4.2.0 Available View build log
3.5.0 Available View build log
Linux (32-bit)
4.2.0 Available View build log
3.5.0 Available View build log
Linux (64-bit)
4.2.0 Available View build log
3.5.0 Available View build log
4.2.0 Available View build log
 
License
ZPL 2.1
Depended by
Lastest release
version 4.2.0 on May 16th, 2013

Object Annotations

This package provides a mechanism to store additional information about objects without need to modify object class.

Annotation factories

There is more to document about annotations, but we'll just sketch out a scenario on how to use the annotation factory for now. This is one of the easiest ways to use annotations -- basically you can see them as persistent, writable adapters.

First, let's make a persistent object we can create annotations for:

>>> from zope import interface
>>> class IFoo(interface.Interface):
...     pass
>>> from zope.annotation.interfaces import IAttributeAnnotatable
>>> from persistent import Persistent
>>> @interface.implementer(IFoo, IAttributeAnnotatable)
... class Foo(Persistent):
...     pass

We directly say that Foo implements IAttributeAnnotatable here. In practice this is often done in ZCML, using the implements subdirective of the content or class directive.

Now let's create an annotation for this:

>>> class IBar(interface.Interface):
...     a = interface.Attribute('A')
...     b = interface.Attribute('B')
>>> from zope import component
>>> @interface.implementer(IBar)
... class Bar(Persistent):
...     component.adapts(IFoo)
...     def __init__(self):
...         self.a = 1
...         self.b = 2

Note that the annotation implementation does not expect any arguments to its __init__. Otherwise it's basically an adapter.

Now, we'll register the annotation as an adapter. To do this we use the factory function provided by zope.annotation:

>>> from zope.annotation import factory
>>> component.provideAdapter(factory(Bar))

Note that we do not need to specify what the adapter provides or what it adapts - we already do this on the annotation class itself.

Now let's make an instance of Foo, and make an annotation for it.

>>> foo = Foo()
>>> bar = IBar(foo)
>>> bar.a
1
>>> bar.b
2

We'll change a and get the annotation again. Our change is still there:

>>> bar.a = 3
>>> IBar(foo).a
3

Of course it's still different for another instance of Foo:

>>> foo2 = Foo()
>>> IBar(foo2).a
1

What if our annotation does not provide what it adapts with component.adapts? It will complain:

>>> class IQux(interface.Interface):
...     pass
>>> @interface.implementer(IQux)
... class Qux(Persistent):
...     pass
>>> component.provideAdapter(factory(Qux)) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: Missing 'zope.component.adapts' on annotation

It's possible to provide an annotation with an explicit key. (If the key is not supplied, the key is deduced from the annotation's dotted name, provided it is a class.)

>>> class IHoi(interface.Interface):
...     pass
>>> @interface.implementer(IHoi)
... class Hoi(Persistent):
...     component.adapts(IFoo)
>>> component.provideAdapter(factory(Hoi, 'my.unique.key'))
>>> isinstance(IHoi(foo), Hoi)
True
Location

Annotation factories are put into the location hierarchy with their parent pointing to the annotated object and the name to the dotted name of the annotation's class (or the name the adapter was registered under):

>>> foo3 = Foo()
>>> new_hoi = IHoi(foo3)
>>> new_hoi.__parent__
<Foo object at 0x...>
>>> new_hoi.__name__
'my.unique.key'
>>> import zope.location.interfaces
>>> zope.location.interfaces.ILocation.providedBy(new_hoi)
True

Please notice, that our Hoi object does not implement ILocation, so a location proxy will be used. This has to be re-established every time we retrieve the object

(Guard against former bug: proxy wasn't established when the annotation existed already.)

>>> old_hoi = IHoi(foo3)
>>> old_hoi.__parent__
<Foo object at 0x...>
>>> old_hoi.__name__
'my.unique.key'
>>> zope.location.interfaces.ILocation.providedBy(old_hoi)
True
LocationProxies

Suppose your annotation proxy provides ILocation.

>>> class IPolloi(interface.Interface):
...     pass
>>> @interface.implementer(IPolloi, zope.location.interfaces.ILocation)
... class Polloi(Persistent):
...     component.adapts(IFoo)
...     __name__ = __parent__ = 0
>>> component.provideAdapter(factory(Polloi, 'my.other.key'))

Sometimes you're adapting an object wrapped in a LocationProxy.

>>> foo4 = Foo()
>>> import zope.location.location
>>> wrapped_foo4 = zope.location.location.LocationProxy(foo4, None, 'foo4')
>>> located_polloi = IPolloi(wrapped_foo4)

At first glance it looks as if located_polloi is located under wrapped_foo4.

>>> located_polloi.__parent__ is wrapped_foo4
True
>>> located_polloi.__name__
'my.other.key'

but that's because we received a LocationProxy

>>> type(located_polloi).__name__
'LocationProxy'

If we unwrap located_polloi and look at it directly, we'll see it stores a reference to the real Foo object

>>> from zope.proxy import removeAllProxies
>>> removeAllProxies(located_polloi).__parent__ == foo4
True
>>> removeAllProxies(located_polloi).__name__
'my.other.key'

CHANGES

4.2.0 (2013-03-18)
  • Don't make AttributeAnnotations available as a view.
4.1.0 (2013-02-24)
  • Added __bool__ method to IAnnotations API for Python 3 compatibility.
4.0.1 (2013-02-11)
  • Added tox.ini.
4.0.0 (2013-02-11)
  • Added support for Python 3.3 and PyPy 1.9.
  • Replaced deprecated zope.component.adapts usage with equivalent zope.component.adapter decorator.
  • Replaced deprecated zope.interface.implements usage with equivalent zope.interface.implementer decorator.
  • Dropped support for Python 2.4 and 2.5.
  • Included zcml dependencies in configure.zcml, require the necessary packages via a zcml extra, added tests for zcml.
3.5.0 (2009-09-07)
  • Add ZODB3 to install_requires, because it's a true requirement of this package, not just a testing requirement, as BTrees are in use.
  • Fix one test that was inactive because it's function was overriden by a mistake.
3.4.2 (2009-03-09)
  • Clean up package description and documentation a bit.
  • Change mailing list address to zope-dev at zope.org, as zope3-dev at zope.org is now retired.
  • Remove old zpkg-related files.
3.4.1 (2008-08-26)
3.4.0 (2007-08-29)
  • Annotation factories are no longer containing the factored object. Instead the objects are located using zope.location. This removes a dependency to zope.app.container.

Subscribe to package updates

Last updated May 16th, 2013

Download Stats

Last month:4

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.