<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
    <channel>
        <title>Pseudointellectual Appendification</title>
        <link>http://www.mischievous.org/</link>
        <description>Able or tending to cause annoyance, trouble, or minor injury.</description>
        <language>en-US</language>
        <copyright>Copyright 2009</copyright>
        <lastBuildDate>Mon, 01 Jun 2009 20:23:37 -0800</lastBuildDate>
        <generator>http://www.sixapart.com/movabletype/</generator>
        <docs>http://www.rssboard.org/rss-specification</docs>
        
        <item>
            <title>Cascading and Coroutines</title>
            <description><![CDATA[<p><a href="http://www.cascading.org/">Cascading</a> looks quite interesting.   Here is a python program that does something similar to the <a href="http://www.cascading.org/documentation/overview.html">Technical Overview</a> seen <strong><code>main</code></strong> in the python program.  </p>

<pre class="brush: python">
    #!/usr/bin/env python
    # encoding: utf-8
    import sys

    def input(theFile, pipe):
        """
        pushes a file a line at a time to a coroutine pipe
        """
        for line in theFile:
            pipe.send(line)
        pipe.close()

    @coroutine
    def extract(expression, pipe, group = 0):
        """
        extract the group from a regex
        """
        import re
        r = re.compile(expression)
        while True:
            line = (yield)
            match = r.search(line)
            if match:
                pipe.send(match.group(0))

    @coroutine
    def sort(pipe):
        """
        sort the input on a pipe
        """
        import heapq
        heap = []
        try:
            while True:
                line = (yield)
                heapq.heappush(heap, line)
        except GeneratorExit:
            while heap:
                pipe.send(heapq.heappop(heap))

    @coroutine
    def group(groupPipe, pipe):
        """
        sends consectutive matching lines from pipe to groupPipe
        """
        cur = None
        g = None
        while True:
            line = (yield)
            if cur is None:
                g = groupPipe(pipe)
            elif cur != line:
                g.close()
                g = groupPipe(pipe)

            g.send(line)
            cur = line

    @coroutine
    def uniq(pipe):
        """
        implements uniq -c
        """
        lines = 0
        try:
            while True:
                line = (yield)
                lines += 1
        except GeneratorExit:
            pipe.send('%s\t%s' % (lines, line))

    @coroutine
    def output(theFile):
        while True:
            line = (yield)
            theFile.write(line + '\n')

    def main():
        input(sys.stdin,
            extract( r'^([^ ]+)',
                sort(
                    group( uniq,
                        output(sys.stdout)
                    )
                )
            )
        )

    if __name__ == '__main__':
        main()
</pre>

<p>You can achieve the same results with the unix command line:</p>

<pre><code>cat  access.log | cut -d ' ' -f 1 | sort | uniq -c
</code></pre>
]]></description>
            <link>http://www.mischievous.org/2009/06/cascading-and-coroutines.html</link>
            <guid>http://www.mischievous.org/2009/06/cascading-and-coroutines.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">python</category>
            
            
                <category domain="http://www.sixapart.com/ns/types#tag">cascading</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">coroutine</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">python</category>
            
            <pubDate>Mon, 01 Jun 2009 20:23:37 -0800</pubDate>
        </item>
        
        <item>
            <title>Python Coroutines and Twitter</title>
            <description><![CDATA[<p>Reading <a href="http://www.dabeaz.com/coroutines/">http://www.dabeaz.com/coroutines/</a> and thought this was a natural for a twitter client. Here is a pretty simple version that just prints the public timeline every 60 seconds.  Next, up removing the time.sleep and scheduling the followStatus function as a task so I can follow more than one stream at a time.</p>

<pre><code>#!/usr/bin/env python
# encoding: utf-8
import time
import twitter

def coroutine(func):
    """
    A decorator function that takes care of starting a coroutine
    automatically on call.

    see: http://www.dabeaz.com/coroutines/
    """
    def start(*args,**kwargs):
        cr = func(*args,**kwargs)
        cr.next()
        return cr
    return start

@coroutine
def statusPrinter():
    """
    Just prints twitter status messages to the screen
    """
    while True:
         status = (yield)
         print status.id, status.user.name, status.text

def followStatus(twitterGetter, target, timeout = 60):
    """
    Follows a twitter status message that takes a since_id
    """
    since_id = None
    while True:
        statuses = twitterGetter(since_id=since_id)
        if statuses:
            # pretty sure these are always in order
            since_id = statuses[0]
            for status in statuses:
                target.send(status)
        # twitter caches for 60 seconds anyway
        time.sleep(timeout)

def main():
    api = twitter.Api()
    followStatus(api.GetPublicTimeline, statusPrinter())

if __name__ == '__main__':
    main()
</code></pre>
]]></description>
            <link>http://www.mischievous.org/2009/06/python-coroutines-and-twitter.html</link>
            <guid>http://www.mischievous.org/2009/06/python-coroutines-and-twitter.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">python</category>
            
            
                <category domain="http://www.sixapart.com/ns/types#tag">coroutine</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">python</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">twitter</category>
            
            <pubDate>Mon, 01 Jun 2009 15:54:25 -0800</pubDate>
        </item>
        
        <item>
            <title>I just finished installing Movable Type 4!</title>
            <description>Welcome to my new blog powered by Movable Type. This is the first post on my blog and was created for me automatically when I finished the installation process. But that is ok, because I will soon be creating posts of my own!</description>
            <link>http://www.mischievous.org/2008/08/i-just-finished-installing-mov.html</link>
            <guid>http://www.mischievous.org/2008/08/i-just-finished-installing-mov.html</guid>
            
            
            <pubDate>Fri, 22 Aug 2008 10:54:30 -0800</pubDate>
        </item>
        
    </channel>
</rss>
