<?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; Everything</title>
	<atom:link href="http://fredrikholmstrom.com/category/everything/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>Programmers and hot air balloons</title>
		<link>http://fredrikholmstrom.com/programmers-and-hot-air-balloons/</link>
		<comments>http://fredrikholmstrom.com/programmers-and-hot-air-balloons/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 09:19:51 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[joke]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=54</guid>
		<description><![CDATA[A man flying in a hot air balloon suddenly realizes he’s lost. He reduces height and spots a man down below. He lowers the balloon further and shouts to get directions, "Excuse me, can you tell me where I am?"]]></description>
			<content:encoded><![CDATA[<p>A man flying in a hot air balloon suddenly realizes he’s lost. He reduces height and spots a man down below. He lowers the balloon further and shouts to get directions, &#8220;Excuse me, can you tell me where I am?&#8221;</p>
<p>The man below says: &#8220;Yes, you&#8217;re in a hot air balloon, hovering 30 feet above this field.&#8221;</p>
<p>&#8220;You must work in Information Technology,&#8221; says the balloonist.</p>
<p>&#8220;I do&#8221; replies the man. &#8220;How did you know?&#8221;</p>
<p>&#8220;Well,&#8221; says the balloonist, &#8220;everything you have told me is technically correct, but It&#8217;s of no use to anyone.&#8221;</p>
<p>The man below replies, &#8220;You must work in management.&#8221;</p>
<p>&#8220;I do&#8221; replies the balloonist, &#8220;But how&#8217;d you know?&#8221;</p>
<p>&#8220;Well&#8221;, says the man, &#8220;you don’t know where you are, or where you’re going, you expect me to be able to help. You’re in the same position you were before we met, but now it’s my fault.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/programmers-and-hot-air-balloons/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Give a person a fish</title>
		<link>http://fredrikholmstrom.com/give-a-person-a-fish/</link>
		<comments>http://fredrikholmstrom.com/give-a-person-a-fish/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 18:30:01 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[qotd]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=50</guid>
		<description><![CDATA[&#8220;Give a person a fish and you feed them for a day. Teach that person to use the Internet and they won&#8217;t bother you for weeks&#8221;
]]></description>
			<content:encoded><![CDATA[<p><em>&#8220;Give a person a fish and you feed them for a day. Teach that person to use the Internet and they won&#8217;t bother you for weeks&#8221;</em></p>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/give-a-person-a-fish/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>My favourite diagram app just got 10000% better</title>
		<link>http://fredrikholmstrom.com/my-favourite-diagram-app-just-got-10000-better/</link>
		<comments>http://fredrikholmstrom.com/my-favourite-diagram-app-just-got-10000-better/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 20:13:02 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[applications]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=45</guid>
		<description><![CDATA[I&#8217;ve been using Dia for ages, but it always had this annoying &#8220;feature&#8221; that the drawing controls and the drawing window would be in two different GUI windows, here&#8217;s a pic:  In theory this sounds great as you can move the windows around separately, in practice it sucks. They finally adopted a tabbed approach [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using <a href="http://projects.gnome.org/dia/" target="_blank">Dia</a> for ages, but it always had this annoying &#8220;feature&#8221; that the drawing controls and the drawing window would be in two different GUI windows, here&#8217;s a pic: <img src="http://jriddell.org/programs/umbrello/html/dia-screenshot.jpg" alt="Old Dia window layout" /> <br />In theory this sounds great as you can move the windows around separately, in practice it sucks. They finally adopted a tabbed approach with everything in the same window: <img src="http://episerversucks.com/upload/dia2.jpg" alt="New Dia window layout" /> <br /> And there was much rejoicing.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/my-favourite-diagram-app-just-got-10000-better/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>The difference between SVN and DVCS</title>
		<link>http://fredrikholmstrom.com/the-difference-between-svn-and-dvcs/</link>
		<comments>http://fredrikholmstrom.com/the-difference-between-svn-and-dvcs/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 10:15:58 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[podcast]]></category>
		<category><![CDATA[stackoverflow]]></category>
		<category><![CDATA[version-control]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=40</guid>
		<description><![CDATA[I'm listening to <a href="http://blog.stackoverflow.com/2009/05/podcast-52/">stackoverflow podcast #52</a> and <a href="http://www.joelonsoftware.com/">Joel Spolsky</a> just gave the best explanation of the difference between the Subversion / CVS generation of version control and the new Mercurial / Git / Distributed Version Control, the important part is bolded.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m listening to <a href="http://blog.stackoverflow.com/2009/05/podcast-52/">stackoverflow podcast #52</a> and <a href="http://www.joelonsoftware.com/">Joel Spolsky</a> just gave the best explanation of the difference between the Subversion / CVS generation of version control and the new Mercurial / Git / Distributed Version Control, the important part is bolded.</p>
<blockquote><p>So this is why everybody is abandoning those version control systems that are of the Subversion generation and they&#8217;re moving to Mercurial and Git, and, to a lesser extent, the other distributed systems. There&#8217;s something that took me a while to understand about Mercurial, but fundamentally Mercurial is &#8211; <strong>Mercurial thinks of the world as a list of changes, and Subversion thinks of the world as a list of different versions of all your files.</strong> And so for Subversion to figure out where you are on a branch to do the merge, all it can do really is do a diff and say &#8216;well, I don&#8217;t know what happened, but the following things &#8211; we&#8217;re different from you guys in the following ways&#8217;, whereas Mercurial has this sort of added info of all the steps that were taken and all the transformations that were done, so it basically has more information to use in the merge and making the merge successful.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/the-difference-between-svn-and-dvcs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Moved blog</title>
		<link>http://fredrikholmstrom.com/moved-blog/</link>
		<comments>http://fredrikholmstrom.com/moved-blog/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 16:58:58 +0000</pubDate>
		<dc:creator>Fredrik Holmström</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://fredrikholmstrom.com/?p=38</guid>
		<description><![CDATA[So my last host crashed, after about two months of trying to get my posts back I managed to get *some* of them back at least, but the ones requiring files and images from the old host are useless since I couldn&#8217;t save any of the uploaded content.
I also changed my domain since I got [...]]]></description>
			<content:encoded><![CDATA[<p>So my last host crashed, after about two months of trying to get my posts back I managed to get *some* of them back at least, but the ones requiring files and images from the old host are useless since I couldn&#8217;t save any of the uploaded content.</p>
<p>I also changed my domain since I got my first+lastname under .com after about two years of waiting. Anyway, I&#8217;ll pick up blogging soon again.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredrikholmstrom.com/moved-blog/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>
	</channel>
</rss>
