[Twisted-web] Re: [Nevow-commits] r962 - One billion times better Choice...

Donovan Preston dp at ulaluma.com
Tue Dec 21 10:17:17 MST 2004


On Dec 21, 2004, at 7:30 AM, Alex Levy wrote:

> What I do (fairly regularly) with my application involves declaring the
> available choices as part of the _implementing_ class, not as part of 
> the
> interface. I typically used choicesAttribute to do this, and was 
> working on
> something along the lines of a choicesMethod, but now it seems that 
> this
> behavior has been deprecated.

Of course. The interface should never contain implementation code, but 
in this case the change just made using Choice so much more uniform and 
simple that it was obviously correct.

> So, what is the new recommended syntax for a Choice where the 
> implementing
> class can change the choices dynamically?

It really depends where you want to look up the method. If it's truly 
the *implementor* that you want, you can do exactly what the 
choicesAttribute backwards-compatibility code does:

Choices(lambda ctx, data: 
getattr(iformless.IConfigurable(ctx).original, 'someAttribute'))

Of course, you probably want to make using this a little nicer:

def choiceMethod(name):
     return lambda ctx, data: 
getattr(iformless.IConfigurable(ctx).original, name)

Also, perhaps you want to look for an already existing data method on 
your Page instead, to make your code more uniform:

def dataMethod(name):
     return lambda ctx, data: inevow.IContainer(ctx).child(ctx, name)

The point is instead of writing special cases in the actual Choice 
code, the programmer can plug in exactly what their case is. Oh, and it 
supports deferreds, too ;-)

dp




More information about the Twisted-web mailing list