[PEAK] Use cases for the priority feature

Christoph Zwerschke cito at online.de
Tue Aug 17 18:37:24 EDT 2010


Am 17.08.2010 23:16 schrieb P.J. Eby:
 > You haven't seen the new combine_using decorator, have you? ;-)

No, I hadn't, I'm learning something new every day here :)

 > from peak.rules import combine_using, when
 >
 > @combine_using(max)
 > def getDiscount(...):
 > return 0
 >
 > @when(getDiscount, "customer=='Elvis'")(value(.1))
 > @when(getDiscount, "product is shoes")(value(.05))

The @'s need to be removed, and value imported from core, right? You 
should document the "value" function somewhere, it's simple but handy, 
but at first I didn't see where it was coming from.

But isn't the problem here that every function gets evaluated, and only 
then the maximum is calculated? While with priorities, only the 
getElvisDiscount method would be evaluated. In this example, it may not 
be a big problem, but in the other use cases?

 > All of your examples can be done this way, e.g.:
 >
 > @combine_using(partial(max,key=attrgetter('quality')))
 > @abstract
 > def getResizeAlgorithm(...)
 >     pass
 >
 > @when(getResizeAlgorithm, "some condition")(
 >     value(ResizingAlgorithm(somefunc, quality=20)))

I think that should be:

@combine_using(abstract, partial(max,key=attrgetter('quality')))
def getResizeAlgorithm(...)
     pass

when(getResizeAlgorithm, "some condition")(
     value(ResizingAlgorithm(somefunc, quality=20)))

Granted, that should work. But it looks a bit overly complicated to me, 
because you have to work with functions getters here, whereas with the 
priority parameter you could use the actual functions and spare some of 
the overhead.

-- Christoph


More information about the PEAK mailing list