<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fredrikholmstrom.com &#187; Programming</title>
	<atom:link href="http://fredrikholmstrom.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://fredrikholmstrom.com</link>
	<description>Java is to javascript what car is to carpet</description>
	<lastBuildDate>Sun, 16 Aug 2009 09:19:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Applying programming logic to making friends</title>
		<link>http://fredrikholmstrom.com/applying-programming-logic-to-making-friends/</link>
		<comments>http://fredrikholmstrom.com/applying-programming-logic-to-making-friends/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 10:22:09 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[jokes]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=42</guid>
		<description><![CDATA[1. Walk out.
2. Go up to a person that looks busy.
3. Talk to the person, and follow him/her.
4. If the person walks through a door that can be locked, make sure you get your foot in.
5. Continue talking to the person.
6. If the police shows up, follow them instead.
7. Talk to the police.
8. Follow the [...]]]></description>
			<content:encoded><![CDATA[<p>1. Walk out.<br />
2. Go up to a person that looks busy.<br />
3. Talk to the person, and follow him/her.<br />
4. If the person walks through a door that can be locked, make sure you get your foot in.<br />
5. Continue talking to the person.<br />
6. If the police shows up, follow them instead.<br />
7. Talk to the police.<br />
8. Follow the police.<br />
9. If the police walks through a door that can be locked, make sure you get your foot in.<br />
10. If you get locked into a room, wait.<br />
11. Go to 1.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/applying-programming-logic-to-making-friends/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>micro DSL for outputting HTML from Python</title>
		<link>http://fredrikholmstrom.com/micro-dsl-for-outputting-html-from-python/</link>
		<comments>http://fredrikholmstrom.com/micro-dsl-for-outputting-html-from-python/#comments</comments>
		<pubDate>Sat, 07 Mar 2009 15:22:14 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=36</guid>
		<description><![CDATA[Should be self explanatory
def render(tag):
    if not tag:
        return &#34;&#34;
    else:
        if isinstance(tag, str):
            return tag
        else:
 [...]]]></description>
			<content:encoded><![CDATA[<p>Should be self explanatory</p>
<pre class="prettyprint">def render(tag):
    if not tag:
        return &quot;&quot;
    else:
        if isinstance(tag, str):
            return tag
        else:
            if len(tag) > 1 and isinstance(tag[1], dict):
                return render_tag(tag[0], tag[1], tag[2:])
            else:
                return render_tag(tag[0], None, tag[1:])

def render_tag(tag, attrs, children):
    if not children:
        return &quot;<%s%s/>&quot; % (tag, render_attrs(attrs))
    else:
        return &quot;<%s%s>%s</%s>&quot; % (tag, render_attrs(attrs), &quot;\n&quot;.join(map(render, children)), tag)

def render_attrs(attrs):
    return &quot;&quot; if not attrs else &quot; &quot; + &quot; &quot;.join(['%s=&quot;%s&quot;' % pair for pair in attrs.items()])

if __name__ == &quot;__main__&quot;:

    # example

    html =  (&quot;html&quot;,
                (&quot;head&quot;,
                    (&quot;title&quot;, &quot;this is my title&quot;),
                    (&quot;meta&quot;, {&quot;name&quot;:&quot;keywords&quot;, &quot;value&quot;:&quot;python, html&quot;}),
                    (&quot;meta&quot;, {&quot;name&quot;:&quot;description&quot;, &quot;value&quot;:&quot;outputting html from python dsl&quot;}),
                    (&quot;script&quot;, {&quot;src&quot;:&quot;static/js/jquery-1.3.2.min.js&quot;}, None) # None forces open
                ),
                (&quot;body&quot;,
                    (&quot;div&quot;, 

                    )
                )
            )

    print render(html)</pre>
<p>Output from the example is something like this:</p>
<pre>
&lt;html>
    &lt;head>
        &lt;title>this is my title&lt;/title>
        &lt;meta name="keywords" value="python, html"/>
        &lt;meta name="description" value="outputting html from python dsl"/>
        &lt;script src="static/js/jquery-1.3.2.min.js">&lt;/script>
    &lt;/head>
    &lt;body>
        &lt;div/>
    &lt;/body>
&lt;/html>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/micro-dsl-for-outputting-html-from-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>TCO-trampoline in Python, take 2</title>
		<link>http://fredrikholmstrom.com/tco-trampoline-in-python-take-2/</link>
		<comments>http://fredrikholmstrom.com/tco-trampoline-in-python-take-2/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 15:17:28 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=34</guid>
		<description><![CDATA[My second (and much nicer) version of a tail call optimization trampoline in python.]]></description>
			<content:encoded><![CDATA[<p>My second (and much nicer) version of a tail call optimization trampoline in python</p>
<pre class="prettyprint">class recur(object):
    def __init__(self, *args):
        self.args = args

    def __call__(self, func):
        return Inner(func)

class Inner(object):
    def __init__(self, func):
        self.func = func

    def __call__(self, *args):
        result = recur(*args)
        call   = self.func

        while True:
            result = call(*result.args)

            if isinstance(result, recur):
                if result.args and isinstance(result.args[0], Inner):
                    call = result.args[0].func
                    result.args = result.args[1:]
            else:
                return result

        return result

if __name__ == &quot;__main__&quot;:

    # recursively sums a list of integers
    # calling itself for each item

    @recur()
    def req_sum(lst, n=0):
        return n if not lst else recur(lst[1:], n+lst[0])

    print req_sum([1,2,3,4])

    # You can also recurse
    # with another function
    # here you have ping and pong
    # calling eachother forever

    pings = 0
    pongs = 0

    @recur()
    def ping(n=0):
        global pings
        print &quot;pong ponged %d times, now I'll ping&quot; % n
        pings += 1
        return recur(pong, pings)

    @recur()
    def pong(n):
        global pongs
        print &quot;ping pinged %d times, now I'll pong&quot; % n
        pongs += 1
        return recur(ping, pongs)

    # if you uncomment this next statement and
    # run the code it will loop forever  

    #ping()</pre>
<p>Again, self explanatory.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/tco-trampoline-in-python-take-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Evolving Django&#8217;s Views</title>
		<link>http://fredrikholmstrom.com/evolving-djangos-views/</link>
		<comments>http://fredrikholmstrom.com/evolving-djangos-views/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 15:12:09 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/2009/01/30/</guid>
		<description><![CDATA[First of all: Last time I used Django was well over a year ago so if this is already possible to do with the current latest code base then shame on me, anyway &#8211; after messing around a bit with Django I got annoyed by the simplicity of the views, they&#8217;re very simple minded. I [...]]]></description>
			<content:encoded><![CDATA[<p>First of all: Last time I used Django was well over a year ago so if this is already possible to do with the current latest code base then shame on me, anyway &#8211; after messing around a bit with Django I got annoyed by the simplicity of the views, they&#8217;re very simple minded. I wanted some way of calling different functions depending on the request method and an easier way to return different output types, this is what I came up with.</p>
<p>The idea is simple, use a class with __call__ implemented instead of a function, within __call__ you can put custom logic to direct the requests to the appropriate callbacks &#8211; here&#8217;s my new base view class and a helper decorator (since Python 2.6 you can decorate classes).</p>
<pre class="prettyprint">
def view(cls):
	return cls()

class ClassView(object):

	fallback = "HTML"

	http_accept = {
		"application/xhtml\+xml": "HTML",
		"text/html":"HTML",
		"application/json": "JSON",
		"text/javascript": "JSON",
		"application/xml": "XML"
	}

	request_method = {
		"GET": "READ",
		"POST": "WRITE",
		"PUT": "WRITE",
		"DELETE": "WRITE"
	}

	def __call__(self, request):
		accept = request.META["HTTP_ACCEPT"]
		method = getattr(self, self.request_method[request.META["REQUEST_METHOD"]])

		for regex, call in self.http_accept.items():
			if regex and re.search(regex, accept):
				return getattr(self, call)(request, method(request))

		return getattr(self, self.fallback)(request, method(request))

	def READ(self, request):
		pass

	def WRITE(self, request):
		pass

	def JSON(self, request, method_vars):
		pass

	def HTML(self, request, method_vars):
		pass

	def XML(self, request, method_vars):
		pass
</pre>
<p>Now when I want to create a new view in django, instead of doing something like this:</p>
<pre class="prettyprint">
def index(request):
     # blah
</pre>
<p>I do this:</p>
<pre class="prettyprint">
# for python < 2.6 remove/comment out this next line:
@view
class index(ClassView):

	def READ(self, request):
		return {"title": "Hello World!"}

	def HTML(self, request, method_vars):
		return render_to_response("frontend/index.html", method_vars)

	def JSON(self, request, method_vars):
		return HttpResponse('{"title":"%(title)s"}' % method_vars)

# for python < 2.6 uncomment the next line
# index = view(index)
</pre>
<p>The READ method will be called on all GET requests, while the WRITE method will be called on all POST, PUT and DELETE. After which the appropriate output method will be selected based on the HTTP Accept header. This allows me to group common functionality in READ for all GET requests to one URL and then still be allowed to do output specific (HTML, XML, JSON, etc.) logic and template rendering based on what content types the client accepts (for example I will probably need to load more objects for HTML output since it usually has a rather advanced layout compared to say JSON which probably only wants to return the object collection as JSON string)</p>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/evolving-djangos-views/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Practical usage of python decorators</title>
		<link>http://fredrikholmstrom.com/practical-usage-of-python-decorators/</link>
		<comments>http://fredrikholmstrom.com/practical-usage-of-python-decorators/#comments</comments>
		<pubDate>Sat, 18 Oct 2008 15:10:03 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=27</guid>
		<description><![CDATA[This might be a somewhat unorthodox usage of python decorators, but a very practical and nice one.]]></description>
			<content:encoded><![CDATA[<p>This might be a somewhat unorthodox usage of python decorators, but a very practical and nice one &#8211; I got a question on IRC from a friend that wanted to store python functions in a dictionary and was complaining that there is no way to use lambdas in python as anonymous closures because of their <a href="http://rapd.wordpress.com/2007/05/09/lambda-in-python/">limitations</a> and the syntax of: <code>def func(): pass; dict["func"] = func;</code> is ugly, and I agree &#8211; so here&#8217;s a nicer version utilizing an extension of the built in dict-class:</p>
<pre class="prettyprint">
class MyDict(dict):
  def __call__(self, func):
    self[func.__name__] = func

foo = MyDict()

@foo
def bar(arg):
  print "from bar %s" % arg

foo["bar"]("Hello World!");
</pre>
<p>The idea and usage is simple: Extend the built in dict class with a subclass that is turned into a decorator, this decorator then stores every function it decorates inside it&#8217;s own dictionary.</p>
<p>Simple, practical and elegant.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/practical-usage-of-python-decorators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python decorators explained</title>
		<link>http://fredrikholmstrom.com/python-decorators-explained/</link>
		<comments>http://fredrikholmstrom.com/python-decorators-explained/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 15:03:50 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=24</guid>
		<description><![CDATA[Python decorators is just syntactic sugar to something that has been possible to do in python since version 1.0, for more background/technical information on decorators check out PEP#0318]]></description>
			<content:encoded><![CDATA[<p>Python decorators is just syntactic sugar to something that has been possible to do in python since version 1.0, for more background/technical information on decorators check out <a href="http://www.python.org/dev/peps/pep-0318/">this pep</a>.</p>
<p>The first and most important thing to remember about decorators is this: <em>Whatever that is returned from the decorator function will replace the original function you&#8217;re decorating.</em> Let&#8217;s start with the simplest of all decorators:</p>
<pre class="prettyprint">
def foo(func):
	print "Decorating %s" % func.__name__
	return func

@foo	# foo is *not* called
def bar():
	print "bar"

bar()
</pre>
<p>This decorator does very little, it takes the function we&#8217;re decorating as it&#8217;s first argument, prints the name of it and then returns it, and because it returns the original function *nothing* happens except that the message <code>Decorating bar</code> is printed when we run the code above and when calling bar() at the last line we will get <code>bar</code> printed also.</p>
<p>As the one comment in the code above says note that foo is *not* called, the @ operator means &#8220;pass this function as the decorator for the function below&#8221; so if we only pass our &#8220;foo&#8221; function in, the calling of it is handled internally in python.</p>
<p>Lets step it up a notch and replace the original function, here&#8217;s the code:</p>
<pre class="prettyprint">
def foo(func):
	def inner():
		print "Bar is gone"
	return inner

@foo # Not called
def bar():
	print "bar"

bar()
</pre>
<p>Our foo-function is a bit more advanced here since it contains an inner function, so what happens here? @foo still tells python that foo should be invoked as the decorator for bar, which it is &#8211; but this time foo returns the function called &#8220;inner&#8221; instead of the original function (which still is passed as the &#8220;func&#8221;-argument to foo, we just don&#8217;t do anything with it), so when we try to call bar on the last line we will get <code>Bar is gone</code> instead. This is because what I said earlier: Whatever is *returned* from the decorator will take the original functions place, so our bar() function is actually gone now and inner() has taken it&#8217;s place.</p>
<p>I guess most of you have seen decorators that take arguments, such as:</p>
<pre class="prettyprint">
@decorator("arg1", "arg2")
def some_function();
	pass
</pre>
<p>These &#8220;decorators&#8221; are actually normal functions that you call with arguments, that returns decorators that are then called by python internally, here&#8217;s a real example:</p>
<pre class="prettyprint">
def foo(string):
	def decorator(func):
		def replacement(times=1):
			print string*times
		return replacement
	return decorator

@foo("hi ho ") # Called
def bar():
	print "bar"

bar()
bar(5)
</pre>
<p>So when we call foo(&#8221;hi ho &#8220;) here, the foo function is *actually* called here and returns a decorator that takes the original decorated function as its only argument, this decorator returns a function called &#8220;replacement&#8221; that takes one argument with a default value of 1. The replacement function that is returned from the decorator replaces the original bar() function, so when we call it the first time we get<br />
<code>hi ho</code> and when we call it the second time with 5 as the argument, we will get: <code>hi ho hi ho hi ho hi ho hi ho </code>. </p>
<p>The important thing to notice in the last example here is that foo is NOT a decorator here, it&#8217;s a function that takes one argument (in this case &#8220;hi ho &#8220;) and returns a decorator (aptly named &#8220;decorator&#8221; here).</p>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/python-decorators-explained/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting up mod_wsgi for apache on Windows</title>
		<link>http://fredrikholmstrom.com/setting-up-mod_wsgi-for-apache-on-windows/</link>
		<comments>http://fredrikholmstrom.com/setting-up-mod_wsgi-for-apache-on-windows/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 14:55:42 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=21</guid>
		<description><![CDATA[I just installed mod_wsgi on my windows (yeah yeah, I know...) laptop, it turned out to be amazingly easy but I figured I'd put up notes on how to do it here anyways.]]></description>
			<content:encoded><![CDATA[<p>I just installed mod_wsgi on my windows (yeah yeah, I know&#8230;) laptop, it turned out to be amazingly easy but I figured I&#8217;d put up notes on how to do it here anyways.</p>
<ul>
<li style="padding-top:20px;">Head over to <a href="http://adal.chiriliuc.com/mod_wsgi/">http://adal.chiriliuc.com/mod_wsgi/</a> and fetch the latest (at the time of this writing it was revision_1018_2.3) apache module for your combination of python/apache
<ul>
<li>mod_wsgi_py24_apache20 is for Python 2.4 and Apache 2.0</li>
<li>mod_wsgi_py24_apache22 is for Python 2.4 and Apache 2.2</li>
<li>mod_wsgi_py25_apache20 is for Python 2.5 and Apache 2.0</li>
<li>mod_wsgi_py25_apache22 is for Python 2.5 and Apache 2.2</li>
</ul>
</li>
<li style="padding-top:20px;">Save the mod_wsgi.so file into your apache modules directory, for me it was located in <em>C:\Program Files\Apache Software Foundation\Apache2.2\modules</em></li>
<li style="padding-top:20px;">Create a directory somewhere outside of your webroot that will host your mod_wsgi-application, for simplicity&#8217;s sake I choose C:\wsgi</li>
<li style="padding-top:20px;">Locate your httpd.conf-file and open it up in a text editor, it&#8217;s usually located in the conf-directory of your apache installation, for me it was: <em>C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf</em></li>
<li style="padding-top:20px;">Do a search for &#8220;LoadModule&#8221; and you&#8217;ll get a block of LoadModule-statements, put this on its own line there: <em>LoadModule wsgi_module modules/mod_wsgi.so</em> so it looks something like this:
<pre style="font-size:12px;">#LoadModule usertrack_module modules/mod_usertrack.so
LoadModule version_module modules/mod_version.so
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
<strong>LoadModule wsgi_module modules/mod_wsgi.so</strong></pre>
</li>
<li style="padding-top:20px;">Now find the &lt;Directory&gt; block for your web-root, for me it was: <em>&lt;Directory &#8220;C:/Program Files/Apache Software Foundation/Apache2.2/htdocs&#8221;&gt;</em>, under it add this block:
<pre style="font-size:12px";>WSGIScriptAlias /wsgi <strong>"C:/wsgi/handler.py"</strong>

&lt;Directory <strong>"C:/wsgi"</strong>>
        AllowOverride None
        Options None
        Order deny,allow
        Allow from all
&lt;/Directory></pre>
<p>Notice that the two bold sections point at where I created my application directory so if you didn&#8217;t choose C:\wsgi you need to change this to reflect whatever directory you chose, the /wsgi part of the just before the first bold statement is the apache alias we want to use for our wsgi application &#8211; which in this case will be /wsgi also. Also note the use of forward instead of backwards slashes even when on windows.
</li>
<li style="padding-top:20px;">
The observant reader might&#8217;ve noticed the /handler.py at the end of the first bolded statement in the bullet point above, this is our handler script / entrypoint into our application. So add this code to handler.py and put it in C:\wsgi</p>
<pre class="prettyprint">
def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]
</pre>
</li>
<li style="padding-top:20px;">
Now just restart apache and go to http://localhost/wsgi and enjoy the beautiful world of python web development
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/setting-up-mod_wsgi-for-apache-on-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Managing asynchronous operations with python generators (part3)</title>
		<link>http://fredrikholmstrom.com/managing-asynchronous-operations-with-python-generators-part3/</link>
		<comments>http://fredrikholmstrom.com/managing-asynchronous-operations-with-python-generators-part3/#comments</comments>
		<pubDate>Mon, 01 Sep 2008 14:52:25 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=19</guid>
		<description><![CDATA[I'm sorry, I lied to you guys - this part will not contain a real world example because I felt there were to many new concepts introduced. In this part we'll go through something called asynchronous programming and how to create a scheduler to keep track of all the tasks we're performing.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sorry, I lied to you guys &#8211; this part will not contain a real world example because I felt there were to many new concepts introduced. In this part we&#8217;ll go through something called asynchronous programming and how to create a scheduler to keep track of all the tasks we&#8217;re performing.</p>
<p>We&#8217;ll start of with the Task-base class, it wraps a generator exposing it&#8217;s own .next()-method and a .suspended()-method that is used by our scheduler to decide if we should run the task or not. A task can basically be anything, but more often then not it&#8217;s some type of operation that involves a delay we can&#8217;t control when it&#8217;s complete &#8211; such as a network call, disk access or something similar.</p>
<p>The directory structure for all of these examples is the following:</p>
<pre>
/
	/demo
		__init__.py
		tasks.py
		scheduler.py
	example_N.py
</pre>
<p><em>demo/tasks.py:</em></p>
<pre class="prettyprint">
class Task(object):
	def __init__(self, generator = None):
		self.generator = generator

	def suspended(self):
		return False

	def next(self):
		return self.generator.next()
</pre>
<p>No doubt a fairly simple class, three methods spanning just one line each. Either download the entire code for these examples or write it yourself and put it in the demo/tasks.py file.</p>
<p>Lets put together our scheduler, the scheduler isn&#8217;t a class &#8211; it&#8217;s just a module with two variables and two functions, it to is fairly simple and spans just about twenty five lines:</p>
<p><em>demo/scheduler.py:</em></p>
<pre class="prettyprint">
queue = []

def add(task):
	global queue
	queue.append(task)

def run():
	global queue

	stack = queue
	queue = []

	while len(stack) > 0:
		task = stack.pop(0)

		try:
			if not task.suspended():
				task.next()
		except StopIteration:
			continue

		queue.append(task)

		if len(stack) == 0 and len(queue) > 0:
			stack = queue
			queue = []
</pre>
<p>One global list named <em>queue</em>, and two functions: add() that just appends an element on the queue and run() that runs through the queue until there are no more generators left in it. Let&#8217;s go through the run() function line by line:</p>
<ul>
<li><em>stack = queue</em> &#8211; We store the queue in a temporary local variable</li>
<li><em>queue = []</em> &#8211; Set queue to an empty list</li>
<li><em>task = stack.pop(0)</em> &#8211; Pop the first element of the stack</li>
<li><em>if not task.suspended():</em> &#8211; If the task isn&#8217;t suspended continue</li>
<li><em>task.next()</em> &#8211; Call .next() on the task which in turn forwards the call to the generator the task wraps</li>
<li><em>except StopIteration:</em> &#8211; If we get a StopIteration exception from the executing task/generator we should just continue with the next one (this will push the task that was responsible for the exception out of the queue)</li>
<li><em>queue.append(task)</em> &#8211; Add the task in question to the queue again</li>
<li><em>if len(stack) == 0 and len(queue) > 0:</em> &#8211; If the stack is empty and the queue isn&#8217;t</li>
<li><em>stack = queue</em> &#8211; Put the queue in the stack</li>
<li><em>queue = []</em> &#8211; And clear the queue</li>
</ul>
<p>Let&#8217;s put this together in a simple example so we can see that our scheduler works as it should:</p>
<p><em>example_1.py</em></p>
<pre class="prettyprint">
from demo import tasks, scheduler

def echo(word):
	while True:
		print word
		yield

scheduler.add(
	tasks.Task(
		echo("Hello")))

scheduler.add(
	tasks.Task(
		echo("World!")))

scheduler.run()
</pre>
<p>If we run this from the the terminal with <code>python example_1.py</code> we will get Hello and World! looped over our screen for eternity:</p>
<pre>
...
Hello
World!
Hello
World!
Hello
World!
Hello
World!
Hello
World!
...
</pre>
<p>Maybe not so exiting, but if you look closely you will see that Hello and World! alternate between each other, meaning that when the generator that prints Hello yields the first time the scheduler will take control passing execution to the generator printing World!, when that has printed out its message the stack is empty so the scheduler fills the stack with the queue again and beings from the top, and it all begins again. </p>
<p>Let&#8217;s add a new function called sleeper() to the mix, here you have <em>example_2.py:</em></p>
<pre class="prettyprint">
from time import sleep
from demo import tasks, scheduler

def echo(word):
	while True:
		print word
		yield

def sleeper(seconds):
	while True:
		sleep(seconds)
		yield

scheduler.add(
	tasks.Task(
		echo("Hello")))

scheduler.add(
	tasks.Task(
		sleeper(1)))

scheduler.add(
	tasks.Task(
		echo("World!")))

scheduler.add(
	tasks.Task(
		sleeper(1)))

scheduler.run()
</pre>
<p>You should be able to figure out what happens when we run this example from the terminal, Hello will print &#8211; and then yield and the scheduler passes execution to the first sleeper, pausing for one second and then passing it to World! printing that which yields and then execution gets passed to our second sleeper generator that pauses execution for one second, this is mainly so you can see that they actually take turn instead of some mindless spaming from an infinite repeating loop without pauses.</p>
<p>Let&#8217;s make use of that .suspended()-method on the Task class shall we? Let&#8217;s create a generator that gets called every second time the scheduler asks for it instead of every iteration, here&#8217;s the code &#8211; add it to the end of <em>demo/tasks.py:</em></p>
<pre class="prettyprint">
class EvenTask(Task):
	def __init__(self, *args, **kwargs):
		super(self.__class__, self).__init__(*args, **kwargs)
		self.counter = 0

	def suspended(self):
		self.counter += 1
		return self.counter % 2 != 0
</pre>
<p>If you don&#8217;t understand the <strong>super(self.__class__, self).__init__(*args, **kwargs)</strong>-line it&#8217;s how you call the parent classes .__init__()-method in Python, .suspended() increases the counter with +1 each time it&#8217;s called but only returns false when we&#8217;re at an even number, allowing the task to be executed every other time.</p>
<p>Here&#8217;s the next example code, <em>example_3.py</em></p>
<pre class="prettyprint">
from time import sleep
from demo import tasks, scheduler

def echo(word):
	while True:
		print word
		yield

def sleeper(seconds):
	while True:
		sleep(seconds)
		yield

scheduler.add(
	tasks.Task(
		echo("Hello")))

scheduler.add(
	tasks.Task(
		sleeper(1)))

scheduler.add(
	tasks.EvenTask(
		echo("World!")))

scheduler.add(
	tasks.Task(
		sleeper(1)))

scheduler.run()
</pre>
<p>It&#8217;s identical to example_2.py except that the echo(&#8221;World!&#8221;)-task now is of the type EvenTask instead of Task, if you run this code you will get Hello Hello World! printed, because the World!-task will skip every other time.</p>
<pre>
...
Hello
Hello
World!
Hello
Hello
World!
Hello
Hello
World!
...
</pre>
<p>So, I&#8217;ll take one last example before calling it for the day &#8211; if you remember the line &#8220;except StopIteration:&#8221; from the scheduler.run()-function we can use that to make a generator drop out of the scheduler&#8217;s run()-loop, i present to you the marvelous <em>example_4.py:</em></p>
<pre class="prettyprint">
from time import sleep
from demo import tasks, scheduler

def echo(word):
	while True:
		print word
		yield

def sleeper(seconds):
	while True:
		sleep(seconds)
		yield

def echo_once(word):
	while True:
		print word
		yield
		raise StopIteration

scheduler.add(
	tasks.Task(
		echo("Hello")))

scheduler.add(
	tasks.Task(
		echo_once("World!")))

scheduler.add(
	tasks.Task(
		sleeper(1)))

scheduler.run()
</pre>
<p>Running this from the terminal will yield (no pun intended) you something like this, only printing &#8220;World!&#8221; once:</p>
<pre>
Hello
World!
Hello
Hello
Hello
...
</pre>
<p>In the next part, number four I will show you a real world example with asynchronous network i/o, i just wanted to introduce the concept of a scheduler before jumping into a more advanced example.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/managing-asynchronous-operations-with-python-generators-part3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Indepth generator usage in Python (part2)</title>
		<link>http://fredrikholmstrom.com/indepth-generator-usage-in-python-part2/</link>
		<comments>http://fredrikholmstrom.com/indepth-generator-usage-in-python-part2/#comments</comments>
		<pubDate>Sat, 30 Aug 2008 14:50:56 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=15</guid>
		<description><![CDATA[After my last post on generators in Python I realized I missed go through one thing that I wanted to mention in the first part, namely how the return keyword interfaces with yield and generators, take this example function and its usage:
]]></description>
			<content:encoded><![CDATA[<p>After my last post on generators in Python I realized I missed go through one thing that I wanted to mention in the first part, namely how the <strong>return</strong> keyword interfaces with yield and generators, take this example function and its usage:</p>
<pre class="prettyprint">
def count_to_3or4():
	counter = 0

	while counter < 3:
		counter += 1
		yield counter

	return counter+1

c = count_to_3or4()
print c.next() # 1
print c.next() # 2
print c.next() # 3
print c.next() # 4, from return - or ?
</pre>
<p>If you've read the previous post, or have a basic understanding of generators you would probably guess that 1, 2, 3 will print from the thee first .next()-calls - <em>but you would be wrong</em>. If you try to run the above code you will get this thrown back in your face:</p>
<pre>
  File "generators.py", line 15
    return counter+1
SyntaxError: 'return' with argument inside generator
</pre>
<p>So you can't use return in generators (functions with yield) - well, yes you can - you just can't use return with a value attached to it. If you call return within a generator function it will exit and any further calls to .next() will throw a StopIteration exception, take this example code:</p>
<pre class="prettyprint">
def count_to_2or3():
	counter = 0

	while counter < 3:
		counter += 1
		yield counter

		if counter is 2:
			return

c = count_to_2or3()
print c.next() # 1
print c.next() # 2
print c.next() # 3, or ?
</pre>
<p>It will print 1 and 2, when you call .next() a third time it will hit return (since counter == 2, the if-clause evaluates to true) and throw a StopIteration exception. Basically "return" inside a generator-function does what "break" does inside a loop.</p>
<h3>Performance</h3>
<p>When you're using generators as a type of iterator together with for (or manually, for that matter) working with large datasets you will see a substantial performance increase over list-generating functions. These two functions will generate the exact same output, but one will be significantly faster and use less memory:</p>
<pre class="prettyprint">
def count_to_list(stop):
	_list = []
	counter = 0

	while counter < stop:
		counter += 1
		_list.append(counter)

	return _list

def count_to_generator(stop):
	counter = 0

	while counter < stop:
		counter += 1
		yield counter
</pre>
<p>The first function will generate a list of numbers (which takes quite some time and memory) and then return that while the second function, our generator will produce one number each time .next() is called on it and only consume as much memory as one integer take up while also being a fair bit faster, running this on my VPS yields (again, no pun intended) these results:</p>
<pre>
fredrik@holmstrom:~/python/generators$ time python list.py

real    0m0.611s
user    0m0.540s
sys     0m0.040s
</pre>
<pre>
fredrik@holmstrom:~/python/generators$ time python generator.py

real    0m0.385s
user    0m0.380s
sys     0m0.000s
</pre>
<p>I didn't measure memory usage here, but trust me - generator.py will consume a lot less memory, this technique is also called "lazy evaluation" in proper CS terms - there's a lot more information on this topic alone, but this will do for now.</p>
<h3>Advanced usage</h3>
<p>As I mentioned in the history introduction in my previous post about generators, Python 2.5 gave generators a substantial usability boost allowing us to pass information <strong>back into</strong> the function through the yield statement and the .send() and .throw() methods on the generator-object. send() works exactly like next() except that you can pass a value back into the function as it's first argument, but there are a few caveats you should look out for - take this snippet of code:</p>
<pre class="prettyprint">
def echo():
	while True:
		print yield
</pre>
<p>Will give you the following SyntaxError: </p>
<pre>
  File "explained.py", line 3
    print yield
              ^

SyntaxError: invalid syntax
</pre>
<p>Changing the <em>print yield</em> to this:</p>
<pre class="prettyprint">
def echo():
	while True:
		val = yield
		print val
</pre>
<p>Will make the code execute properly, however this seems pretty non-pythonic having to store the variable we want to store the result in a temporary variable - so instead we can do this:</p>
<pre class="prettyprint">
def echo():
	while True:
		print (yield)
</pre>
<p>Wrapping the yield in parenthesizes will allow you to use the result of it directly instead of storing it in a temporary variable, so let's put our echo() generator to use:</p>
<pre class="prettyprint">
def echo():
	while True:
		print (yield)

e = echo()
e.send("Hello")
e.send("World!")
</pre>
<p>But, running this code will show you the second caveat of trying to pass values back into the generator function, this TypeError will be thrown in your face if you run this code:</p>
<pre>
Traceback (most recent call last):
  File "generator.py", line 6, in <module>
    e.send("Hello")
TypeError: can't send non-None value to a just-started generator
</pre>
<p>Remember how I said that yield <em>paused</em> the execution of the generator function and that when you call the generator function (in this case <code>e = echo()</code>) no code is yet to be executed until you call .next() on your generator-object? So if .send() can be used to pass data back into a yield statement while the generator is paused, we can't call .send() when no code has been executed and no yield statement has paused the generator, right? </p>
<p>What this means in practice is that you either have to call .next() <strong>or</strong> .send(None) the first time you call a generator, and when the generator reaches its first yield statement it will pause execution waiting for another call to .send() (or .next() if you don't want to pass any data back) that will pass data back into it at the yield statement, confusing? So changing the code above to this:</p>
<pre class="prettyprint">
def echo():
	while True:
		print (yield)

e = echo()
e.send(None) # or e.next()
e.send("Hello")
e.send("World!")
</pre>
<p>Will make it run, printing:</p>
<pre>
Hello
World!
</pre>
<p>To illustrate exactly what's happening here, I'll take another example - slightly more advanced but still achieving the same result as above:</p>
<pre class="prettyprint">
def echo():
	counter = 0

	while True:
		counter += 1
		print (yield counter)

e = echo()
print "Yeild nr %s" % e.send(None) 	# Sending nothing in (since we havn't paused
					# anything with yield yet) and yielding nr 1
					# back to the print statement

print "Yeild nr %s" % e.send("Hello")	# the pause from nr 1 gets resumed, passing "Hello"
					# back in and printing it, then doing another loop
					# and yielding nr 2 back and pausing execution

print "Yeild nr %s" % e.send("World!")	# the pause from nr 2 gets resumed, passing "World!"
					# back in and printing it, then doing another loop
					# and yielding nr 3 back to and pausing execution

					# If we would call the same e.send("Blah"), etc.
					# here we could go on forever since the yield
					# statement is stuck in a "while True"-loop
</pre>
<p>Make sure to read the comments in the above code since I figured it would be a lot easier to explain if the comments where attached to the correct line, running the above code will yield (again, no pun intended ;p) the following results: </p>
<pre>
Yeild nr 1
Hello
Yeild nr 2
World!
Yeild nr 3
</pre>
<p>Quite simple, and yet so powerful. There is one last thing I want to demonstrate in this, second part, of the tutorial - the method .throw() those of you familiar with other languages then python might recognize the word throw and figure it would have something to do with exceptions, and you'd be correct - it does.</p>
<p>As I've demonstrated, .send() sends in data to the paused yield statement, and .throw() does something similar: it sends in an exception that gets thrown and the paused yield statements line, let's demonstrate:</p>
<pre class="prettyprint">
def exceptional():
	while True:
		yield

e = exceptional()
e.next()
e.throw(Exception)
</pre>
<p>Will give you this output: </p>
<pre class="prettyprint">
Traceback (most recent call last):
  File "generator.py", line 7, in <module>
    e.throw(Exception)
  File "generator.py", line 3, in exceptional
    yield
Exception
</pre>
<p>Which is correct, because you sent an Exception in. It is possible to call .throw() as the first method on a new generator object, before any call to .next() or .send(), however that will throw an exception before any code is executed in the method and you will not have a chance to handle it.</p>
<p>In the stack trace above you also see that the exception is actually thrown at the "yield" line when it's resumed after being paused by .next() the first time.</p>
<p>Let's do a more advanced example, with a custom exception class:</p>
<pre class="prettyprint">
def exceptional():
	counter = 0
	while True:
		try:
			counter += 1
			yield counter
		except DemoException, exc:
			print "Caught exception with message: %s" % exc

class DemoException(Exception):
	pass

e = exceptional()
print e.next()
print e.throw(DemoException("Hello World"))
</pre>
<p>The above code will print this: </p>
<pre>
1
Caught exception with message: Hello World
2
</pre>
<p>And here's the magic - if you handle the exception that gets thrown in at the line yield was called at (by wrapping it in a try/except/finally-block) the code will continue executing like it should and .throw() will return the result of the next invocation of yield. All in all .send() and .throw() work exactly the same way except that .throw() raises whatever you feed it with as an exception.</p>
<p>The ability to pass errors (exceptions) *into* generators allows you to do some really neat error handling that doesn't require your wrapping code to have any information about the generator resulting in a very clean and loosely coupled code. </p>
<p>In the next, and last, part I will go through a real world example using asynchronous i/o and network calls utilizing all the techniques explained in these two posts.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/indepth-generator-usage-in-python-part2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to generators in Python</title>
		<link>http://fredrikholmstrom.com/introduction-to-generators-in-python/</link>
		<comments>http://fredrikholmstrom.com/introduction-to-generators-in-python/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 14:32:18 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=13</guid>
		<description><![CDATA[First, some history&#8230;
Generators is a concept that was introduced in Python at version 2.2, back then they were unidirectional that only allowed information to be passed out of the generator and not back into it, which limited their use to simple iterators and not much else. This was changed / enhanced in Python 2.5 when [...]]]></description>
			<content:encoded><![CDATA[<p><em>First, some history&#8230;</em></p>
<p>Generators is a concept that was introduced in Python at version 2.2, back then they were unidirectional that only allowed information to be passed out of the generator and not back into it, which limited their use to simple iterators and not much else. This was changed / enhanced in Python 2.5 when both data and exceptions now can be passed <em>back into</em> to generator. The changes made in 2.5 allowed for generators to be used as <a href="http://en.wikipedia.org/wiki/Coroutine">coroutines</a> enabling them to function in complex event-driven programming such as asynchronous I/o, games, etc.</p>
<p>So how does one define a generator in Python? It&#8217;s actually very simple, you just define a normal subroutine (or function, if you will) that has the keyword <strong>yield</strong> somewhere inside of its body, here&#8217;s a quick example:</p>
<pre class="prettyprint">
def foo():
    yield
</pre>
<p>What does yield do to a subroutine then? When a subroutine encounters the yield expression it suspends execution so that it can be resumed at a later time, as chosen by the programmer. You basically tell the routine <em>&#8220;I don&#8217;t want to continue executing you now, but at a later stage I might want to and you should resume from the point where the yield statement was and not start over&#8221;</em>, it&#8217;s also important to note that when yield is called the subroutine&#8217;s state (variable values, etc.) are all saved, so when you continue executing it everything will be the way you left it.</p>
<p>When you call a generator-function (a subroutine/function with the yield-keyword in its body) you don&#8217;t get a result back, instead you get a generator-object back that is used to control the execution of the subroutine, take the foo()-routine we defined above, if we do this:</p>
<pre class="prettyprint">
gen = foo()
print gen
</pre>
<p>This is what python will print about the &#8220;result&#8221; of foo(): <code>&lt;generator object at 0x2b8cbb061098&gt;</code>, so when we call a generator function we get a generator object back, not the result of the function call. Note that <em>none of the code inside foo() has yet been executed</em>, as I&#8217;ve said the generators execution is controlled through the generator-object, primarily by it&#8217;s next()-method which will start/resume execution until a yield statement is found, and then return. So if we do this instead:</p>
<pre class="prettyprint">
gen = foo()
print gen.next()
</pre>
<p>We get back this: <code>None</code>, not very useful at all, if we try calling gen.next() again we will get something like this: </p>
<pre>
Traceback (most recent call last):
  File "generators.py", line 7, in <module>
    print gen.next()
StopIteration
</pre>
<p>Because the yield statement only gets executed once in our foo-generator and the generator then reaches its end, we can only &#8220;resume&#8221; execution with next() once. So what if add two yields to foo() instead, making the code look like this:</p>
<pre class="prettyprint">
def foo():
	yield
	yield

gen = foo()
print gen.next()
print gen.next()
</pre>
<p>This works, giving us back:</p>
<pre>
None
None
</pre>
<p>calling gen.next() a third time will, again, raise a StopIteration-exception. We&#8217;re still only getting back a lot of nothing (None) from our generators, how about passing something back out from our yield statements, modifying foo again making it look like this:</p>
<pre class="prettyprint">
def foo():
	yield "Hello"
	yield "World!"

gen = foo()
print gen.next()
print gen.next()
</pre>
<p>Will yield (no pun intended) this result:</p>
<pre>
Hello
World!
</pre>
<p>Kind of what you were expecting, huh? So let&#8217;s do something a bit more interesting, or well &#8211; something that shows what generators are useful for:</p>
<pre class="prettyprint">
def counter(count_to):
	counter = 0;

	while counter < count_to:
		counter = counter+1
		yield counter
</pre>
<p>As you see this generator named counter takes one argument, an integer which decides how far we should count, remember when we call counter(3) to count to three no code inside the generator gets executed until we call the generator-objects next() method, it then executes normally until it hits a yield statement and then suspends returning (through next()) whatever we fed to yield, let's see it in action:</p>
<pre class="prettyprint">
c = counter(3)
print c.next()
print c.next()
print c.next()
</pre>
<p>This will, maybe not to our surprise now, print:</p>
<pre>
1
2
3
</pre>
<p>When the three gets "yielded" to us, we can not call next() again without raising an StopIteration-exception because the while-loops condition would return false skipping the yield statement within it and counter() would end, without yielding anything back to us through next().</p>
<p>What happens if we call counter() several times? We will get several generator-objects each representing one invocation of counter() with its own internal state, you can almost think of it like creating an two object instances of a class:</p>
<pre class="prettyprint">
c1 = counter(3)
c2 = counter(4)
print c1.next()
print c2.next()
print c1.next()
print c2.next()
print c1.next()
print c2.next()

print c2.next() # We can run c2 once more c1 since its counting to four and c1 to three
</pre>
<p>The above code will print,</p>
<pre class="prettyprint">
1
1
2
2
3
3
4
</pre>
<p>, demonstrating that each invocation of a generator function creates its own generator-object and scope. Generators are used everywhere in python, in most cases they are used as iterators together with the <strong>for</strong> statement but they have other uses to. Using a generator together with a for statement is very straightforward, take the above counter()-function, we can use it the same way range() is used in python:</p>
<pre class="prettyprint">
for i in counter(5):
	print i
</pre>
<p>The for-language construct in python has a built in way of handling generators, when it gets fed a generator-object (the result of calling counter(5) in this case) it will call .next() on it putting the value returned in the iteration-variable, <strong>i</strong> in this case. When for gets an StopIteration exception thrown from the generator for calling .next() one to many times it will silently kill the exception and stop the loop, neat huh? </p>
<p>Lets write a, again useless, generator-function that iterates through every letter in a word and call it with for:</p>
<pre class="prettyprint">
def letters(word):
	for i in range(len(word)):
		yield word[i]

for letter in letters("Hello World"):
	print letter
</pre>
<p>I think you can guess what this will print, yeah. While the above function is practically useless in python - it's a good example on how generators and the for statement work together. I hope this run-through gave you a quick look into what generators are, if you're interested in learning more about them make sure to check out <a href="http://loveandtheft.org/2008/08/30/indepth-generator-usage-in-python-part2/">part two</a> of this article series.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/introduction-to-generators-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
