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

Matt Goodall matt at pollenation.net
Sun Jan 23 07:18:23 MST 2005


On Sun, 2005-01-23 at 10:24 +0000, Michael Hudson wrote:
> Mary Gardiner <mary-twisted at puzzling.org> writes:
> 
> > I've got a Page object that derives all its data from a dictionary, eg:
> >
> > {
> >     'project_name': 'project',
> >     'project_description': 'hello',
> >     # ... you get the idea
> > }
> >
> > 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']
> >
> >     def data_description(self, ctx, data):
> >         return self.d['project_description']
> >
> >     # and so on for every key in the dictionary
> >
> > 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?
> 
> I think something like this:
> 
>     def data_dict(self, thing):
>         return self.d[thing]
> 
> then nevow:data="dict project_name" shoud work.  I think.

This is a parameterised data_ method, right? In which case it should
look like:

        def data_dict(self, thing):
            def _(ctx, data):
                return self.d[thing]
            return _

Parameterised render_ methods look similar.

Basically, Nevow expects parameterised data_ and render_ methods to
return a callable (the _ function in the above example) with a signature
of (ctx, data).

The "weird" syntax is due to some crazy magic in the stan flatteners.
There's not a lot we can do about that right now, although I think it
may be possible to fix in the future.

Having said all that, there may be better ways of doing this anyway,
i.e. provide an inevow.IContainer adapter or use the standard adapters
Nevow provides for tuple, list and dict. See my other posts for more.

Cheers, Matt




More information about the Twisted-web mailing list