[PEAK] Use cases for the priority feature

Christoph Zwerschke cito at online.de
Tue Aug 17 16:09:56 EDT 2010


The posting on how to fix typical AmbiguousMethods errors is surely 
helpful and raised the question whether a "prioritzed" methods feature 
as discussed here is really needed: 
http://dirtsimple.org/2010/08/simplifying-prioritized-methods-in-peak.html

So I tried to come up with some legit use cases for priorities:

1) You can take the example from the docs where you get different 
discounts based on the customer, type of product.

Assume you get a discount of 10% percent if your name is Elvis, 5% if 
you buy shoes. As usual the rule shall be that you can only get the 
highest available account, i.e. if you're Elvis you get shoes at 10% 
discount, not 15%.

With the priority feature, you could write:

@when(getDiscount, "customer=='Elvis'", priority=10)
def ElvisGetsTenPercentOff(product, customer, options):
     return 0.1

@when(getDiscount, "product is shoes", priority=5)
def ShoesAretOnOffer(product, customer, options):
     return 0.05

2) Assume you have a generic function for resizing pictures. Depending 
on the format, size and number of colors, different algorithms are used. 
If the output quality of the algorithm is different, you could use that 
quality as the priority parameter.

3) Assume you have a generic "notify" method, and depending on whether 
the user has set E-Mail, Google Wave, IRC, etc. addresses, the 
corresponding notification method is chosen. You want to choose the most 
sophisticated method where a notification address is available, and make 
it configurable that e.g. Google Wave now has a lower priority, maybe 
even reading priorities from a config file.

Does this make sense?

-- Christoph


More information about the PEAK mailing list