simple registry failure - __adapt__
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.h... 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() ---- I get the following error: exceptions.AttributeError: class Counter has no attribute '__adapt__' /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/twisted/web/server.py, line 152 in process 150 self.postpath = map(unquote, string.split(self.path[1:], '/')) 151 try: 152 resrc = self.site.getResourceFor(self) 153 self.render(resrc) ... I would be happy to send the full traceback if it would be useful in identifying the problem. I am using Twisted, Twisted Web, and Zope Interfaces installed from the 2005-11-06 Twisted "Sumo" build on an OS X 10.3.9 G5 running python 2.3 (the latest for OS 10.3.x). cheers, Charlie
On Sat, 7 Jan 2006 13:48:43 -0500, charlie detar <chazen@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.h...
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
participants (2)
-
charlie detar
-
Jean-Paul Calderone