Class
zope.app.catalog.attribute.AttributeIndex

Index interface-defined attributes

Mixin for indexing a particular attribute of an object after first adapting the object to be indexed to an interface.

The class is meant to be mixed with a base class that defines an index_doc method:

>>> class BaseIndex(object):
...     def __init__(self):
...         self.data = []
...     def index_doc(self, id, value):
...         self.data.append((id, value))

The class does two things. The first is to get a named field from an object:

>>> class Data:
...     def __init__(self, v):
...         self.x = v
>>> class Index(AttributeIndex, BaseIndex):
...     pass
>>> index = Index('x')
>>> index.index_doc(11, Data(1))
>>> index.index_doc(22, Data(2))
>>> index.data
[(11, 1), (22, 2)]

A method can be indexed:

>>> Data.z = lambda self: self.x + 20
>>> index = Index('z', field_callable=True)
>>> index.index_doc(11, Data(1))
>>> index.index_doc(22, Data(2))
>>> index.data
[(11, 21), (22, 22)]

But you'll get an error if you try to index a method without setting field_callable:

>>> index = Index('z')
>>> index.index_doc(11, Data(1))

The class can also adapt an object to an interface:

>>> from zope.interface import Interface
>>> class I(Interface):
...     pass
>>> class Data:
...     def __init__(self, v):
...         self.x = v
...     def __conform__(self, iface):
...         if iface is I:
...             return Data2(self.x)
>>> class Data2:
...     def __init__(self, v):
...         self.y = v*v
>>> index = Index('y', I)
>>> index.index_doc(11, Data(3))
>>> index.index_doc(22, Data(2))
>>> index.data
[(11, 9), (22, 4)]

When you define an index class, you can define a default interface and/or a default interface:

>>> class Index(AttributeIndex, BaseIndex):
...     default_interface = I
...     default_field_name = 'y'
>>> index = Index()
>>> index.index_doc(11, Data(3))
>>> index.index_doc(22, Data(2))
>>> index.data
[(11, 9), (22, 4)]

Base classes

Implemented Interfaces

Attributes/Properties

Methods

Known Subclasses