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