[Twisted-web] Page object design: wrapping a dictionary containing data

Andy Gayton andy at thecablelounge.com
Sun Jan 23 03:14:14 MST 2005


Mary Gardiner wrote:

 > What's a good way for the Page object to wrap that dictionary that
 > doesn't involve doing this:
 >
 >     def data_name(self, ctx, data):
 >         return self.d['project_name']
 >
 > ...
 >
 > I vaguely recall that the equivalent of this used to be possible in
 > Woven:
 >
 >     def data_project(self, ctx, data):
 >         return self.d
 >
 > and then that the data items were accessible by (say)
 > stan.directive("project/project_name"). This doesn't look like its
 > supported in Nevow. Is this correct? What alternative designs are there
 > for something like this?

heya Mary,

there's a lot of Nevow magic I don't know, but in case something doesn't 
exist, something like the following should work, albeit, pretty crude:

python -c '
class C:
     def __getattr__( self, attr ):
         if attr.startswith( "data_" ):
             return lambda ctx, data, s=self, a=attr : s.d["project_%s" 
% a[len("data_"):]]
         raise AttributeError

c = C()

c.d  = { "project_name" : "roar" }
ctx  = "ned"
data = "fred"

print c.data_name( ctx, data )
'

Andy.



More information about the Twisted-web mailing list