<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1700157236206200597</id><updated>2010-07-30T07:21:08.273-07:00</updated><title type='text'>Valued Lessons</title><subtitle type='html'>I've learned.  I'll share.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/-/programming'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/search/label/programming'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>14</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-3111670930227756908</id><published>2009-08-14T17:16:00.000-07:00</published><updated>2009-08-14T17:44:56.875-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reactive'/><category scheme='http://www.blogger.com/atom/ns#' term='monad'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='generator'/><title type='text'>The Code for Reactive Programming in Python</title><content type='html'>I packaged some python code that you can run for each of the steps I've shown in my articles about Reactive Programming, &lt;a href="http://www.valuedlessons.com/2009/08/simple-rx-reactive-programming-in.html"&gt;how you could have invented it&lt;/a&gt; and &lt;a href="http://www.valuedlessons.com/2009/08/more-ways-to-do-reactive-programming-in.html"&gt;more ways to do it.&lt;/a&gt; 

I apologize that it's not in a more usable form.  Go ahead and copy and paste to wherever you like.   If you put it online somewhere more convenient, just put a link in the comments. 

I put the control of which example runs at the very end.  Just comment the appropriate line for the example you want to run.

And, here it is:
 
&lt;pre class="code"&gt;
import time

## simplified event stuff 
class Event:
    def __init__(self):
        self.handlers = []

    def handle(self, handler):
        self.handlers.append(handler)
        return self #so += will work

    def fire(self, val = None):
        for handler in self.handlers:
            handler(val)

def echo(val):
    print val
    return val

def simple_click_event_example():
    click = Event()
    click.handle(echo)
    click.fire("left") #prints "left"

def click_event_manipulation_example():
    def left_double_click_from_click(click, threshold):
        dlclick = Event()
        last_lclick_time = [0] #closure hack

        def click_handler(click_value):
            if click_value == "left":
                lclick_time = time.time()
                if (lclick_time - last_lclick_time[0]) &lt; threshold:
                    dlclick.fire("double left")
                last_lclick_time[0] = lclick_time
        
        click.handle(click_handler)
        return dlclick


    click   = Event()
    dlclick = left_double_click_from_click(click, .01)
    dlclick.handle(echo)

    click.fire("left")
    time.sleep(.02)
    click.fire("left")
    click.fire("right")
    click.fire("left") #prints "double left"
    
class EventFireRecord:
    def __init__(self, value, time):
        self.value = value
        self.time  = time

def click_event_maniuplation_refactored_example():
    def doubleize_event(evt, threshold, combine):
        double_evt = Event()

        last_fire  = EventFireRecord(None, 0)
        def evt_handler(value):
            fire_time = time.time()
            if (fire_time - last_fire.time) &lt; threshold:
                double_evt.fire(combine(last_fire.value, value))
            last_fire.__init__(value, fire_time)
        
        evt.handle(evt_handler)
        return double_evt

    def filter_event(evt, predicate):
        filtered_evt = Event()

        def evt_handler(value):
            if predicate(value):
                filtered_evt.fire(value)

        evt.handle(evt_handler)
        return filtered_evt

    click  = Event()
    lclick = filter_event(click, lambda value : value == "left")
    dlclick = doubleize_event(lclick, .01, lambda click1, click2 : "double left")
    dlclick.handle(echo)

    click.fire("left")
    time.sleep(.02)
    click.fire("left")
    click.fire("right")
    click.fire("left") #prints "double click"


def click_event_handle_with_result_example():
    def handle_with_result(evt, handler_with_result):
        evt_out = Event()

        def handler(value):
            result = handler_with_result(value)
            if result is not None:
                evt_out.fire(result)

        evt.handle(handler)
        return evt_out

    def doubleize_r(evt, threshold):
        last_fire = EventFireRecord(None, 0)

        def handler(value):
            fire_time = time.time()
            try:
                if (fire_time - last_fire.time) &lt; threshold:
                    return (last_fire.value, value)
            finally:
                last_fire.__init__(value, fire_time)

        return handle_with_result(evt, handler)

    def filter_r(evt, predicate):
        def handler(value):
            if predicate(value):
                return value

        return handle_with_result(evt, handler)


    clicks   = Event()
    dlclicks = doubleize_r(filter_r(click, lambda value : value == "left"), .01)
    dlclicks.handle(echo)

    clicks.fire("left")
    time.sleep(.02)
    clicks.fire("left")
    clicks.fire("right")
    clicks.fire("left") #prints ("left", "left")


def click_event_choosing_by_returning_event_example():
    def handle_with_result(evt, handler_with_result):
        evt_out = Event()

        def handler(value):
            result = handler_with_result(value)
            if result is None:
                pass #ignore
            elif isinstance(result, Event):
                result.handle(evt_out.fire)
            elif isinstance(result, list):
                for value_out in result:
                    evt_out.fire(value_out)
            else:
                evt_out.fire(result)
                    

        evt.handle(handler)
        return evt_out

    def filter_r(evt, predicate):
        def handler(value):
            if predicate(value):
                return value

        return handle_with_result(evt, handler)

    def value_filter_r(evt, value):
        return filter_r(evt, lambda val : val == value)

    def click_choose_r(keys, clicks):
        def key_handler(char):
            #TODO: unsubscribe from event after either "l" or "r"
            if char == "l":
                return value_filter_r(clicks, "left")
            elif char == "r":
                return value_filter_r(clicks, "right")
            elif char == "f":
                return ["fake", "fake"]

        return handle_with_result(keys, key_handler)

    keys   = Event()
    clicks = Event()
    choosen_clicks = click_choose_r(keys, clicks)

def click_event_looks_like_streams_example():
    class Event:
        def __init__(self):
            self.handlers = []

        def handle(self, handler):
            self.handlers.append(handler)
            return self #so += will work

        def fire(self, val = None):
            for handler in self.handlers:
                handler(val)

        def bind(evt, handler_with_result):
            evt_out = Event()

            def handler(value):
                result = handler_with_result(value)
                if result is not None:
                    Event.unit(result).handle(evt_out.fire)

            evt.handle(handler)
            return evt_out

        @classmethod
        def unit(cls, val):
            if isinstance(val, cls):
                return val
            elif isinstance(val, list):
                return MockEvent(*val)
            else:
                return MockEvent(val)

        __rshift__ = bind

    class MockEvent:
        def __init__(self, *vals):
            self.vals = vals

        def handle(self, handler):
            for val in self.vals:
                handler(val)  

    def doublize_r(threshold, combine):
        last_fire = EventFireRecord(None, 0)

        def handler(value):
            fire_time = time.time()
            try:
                if (fire_time - last_fire.time) &lt; threshold:
                    return combine(last_fire.value, value)
            finally:
                last_fire.__init__(value, fire_time)

        return handler

    def filter_r(predicate):
        def handler(value):
            if predicate(value):
                return value

        return handler

    def value_filter_r(value):
        return filter_r(lambda val : val == value)

    def click_choose_r(**clicks_by_char):
        def key_handler(char):
            return clicks_by_char.get(char)

        return key_handler


    clicks   = Event()
    keys     = Event()
    dlclicks = clicks &gt;&gt; value_filter_r("left") &gt;&gt; doublize_r(.01, lambda l1, l2: "double left")
    keys &gt;&gt; click_choose_r(d = dlclicks, f = ["fake", "fake"]) &gt;&gt; echo

    clicks.fire("left")
    clicks.fire("left")
    keys.fire("f") #prints "fake" and then "fake" again
    keys.fire("d")
    clicks.fire("right")
    clicks.fire("right")
    time.sleep(.02)
    clicks.fire("left")
    clicks.fire("left") #print ("double left")


## basic consumer (receiver) using generators

receive = object()

def receiver_example():
    def receiver(gen_rcvr):
        def gen_and_start_rcvr(*args, **kargs):
            rcvr = gen_rcvr(*args, **kargs)
            rcvr.send(None)
            return rcvr
        return gen_and_start_rcvr

    @receiver
    def sum_r(title):
        total = 0
        while True:
            total += yield receive
            print "%s: %d" % (title, total)

    @receiver
    def count_r(title):
        count = 0
        while True:
            yield receive
            count += 1
            print "%s: %d" % (title, count)

    num_key      = Event()
    sum_nums     = sum_r("total nums")
    num_key.handle(sum_nums.send)

    num_key.fire(1) #prints "total nums: 1"
    num_key.fire(2) #prints "total nums: 3" 
    num_key.fire(3) #prints "total nums: 6"

## make retiterators that can also output values via an event fire
def remitter_example():
    class Remitter:
        def __init__(self, receiver_from_event_out):
            self.receiverFromEventOut = receiver_from_event_out

        def __rrshift__(self, event_in):
            event_out = Event()
            rcvr      = self.receiverFromEventOut(event_out)
            event_in.handle(rcvr.send)
            return event_out

    def remitter(gen_rcvr):
        def gen_remitter(*args, **kargs):
            def receiver_from_event_out(event_out):
                rcvr = gen_rcvr(event_out, *args, **kargs)
                rcvr.send(None)
                return rcvr
            return Remitter(receiver_from_event_out)
        return gen_remitter

    @remitter
    def double_detect_r(double_click_event, threshold):
        last_click_time = 0
        while True:
            yield receive
            current_click_time = time.time()
            if (current_click_time - last_click_time) &lt; threshold:
                double_click_event.fire()
            last_click_time = current_click_time

    @remitter
    def print_r(_, message):
        while True:
           val = yield receive
           print message

    mouse_click  = Event()

    mouse_click &gt;&gt; print_r("left")
    mouse_click &gt;&gt; double_detect_r(.01) &gt;&gt; print_r("double left")

    mouse_click.fire() #prints "left"
    time.sleep(.02)
    mouse_click.fire() #prints "left"
    mouse_click.fire() #prints "left" and "double left"

## make retiterators out of generators that can send and receive

def remitter_example_yield_out():
    from collections import defaultdict

    class Remitter:
      def __init__(self, ritr):
          self.ritr     = ritr
          self.eventOut = Event()

      def send(self, val_in):
          ritr      = self.ritr
          event_out = self.eventOut

          while True:
              val_out = ritr.send(val_in)
              if val_out is receive:
                  break
              else:
                  event_out.fire(val_out)            

      def handle(self, handler):
          self.eventOut.handle(handler)

      def handlein(self, *events):
          for event in events:
              event.handle(self.send)

      def __rrshift__(self, event_in):
          try:
              self.handlein(*event_in)
          except:
              self.handlein(event_in)
          return self


    def remitter(gen_rcvr):
        def gen_remitter(*args, **kargs):
            ritr = gen_rcvr(*args, **kargs)
            ritr.send(None)
            return Remitter(ritr)
        return gen_remitter


    @remitter
    def double_detect_r(threshold):
        last_click_time = 0
        while True:
            yield receive
            current_click_time = time.time()
            if (current_click_time - last_click_time) &lt; threshold:
                yield
            last_click_time = current_click_time

    @remitter
    def map_r(f, *args, **kargs):
        while True:
           val = yield receive
           yield f(val, *args, **kargs)

    @remitter
    def print_r(format):
        while True:
           val = yield receive
           print message % val

    def label_r(label):
        return map_r(lambda val : (label, val))

    @remitter
    def label_count_r():
        count_by_label = defaultdict(int)
        while True:
            (label, val) = yield receive
            count_by_label[label] += 1
            yield count_by_label.copy()

    def fix_click_counts(count_by_label, single_label, double_label):
        count_by_label[single_label] -= (count_by_label[double_label] * 2) #every double left "cancels" a single click
        return count_by_label.copy()

    def print_label_counts(count_by_label, *labels):
        print ", ".join("%d %s" % (count, label) for (label, count) in count_by_label.iteritems())


    mouse_clicks = Event()

    ([mouse_clicks &gt;&gt; label_r("single"),
      mouse_clicks &gt;&gt; double_detect_r(.01) &gt;&gt; label_r("double")] 
     &gt;&gt; label_count_r() &gt;&gt; map_r(fix_click_counts, "single", "double") &gt;&gt; map_r(print_label_counts))

    #prints
    #0 double, 1 single
    #0 double, 2 single
    #0 double, 3 single
    #1 double, 1 single
    mouse_clicks.fire() 
    time.sleep(.02)
    mouse_clicks.fire() 
    mouse_clicks.fire()


def remitter_without_yield_in_hack_example():
    class Receive:
        def __init__(self, val = None):
            self.d = val

    class Remitter:
      def __init__(self, receive, ritr):
          self.receive  = receive
          self.ritr     = ritr
          self.eventOut = Event()

      def send(self, val_in):
          self.receive.d = val_in

          ritr      = self.ritr
          event_out = self.eventOut
          while True:
              val_out = ritr.next()
              if isinstance(val_out, Receive):
                  break
              else:
                  event_out.fire(val_out)

      def handle(self, handler):
          self.eventOut.handle(handler)

      def handlein(self, *events):
          for event in events:
              event.handle(self.send)

      def __rrshift__(self, event_in):
          try:
              self.handlein(*event_in)
          except:
              self.handlein(event_in)
          return self

    def remitter(gen_rcvr):
        def gen_remitter(*args, **kargs):
            receive = Receive()
            ritr = gen_rcvr(receive, *args, **kargs)
            ritr.send(None)
            return Remitter(receive, ritr)
        return gen_remitter

    @remitter
    def double_detect_r(receive, threshold):
        last_click_time = 0
        while True:
            yield receive
            current_click_time = time.time()
            gap                = current_click_time - last_click_time
            if gap &lt; threshold:
                yield gap
            last_click_time = current_click_time

    @remitter
    def average_r(receive):
        total   = 0.0
        count   = 0
        while True:
            yield receive
            total += receive.d
            count += 1
            yield total/count

    @remitter
    def print_r(receive, format):
        while True:
            yield receive
            print format % (receive.d)

    mouse_clicks = Event()
    mouse_clicks &gt;&gt; double_detect_r(.05) &gt;&gt; average_r() &gt;&gt; print_r("double left; average gap is %s seconds")

    mouse_clicks.fire() 
    time.sleep(.1)
    mouse_clicks.fire() 
    time.sleep(.01)
    mouse_clicks.fire() #prints #double left; average gap is 0.01... seconds
    time.sleep(.02) 
    mouse_clicks.fire() #double left; average gap is 0.015... seconds

if __name__ == "__main__":
    #simple_click_event_example()
    #click_event_manipulation_example()
    #click_event_maniuplation_refactored_example()
    #click_event_handle_with_result_example()
    #click_event_choosing_by_returning_event_example()
    #click_event_looks_like_streams_example()
    #remitter_example()
    #remitter_example_yield_out()
    remitter_without_yield_in_hack_example()

&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-3111670930227756908?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/3111670930227756908/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2009/08/code-for-reactive-programming-in-python.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/3111670930227756908'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/3111670930227756908'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2009/08/code-for-reactive-programming-in-python.html' title='The Code for Reactive Programming in Python'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-3720127284642222356</id><published>2009-08-12T13:42:00.000-07:00</published><updated>2009-08-12T13:59:37.155-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reactive'/><category scheme='http://www.blogger.com/atom/ns#' term='wes'/><category scheme='http://www.blogger.com/atom/ns#' term='monad'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='linq'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><category scheme='http://www.blogger.com/atom/ns#' term='events'/><title type='text'>Rx Simplified (Reactive Programming in Python)</title><content type='html'>Lately, there's been interest in "reactive programming", especially with &lt;a href="http://themechanicalbride.blogspot.com/2009/07/introducing-rx-linq-to-events.html"&gt;Rx&lt;/a&gt;.   What is Rx?   I've seen descriptions like "reactive programming with LINQ", "the dual of the enumerator/iterator" and even "a variation of the continuation monad".  Oh right...uh...monad?  dual?  what's going on?  
&lt;p&gt;
 If you like things like "monads", "duals", and category theory, go watch &lt;a href="http://channel9.msdn.com/shows/Going+Deep/Kim-Hamilton-and-Wes-Dyer-Inside-NET-Rx-and-IObservableIObserver-in-the-BCL-VS-2010/"&gt;this video&lt;/a&gt;, especially until the end.  It's pretty funny.
&lt;p&gt;
But if those things make your eyes glaze over and you're left wondering what Rx really is, &lt;b&gt;I want to give you a simple explanation of what Rx is all about.  In fact, I'll show how you could have invented it yourself&lt;/b&gt;.   We'll do so with simple event-based code written in Python.  
&lt;p&gt;

&lt;h2&gt;Step 1: write simple event handlers&lt;/h2&gt;

Imagine we have a mouse click event that fires either "left" or "right", and we want to make a new event that fires "double left" when there's a double left click.  We might write something like this (including a simple Event class).

&lt;pre class="code"&gt;

import time

class Event:
    def __init__(self):
        self.handlers = []

    def handle(self, handler):
        self.handlers.append(handler)

    def fire(self, val = None):
        for handler in self.handlers:
            handler(val)

def echo(val):
    print val
    return val

def left_double_click_from_click(click, threshold):
    dlclick = Event()
    last_lclick_time = [0] #closure hack

    def click_handler(click_value):
        if click_value == "left":
            lclick_time = time.time()
            if (lclick_time - last_lclick_time[0]) &lt; threshold:
                dlclick.fire("double left")
            last_lclick_time[0] = lclick_time

    click.handle(click_handler)
    return dlclick


click   = Event()
dlclick = left_double_click_from_click(click, .01)
dlclick.handle(echo)

click.fire("left")
time.sleep(.02)
click.fire("left")
click.fire("right")
click.fire("left") #prints "double click"

&lt;/pre&gt;

&lt;h2&gt;Step 2: refactor event handlers&lt;/h2&gt;

It works and it's pretty simple.  But, we could refactor quite a bit.  If we do so, we might write something like this (notice I like the suffix "_r" for "reactive"):

&lt;pre class="code"&gt;

class EventFireRecord:
    def __init__(self, value, time):
        self.value = value
        self.time  = time

def doubleize_r(evt, threshold, combine):
    double_evt = Event()

    last_fire  = EventFireRecord(None, 0)
    def evt_handler(value):
        fire_time = time.time()
        if (fire_time - last_fire.time) &lt; threshold:
            double_evt.fire(combine(last_fire.value, value))
        last_fire.__init__(value, fire_time)

    evt.handle(evt_handler)
    return double_evt

def filter_r(evt, predicate):
    filtered_evt = Event()

    def evt_handler(value):
        if predicate(value):
            filtered_evt.fire(value)

    evt.handle(evt_handler)
    return filtered_evt

click   = Event()
dlclick = doubleize_r(filter_r(click, lambda value : value == "left"), .01, lambda l1, l2: "double left")
dlclick.handle(echo)

click.fire("left")
time.sleep(.02)
click.fire("left")
click.fire("right")
click.fire("left") #prints "double left"

&lt;/pre&gt;

That looks better and is more generic.  The logic of "double click" is now contained all on one line.  But, we could do even better.  Notice that we repeat ourselves a little with filter_r and doublize_r.  The pattern looks like this:

&lt;pre class="code"&gt;
evt_out = Event()

def handler(value):
   ...
   evt_out.fire(value)
   ...

evt_in.handle(handler)
return evt_out
&lt;/pre&gt;

What if we refactor to pull out that common pattern by making a special handler that returns a value and a special "handle_with_result" that looks like this pattern?

&lt;pre class="code"&gt;
def handler(value):
    ...
    return value

evt_out = handle_with_result(evt_in, handler)
&lt;/pre&gt;

&lt;h2&gt;Step 3: make a higher-level "handle" function&lt;/h2&gt;

If we do that, our code might look like this:

&lt;pre class="code"&gt;
def handle_with_result(evt, handler_with_result):
    evt_out = Event()

    def handler(value):
        result = handler_with_result(value)
        if result is not None:
            evt_out.fire(result)

    evt.handle(handler)
    return evt_out

def doubleize_event(evt, threshold, combine):
    last_fire = EventFireRecord(None, 0)

    def handler(value):
        fire_time = time.time()
        try:
            if (fire_time - last_fire.time) &lt; threshold:
                return combine(last_fire.value, value)
        finally:
            last_fire.__init__(value, fire_time)

    return handle_with_result(evt, handler)

def filter_event(evt, predicate):
    def handler(value):
        if predicate(value):
            return value

    return handle_with_result(evt, handler)


click  = Event()
dlclick = doubleize_event(filter_event(click, lambda value : value == "left"), .01, lambda l1, l2 : "double left")
dlclick.handle(echo)

click.fire("left")
time.sleep(.02)
click.fire("left")
click.fire("right")
click.fire("left") #prints "double left"

&lt;/pre&gt;

It works, and our code looks better than ever.  handle_with_result is very useful.
&lt;p&gt;
But,  we are now missing something: what if we want to return multiple values?  Or do something more interesting, like listen to an keyboard event and return left-clicks if the user clicks "l" and right clicks if they type "r" and two "fake" clicks if they type "f".  We'd like to write something like this:

&lt;pre class="code"&gt;
def choose_clicks(keys, clicks):
    def key_handler(char):
        if char == "l":
            return filter_event("left", clicks)
        elif char == "r":
            return filter_event("right", clicks)
        elif char == "f":
            return ["fake", "fake"]

    retrn handle_with_result(keys, key_handler)
&lt;/pre&gt;

If we change handle_with_result to handle this, we might get something like this:

&lt;pre class="code"&gt;

def handle_with_result(evt, handler_with_result):
    evt_out = Event()

    def handler(value):
        result = handler_with_result(value)
        if result is None:
            pass #ignore
        elif isinstance(result, Event):
            result.handle(evt_out.fire)
        elif isinstance(result, list):
            for value_out in result:
                evt_out.fire(value_out)
        else:
            evt_out.fire(result)


    evt.handle(handler)
    return evt_out

def filter_r(evt, predicate):
    def handler(value):
        if predicate(value):
            return value

    return handle_with_result(evt, handler)

def value_filter_r(evt, value):
    return filter_r(evt, lambda val : val == value)

def choose_clicks(keys, clicks):
    def key_handler(char):
        #TODO: unsubscribe from event after either "l" or "r"
        if char == "l":
            return value_filter_r(clicks, "left")
        elif char == "r":
            return value_filter_r(clicks, "right")
        elif char == "f":
            return ["fake", "fake"]

    return handle_with_result(keys, key_handler)

keys   = Event()
clicks = Event()
choosen_clicks = choose_clicks(keys, clicks)

choosen_clicks.handle(echo)
clicks.fire("left")
keys.fire("a")
clicks.fire("right")
keys.fire("l")
clicks.fire("left") # print "left"
clicks.fire("right")
clicks.fire("left") # print "left"
keys.fire("f") #prints "fake" and then "fake" again

&lt;/pre&gt;

Great.  Now if we just add a little bit of syntax sugar to this, we can make events look like streams:

&lt;h2&gt;Step 4: add some syntax sugar&lt;/h2&gt;

&lt;pre class="code"&gt;
class Event:
    def __init__(self):
        self.handlers = []

    def handle(self, handler):
        self.handlers.append(handler)
        return self #so += will work

    def fire(self, val = None):
        for handler in self.handlers:
            handler(val)

    def bind(evt, handler_with_result):
        evt_out = Event()

        def handler(value):
            result = handler_with_result(value)
            if result is not None:
                Event.unit(result).handle(evt_out.fire)

        evt.handle(handler)
        return evt_out

    @classmethod
    def unit(cls, val):
        if isinstance(val, cls):
            return val
        elif isinstance(val, list):
            return MockEvent(*val)
        else:
            return MockEvent(val)

    __rshift__ = bind

class MockEvent:
    def __init__(self, *vals):
        self.vals = vals

    def handle(self, handler):
        for val in self.vals:
            handler(val)  

def doublize_r(threshold, combine):
    last_fire = EventFireRecord(None, 0)

    def handler(value):
        fire_time = time.time()
        try:
            if (fire_time - last_fire.time) &lt; threshold:
                return combine(last_fire.value, value)
        finally:
            last_fire.__init__(value, fire_time)

    return handler

def filter_r(predicate):
    def handler(value):
        if predicate(value):
            return value

    return handler

def value_filter_r(value):
    return filter_r(lambda val : val == value)

def click_choose_r(**clicks_by_char):
    def key_handler(char):
        return clicks_by_char.get(char)

    return key_handler


clicks   = Event()
keys     = Event()
dlclicks = clicks &gt;&gt; value_filter_r("left") &gt;&gt; doublize_r(.01, lambda l1, l2: "double click")
keys &gt;&gt; click_choose_r(d = dlclicks, f = ["fake", "fake"]) &gt;&gt; echo

clicks.fire("left")
clicks.fire("left")
keys.fire("f") #prints "fake" and then "fake" again
keys.fire("d")
clicks.fire("right")
clicks.fire("right")
time.sleep(.02)
clicks.fire("left")
clicks.fire("left") #print ("left", "left")

&lt;/pre&gt;

&lt;h2&gt;So what have we made?&lt;/h2&gt;

Wonderful.  &lt;b&gt;We've made events look like streams by making a slick way of creating event handlers&lt;/b&gt;. In fact, if you look closely at what I did in that last step, you'll notice that I renamed "handle_with_result" to "bind" and moved some code into a method called "unit".  That's all it takes to turn Event into a monad, which is exactly what Rx does.  Congratulations, &lt;b&gt;we just reinvented monads and Rx, just by refactoring our event handler code&lt;/b&gt; and in the process &lt;b&gt;we've discovered what Rx really is: a fancy way of writing event handlers&lt;/b&gt;, specifically event handlers that fire events that trigger other event handlers that fire events, and so on in big chain that looks like a query.  So when your eyes glaze over about "duals" and "monads" and "reactive programming", just say to yourself: I'm making a fancy event handler.  Because in the end, that's all you're really doing.  
&lt;p&gt;
In fact, if you want to do so in Python, now you have a basic implementation to start with!  Of course, this is just a toy implementation.  It lack error handling, unsubscribing, end-of-stream, and concurrency.  But it ain't bad for just 50 lines of code.  And it lets you see the essence of Rx fairly easily.
&lt;p&gt;
Oh, and what's the big deal with monads, you ask?  Nothing much.  It's just that if you provide "bind" and "unit" (called "select many" and "select" in LINQ, I think), LINQ gives you some nice syntax sugar that makes your event handler look like a query.  It's really pretty slick, especially now that they've added "extension methods".  

&lt;h2&gt;And next time...&lt;/h2&gt;

In future posts, I'll explore different ways of making slick event handlers, but without monads.  And hopefully we'll get make this handle concurrency, which is what asynchronous programming is all about.  In fact, I expect we'll start to see a serious blurring of lines between Rx, message passing, and data flow programming.  
&lt;p&gt;
For now, when you start working with Rx, just remember: I'm making a big, fancy event handler.  An "Observable" is just like and event and an "Observer" is just like an event handler.

&lt;h3&gt;P.S.&lt;/h3&gt;

What's with the lame names?  They started off with cool names like "Reactive" and "Rx" and then give us Observable, Observer, Subscribe, OnNext, OnDone, and OnError.  Yuck.  Think what an opportunity they missed!  We could have had names like Emitter, Reactor, Chain, Emit, Extinguish, and Explode.  Judge for yourself:

&lt;pre class="code"&gt;
observable.Subscribe(observer)
observer.OnNext(val)
observer.OnDone()
observer.OnError(e)
&lt;/pre&gt; 

or 

&lt;pre class="code"&gt;
emitter.chain(reactor)
reactor.emit("foo")
reactor.extinguish()
reactor.explode(e)
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-3720127284642222356?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/3720127284642222356/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2009/08/simple-rx-reactive-programming-in.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/3720127284642222356'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/3720127284642222356'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2009/08/simple-rx-reactive-programming-in.html' title='Rx Simplified (Reactive Programming in Python)'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-3195652850700456668</id><published>2009-01-14T12:06:00.000-08:00</published><updated>2009-01-14T12:07:03.222-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='bits'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Popcount in Python (with benchmarks)</title><content type='html'>&lt;h3&gt;Purpose&lt;/h3&gt;

A slightly uncommon bit-twiddling operation is "popcount", which
counts the number high bits.  While uncommon, it's crucial for
implementing Array Mapped Tries, which is a way to implement immutable
maps and vectors.  I'm trying to implement immutable maps and vectors,
so I needed to implement popcount.

&lt;p&gt;
I found A TON of ways, but had no idea which was fastest.  So, I
implemented them all and tested them.

The short answer is to do this:

&lt;pre class="code"&gt;
#http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable
POPCOUNT_TABLE16 = [0] * 2**16
for index in xrange(len(POPCOUNT_TABLE16)):
    POPCOUNT_TABLE16[index] = (index &amp;amp; 1) + POPCOUNT_TABLE16[index &gt;&gt; 1]

def popcount32_table16(v):
    return (POPCOUNT_TABLE16[ v        &amp;amp; 0xffff] +
            POPCOUNT_TABLE16[(v &gt;&gt; 16) &amp;amp; 0xffff])
&lt;/pre&gt;

And here's the long answer:

&lt;h3&gt;Results&lt;/h3&gt;
I ran popcount on 30,000 random ints between 0 and 2&lt;super&gt;31&lt;/super&gt;-1.  Here are the results from my
Linux Desktop with a 3.2Ghz Core 2 Quad:

&lt;p&gt;
&lt;div align="center"&gt;
  &lt;table border="1" cellpadding="4"&gt;
    &lt;tr&gt;&lt;td&gt;Name       &lt;/td&gt;&lt;td&gt;Time&lt;/td&gt;&lt;td&gt;Max Bits&lt;/td&gt;&lt;td&gt;URL&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;c_bagwell  &lt;/td&gt;&lt;td&gt;0.0030&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://lamp.epfl.ch/papers/idealhashtrees.pdf&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;c_bsdmacro &lt;/td&gt;&lt;td&gt;0.0030&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;c_parallel &lt;/td&gt;&lt;td&gt;0.0031&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;c_ops64    &lt;/td&gt;&lt;td&gt;0.0036&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSet64&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;table16    &lt;/td&gt;&lt;td&gt;0.0098&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;c_table8   &lt;/td&gt;&lt;td&gt;0.0110&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;table+kern &lt;/td&gt;&lt;td&gt;0.0132&lt;/td&gt;&lt;td&gt; -&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;table8     &lt;/td&gt;&lt;td&gt;0.0163&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;bsdmacro   &lt;/td&gt;&lt;td&gt;0.0190&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;parallel   &lt;/td&gt;&lt;td&gt;0.0199&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;ops64      &lt;/td&gt;&lt;td&gt;0.0207&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSet64&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;bagwell    &lt;/td&gt;&lt;td&gt;0.0242&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://lamp.epfl.ch/papers/idealhashtrees.pdf&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;freebsd    &lt;/td&gt;&lt;td&gt;0.0257&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;timpeters3 &lt;/td&gt;&lt;td&gt;0.0277&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://mail.python.org/pipermail/python-list/1999-July/007696.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;timpeters2 &lt;/td&gt;&lt;td&gt;0.0580&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://mail.python.org/pipermail/python-list/1999-July/007696.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;timpeters  &lt;/td&gt;&lt;td&gt;0.0724&lt;/td&gt;&lt;td&gt; ?&lt;/td&gt;&lt;td&gt;http://mail.python.org/pipermail/python-list/1999-July/007696.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;kernighan  &lt;/td&gt;&lt;td&gt;0.0745&lt;/td&gt;&lt;td&gt; -&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;elklund    &lt;/td&gt;&lt;td&gt;0.0889&lt;/td&gt;&lt;td&gt; -&lt;/td&gt;&lt;td&gt;http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;naive      &lt;/td&gt;&lt;td&gt;0.1519&lt;/td&gt;&lt;td&gt; -&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;seistrup   &lt;/td&gt;&lt;td&gt;0.2447&lt;/td&gt;&lt;td&gt; ?&lt;/td&gt;&lt;td&gt;http://mail.python.org/pipermail/python-list/1999-July/007696.html&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
&lt;/div&gt;

&lt;p&gt;
And here are the results for my MacBook with a 2.0Ghz Core 2 Duo:

&lt;p&gt;
&lt;div align="center"&gt;
  &lt;table border="1" cellpadding="4"&gt;
    &lt;tr&gt;&lt;td&gt;Name       &lt;/td&gt;&lt;td&gt;Time&lt;/td&gt;&lt;td&gt;Max Bits&lt;/td&gt;&lt;td&gt;URL&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;table16    &lt;/td&gt;&lt;td&gt;0.0279&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;table+kern &lt;/td&gt;&lt;td&gt;0.0372&lt;/td&gt;&lt;td&gt; -&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;table8     &lt;/td&gt;&lt;td&gt;0.0417&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;bsdmacro   &lt;/td&gt;&lt;td&gt;0.0481&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;bagwell    &lt;/td&gt;&lt;td&gt;0.0596&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://lamp.epfl.ch/papers/idealhashtrees.pdf&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;timpeters3 &lt;/td&gt;&lt;td&gt;0.0744&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://mail.python.org/pipermail/python-list/1999-July/007696.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;parallel   &lt;/td&gt;&lt;td&gt;0.0807&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;ops64      &lt;/td&gt;&lt;td&gt;0.1290&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSet64&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;timpeters2 &lt;/td&gt;&lt;td&gt;0.1439&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;http://mail.python.org/pipermail/python-list/1999-July/007696.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;kernighan  &lt;/td&gt;&lt;td&gt;0.1527&lt;/td&gt;&lt;td&gt; -&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;timpeters  &lt;/td&gt;&lt;td&gt;0.1668&lt;/td&gt;&lt;td&gt; ?&lt;/td&gt;&lt;td&gt;http://mail.python.org/pipermail/python-list/1999-July/007696.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;freebsd    &lt;/td&gt;&lt;td&gt;0.1772&lt;/td&gt;&lt;td&gt; -&lt;/td&gt;&lt;td&gt;http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;elklund    &lt;/td&gt;&lt;td&gt;0.1913&lt;/td&gt;&lt;td&gt; -&lt;/td&gt;&lt;td&gt;http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;naive      &lt;/td&gt;&lt;td&gt;0.2889&lt;/td&gt;&lt;td&gt; -&lt;/td&gt;&lt;td&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;seistrup   &lt;/td&gt;&lt;td&gt;0.6106&lt;/td&gt;&lt;td&gt; ?&lt;/td&gt;&lt;td&gt;http://mail.python.org/pipermail/python-list/1999-July/007696.html&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
&lt;/div&gt;

&lt;p&gt;
&lt;h3&gt;Observations&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;Implementations written in C (using &lt;a href="http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/"&gt;Pyrex&lt;/a&gt;) are 5-6 times faster than Python.&lt;/li&gt;
  &lt;li&gt;My 3.2 Ghz Linux Desktop is 200% faster than my 2.0 Ghz MacBook even though it only has a 60% clockspeed advantage.  I'm not sure what to make of that.  Python doesn't use multiple cores well, so I'm sure it's not that.&lt;/li&gt;
  &lt;li&gt;If you want to run popcount on 32-bit values in pure Python, table16 is the fastest by far, and only 3 times slower than implementations in C.&lt;/li&gt;
  &lt;li&gt;If you need to run popcount on arbitrarily large integers, kernighan is the best, but doing a hybrid of table16 and kernighan is probably better.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;

If you don't mind using about 64KB of memory, here's is the fastest popcount in pure python:

&lt;pre class="code"&gt;
#http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable
POPCOUNT_TABLE16 = [0] * 2**16
for index in xrange(len(POPCOUNT_TABLE16)):
    POPCOUNT_TABLE16[index] = (index &amp;amp; 1) + POPCOUNT_TABLE16[index &gt;&gt; 1]

def popcount32_table16(v):
    return (POPCOUNT_TABLE16[ v        &amp;amp; 0xffff] +
            POPCOUNT_TABLE16[(v &gt;&gt; 16) &amp;amp; 0xffff])
&lt;/pre&gt;

If you do mind the memory usage, here's a slighly slower version:

&lt;pre class="code"&gt;
#http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable
POPCOUNT_TABLE8 = [0] * 2**8
for index in xrange(len(POPCOUNT_TABLE8)):
    POPCOUNT_TABLE8[index] = (index &amp;amp; 1) + POPCOUNT_TABLE8[index &gt;&gt; 1]

def popcount32_table8(v):
    return (POPCOUNT_TABLE8[ v        &amp;amp; 0xff] +
            POPCOUNT_TABLE8[(v &gt;&gt; 8)  &amp;amp; 0xff] +
            POPCOUNT_TABLE8[(v &gt;&gt; 16) &amp;amp; 0xff] +
            POPCOUNT_TABLE8[ v &gt;&gt; 24        ])
&lt;/pre&gt;

And if you need to handle values of more than 32 bits, one of these two are the best:

&lt;pre class="code"&gt;
#http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
def popcount_kernighan(v):
    c = 0
    while v:
        v &amp;amp;= v - 1
        c += 1
    return c

POPCOUNT32_LIMIT = 2**32-1
POPCOUNT_TABLE8 = [0] * 2**8
for index in xrange(len(POPCOUNT_TABLE8)):
    POPCOUNT_TABLE8[index] = (index &amp;amp; 1) + POPCOUNT_TABLE8[index &gt;&gt; 1]

def popcount_hybrid(v):
    if v &lt;= POPCOUNT32_LIMIT:
        return (POPCOUNT_TABLE16[ v        &amp;amp; 0xffff] +
                POPCOUNT_TABLE16[(v &gt;&gt; 16) &amp;amp; 0xffff])
    else:
        c = 0
        while v:
            v &amp;amp;= v - 1
            c += 1
        return c

&lt;/pre&gt;

If it needs to be faster than that, write in C!

&lt;h3&gt;Test For Yourself&lt;/h3&gt;

If you want to test these yourself, here's some code you can run.

&lt;pre class="code"&gt;
#http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive
def popcount_naive(v):
    c = 0
    while v:
        c += (v &amp;amp; 1)
        v &gt;&gt;= 1
    return c

#http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
def popcount_kernighan(v):
    c = 0
    while v:
        v &amp;amp;= v - 1
        c += 1
    return c

#http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable
POPCOUNT_TABLE8 = [0] * 2**8
for index in xrange(len(POPCOUNT_TABLE8)):
    POPCOUNT_TABLE8[index] = (index &amp;amp; 1) + POPCOUNT_TABLE8[index &gt;&gt; 1]

def popcount32_table8(v):
    return (POPCOUNT_TABLE8[ v        &amp;amp; 0xff] +
            POPCOUNT_TABLE8[(v &gt;&gt; 8)  &amp;amp; 0xff] +
            POPCOUNT_TABLE8[(v &gt;&gt; 16) &amp;amp; 0xff] +
            POPCOUNT_TABLE8[ v &gt;&gt; 24        ])

POPCOUNT_TABLE16 = [0] * 2**16
for index in xrange(len(POPCOUNT_TABLE16)):
    POPCOUNT_TABLE16[index] = (index &amp;amp; 1) + POPCOUNT_TABLE16[index &gt;&gt; 1]

def popcount32_table16(v):
    return (POPCOUNT_TABLE16[ v        &amp;amp; 0xffff] +
            POPCOUNT_TABLE16[(v &gt;&gt; 16) &amp;amp; 0xffff])

POPCOUNT32_LIMIT = 2**32-1
def popcount_table16_kernighan(v):
    if v &lt;= POPCOUNT32_LIMIT:
        return (POPCOUNT_TABLE16[ v        &amp;amp; 0xffff] +
                POPCOUNT_TABLE16[(v &gt;&gt; 16) &amp;amp; 0xffff])
    else:
        c = 0
        while v:
            v &amp;amp;= v - 1
            c += 1
        return c


#http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSet64
def popcount32_ops64(v):
    return ((((v &amp;amp; 0xfff) * 0x1001001001001 &amp;amp; 0x84210842108421)            % 0x1f) +
            ((((v &amp;amp; 0xfff000) &gt;&gt; 12) * 0x1001001001001 &amp;amp; 0x84210842108421) % 0x1f) +
            (((v &gt;&gt; 24) * 0x1001001001001 &amp;amp; 0x84210842108421)              % 0x1f))
   

#http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
#also http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html
def popcount32_parallel(v):
    v = v                - ((v &gt;&gt; 1) &amp;amp; 0x55555555)
    v = (v &amp;amp; 0x33333333) + ((v &gt;&gt; 2) &amp;amp; 0x33333333)
    #v = (v &amp;amp; 0x0F0F0F0F) + ((v &gt;&gt; 4) &amp;amp; 0x0F0F0F0F)
    #v = v                + ((v &gt;&gt; 4) &amp;amp; 0x0F0F0F0F)
    v = (v + (v &gt;&gt; 4)) &amp;amp; 0x0F0F0F0F
    v = (v * 0x1010101) &gt;&gt; 24
    return v % 256 #I added %256. I'm not sure why it's needed. It's probably because of signed ints in Python

def popcount32_bagwell(v):
    v = v                - ((v &gt;&gt; 1) &amp;amp; 0x55555555)
    v = (v &amp;amp; 0x33333333) + ((v &gt;&gt; 2) &amp;amp; 0x33333333)
    v = (v &amp;amp; 0x0F0F0F0F) + ((v &gt;&gt; 4) &amp;amp; 0x0F0F0F0F)
    v = v                +  (v &gt;&gt; 8)
    v = (v + (v &gt;&gt; 16)) &amp;amp; 0x3F
    return v

#http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html
def popcount_elklund(v):
    c = 0
    while v:
        v ^= v &amp;amp; -v
        c += 1
    return c

#http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html
def popcount32_freebsd(v):
    v = (v &amp;amp; 0x55555555) + ((v &amp;amp; 0xaaaaaaaa) &gt;&gt; 1);
    v = (v &amp;amp; 0x33333333) + ((v &amp;amp; 0xcccccccc) &gt;&gt; 2);
    v = (v &amp;amp; 0x0f0f0f0f) + ((v &amp;amp; 0xf0f0f0f0) &gt;&gt; 4);
    v = (v &amp;amp; 0x00ff00ff) + ((v &amp;amp; 0xff00ff00) &gt;&gt; 8);
    v = (v &amp;amp; 0x0000ffff) + ((v &amp;amp; 0xffff0000) &gt;&gt; 16);
    return v

#http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html
def popcount32_bsdmacro(v):
    #define BITCOUNT(x) (((BX_(x)+(BX_(x)&gt;&gt;4)) &amp;amp; 0x0F0F0F0F) % 255)
    #define BX_(x) ((x) - (((x)&gt;&gt;1)&amp;amp;) - (((x)&gt;&gt;2)&amp;amp;0x33333333) - (((x)&gt;&gt;3)&amp;amp;0x11111111))
    #
    #def bx(v): (v - ((v &gt;&gt; 1) &amp;amp; 0x77777777) - ((v &gt;&gt; 2) &amp;amp; 0x33333333) - ((v &gt;&gt; 3) &amp;amp; 0x11111111))
    #def bc(v): ((bx(v) + (bx(v) &gt;&gt; 4)) &amp;amp; 0x0F0F0F0F) % 255

    v = (v - ((v &gt;&gt; 1) &amp;amp; 0x77777777) - ((v &gt;&gt; 2) &amp;amp; 0x33333333) - ((v &gt;&gt; 3) &amp;amp; 0x11111111))
    v = ((v + (v &gt;&gt; 4)) &amp;amp; 0x0F0F0F0F)
    return v % 255



#http://mail.python.org/pipermail/python-list/1999-July/007696.html
import marshal, array, struct, string
POPCOUNT8_TRANS_TABLE = "".join(map(chr, POPCOUNT_TABLE8))

#changed by me to match new dumps() and use sum()
def popcount_timpeters(v):
    counts = array.array("B", string.translate(marshal.dumps(v), POPCOUNT8_TRANS_TABLE))
    # overwrite type code
    counts[0] = 0
    return sum(counts)

#changed by me to add loop unrolling and not setting digitcounts[0]
def popcount32_timpeters2(v):
    counts = array.array("B", string.translate(marshal.dumps(v), POPCOUNT8_TRANS_TABLE))
    return counts[1] + counts[2] + counts[3] + counts[4]

#improved by me: no need to translate type char
def popcount32_timpeters3(v):
    dumped = marshal.dumps(v)
    return POPCOUNT_TABLE8[ord(dumped[1])] + POPCOUNT_TABLE8[ord(dumped[2])] + POPCOUNT_TABLE8[ord(dumped[3])] + POPCOUNT_TABLE8[ord(dumped[4])]

#http://mail.python.org/pipermail/python-list/1999-July/007696.html
_run2mask = {1: 0x5555555555555555L,
             2: 0x3333333333333333L,
             4: 0x0F0F0F0F0F0F0F0FL,
             8: 0x00FF00FF00FF00FFL}

def buildmask2(run, n):
    run2 = run + run
    k = (n + run2 - 1) / run2
    n = k * run2
    try:
        answer = _run2mask[run]
        k2 = 64 / run2
    except KeyError:
        answer = (1L &lt;&lt; run) - 1
        k2 = 1
    while k &gt; k2:
        k2 = k2 + k2
        if k &gt;= k2:
            answer = answer | (answer &lt;&lt; (run * k2))
        else:
            answer = answer | (answer &lt;&lt; (run2 * (k - k2/2)))
            k2 = k
    if k2 &gt; k:
        answer = answer &gt;&gt; (run2 * (k2 - k))
    return answer, n

def nbits(v):
    return 32 #???

def popcount_seistrup(v):
    lomask, n = buildmask2(1, nbits(v))
    v = v - ((v &gt;&gt; 1) &amp;amp; lomask)
    target = 2
    while n &gt; target:
        lomask, n = buildmask2(target, n)
        v = (v &amp;amp; lomask) + ((v &gt;&gt; target) &amp;amp; lomask)
        target = target + target
        for i in range(1, target/2):
            if n &lt;= target:
                break
            n = n &gt;&gt; 1
            n = (n + target - 1) / target * target
            v = (v &amp;amp; ((1L &lt;&lt;&gt;&gt; n)
    return int(v)
   

if __name__ == "__main__":
    import time, random

    def time_func(func):
        before = time.time()
        result = func()
        after  = time.time()
        span   = after - before
        return result, span

    popcounts = [popcount_naive,       
                 popcount32_table8,    
                 popcount32_table16,   
                 popcount_table16_kernighan,
                 popcount_kernighan,   
                 popcount32_ops64,     
                 popcount32_parallel,  
                 popcount32_bagwell,   
                 popcount_elklund,     
                 popcount32_freebsd,   
                 popcount32_bsdmacro,  
                 popcount_seistrup,    
                 popcount_timpeters,   
                 popcount32_timpeters2,
                 popcount32_timpeters3,
                 ]

    test_count      = 30000
    max_int         = 2**31 - 2
    ints            = range(0, 257) + [random.randint(0, max_int) for _ in xrange(test_count)] + range(max_int - 100, max_int+1)
    expected_counts = map(popcount_naive, ints)
    for popcount in popcounts:
        counts, timespan = time_func(lambda : map(popcount, ints))
        print "%5.5f %s" % ((timespan if (counts == expected_counts) else -1), popcount.__name__) #-1 means failure

&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-3195652850700456668?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/3195652850700456668/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2009/01/popcount-in-python-with-benchmarks.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/3195652850700456668'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/3195652850700456668'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2009/01/popcount-in-python-with-benchmarks.html' title='Popcount in Python (with benchmarks)'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-7204022690748669232</id><published>2008-11-11T11:05:00.000-08:00</published><updated>2008-12-09T09:53:20.668-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='decorator'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Easy Python Decorators with the decorator decorator</title><content type='html'>&lt;p&gt;
Decorators in python are very powerful, but are often a pain to get
right, especially when you want to pass arguments to the decorator.
Typically, it involves defining a function in a function in a
function, etc, up to 4 layers deep.  Can we make it easier?  What if
we even made a decorator to make decorators?  

&lt;p&gt;
Perhaps we could use it catch and log errors:

&lt;pre class="code"&gt;
@decorator
def log_error(func, *args, **kargs):
   try:
       return func(*args, **kargs)
   except Exception, error:
       print "error occurred: %r" % error

@log_error
def blowup():
   raise Exception("blew up")

blowup() # this gets caught and prints the error
&lt;/pre&gt;

&lt;p&gt;
Or maybe we'd like to synchronize a function to make it
thread-safe:

&lt;pre class="code"&gt;
@decorator
def synchronized(lock, func, *args, **kargs):
    lock.acquire()
    try:
        return func(self, *args, **kargs)
    finally:
        lock.release()

missle_lock = thread.RLock()

@synchronized(missle_lock)
def launch_missiles():
    pass
&lt;/pre&gt;
    
&lt;p&gt;
Or we could even use arguments to do do some object __init__ trickery
(something I've had to do when working with wxpython):

&lt;pre class="code"&gt;
@decorator
def inherits_format(method, *args, **kargs):
    self, parent = args[:2]
    self.format = parent.format
    return method(*args, **kargs)

class Child:
    @inherits_format
    def __init__(self, parent):
        pass

class Parent:
    def __init__(self, format):
        self.format = format

format = object()
child  = Child(Parent(format))
assert child.format is format
&lt;/pre&gt;

&lt;p&gt;
If you've never worked python decorators, these are few examples of
how powerful they are.  But you'll probably never want to write your
own because it can be a pain.  If that's the case, then &lt;b&gt;the decorator
decorator is for you!&lt;/b&gt;  Here's the code for it.  It's a little
tricky, but all you have to do is import it, slap @decorator before your
decorator, make sure you call func(*args, **kargs), and you're all
set.  &lt;b&gt;This is as easy as decorators can get.&lt;/b&gt;

&lt;p&gt;&lt;b&gt;
Update&lt;/b&gt;: &lt;a href="http://ndanger.org/blog/"&gt;Dave&lt;/a&gt; left a comment and notified me that there is another, much more complete, implementation of the same idea at &lt;a href="http://www.phyast.pitt.edu/~micheles/python/documentation.html"&gt;http://www.phyast.pitt.edu/~micheles/python/documentation.html&lt;/a&gt;.  So, if you want a very complete module, go there.  If you want something small and simple to cut and paste into your own code, use the following.


&lt;pre class="code"&gt;
def decorator(decorator_func):
    decorator_expected_arg_count = decorator_func.func_code.co_argcount - 1
    if decorator_expected_arg_count:
        def decorator_maker(*decorator_args):
            assert len(decorator_args) == decorator_expected_arg_count, "%s expects %d args" % (decorator_func.__name__, decorator_expected_arg_count)

            def _decorator(func):
                assert callable(func), "Decorator not given a function.  Did you forget to give %r arguments?" % (decorator_func.__name__)

                def decorated_func(*args, **kargs):
                    full_args = decorator_args + (func,) + args
                    return decorator_func(*full_args, **kargs)

                decorated_func.__name__ = func.__name__
                return decorated_func
            return _decorator
        return decorator_maker
    else:
        def _decorator(func):
            def decorated_func(*args, **kargs):
                return decorator_func(func, *args, **kargs)

            decorated_func.__name__ = func.__name__
            return decorated_func
        return _decorator
    
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-7204022690748669232?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/7204022690748669232/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2008/11/easy-python-decorators-with-decorator.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/7204022690748669232'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/7204022690748669232'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2008/11/easy-python-decorators-with-decorator.html' title='Easy Python Decorators with the decorator decorator'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-2663889366745000471</id><published>2008-10-14T20:30:00.000-07:00</published><updated>2008-10-14T20:33:19.242-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='osx'/><category scheme='http://www.blogger.com/atom/ns#' term='dtrace'/><title type='text'>How to DTrace Python in OSX</title><content type='html'>&lt;p&gt;
&lt;a href="http://en.wikipedia.org/wiki/DTrace"&gt;DTrace&lt;/a&gt; is an
incredible tool.  It basically lets you do profiling of a live
application with no performance penatly.  I'm writing a Python that
needed some profiling, and I found the "normal" techniques like the
profile/cProfile module very lacking.  Luckily, Mac OSX comes with
DTrace and it even works with Python.  The only snag is that it's hard
to find how to use the darn thing.  I finally figured it out, so I
figured it pass on the knowledge.

&lt;p&gt;
So, here's how you use dtrace on your python application in Mac OSX:

&lt;ol&gt;
  &lt;li&gt;Get &lt;a href="http://opensolaris.org/os/community/dtrace/dtracetoolkit/"&gt;DTraceToolkit&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Edit Python/py_cputime.d by replacing "function-entry" with "entry" and "function-return" with "exit".&lt;/li&gt;
  &lt;li&gt;Call "sudo dtrace -s Python/py_cputime.d"&lt;/li&gt;
  &lt;li&gt;Let it sit there a while and hit ctrl-c.&lt;/li&gt;
  &lt;li&gt;Enjoy the results&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
I can only assume you have to edit the file because of some difference
between Solaris and OSX.  You can try files other than py_cputime.d,
but you might have to edit them too.  Not all of them work, but most
do.

&lt;p&gt;
The last thing to know is that you have to use the python that comes
with OSX.  A custom-built python doesn't seem to work.

&lt;p&gt;
Hope that helps!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-2663889366745000471?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/2663889366745000471/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2008/10/how-to-dtrace-python-in-osx.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/2663889366745000471'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/2663889366745000471'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2008/10/how-to-dtrace-python-in-osx.html' title='How to DTrace Python in OSX'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-1873044110438895960</id><published>2008-10-14T17:09:00.000-07:00</published><updated>2008-12-09T09:54:26.651-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Python Memory Usage: What values are taking up so much memory?</title><content type='html'>&lt;style type="text/css"&gt;
.nobr br { display: none }
&lt;/style&gt;

&lt;p&gt;
Python seems to use a lot of memory.  So what exactly is the overhead
of each type of value? Short answer:
&lt;/p&gt;

&lt;div align="center"&gt;
  &lt;table border="1" cellpadding="4"&gt;
    &lt;tr&gt;&lt;td&gt;int&lt;/td&gt;                                   &lt;td&gt; 24&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;float&lt;/td&gt;                                 &lt;td&gt; 24&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;tuple&lt;/td&gt;                                 &lt;td&gt; 63&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;list&lt;/td&gt;                                  &lt;td&gt;101&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;dict&lt;/td&gt;                                  &lt;td&gt;298&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;old-style class&lt;/td&gt;                       &lt;td&gt;345&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;new-style class&lt;/td&gt;                       &lt;td&gt;336&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;subclassed tuple&lt;/td&gt;                      &lt;td&gt; 79&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Record&lt;/td&gt;                                &lt;td&gt; 79&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Record with old class mixin&lt;/td&gt;           &lt;td&gt; 79&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Record with new class mixin&lt;/td&gt;           &lt;td&gt; 79&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  Measured in bytes using Python 2.5 in 64-bit Ubuntu Linux
&lt;/div&gt;

&lt;p&gt;
I measured these by running a simple program that loaded up 1,000,000 values in a list and then did time.sleep(1000).  I ran that for different value types and then ran "top" to see how much memory was being used.  I took that value, substracted the memory usage for a list of all the same value (14 bytes each), subtracted the value of a child value (usually an int, 24 bytes each), and then divided by 1,000,000.  I'll include the code I ran at the end if you want to cut and paste.

So what lessons do we learn from this?

&lt;p&gt;
&lt;ul&gt;
  &lt;li&gt;Python objects are very expensive at over 300 bytes each.&lt;/li&gt;
  &lt;li&gt;Tuples have 1/5 as much overhead.&lt;/li&gt;
  &lt;li&gt;Records are almost as good as tuples, even when a mixin is added.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
So, &lt;b&gt;if you want to have lots of values in memory without using lots of memory, use &lt;a href="http://www.valuedlessons.com/2007/12/immutable-data-in-python-record-or.html"&gt;Record&lt;/a&gt;&lt;/b&gt;.
&lt;/p&gt;

&lt;p&gt;
If you want to run the test for yourself, here's the code.  Just comment out the "make_val" that you want to test.
&lt;/p&gt;

&lt;pre class="code"&gt;
import time
from Record import Record

class TupleClass(tuple):
    pass

class RecordClass(Record("val")):
    pass

class OldClass:
    def __init__(self, val):
        self.val = val

    def method(self):
        pass

class NewClass(object):
    def __init__(self, val):
        self.val = val

    def method(self):
        pass

class RecordWithOldClass(Record("val"), OldClass):
    pass

class RecordWithNewClass(Record("val"), NewClass):
    pass

make_val = lambda i : 1 #nothing (base overhead)
#make_val = lambda i : i    
#make_val = float           
#make_val = lambda i : (i,) 
#make_val = lambda i : [i]  
#make_val = lambda i : {i:i}
#make_val = TupleClass
#make_val = RecordClass
#make_val = OldClass
#make_val = NewClass
#make_val = RecordWithOldClass
#make_val = RecordWithNewClass

count = 1000000
lst = [make_val(i) for i in xrange(count)]
time.sleep(100000)
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-1873044110438895960?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/1873044110438895960/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2008/10/blog-post.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/1873044110438895960'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/1873044110438895960'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2008/10/blog-post.html' title='Python Memory Usage: What values are taking up so much memory?'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-937727408962662534</id><published>2008-06-27T12:43:00.001-07:00</published><updated>2008-10-14T19:51:32.585-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='actor'/><category scheme='http://www.blogger.com/atom/ns#' term='erlang'/><category scheme='http://www.blogger.com/atom/ns#' term='shareever'/><category scheme='http://www.blogger.com/atom/ns#' term='message-passing'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='concurrency'/><title type='text'>My Experience with Message Passing Concurrency</title><content type='html'>I'm working on a peer-to-peer file synchronization program. It's &lt;i&gt;really&lt;/i&gt; concurrent and distributed, and it's forced me to
learn a thing or two about the concurrency models that we use as programmers.  Over the past few years, I've tried both the shared-state model common to C, C++, Java, C#, Python, etc, and the message-passing model unique to Erlang and Scala.  In my experience, &lt;b&gt;the message-passing model (aka Actor Model) is far superior to the shared-state model for writing distributed applications&lt;/b&gt;.  After having used both extensively, I'd go so far as to say that &lt;b&gt;for distributed programming, shared-state concurrency is upside down and backwards&lt;/b&gt;.  
&lt;p&gt;

Here's my informal proof that message-passing concurrency is necessary in a distributed system:
&lt;p&gt;

&lt;ol&gt;
  &lt;li&gt;Since the system in distributed, real shared state is impossible.&lt;/li&gt;
  &lt;li&gt;The only way for the distributed components of an application to
  communicate is by sending a message from one to another.&lt;/li&gt;
  &lt;li&gt;Everything else is an abstraction on top of message passing.&lt;/li&gt;
&lt;/ol&gt;

HTTP is an abstraction on top of message-passing.  AJAX is an abstraction on top of HTTP on top of message-passing.  XML-RPC is an abstraction on top of HTTP on top of message-passing.  SOAP is an abstraction on top of an abstraction on top of an abstraction on top of HTTP on top of message-passing.   Any RPC mechanism is an abstraction on top of message-passing.  SQL queries to a remote database are an abstraction on top of message-passing.  CORBA is a nasty, tangled mess on top of a foundation of message-passing.
&lt;p&gt;

See a pattern?
&lt;p&gt;

Not only are all of these abstractions, but they are &lt;i&gt;leaky&lt;/i&gt; abstractions.  Just about all RPC (Remote Procedure Call) frameworks try to pretend that remote objects or local.  But the facade is impossible to keep from leaking.  Calls to a remote object might fail or take arbitrarily long.  If you want to make the same call to two different remote nodes, those calls must be made synchronously and sequentially; in order to call them in parallel on the remote nodes, they most be called in parallel on the local node.  If a call to a remote node triggers a call back to the local node, which may trigger a call to a third node, you end up with a huge spaghetti mess of calls and threads.
&lt;p&gt;

I've been down that road.  It wasn't pretty.  There is a better way.
&lt;p&gt;

I think AJAX has opened our eyes a bit.   It's a lot closer to message-passing than it is to RPC.  In a REST architecture, you "send" messages by POSTing to a URL and you "receive" messages by GETing a URL.  All messages are clearly local copies that were serialized and deserialized from the remote data.  There isn't any leaky abstraction of data or classes pretending to be in two places at once.   Data, timeouts, and failures are in your face and you have to deal with them.  So, AJAX is a lot less leaky.
&lt;p&gt;

But AJAX is far from perfect.  I can't help but think of the Greenspun's Tenth Rule of Programming with a twist on distributed programming:  "Any sufficiently complicated distributed platform contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of a message-passing system (Erlang)".  Once you get message-passing in your head, you can't help but think of AJAX in that way. 
&lt;p&gt;

I've been down a better road.  It was much more pleasant. 
&lt;p&gt;

About six months ago, I rewrote major portions of our application with message-passing concurrency.  I took a long time.  It was tricky.  It hurt my head.  But it worked.   It's a success.  It's capable of things that the shared-state system could never do.
&lt;p&gt;

Having done it, I can emphatically say that if I could start all over, I would go with message-passing.  Most of the work was building the infrastructure and wrapping my head around a new way of thinking.  But now that I've done both of those, I've paid the costs and I'm reaping the rewards.  We're moving the application in directions that would have been nearly in possible with the old concurrency model.  
&lt;p&gt;

&lt;b&gt;In my experience, message-passing concurrency is the best way to write distributed applications.&lt;/b&gt;  But it isn't an easy road to go down.  Support for it just isn't there in most programming languages and environments.  So far, Erlang has been the pioneer.  I would humbly agree that the way I've implemented message-passing in Python, compared to Erlang, looks like an ad-hoc, bug-ridden half-implementation.  But for various reasons, I can't use Erlang.  I'm hoping for someone to create Erlang++ or E# or Eython or something that combines the concurrency model of Erlang with a modern programming language.  Until then, I'll just keep on cobbling together what I can onto the programming language I happen to be using.  
&lt;p&gt;

More on that in another article.&lt;p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-937727408962662534?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/937727408962662534/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2008/06/my-experience-with-message-passing.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/937727408962662534'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/937727408962662534'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2008/06/my-experience-with-message-passing.html' title='My Experience with Message Passing Concurrency'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-7346141537929756046</id><published>2008-04-28T15:10:00.000-07:00</published><updated>2008-12-09T09:56:01.904-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='events'/><title type='text'>Events in Python</title><content type='html'>I'm a huge fan of the &lt;a href="http://en.wikipedia.org/wiki/Actor_model"&gt; Actor Model &lt;/a&gt;.  I think that for most applications, it's the best way to do concurrency.  As CPUs get more cores, concurrency becomes more important for programmers, and I think the Actor Model will become more important, too.
&lt;p&gt;

But, sometimes you need something more light-weight for handling "events".  For example, imagine you have some code listening for file changes in a particular directory.  What you'd like to do is make an "event" that is "fired" whenever a file change is detected.  When fired, there may be a "handler" or "listener" which is notified of the event.  That "handler" is ultimately just some code which is executed when the event occurs.
&lt;p&gt;

A while ago, &lt;b&gt;I wanted an event system like this for Python&lt;/b&gt;.  I didn't see anything builtin or any library available, so I decided to write my own.  I'd like to share what I created with you.
&lt;p&gt;

But first, I want to follow an example through other programming languages to give you an idea of what I was trying to accomplish and how it compares with what's out there.  After that, I'll give you my implemenation in Python.  Our example will be listening for changes on the file system.  We want to keep the "watcher" code decoupled from the rest of the code, so we use events.  
&lt;p&gt;

The best implementation of events that I've used is in C#, so we'll start there.  In C# 1.0, our file watcher would looks something like this:
&lt;p&gt;

&lt;pre class="code"&gt;
public delegate void FileChangeHandler(string source_path);

class FileWatcher
{
    public event FileChangeHandler FileChanged;

    public void WatchForChanges()
    {
       ...
       if(FileChanged != null)
       {
          FileChanged(source_path);
       }
       ...
    }
}

class FileChangeLogger
{
    public void OnFileChanged(string source_path)
    {
        Console.WriteLine(String.Format("{0} changed.", source_path));
    }
}

watcher = FileWatcher();
logger  = FileChangeLogger();
watcher.FileChanged += new FileChangeHandler(logger.OnFileChanged);
watcher.WatchForChanges();


&lt;/pre&gt;

It's pretty nice.  The best part is at the end, where you can write "watcher.FileChange += ...".  But, you need to type the completely useless "new FileChangeHandler", and you also need to wrap it all in a separate FileChangeLogger class.  Luckily, in C# 2.0, they added &lt;a href="http://msdn2.microsoft.com/en-us/magazine/cc163970.aspx"&gt;Anonymous Delegates&lt;/a&gt;, which makes this much nicer:
&lt;p&gt;

&lt;pre class="code"&gt;
watcher.FileChanged += delegate(string source_path)
{
    Console.WriteLine(String.Format("{0} changed.", source_path));
}
&lt;/pre&gt;

And in C# 3.0, they've made it even nicer!

&lt;pre class="code"&gt;
watcher.FileChanged += (source_path =&gt; Console.WriteLine(String.Format("{0} changed.", source_path)));
&lt;/pre&gt;

&lt;b&gt;C# 3.0 has an event system that's downright slick, with type-inferencing and everything&lt;/b&gt;.  I don't think it gets much better than that.  
&lt;p&gt;

Actually, there is one thing.  If there's no handler registered with the event, calling "FileChanged()" will blow up because it sees FileChanged == null until a handler is registered.  This means that you have to write "if(Event != null){Event(...):}" every single time you fire the event.  Every single time.  If you forget, your code will seem to work fine until there's no handler, in which case it will blow up and you'll smack your forhead because you forgot that you have to repeat that line of code every single time you fire an event.  I really mean every single time.  It's by far the worst wart on an otherwise elgant system.  I have no idea why the designers of C# thought this was a good idea.  When would you ever want to fire and event and have it blow up in your face?
&lt;p&gt;


Anyway, let's try a different programming language, perhaps Java.  It has the worst implementation of events I've ever seen.   Here's our FileWatcher example: 
&lt;p&gt;

...
&lt;p&gt;

...
&lt;p&gt;

Ok, nevermind, I don't have the heart.  I can imagine the code full of IListeners, and implementations, and keeping an array of them, and iterating over them, etc, and I just can't do it.  I got seriously upset at one useless line in C#.  In Java, it's at least 10 times worse.  If you really have the stomach for it, go look at http://java.sun.com/docs/books/tutorial/uiswing/events/index.html.   To me, it appears that &lt;b&gt;in Java, events are one giant hack around the lack of first-class functions.&lt;/b&gt;  If Java had first-class functions, none of that nonsense would be necessary.  
&lt;p&gt;


Now that we've seen a good implementation of events in C# and avoided a bad one in Java, let's make one for Python.  I'd rather it be like the C# event system, so let's see what our example would look like:
&lt;p&gt;

&lt;pre class="code"&gt;
from Event import Event

class FileWatcher:
    def __init__(self):
        self.fileChanged = Event()

    def watchFiles(self):
        ...
        self.fileChanged(source_path)
        ...

def log_file_change(source_path):
    print "%r changed." % (source_path,)

watcher              = FileWatcher()
watcher.fileChanged += log_file_change

&lt;/pre&gt;

I think that looks pretty good.  So what does the implementation of Event look like?
&lt;p&gt;

&lt;pre class="code"&gt;

class Event(IEvent):
    def __init__(self):
        self.handlers = set()

    def handle(self, handler):
        self.handler.add(handler)
        return self

    def unhandle(self, handler):
        try:
            self.handlers.remove(handler)
        except:
            raise ValueError("Handler is not handling this event, so cannot unhandle it.")
        return self

    def fire(self, *args, **kargs):
        for handler in self.handlers:
            handler(*args, **kargs)

    def getHandlerCount(self):
        return len(self.handlers)

    __iadd__ = handle
    __isub__ = unhandle
    __call__ = fire
    __len__  = getHandlerCount
&lt;/pre&gt;

Wow.  That was pretty short.  Actually, this is one of the reasons I love Python.  If the language doesn't have a feature, we can probably add it.  &lt;b&gt;We just added one of C#'s best features to Python in 26 lines of code&lt;/b&gt;.  More importantly, we now have a nice, light-weight, easy-to-use event system for Python.  
&lt;p&gt;

For all of you how like full examples that you can cut and paste, here is one that you can run.  Enjoy!
&lt;p&gt;

&lt;pre class="code"&gt;

class Event:
    def __init__(self):
        self.handlers = set()

    def handle(self, handler):
        self.handlers.add(handler)
        return self

    def unhandle(self, handler):
        try:
            self.handlers.remove(handler)
        except:
            raise ValueError("Handler is not handling this event, so cannot unhandle it.")
        return self

    def fire(self, *args, **kargs):
        for handler in self.handlers:
            handler(*args, **kargs)

    def getHandlerCount(self):
        return len(self.handlers)

    __iadd__ = handle
    __isub__ = unhandle
    __call__ = fire
    __len__  = getHandlerCount

class MockFileWatcher:
    def __init__(self):
        self.fileChanged = Event()

    def watchFiles(self):
        source_path = "foo"
        self.fileChanged(source_path)

def log_file_change(source_path):
    print "%r changed." % (source_path,)

def log_file_change2(source_path):
    print "%r changed!" % (source_path,)

watcher              = MockFileWatcher()
watcher.fileChanged += log_file_change2
watcher.fileChanged += log_file_change
watcher.fileChanged -= log_file_change2
watcher.watchFiles()

&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-7346141537929756046?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/7346141537929756046/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2008/04/events-in-python.html#comment-form' title='12 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/7346141537929756046'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/7346141537929756046'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2008/04/events-in-python.html' title='Events in Python'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>12</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-3057940394561666040</id><published>2008-04-01T21:37:00.000-07:00</published><updated>2008-12-09T09:56:53.451-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monad'/><category scheme='http://www.blogger.com/atom/ns#' term='parsing'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>You Could Have Invented Monadic Parsing</title><content type='html'>Even though Pysec (and functional programming in python in general) &lt;a href="http://www.valuedlessons.com/2008/03/why-are-my-monads-so-slow.html"&gt;turned out to be 2-3x slower&lt;/a&gt;, monadic parsing is still great for certain tasks.  After I wrote about Pysec &lt;a href="http://www.valuedlessons.com/2008/02/pysec-monadic-combinatoric-parsing-in.html"&gt;the first time&lt;/a&gt;, I received comments regarding &lt;a href="http://pyparsing.wikispaces.com/"&gt;pyparsing&lt;/a&gt;, which is a great parsing library that looks very similar.  Today, I'd like to show you a very simple example where monadic parsing really shines above all other parsing techniques, including pyparsing.  This is a real-world need that I had, not just an academic thought-experiment.  
&lt;p&gt;

The situation is that I'm writing a peer-to-peer file synchronizer and I need to serialize binary data in an efficient way.  I've choosen to use what I call "sized binary", which is basically &lt;a href="http://en.wikipedia.org/wiki/Netstrings"&gt;netstrings&lt;/a&gt; without the comma or &lt;a href="http://en.wikipedia.org/wiki/Bencode"&gt;bencode's byte string&lt;/a&gt;.   The byte-string "ABC", for example, would be encoded as "3:ABC", or "Hello World!" would be "12:Hello World!".  This is very efficient because "ABC" or "Hello World!" can be &lt;b&gt;any&lt;/b&gt; bytes \x00-\xFF, without any sort of encoding or decoding like &lt;a href="http://en.wikipedia.org/wiki/Uuencode"&gt;uuencode&lt;/a&gt;.
&lt;p&gt;

Stop and think for a minute how you would define a grammer to parse "3:ABC".  It's so simple, right?  Well, try and write a grammar for it.  Try using BNF, EBNF, yacc/bison, pyparsing, SimpleParse, or any parsing libarary you know of, in any programming language.  If you manage, please write me an email and let me know how you did it, because I'm not so sure it's even possible.  Maybe it is in the &lt;a href="http://www.ibm.com/developerworks/linux/library/l-cpregex.html"&gt;Perl6 grammars&lt;/a&gt;; they're throwing the kitchen sink into that thing :).  The best I've seen is that the creator of pyparsing hacked a grammar object to change how it parses the "ABC" part after a different object parsed the "3" part.  This basically uses the grammar object as a global variable.  It works in a single-core world, but it won't work in the many-core world we're headed for.   
&lt;p&gt;

But now you're getting bored with me because it's silly ringing my hands over such a simple format.  I bet you could parse this format in just 4 lines of code:
&lt;p&gt;

&lt;pre class="code"&gt;
def parse_sized_binary(stream):
    size_bytes, stream = stream.readUntil(":")
    size               = int(size_bytes)
    bytes, stream      = stream.read(size)
    return bytes, stream
&lt;/pre&gt;

You're right, it's really simple, at least until you have to embed this little "sized binary language" inside of a bigger language.  Perhaps, like me, you need to embed binary data inside of a bigger data structure, and so you need to define a serialization for things like ints, floats, lists, and hash tables.  For that stuff, you can define a grammer for a language like &lt;a href="http://en.wikipedia.org/wiki/JSON"&gt;JSON&lt;/a&gt; or &lt;a href="http://en.wikipedia.org/wiki/YAML"&gt;YAML&lt;/a&gt; and hand it over to a grammar-based parser library.  But now you need a way to tell the library, "OK, when you see my binary data, hand control over to my four lines of code, and then I'll hand control back".  You might even say "You know, why don't you just let me hand you any function of the form stream -&gt; (value, stream) and let me control the parsing for a while".  
&lt;p&gt;

&lt;b&gt;Congratulations, you just invented monadic parsing&lt;/b&gt;.  The function you wrote, parse_sized_binary, of the form stream -&gt; (bytes, stream), is a Parser Monad.  Monadic parsing is just combining lots of these little parsers together.  And so, it's incredibly easy to insert arbitrary parsing code, like parse_sized_binary, into any parser.  
&lt;p&gt;

As a complete example, using Pysec, here's a full parser for a language just like &lt;a href="http://en.wikipedia.org/wiki/Bencode"&gt;bencode&lt;/a&gt; but with support for floats and unicode.  Notice that our parse_size_binary is renamed to bencode_sized and is given a more functional definition. 
&lt;p&gt;

&lt;pre class="code"&gt;
from Pysec import Stub, until, lift, read, many_until, branch

bencode_any     = Stub()
bencode_sized   = until(":") &gt;&gt; lift(int) &gt;&gt; read
bencode_unsized = until("e")
bencode_list    = many_until(bencode_any, "e")
bencode_any.set(branch({"s" : bencode_sized,
                        "u" : bencode_sized &gt;&gt; lift(lambda bytes : bytes.decode("utf8")),
                        "i" : bencode_unsized &gt;&gt; lift(int),
                        "f" : bencode_unsized &gt;&gt; lift(float),
                        "l" : bencode_list,
                        "d" : bencode_list &gt;&gt; lift(dict)}))
&lt;/pre&gt;

I think this is sort of like &lt;a href="http://en.wikipedia.org/wiki/Greenspun's_Tenth_Rule"&gt;Greenspun's Tenth Rule&lt;/a&gt;.  Any implementation of netstrings/sized-binary-data probably has an ad-hoc implementation of monadic parsing in it.  Even my own non-monadic version of this parser (which I use for performance reasons) has an ad-hoc implementation of monadic parsing.  I'm convinced that if you write a parser for a similar format, you'll implement/invent monadic parsing as well, even if you don't know it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-3057940394561666040?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/3057940394561666040/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2008/04/you-could-have-invented-monadic-parsing.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/3057940394561666040'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/3057940394561666040'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2008/04/you-could-have-invented-monadic-parsing.html' title='You Could Have Invented Monadic Parsing'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-2556746310926583047</id><published>2008-03-19T14:10:00.000-07:00</published><updated>2008-10-14T19:57:58.801-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monad'/><category scheme='http://www.blogger.com/atom/ns#' term='parsing'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Why are my monads so slow?</title><content type='html'>If you've read the last few posts, you know that I choose to use monadic parsing for some application code I am writing.  At an abstract level, it turned out beautifully.  I was able to write the parser very elgantly and compactly despite the serialization format having some unique requirements.  But at a practical level, it failed pretty miserably.  &lt;b&gt;It was horribly slow&lt;/b&gt;.  With a deadline looming, I had to abandon my monadic parser and go back to using an older serialization format.  But not content to give up so easily, I decided to figure out &lt;b&gt;why are my monads so slow?&lt;/b&gt;  I hope that my investigation may be of help to you if you choose to use similar techniques in your code.
&lt;p&gt;

First, I tried running a profiler like &lt;a href="http://docs.python.org/lib/module-profile.html"&gt;cProfile&lt;/a&gt;.  While such profilers normally work quite well, they don't work so well with lots of first-class functions.  And since monadic code is full of first-class functions, the profiler didn't give much valuable information.  
&lt;p&gt;

So, I had to do optimizing the "old fashioned" way.   I wrote a benchmark and ran both of my serializers against it.  To start, I had two data points: one in traditional imperitave code and one in fully monadic code, and this is what I got:
&lt;p&gt;

&lt;pre&gt;
imperative: 0.465 seconds
monadic:    3.893 seconds
&lt;/pre&gt;

Monadic code was 8x slower!  But why?  To narrow it down, I added some more data points by implementing the serializer in gradually more monadic ways.  For example, I wrote a serializer in a functional/immutable style that passes around a "stream" rather than a string, and then another that passed around "state" in a very monadic way, but withoug using the monad class that I used in Pysec.  Then I got this:
&lt;p&gt;

&lt;pre&gt;
imperative:                0.465 seconds
functional with stream:    1.018 seconds
almost monadic with state: 2.450 seconds
monadic:                   3.893 seconds
&lt;/pre&gt;

This gave some answers.  One answer it gave was that passing around an immutable "stream" object is twice as expensive as merely passing around a string.  Also, passing around a state object on top of that is even more expensive.  With these clues, I was able to optimize in certain ways and reduce it to this:
&lt;p&gt;

&lt;pre&gt;
imperative:                0.465 seconds
functional with stream:    1.018 seconds
almost monadic with state: 1.098 seconds
monadic:                   1.283 seconds
&lt;/pre&gt;

&lt;p&gt;
That's a nice 3x improvement.  What things were I able to do?  By far the biggest cost that I was able to eliminate was object creation.  I flattened two layers of abstraction into one, and thus split the number of objects created in half.  The second thing I did was I created a "read until" operation that could be used more efficiently in the "grammar" of the parser.  This is a form of pushing performance-critical code down the layers of abstraction.  Finally, I didn't used decorators, for some often-called functions.  

&lt;p&gt;
In the end, &lt;b&gt;it looks like monadic code is about 2-3x slower in Python&lt;/b&gt;.  Almost all of that is actually the result of just doing things in a functional/immutable way.  In particular, creating data structures appears to be the main bottleneck.  In functional languagues, these types of operations are very common and optimized heavily, and so are typically very fast.  It looks like it's not the same in Python.  Just like you have to avoid recursion because there's no tail recursion, it looks like you have to avoid a functional/immutable coding style if you care about performance because object creation is so slow.  On the other hand, if you don't mind the peformance hit, it makes the code much more elegant, just like recursion usually does.

&lt;p&gt;
One thing of interest is that there is essentially no performance penatly for using monads over using a functional/immutable code style.  The 20% penatly seen between "almost monadic" and "monadic" is only because I'm wrapping the monad in a Parser class which allows nice operator overloading. 

&lt;p&gt;
Here's a summary of what you can do to speed up any functional/immutable-style code, including monadic code when writing in Python:

&lt;ul&gt;
&lt;li&gt;Make object creation as fast as possible.  Don't do anything fancy in __init__.&lt;/li&gt;
&lt;li&gt;Use as few layers of abstraction as possible, especially when there is an object created in each layer.&lt;/li&gt;
&lt;li&gt;Push common or expensive operations down the layers of abstaction, especially if it avoids creating objects.&lt;/li&gt;
&lt;li&gt;Avoid using decorators for heavily used functions.&lt;/li&gt;
&lt;li&gt;Don't use wrapper classes if you don't have to.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
As a final thought, I'd like to mention that while there's currently a substantial performance penalty for using immutable data structures, that style is going to become increasingly important as we enter the many-core era.  No matter what style of concurrency you like, immutable data is always easier to work with when concurrency is involved.   Concurrency and mutable data are just a bad combo.  I think that it's going to be very important for language designers to address this when working on the peformance of their languages.  I certainly hope future versions Python are much faster with immutable data.  If they are, then the peformance penatly of using Monads will almost disappear.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-2556746310926583047?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/2556746310926583047/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2008/03/why-are-my-monads-so-slow.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/2556746310926583047'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/2556746310926583047'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2008/03/why-are-my-monads-so-slow.html' title='Why are my monads so slow?'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-391176010328540725</id><published>2008-01-21T11:30:00.000-08:00</published><updated>2010-03-06T11:34:19.845-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='productivity'/><category scheme='http://www.blogger.com/atom/ns#' term='mediserve'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Garlic Programmers for Silver Code?</title><content type='html'>&lt;p&gt;
I just read Larry O'Brien's article about &lt;a href="http://www.knowing.net/index.php/2008/01/14/no-silver-programmers/ "&gt; there being no silver programmers&lt;/a&gt;.  He makes the case that although there may be great variance in programmer productivity, "the significant thing is not that some professional programmers are awesome, it's that some professional programmers suck".  I admire Larry O'Brien greatly, and I think there is much wisdom in what he says here.

&lt;p&gt;
But I think he's missing something.

&lt;p&gt;
I have noticed a trend in my own productivity: &lt;b&gt;my productivity varies greatly with the code base I'm working on&lt;/b&gt;.  This variance may be greater than that between individual programmers.  While many are rightly concerned with individual and team productivity, no ever talks about code base productivity.  If we do, where does it lead us?

&lt;p&gt;
A few years ago, I was working for a small medical software company.  One project was a new venture with a new code base.  Though new, the code was already difficult to work with. Its design forced the developer to duplicate work.  In my first meeting about the project, I was told we had to do lots of "fat fingering".  I didn't even know what that meant.  It turned out to mean typing almost the exact same thing over and over.  I was horrified and ultimately wrote a code generation tool to automate the work.  Though creating and maintaining it was extra work in itself, without it, &lt;b&gt;doing many common tasks in this code easily took 3x longer than necessary&lt;/b&gt;.

&lt;p&gt;
After that project was "shelved", I was moved to the company's main product, which was older and had a much more "mature" code base.  Actually, half was new-style (similar to what I had been working on) and half was old-style.  The old-style code was even worse to work with.  It was so bad that I was almost giddy whenever I could work on the new-style code.  I'm sure you know what kind of code I'm talking about.  &lt;b&gt;Doing anything in this code easily took 3x even longer&lt;/b&gt;.

&lt;p&gt;
Combined, that's 9x slower.  That seems too high.  Is it even possible?  How can a code base reduce productivity so much?  Here are a few possibilities I thought of;  I'm sure you can think of more:
&lt;ul&gt;
  &lt;li&gt;Bad code leads to more code, and more code is slower to work with&lt;/li&gt;
  &lt;li&gt;Bad code is hard to understand&lt;/li&gt;
  &lt;li&gt;Bad code is dangerous to change&lt;/li&gt;
  &lt;li&gt;Bad code leads duplication of effort&lt;/li&gt;
  &lt;li&gt;Bad code leads to a slow change-compile-test cycle, leading to wasted time and loss of focus&lt;/li&gt;
  &lt;li&gt;Bad code from third party libraries can drive you crazy by crashing, malfunctioning randomly, or having little documentation&lt;/li&gt;
  &lt;li&gt;Bad code saps energy, interest, and morale&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
&lt;b&gt;A bad code base makes any programmer less productive.&lt;/b&gt;  Have you ever noticed that when you work from scratch with no code base to weigh you down, you can be much more productive?  Imagine that as your baseline productivity.  Now think of how long it takes you to accomplish something similar on a project that you're working on right now.  How does it compare?  I bet you're slower on the existing project.

&lt;p&gt;
But wait just a minute.  Imagine if my list of the effects of bad code were reversed.  What if, instead, it read:

&lt;ul&gt;
  &lt;li&gt;Good code leads to less code, and less code is easier to work with&lt;/li&gt;
  &lt;li&gt;Good code is easy to understand&lt;/li&gt;
  &lt;li&gt;Good code is safe to change&lt;/li&gt;
  &lt;li&gt;Good code avoids duplication of effort&lt;/li&gt;
  &lt;li&gt;Good code leads to a quick change-compile-test cycle&lt;/li&gt;
  &lt;li&gt;Good code uses libraries which remove the need for "dirty work" and let you focus on the important problems&lt;/li&gt;
  &lt;li&gt;Good code is fun to work with!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
&lt;b&gt;A good code base makes any programmer more productive.&lt;/b&gt;  Have you ever worked on a project that was so wonderful that you could crank out magnificent results almost effortlessly?  Perhaps &lt;a href="http://www.gnu.org/software/emacs/"&gt;it has a great DSL embedded in it&lt;/a&gt; or has &lt;a href="http://www.loudthinking.com/about.html"&gt;powerful infrastructure&lt;/a&gt;. 

&lt;p&gt;
Even with conservative estimates, &lt;b&gt;if bad code slows us down 5x and good code speeds us up just 2x faster, that's easily a 10x difference. &lt;/b&gt;  

&lt;p&gt;
Maybe silver programmers don't exist.  But is there "silver code"?  What if there are programmers who are more adept at writing silver code?  If they can make a code base that's just 2x easier to work with long-term, then that makes everyone that works on that code 2x more productive, possibly forever.  If such programmers exist, then they must be extremely valuable, not so much because of their own productivity, but because of the effect they will have on the productivity of others in the future.  Perhaps rather than "silver bullet" programmers that kill werewolves, these are just "garlic programmers" that ward of vampires.

&lt;p&gt;
Given that productivity can vary so much with quality of existing code, &lt;b&gt;perhaps its worth looking for "garlic programmers" who will write code that will keep the rest us of productive long-term.&lt;/b&gt;  I leave it as an exercise for the reader to figure out how to measure a programmer's garlic-ness :).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-391176010328540725?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/391176010328540725/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2008/01/garlic-programmers-for-silver-code.html#comment-form' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/391176010328540725'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/391176010328540725'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2008/01/garlic-programmers-for-silver-code.html' title='Garlic Programmers for Silver Code?'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-3350354136684239329</id><published>2008-01-09T10:34:00.000-08:00</published><updated>2008-12-09T09:59:59.612-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='monad'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='continuation'/><title type='text'>Monads in Ruby (with nice syntax!)</title><content type='html'>&lt;p&gt;
My last &lt;a href="http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax.html"&gt;article&lt;/a&gt; was about how you can do monads in python with nice syntax. Now I'd like to present nice monad syntax in Ruby. I'm not explaining what monads are or how you can use them. For now, I'm expecting you to be familiar with monads. If you aren't, go read a &lt;a href="http://sigfpe.blogspot.com/2006/08/you-could-have-invented-monads-and.html"&gt;nice article&lt;/a&gt; on them.

&lt;p&gt;
Imagine you have a Monad base class like this:

&lt;pre class="code"&gt;
class Monad
  def bind(func)
    raise "not implemented"
  end

  def self.unit(val)
    raise "not implemented"
  end

  # bind to block
  def bindb(&amp;func)
    bind(func)
  end
end
&lt;/pre&gt;

&lt;p&gt;
As an aside, Please excuse there being a "bind" and a "bindb".  It's silly that Ruby differentiates between a block and Proc like that.  I
also think it's silly that I have to keep typing "lambda {|val| func(val)}" to turn a method into a Proc and "proc.call(val)" to call a Proc.  In python, methods, functions, and lambdas are all the same thing, and it's a lot easier to code with.  In this sense, python is way better.  But Ruby has really slick syntax for passing in the block, and python's lambda restrictions are annoying.  Why can't we have the best of both?  I guess then I'd need Scala or Perl6.  End aside.

&lt;p&gt;
Let's implement the Either monad, which I call it Failable:

&lt;pre class="code"&gt;
class Failable &amp;lt; Monad
  def initialize(value, success)
    @value   = value
    @success = success
  end

  def bind(bindee)
    if @success
      bindee.call(@value)
    else
      self
    end
  end

  def self.unit(val)
    self.new(val, true)
  end

  def to_s
    if @success
      "Success(#{@value})"
    else
      "Failure(#{@value})"
    end
  end
end

def success(val)
  Failable.new(val, true)
end

def failure(val)
  Failable.new(val, false)
end
&lt;/pre&gt;

&lt;p&gt;
Now we can write some code that safely handles divide by zero without using exceptions (actually, exceptions essentiatlly are the Either monad, but never mind that for now):

&lt;pre class="code"&gt;
def fdiv(a, b)
  if b == 0
    failure("cannot divide by zero")
  else
    success(a / b)
  end
end

def fdiv_with_binding(first_divisor)
  fdiv(2.0, first_divisor).bindb do |val1|
    fdiv(3.0, 1.0)        .bindb do |val2|
      fdiv(val1, val2)    .bindb do |val3|
        success(val3)
      end
    end
  end
end

puts fdiv_with_binding(1.0)
puts fdiv_with_binding(0.0)
&lt;/pre&gt;

&lt;p&gt;
Which prints:

&lt;pre class="code"&gt;
Success(0.666666666666667)
Failure(cannot divide by zero)
&lt;/pre&gt;

&lt;p&gt;
But it's not very pretty.  Luckily, ruby has callcc, which makes it very easy to define a function which I'll call "rbind" which can do this:

&lt;pre class="code"&gt;
def fdiv_with_rbinding(first_divisor)
  with_monad Failable do
    val1 = rbind fdiv(2.0, first_divisor)
    val2 = rbind fdiv(3.0, 1.0)
    val3 = rbind fdiv(val1, val2)
    val3
  end
end

def with_monad(monad_class, &amp;amp block)
  begin
    val = block.call()
    if val.class == monad_class
      val
    else
      monad_class.unit(val)
    end
  rescue MonadEscape =&gt; escape
    escape.monad
  end
end

def rbind(monad)
  begin
    checked_callcc {|cc| monad.bind(cc)}
  rescue ContinuationUnused =&gt; unused
    raise MonadEscape.new(unused.result)
  end
end

class MonadEscape &lt; RuntimeError
  attr :monad

  def initialize(monad)
    @monad = monad
  end
end

def checked_callcc(&amp;with_cc)
  callcc do |cont|
    val = with_cc.call(lambda {|val| cont.call(val)})
    raise ContinuationUnused.new(val) if cont
    val
  end
end

class ContinuationUnused &lt; RuntimeError
  attr :result
  
  def initialize(result)
    @result = result
  end
end  
&lt;/pre&gt;

&lt;p&gt;
Ruby's block syntax makes it very nice to say "with_monad Monad do", which I like.  But I don't really like seeing "= rbind".  I'd really like it if we could override "&lt;&lt;" and read that as "=&lt;&lt;", but I think we're stuck with unary operators ("+", "-", "~", all of which look bad here) or words.  At least we can choose our name, unlike in python where we have to use "yield".  Does anyone have any idea for a better name?

&lt;p&gt;
Maybe "xx" would work:

&lt;pre class="code"&gt;
def fdiv_with_rbinding(first_divisor)
  with_monad Failable do
    val1 =xx fdiv(2.0, first_divisor)
    val2 =xx fdiv(3.0, 1.0)
    val3 =xx fdiv(val1, val2)
    val3
  end
end
&lt;/pre&gt;

&lt;p&gt;
So there you have it.  You can have nice monad syntax in Ruby using callcc. Unfortunately, I've read that not all implementations of Ruby have continuatoins, which means JRuby and IronRuby may be left out in the cold.  Keep in mind that Ruby's "yield" syntax is NOT a true generator, which means that we can't use the same trick that we used in python.  It's callcc or nothing.

&lt;p&gt;
Here's the complete code in case you want to cut and paste it:

&lt;pre class="code"&gt;
############# Monad Base ##############

class Monad
  def bind(func)
    raise "not implemented"
  end
  
  def self.unit(val)
    raise "not implemented"
  end

  # bind to block
  def bindb(&amp;func)
    bind(func)
  end
end

def with_monad(monad_class, &amp;block)
  begin
    val = block.call()
    if val.class == monad_class
      val
    else
      monad_class.unit(val)
    end
  rescue MonadEscape =&gt; escape
    escape.monad
  end
end

# "reverse" bind, or bind to the return value, or bind to the continuation
def rbind(monad)
  begin
    mycallcc {|cc| monad.bind(cc)}
  rescue ContinuationUnused =&gt; unused
    raise MonadEscape.new(unused.result)
  end
end


class MonadEscape &lt; RuntimeError
  attr :monad

  def initialize(monad)
    @monad = monad
  end
end

def mycallcc(&amp;with_cc)
  used = false
  val  = callcc do |cc|
    fake_cc = lambda do |val| 
      used = true
      cc.call(val)
    end

    with_cc.call(fake_cc)
  end

  raise ContinuationUnused.new(val) unless used
  val
end 

class ContinuationUnused &lt; RuntimeError
  attr :result
  
  def initialize(result)
    @result = result
  end
end  

############# Failable Monad ##################

class Failable &lt; Monad
  def initialize(value, success)
    @value   = value
    @success = success
  end
      
  def bind(bindee)
    if @success
      bindee.call(@value)
    else
      self
    end
  end

  def self.unit(val)
      self.new(val, true)
  end

  def to_s
    if @success
        "Success(#{@value})"
    else
        "Failure(#{@value})"
    end
  end
end

def success(val)
  Failable.new(val, true)
end

def failure(val)
  Failable.new(val, false)
end

######## Failable Monad Example ##########

def fdiv(a, b)
  if b == 0
    failure("cannot divide by zero")
  else
    success(a / b)
  end
end

def with_failable_binding(first_divisor)
  fdiv(2.0, first_divisor).bindb { |val1|
    fdiv(3.0, 1.0)        .bindb { |val2|
      fdiv(val1, val2)    .bindb { |val3|
        success(val3)
      }
    }
  }
end

def xx(monad)
  rbind(monad)
end

def with_failable_rbinding(first_divisor)
  with_monad Failable do
    val1 =xx fdiv(2.0, first_divisor)
    val2 =xx fdiv(3.0, 1.0)
    val3 =xx fdiv(val1, val2)
    val3
  end
end

puts with_failable_binding(1.0)
puts with_failable_binding(0.0)
puts with_failable_rbinding(1.0)
puts with_failable_rbinding(0.0)
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-3350354136684239329?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/3350354136684239329/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2008/01/monads-in-ruby-with-nice-syntax.html#comment-form' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/3350354136684239329'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/3350354136684239329'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2008/01/monads-in-ruby-with-nice-syntax.html' title='Monads in Ruby (with nice syntax!)'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-6799472260664409462</id><published>2008-01-07T11:21:00.000-08:00</published><updated>2008-12-09T10:00:50.432-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='haskell'/><category scheme='http://www.blogger.com/atom/ns#' term='actor'/><category scheme='http://www.blogger.com/atom/ns#' term='monad'/><category scheme='http://www.blogger.com/atom/ns#' term='message-passing'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='linq'/><category scheme='http://www.blogger.com/atom/ns#' term='generator'/><category scheme='http://www.blogger.com/atom/ns#' term='continuation'/><title type='text'>Monads in Python (with nice syntax!)</title><content type='html'>&lt;p&gt;
&lt;span style="font-weight: bold;"&gt;Update:&lt;/span&gt; I've been informed that I didn't clearly explain what monads are and what the code examples are supposed to do.  Sorry.  I guess I assumed too much preexposure to monads.  If you're no familiar with them, there are already so many tutorials on monads that I'll simply direct you to two of my favorites: &lt;a href="http://www.loria.fr/%7Ekow/monads/index.html"&gt;spacesuits&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Monads_in_functional_programming"&gt;wikipedia&lt;/a&gt;.  I've also added more explanations of what the code snippets are supposed to do.  I hope that helps.
&lt;p&gt;
&lt;span style="font-weight: bold;"&gt;Update 2: &lt;/span&gt;By popular demand, I'm including some code that you can actually run :).  See the bottom of the article.
&lt;p&gt;
Recently, I used monads in production code for a soon-to-be-publically-released application.  Although many think they are strange, estoric, and perhaps useless, monads were the best way to solve the problem.  My code is not in Haskell; it's in Python.  I'm not doing anything wierd like IO in a purely functional way; I'm just parsing a file.
&lt;p&gt;
The crazy, unexpected conclusion I came to is that &lt;span style="font-weight: bold;"&gt;you can and should use monads in your   code in almost any programming language.   &lt;/span&gt;There are two   parts to this: "can" and "should".  I think I'll save "should" for another article. Right now, I'm excited to show you how you can.
&lt;p&gt;
As a preview for "should", please consider that you may be using monads already without knowing it.  &lt;a href="http://www.google.com/url?sa=t&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=http%3A%2F%2Fresearch.microsoft.com%2F%7Eemeijer%2FPapers%2FLINQ20.pdf&amp;amp;ei=RYiCR8DJO5HEgwPei8GCBw&amp;amp;usg=AFQjCNE8AG_VXzu_tHYeXDy7Jw85eWUcig&amp;amp;sig2=SLBcZbWmgDnnAwaEygDrCA"&gt;LINQ in C# is a monad (pdf)&lt;/a&gt;, so if you've ever used it, you've used a monad. The C# guys used monads for queries for the same reason I'm using for them for parsing: they're the right tool for the job.  But unlike them, I can't change the syntax of the programming language.
&lt;p&gt;
The biggest challange with using monads in a "normal" programming language is that monads involve lots of closures. This is exactly the same problem you run into with &lt;a href="http://blogs.msdn.com/wesdyer/archive/2007/12/22/continuation-passing-style.aspx"&gt;CPS&lt;/a&gt;, which isn't surprising since a monad's "bind" operator is CPS and since continuations can be implemented with monads.  By the way, if your programming laungage doesn't have closures (meaning you are stuck programming in C, C++, or Java), then monads are probably out of the question.   Assuming you have closures and use them with monads directly, you end up with code like the following.  It's python using the &lt;a href="http://en.wikipedia.org/wiki/Monads_in_functional_programming#Maybe_monad"&gt;Maybe monad&lt;/a&gt;  to handle divide by zero errors.  I'm using "&gt;&gt;" (__rshift__) overloaded to mean "bind".

&lt;pre class="code"&gt;
def mdiv(a, b):
    if b == 0:
        return Nothing
    else:
        return Something(a / b)

def with_maybe():
    return \
    mdiv(2.0, 2.0)    &gt;&gt; (lambda val1 :
    mdiv(3.0, 0.0)    &gt;&gt; (lambda val2 :
    mdiv(val1, val2)  &gt;&gt; (lambda val3 :
    Something(val3))))                       
&lt;/pre&gt;
&lt;p&gt;
That's not very pretty.   We need a way to clean that up.  How we can do so depends on the programming language.  Haskell has "do" syntax built-in, which makes monadic code look like an impertive language or even a list comprehension.  Ruby and Scheme have call/cc which makes it trivial to wrap a bind call with a continuation to make any monadic code look "normal".  C# has LINQ, which is practically Haskell's do notation with funny names.
&lt;p&gt;
But I'm using python.  What does python have?  Luckily for me, in python 2.5 they added bidirectional generators, and I found a way to use them to make something like "do" notation.   Now we can write the above code like this (@do and mreturn are defined later):

&lt;pre class="code"&gt;
@do(Maybe)
def with_maybe(first_divisor):
    val1 = yield mdiv(2.0, 2.0)
    val2 = yield mdiv(3.0, 0.0)
    val3 = yield mdiv(val1, val2)
    mreturn(val3)
&lt;/pre&gt;
&lt;p&gt;
I even copied the names "do" and "return" from Haskell, although I had to spell "return" as "mreturn".  All you really have to remember is that "yield" means "bind" and that the end result is a monad.  There are limitations to this technique, but it's working very well for me so far.  I've implemented the Maybe monad, the Error monad, the StateChanger monad, and the Continuation monad (which will require another article to explain).  I particularly like the continuation monad because it allows me to write callcc, which lets me do threadless actors (message passing) in python:


&lt;pre class="code"&gt;
from collections import deque

class Mailbox:
    def __init__(self):
        self.messages = deque()
        self.handlers = deque()

    def send(self, message):
        if self.handlers:
            handler = self.handlers.popleft()
            handler(message)()
        else:
            self.messages.append(message)

    def receive(self):
        return callcc(self.react)

    @do(ContinuationMonad)
    def react(self, handler):
        if self.messages:
            message = self.messages.popleft()
            yield handler(message)
        else:
            self.handlers.append(handler)
           done(ContinuationMonad.zero())

@do(ContinuationMonad)
def insert(mb, values):
    for val in values:
        mb.send(val)

@do(ContinuationMonad)
def multiply(mbin, mbout, factor):
    while True:
        val = (yield mbin.receive())
        mbout.send(val * factor)

@do(ContinuationMonad)
def print_all(mb):
    while True:
        print (yield mb.receive())

original   = Mailbox()
multiplied = Mailbox()

print_all(multiplied)()
multiply(original, multiplied, 2)()
insert(original, [1, 2, 3])()
&lt;/pre&gt;
&lt;p&gt;
A few months ago, I wrote a similar implementation of threadless actors in python.  It used generators in a similar way, but it was 10 times as much code.   I was shocked at how short this implementation ended up being.   You might think that it's because the continuation monad implementation is big.  Nope.  It's just as short (Monad defined later, and Record defined &lt;a href="http://www.valuedlessons.com/2007/12/immutable-data-in-python-record-or.html"&gt;here&lt;/a&gt;):

&lt;pre class="code"&gt;
class ContinuationMonad(Record("run"), Monad):
    def __call__(self, cont = lambda a : a):
        return self.run(cont)

    def bind(self, bindee):
        return ContinuationMonad(lambda cont : self.run(lambda val : bindee(val).run(cont)))

    @classmethod
    def unit(cls, val):
       return cls(lambda cont : cont(val))

    @classmethod
    def zero(cls):
        return cls(lambda cont : None)

def callcc(usecc):
 return ContinuationMonad(lambda cont : usecc(lambda val : ContinuationMonad(lambda _ : cont(val))).run(cont))

&lt;/pre&gt;So, you can use monads with elegant syntax in any language that has closures and any of the following:
&lt;ul&gt;
&lt;li&gt;do syntax (Haskell, C#)&lt;/li&gt;
&lt;li&gt;call/cc (Scheme, Ruby)&lt;/li&gt;
&lt;li&gt;bidirectional generators (Python 2.5, a future JavaScript?)&lt;/li&gt;
&lt;li&gt;coroutines (Lua, Io)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The only think I haven't shown you is the implementation of Monad, @do, mreturn, and done.  It has a few nasty details related to using generators and decorators in python, but here's the gist of it:

&lt;pre class="code"&gt;
class Monad:
    def bind(self, func):
        raise NotImplementedError

    def __rshift__(self, bindee):
        return self.bind(bindee)

    def __add__(self, bindee_without_arg):
        return self.bind(lambda _ : bindee_without_arg())


@decorator_with_args
def do(func, func_args, func_kargs, Monad):
    itr = func(*func_args, **func_kargs)

    def send(val):
        try:
            monad = itr.send(val)
            return monad.bind(send)
        except MonadReturn, ret:
            return Monad.unit(ret.value)
        except Done, done:
            return done.monad

     return send(None)

def mreturn(val):
    raise MonadReturn(val)

def done(val):
    raise Done(val)

&lt;/pre&gt;

&lt;p&gt;That's it.  If you've made it all the way to the end of this long article, I hope you've found inspiration for using monads in your own applications, especially if you are coding in python.  If so, here's some code that you can run:


&lt;pre class="code"&gt;
import types

###### Base Monad and @do syntax#########

class Monad:
    def bind(self, func):
        raise NotImplementedError

    def __rshift__(self, bindee):
        return self.bind(bindee)

    def __add__(self, bindee_without_arg):
        return self.bind(lambda _ : bindee_without_arg())

def make_decorator(func, *dec_args):
    def decorator(undecorated):
        def decorated(*args, **kargs):
            return func(undecorated, args, kargs, *dec_args) 
        
        decorated.__name__ = undecorated.__name__
        return decorated
    
    decorator.__name__ = func.__name__
    return decorator

def make_decorator_with_args(func):
    def decorator_with_args(*dec_args):
        return make_decorator(func, *dec_args)
    return decorator_with_args

decorator           = make_decorator
decorator_with_args = make_decorator_with_args

@decorator_with_args
def do(func, func_args, func_kargs, Monad):
    @handle_monadic_throws(Monad)
    def run_maybe_iterator():
        itr = func(*func_args, **func_kargs)

        if isinstance(itr, types.GeneratorType):
            @handle_monadic_throws(Monad)
            def send(val):
                try:
                    # here's the real magic
                    monad = itr.send(val) 
                    return monad.bind(send)
                except StopIteration:
                    return Monad.unit(None)
                
            return send(None)
        else:
            #not really a generator
            if itr is None:
                return Monad.unit(None)
            else:
                return itr

    return run_maybe_iterator()

@decorator_with_args
def handle_monadic_throws(func, func_args, func_kargs, Monad):
    try:
        return func(*func_args, **func_kargs)
    except MonadReturn, ret:
        return Monad.unit(ret.value)
    except Done, done:
        assert isinstance(done.monad, Monad)
        return done.monad

class MonadReturn(Exception):
    def __init__(self, value):
        self.value = value
        Exception.__init__(self, value)

class Done(Exception):
    def __init__(self, monad):
        self.monad = monad
        Exception.__init__(self, monad)

def mreturn(val):
    raise MonadReturn(val)

def done(val):
    raise Done(val)

def fid(val):
    return val

##### Failable Monad ######

class Failable(Monad):
    def __init__(self, value, success):
        self.value   = value
        self.success = success

    def __repr__(self):
        if self.success:
            return "Success(%r)" % (self.value,)
        else:
            return "Failure(%r)" % (self.value,)    

    def bind(self, bindee):
        if self.success:
            return bindee(self.value)
        else:
            return self

    @classmethod
    def unit(cls, val):
        return cls(val, True)

class Success(Failable):
    def __init__(self, value):
        Failable.__init__(self, value, True)

class Failure(Failable):
    def __init__(self, value):
        Failable.__init__(self, value, False)

def failable_monad_examle():
    def fdiv(a, b):
        if b == 0:
            return Failure("cannot divide by zero")
        else:
            return Success(a / b)

    @do(Failable)
    def with_failable(first_divisor):
        val1 = yield fdiv(2.0, first_divisor)
        val2 = yield fdiv(3.0, 1.0)
        val3 = yield fdiv(val1, val2)
        mreturn(val3)

    print with_failable(0.0)
    print with_failable(1.0)

###### StateChanger Monad #########

class StateChanger(Monad):
    def __init__(self, run):
        self.run = run

    def bind(self, bindee):
        run0 = self.run

        def run1(state0):
            (result, state1) = run0(state0)
            return bindee(result).run(state1)

        return StateChanger(run1)

    @classmethod
    def unit(cls, val):
        return cls(lambda state : (val, state))

def get_state(view = fid):
    return change_state(fid, view)

def change_state(changer, view = fid):
    def make_new_state(old_state):
        new_state    = changer(old_state)
        viewed_state = view(old_state)
        return (viewed_state, new_state)
    return StateChanger(make_new_state)


def state_changer_monad_example():
    @do(StateChanger)
    def dict_state_copy(key1, key2):
        val = yield dict_state_get(key1)
        yield dict_state_set(key2, val)
        mreturn(val)

    @do(StateChanger)
    def dict_state_get(key, default = None):
        dct = yield get_state()
        val = dct.get(key, default)
        mreturn(val)

    @do(StateChanger)
    def dict_state_set(key, val):
        def dict_set(dct, key, val):
            dct[key] = val
            return dct

        new_state = yield change_state(lambda dct: dict_set(dct, key, val))
        mreturn(val)

    @do(StateChanger)
    def with_dict_state():
        val2 = yield dict_state_set("a", 2)
        yield dict_state_copy("a", "b")
        state = yield get_state()
        mreturn(val2)

    print with_dict_state().run({}) # (2, {"a" : 2, "b" : 2})

###### Continuation Monad #########

class ContinuationMonad(Monad):
    def __init__(self, run):
        self.run = run

    def __call__(self, cont = fid):
        return self.run(cont)        

    def bind(self, bindee):
        return ContinuationMonad(lambda cont : self.run(lambda val : bindee(val).run(cont)))

    @classmethod
    def unit(cls, val):
        return cls(lambda cont : cont(val))

    @classmethod
    def zero(cls):
        return cls(lambda cont : None)
    
def callcc(usecc):
    return ContinuationMonad(lambda cont : usecc(lambda val : ContinuationMonad(lambda _ : cont(val))).run(cont))
    
def continuation_monad_example():
    from collections import deque

    class Mailbox:
        def __init__(self):
            self.messages = deque()
            self.handlers = deque()

        def send(self, message):
            if self.handlers:
                handler = self.handlers.popleft()
                handler(message)()
            else:
                self.messages.append(message)

        def receive(self):
            return callcc(self.react)

        @do(ContinuationMonad)
        def react(self, handler):
            if self.messages:
                message = self.messages.popleft()
                yield handler(message)
            else:
                self.handlers.append(handler)
                done(ContinuationMonad.zero())

    @do(ContinuationMonad)
    def insert(mb, values):
        for val in values:
            mb.send(val)

    @do(ContinuationMonad)
    def multiply(mbin, mbout, factor):
        while True:
            val = (yield mbin.receive())
            mbout.send(val * factor)

    @do(ContinuationMonad)
    def print_all(mb):
        while True:
            print (yield mb.receive())

    original   = Mailbox()
    multiplied = Mailbox()

    print_all(multiplied)()
    multiply(original, multiplied, 2)()
    insert(original, [1, 2, 3])()

if __name__ == "__main__":
    failable_monad_examle()
    state_changer_monad_example()
    continuation_monad_example()

&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-6799472260664409462?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/6799472260664409462/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax.html#comment-form' title='13 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/6799472260664409462'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/6799472260664409462'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax.html' title='Monads in Python (with nice syntax!)'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>13</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1700157236206200597.post-3759062215680956156</id><published>2007-12-31T14:29:00.000-08:00</published><updated>2008-12-09T10:01:33.147-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='mutability'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='concurrency'/><title type='text'>Immutable Data in Python (Record or Named Tuple)</title><content type='html'>&lt;p&gt;
A valued lesson that I have learned the hard way is that mutable data can get nasty, and that it's really nice to have immutable data structures.  I've been writing a lot of python recently, and after a while I realized that I was writing a lof of this:

&lt;pre class="code"&gt;
class SomeDataStructure:
    def __init__(self, arg1, arg2):
        self.prop1 = arg1
        self.prop2 = arg2

&lt;/pre&gt;

&lt;p&gt;
Not only was this repetitive, but I found that little mutability bugs were cropping up on me.  It's just too easy to trip over them when the default is mutability, as it is in python. In fact, immutable data structures aren't even a built-in option in python.

&lt;p&gt;
&lt;span style="font-weight: bold;"&gt;Finally the pain of using mutable data structures grew too large. &lt;/span&gt;&lt;span&gt;I looked for a way and couldn't find anything, so I created my own.  &lt;/span&gt;It supports getters, setters, inheritance, default values, altering several values at once, and I think the syntax is nice.  I've been using it for everything the last few months and it's been great.    It's helped with code repetition, concurrency, and serialization.

&lt;p&gt;
I hope you can learn from my valued lesson and not repeat my mistakes.  Here's how you use it.

&lt;pre class="code"&gt;
class Person(Record("name", "age")):
    pass

class OldPerson(Person):
    @classmethod
    def prepare(cls, name, age = None):
        return (name, age)

peter   = Person("Peter", 26)
wes     = Person("Wes", 28)
grandpa = OldPerson("Bubba")
wes2    = wes.setAge(29)
wes3    = wes.alter(name = "Grandpa", age = 57)
print peter, grandpa, wes.name, wes2.age

&lt;/pre&gt;

&lt;p&gt;
Here is the code.  This is actually a simplified version of the original that I rewrote on my own time. I use a more comlete/complex version in the code I'm writing for my employer.  

&lt;pre class="code"&gt;
def Record(*props):
    class cls(RecordBase):
        pass

    cls.setProps(props)

    return cls

class RecordBase(tuple):
    PROPS = ()

    def __new__(cls, *values):
        if cls.prepare != RecordBase.prepare:
            values = cls.prepare(*values)
        return cls.fromValues(values)

    @classmethod
    def fromValues(cls, values):
        return tuple.__new__(cls, values)

    def __repr__(self):
        return self.__class__.__name__ + tuple.__repr__(self)

    ## overridable
    @classmethod
    def prepare(cls, *args):
        return args

    ## setting up getters and setters
    @classmethod
    def setProps(cls, props):
        for index, prop in enumerate(props):
            cls.setProp(index, prop)
        cls.PROPS = props

    @classmethod
    def setProp(cls, index, prop):
        getter_name = prop
        setter_name = "set" + prop[0].upper() + prop[1:]

        setattr(cls, getter_name, cls.makeGetter(index, prop))
        setattr(cls, setter_name, cls.makeSetter(index, prop))

    @classmethod
    def makeGetter(cls, index, prop):
        return property(fget = lambda self : self[index])

    @classmethod
    def makeSetter(cls, index, prop):
        def setter(self, value):
            values = (value if current_index == index
                            else current_value
                      for current_index, current_value
                      in enumerate(self))
            return self.fromValues(values)
        return setter

&lt;/pre&gt;

&lt;p&gt;
For comparison, I've seen similar ideas at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/500261 and http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303439 but I don't like their implementation or design as much, especially since they lack proper setters.

&lt;p&gt;
I've also read that future versions of Python will have NamedTuples, which is something I wish it had already.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1700157236206200597-3759062215680956156?l=www.valuedlessons.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.valuedlessons.com/feeds/3759062215680956156/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.valuedlessons.com/2007/12/immutable-data-in-python-record-or.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/3759062215680956156'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1700157236206200597/posts/default/3759062215680956156'/><link rel='alternate' type='text/html' href='http://www.valuedlessons.com/2007/12/immutable-data-in-python-record-or.html' title='Immutable Data in Python (Record or Named Tuple)'/><author><name>Peter Thatcher</name><uri>http://www.blogger.com/profile/01092342988993218446</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='14656196412694126258'/></author><thr:total>6</thr:total></entry></feed>