[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accumulator



Avi Bryant wrote:
> 
>...
> 
> def compute_price
>   get_base_price() {|price|
>     if price > tax_threshold
>       get_tax(price) {|tax|
>          price += tax
>       }
>     end
>     do_something_with(price)
>   }
> end
> 
> Can you give me the equivalent python?

I'm going to avoid deeply nested expressions, but I won't add any return
statements. I could use Python closures and CPS-ish style, but I don't
even see a need for them, unless I've misunderstood something big.
Here's the algorithm:

def add_tax_if_necessary(context):
   if context.price > tax_threshold:
       add_tax(context)

def add_tax(context):
    context.price += get_tax(context.price)

def compute_price():
   context = Struct(price = get_base_price())
   add_tax_if_necessary(context)
   do_something_with(context.price)

The algorithm makes no use of any unusual Python features. I prefer this
unrolled functional version because in a web server I could associate
each function with a page using a simple dictionary. The context object
could use whatever is available in my app server, like the .NET
framework or J2EE. The app server would make sure that the context was
available on whatever machine a request came on in. (Zope may have this
capability, not sure)

Here's scaffolding code to make it a runnable script:

# this helper class would be in a shared module
class Struct:
   def __init__(self, **kwargs):
       for key, value in kwargs.items():
            setattr(self, key, value)
            print key, value

def get_base_price(): 
    return input("price: ")

def get_tax(price):
    return price/10.0

tax_threshold = 100

def do_something_with(x):
    print x

while 1:
    compute_price()



Here's the algorithm again in a closures-style.

def compute_price():
   context = Struct(price = get_base_price())

   def add_tax_if_necessary():
       if context.price > tax_threshold:
           add_tax()

   def add_tax():
        context.price += get_tax(context.price)

   add_tax_if_necessary()

   do_something_with(context.price)


The only "price" I had to pay because of Python's to avoid Python's weak
lambda is giving functions names! If that offends you, then call them
temp1 and temp2. ;)

 Paul Prescod