[Twisted-web] simple registry failure - __adapt__
Jean-Paul Calderone
exarkun at divmod.com
Sat Jan 7 12:01:59 MST 2006
On Sat, 7 Jan 2006 13:48:43 -0500, charlie detar <chazen at gmail.com> wrote:
>Hi,
>
>I noticed an email from Dickon Reed referencing this issue last June
>(http://twistedmatrix.com/pipermail/twisted-web/2005-June/001536.html),
>but there is no reply in the archives - I was wondering if someone had
>discovered a solution?
>
>I am attempting to write a simple rpi script which uses a registry, as
>described in the documentation:
>http://twistedmatrix.com/projects/web/documentation/howto/using-twistedweb.html#auto17
>
>My rpy script is as follows:
>----
>from twisted.web import resource
>
>class Counter:
> def __init__(self):
> self.value = 0
> def increment(self):
> self.value += 1
> def getValue(self):
> return self.value
>
>counter = registry.getComponent(Counter)
>if not counter:
> registry.setComponent(Counter, Counter())
>counter = registry.getComponent(Counter)
>
>class MyResource(resource.Resource):
> def render_GET(self, request):
> counter.increment()
> return "you are visitor %d" % counter.getValue()
>
>resource = MyResource()
>----
The component registry is not intended as a dumping ground for
random global variables. I am not sure if the particular usage
above was ever supported behavior (I am inclined to think not,
but tpc was some heinous stuff, so who knows), but it is definitely
no longer supported.
Replace the argument to getComponent and the first argument to
setComponent with an interface (a subclass of zope.interface.Interface),
or just skip components entirely and use a boring dictionary:
import myproject
counter = myproject.globals.get(Counter, None)
if counter is None:
counter = myproject.globals[Counter] = Counter()
Hope this helps,
Jean-Paul
More information about the Twisted-web
mailing list