This is the talk I gave on the Zope Component Architecture at Europython 2010. It gives a general outline of the ZCA concepts (Interfaces, Utilities and Adaptors) along with two examples where the ZCA would be useful.
• A real world problem.
• An explanation of the core ZCA concepts.
• A simple model of the problem to use during the talk.
• Code to apply concepts in the context of the simple
Outline of the problem
which led me to
appreciate the ZCA
The newsletter problem
• Types designed for display on the web which we couldn't
• But which were in a good class hierarchy.
• But we need to reformat some of them for display in a
newsletter—and format the rest in a generic manner.
• And, as always, we’re on a deadline.
• The ZCA can help us with this problem
A Typical Day
As Gregor Samsa awoke one morning from
item just gets
uneasy dreams he found himself transformed a title and
in his bed into a monstrous vermin. More...
Birthday News: “I feel no older” A news item
After celebrating his 27th birthday, gets a picture
a man claimed today to “feel no
older” than the day before. More... beside it.
Paris in the Autumn view the album And for a
why not just
Modelling the problem
• I’m going to use the problem I just described as a basis.
• We’re going to work through a simpliﬁed version to go
over the primary concepts in the ZCA.
• First, let’s look at the concepts.
Let’s talk about Zope
• Zope = Zope Object Publishing Environment.
• Zope’s model is that of an object graph.
• Both content and functionality are stored in Zope’s object
database (the ZODB).
• Zope 2 was a bit all or nothing: inventing wheels, very
• Zope 3 was the reformulation of Zope into components.
• The ZCA was built to meld these components together
into a web platform.
• It’s not crazy.
• A Service Locator—it helps the parts of
your application ﬁnd each other without
• Components are registered with a central
registry, which responds to queries.
• Conﬁguration decides what component
provides a given function.
• Though it would be possible to write a
plugin architecture for your project, the ZCA
is already there and well tested.
We provide I need
function X function X
I can help
• Deﬁnes a set of functionality a thing promises to provide.
• A gentleman’s agreement, not a contract.
• Can be implemented any object, unlike some other
• The core of the ZCA:
• You can ask for an object which implements an interface;
• Or an object to convert from one interface to another.
LDAP User Code
SQL DB isValidUser()
• A utility is an object which implements a speciﬁc interface.
• You register the object with the ZCA, telling the ZCA the
object is a utility which implements the interface.
• Then ask for it later from anywhere in your program.
LDAP User Code
SQL DB isValidUser()
• Adapters can convert from one or more interface to
• In Zope they are often used to add functionality in addition
to just altering and interface.
SQL DB Adapter User Code
• Multi-adaptors adapt more than one input interface to a
single output interface.
• Zope uses this for rendering pages by adapting as follows:
Adapter (rendered page)
Is this too far?
• Zope 3 uses multi-adaptors to register event handlers:
Like the snappily named
Event type is IObjectModifiedEvent
The simpliﬁed model
Selects items for
select from. IItemFinder
Renderer for most items Rendering
Renderer for news items
Renderer for photo albums
The content objects
# Note they have no knowledge of the
class StandardItem(object): class NewsItem(StandardItem):
def __init__(self, title): def __init__(self, title,
self.title = title description, date):
self.title = title
@property self.description = description
def url(self): self.date = date
"""Generate a url"""
class SubStandardItem(StandardItem): class PhotoAlbum(StandardItem):
"""This item should be renderable
by the adaptor for StandardItem""" def __init__(self, title,
def __init__(self, title): self.title = title
self.title = "SUB! %s" % title self.thumbnails = thumbnails
A utility to ﬁnd them
"""Return a list of items which descend from StandardItem."""
def __call__(self): The ZCA bits
"""Hardcode for demo purposes"""
StandardItem("A Standard Item"),
SubStandardItem("A second standard item"),
NewsItem("Man Feels No Different on Birthday",
Man reports feeling no different on 27th birthday
from the day before.""",
"10th July, 2010"),
The world is going to end tomorrow at 14.27.""",
"13th June, 2010"),
[... some more items ...]
Adaptors to render them
def render(): Our interface
"""Render an item"""
"""Base class to do what all the renderers need to do"""All renderers
def __init__(self, context): implement
self.context = context
def render(self): and adapt a
return "*** %s (Standard Item)n %s" % (
self.context.title, self.context.url) content item
class NewsItemToNewsletterBlockRenderer(BaseRenderer): to it
return "*** %s (News)n %sn %snn %s" % (
self.context.title, self.context.date, self.context.url,
[...format the body...]
# These all use the Global registry. It's possible to
# have separate registries if needed for your application.
from zope.component import provideAdapter, provideUtility,
# (1) A utility needs to be instantiated before registering.
# (2) Adaptors will be created as needed so don’t need
And use them
Get the utility and call it to get item list:
finder = getUtility(IItemFinder)
items = finder()
The ZCA will find the right renderer:
body = "nn".join([
INewsletterBlockRenderer(x).render() for x in finder ])
We hope you enjoyed this update,
""" % body
• ZCA allows for building robustly componentised applications.
• Core is: Interfaces, Utilities and Adapters.
• It’s well tested and small enough to easily learn.
• It’s powerful enough for production use--and widely
• Suggested further reading:
Get in contact:
Thank you for watching! Or catch me in the hallways!