<?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>blog.teemu.im &#187; Programming</title>
	<atom:link href="http://blog.teemu.im/tags/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.teemu.im</link>
	<description>i'm teemu and this is my weblog</description>
	<lastBuildDate>Mon, 31 Oct 2011 08:44:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Playing Around With Openstack&#8217;s Object Storage</title>
		<link>http://blog.teemu.im/2010/10/22/playing-around-with-openstacks-object-storage/</link>
		<comments>http://blog.teemu.im/2010/10/22/playing-around-with-openstacks-object-storage/#comments</comments>
		<pubDate>Fri, 22 Oct 2010 01:51:56 +0000</pubDate>
		<dc:creator>Teemu Harju</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Openstack]]></category>
		<category><![CDATA[Swift]]></category>

		<guid isPermaLink="false">http://blog.teemu.im/?p=342</guid>
		<description><![CDATA[Couple of weeks ago I found out about the Openstack project and I found it immediately to be very interesting. What I&#8217;ve been playing around with the most is the object storage part of Openstack called Swift. I&#8217;ll show here how you can use Swift with a couple of different libraries. The nice thing about [...]]]></description>
			<content:encoded><![CDATA[<p>Couple of weeks ago I found out about the <a href="http://openstack.org">Openstack project</a> and I found it immediately to be very interesting. What I&#8217;ve been playing around with the most is the <a href="http://www.openstack.org/projects/storage/">object storage</a> part of Openstack called <a href="http://swift.openstack.org/">Swift</a>. I&#8217;ll show here how you can use Swift with a couple of different libraries. The nice thing about Swift is that it is basically the <a href="http://www.rackspacecloud.com/cloud_hosting_products/files">Rackspace Cloudfiles</a> storage, so the same libraries that work with Cloudfiles, should work with Swift as well. Well, they require some small modifications. But, I&#8217;ll show you here two libraries that I know are working already. Of course, you will need a Swift instance running somewhere and instructions on how to setup one you can read the <a href="http://swift.openstack.org/development_saio.html">&#8220;Swift All In One&#8221;</a> document that shows how you can run Swift on a single server.</p>
<p>The first library I&#8217;ll show here is the <a href="http://github.com/rackspace/python-cloudfiles">python-cloudfiles</a>. I recommend using the latest one from Github, since the one that you can get for example from Ubuntu repositories does not support Swift and the one you can get from <a href="http://pypi.python.org/pypi/python-cloudfiles/1.7.0">Python Package Index</a> had a bug that made it not work with Swift.</p>
<p>Here I&#8217;ll show you how you can connect to your local Swift instance using the <code>authurl</code> parameter and how you can create containers and objects using <code>python-cloudfiles</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> cloudfiles.<span style="color: black;">connection</span> <span style="color: #ff7700;font-weight:bold;">import</span> Connection
&nbsp;
conn = Connection<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;test:test&quot;</span>, <span style="color: #483d8b;">&quot;test&quot;</span>, authurl=<span style="color: #483d8b;">&quot;http://127.0.0.1:11000/v1.0&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
container = conn.<span style="color: black;">create_container</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;test&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
obj = container.<span style="color: black;">create_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;test.txt&quot;</span><span style="color: black;">&#41;</span>
obj.<span style="color: black;">content_type</span> = <span style="color: #483d8b;">&quot;text/plain&quot;</span>
obj.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;test&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Pretty straightforward&#8230; right?</p>
<p>Next, I&#8217;ll show you another library that works with Swift called <a href="http://github.com/ddossot/cferl">cferl</a>. It&#8217;s a Erlang library for Cloudfiles and I made some <a href="http://github.com/ddossot/cferl/commit/38689bebcc0f229b3c75d113aed9532da745667d">simple patches</a> to it to make it work with Swift.</p>
<p>Here&#8217;s how you can do the same things as in previous example using <code>cferl</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;">ibrowse:<span style="color: #ff3c00;">start</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Connection</span><span style="color: #109ab8;">&#125;</span> <span style="color: #014ea4;">=</span> cf<span style="color: #ff4e18;">erl</span>:<span style="color: #ff3c00;">connect</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;test:test&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #ff7800;">&quot;test&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #ff7800;">&quot;http://127.0.0.1:11000/v1.0&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Container</span><span style="color: #109ab8;">&#125;</span> <span style="color: #014ea4;">=</span> <span style="color: #45b3e6;">Connection</span>:<span style="color: #ff3c00;">create_container</span><span style="color: #109ab8;">&#40;</span><span style="color: #ee3800;">&lt;&lt;</span><span style="color: #ff7800;">&quot;test&quot;</span><span style="color: #ee3800;">&gt;&gt;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Object</span><span style="color: #109ab8;">&#125;</span> <span style="color: #014ea4;">=</span> <span style="color: #45b3e6;">Container</span>:<span style="color: #ff3c00;">create_object</span><span style="color: #109ab8;">&#40;</span><span style="color: #ee3800;">&lt;&lt;</span><span style="color: #ff7800;">&quot;test.txt&quot;</span><span style="color: #ee3800;">&gt;&gt;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
ok <span style="color: #014ea4;">=</span> <span style="color: #45b3e6;">Obje</span><span style="color: #ff4e18;">ct</span>:<span style="color: #ff3c00;">write_data</span><span style="color: #109ab8;">&#40;</span><span style="color: #ee3800;">&lt;&lt;</span><span style="color: #ff7800;">&quot;test&quot;</span><span style="color: #ee3800;">&gt;&gt;</span><span style="color: #6bb810;">,</span> <span style="color: #ee3800;">&lt;&lt;</span><span style="color: #ff7800;">&quot;text/plain&quot;</span><span style="color: #ee3800;">&gt;&gt;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span></pre></div></div>

<p>Ok, that&#8217;s it. Now you can start playing with Swift and storing petabytes of data in it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teemu.im/2010/10/22/playing-around-with-openstacks-object-storage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise in Python Decorators</title>
		<link>http://blog.teemu.im/2010/10/03/exercise-in-python-decorators/</link>
		<comments>http://blog.teemu.im/2010/10/03/exercise-in-python-decorators/#comments</comments>
		<pubDate>Sun, 03 Oct 2010 05:52:45 +0000</pubDate>
		<dc:creator>Teemu Harju</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.teemu.im/?p=311</guid>
		<description><![CDATA[Recently, I found myself in need of a web server that I can use to simulate a behavior of a certain website. I wanted to just copy the output of that website and deliver it using this web server. The problem was that serving static content is naturally way faster than serving dynamic web application, [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I found myself in need of a web server that I can use to simulate a behavior of a certain website. I wanted to just copy the output of that website and deliver it using this web server. The problem was that serving static content is naturally way faster than serving dynamic web application, so for my simulation I needed to make the web server wait for a certain period of time before returning the static file. Being a Python fan I decided to use <a href="http://www.tornadoweb.org">Tornado</a> as the web server. Now, all I needed to do is slow it down.</p>
<p>Ok, I could just simply do this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span></pre></div></div>

<p>&#8230; to make my server wait half a second before returning, but since Tornado is using asynchronous networking and hence runs in a single thread, this would block all other requests made to my server. Not good&#8230;</p>
<p>I need to do this asynchronously. Here is an example on how this can be done with Tornado without blocking.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> tornado.<span style="color: black;">web</span>
<span style="color: #ff7700;font-weight:bold;">import</span> tornado.<span style="color: black;">ioloop</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> RequestHandler<span style="color: black;">&#40;</span>tornado.<span style="color: black;">web</span>.<span style="color: black;">RequestHandler</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> _finish_request<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">finish</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    @tornado.<span style="color: black;">web</span>.<span style="color: black;">asynchronous</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        ioloop = tornado.<span style="color: black;">ioloop</span>.<span style="color: black;">IOLoop</span>.<span style="color: black;">instance</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        ioloop.<span style="color: black;">add_timeout</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">0.5</span>, <span style="color: #008000;">self</span>._finish_request<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">add_header</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Content-Type&quot;</span>, <span style="color: #483d8b;">&quot;text/plain&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello, world&quot;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>So, this is an example of an asynchronous Tornado request handler that will wait for half a second before returning and will not block other requests while doing that. Here we are using decorator <code>tornado.web.asynchronous</code> on line 11 to tell Tornado that this request should not be returned immediately and we need to call <code>tornado.web.RequestHandler.finish()</code> on our own. The timeout is implemented by calling <code>tornado.ioloop.IOLoop.add_timeout()</code> method which is given a callback method that will finish the request.</p>
<p>Now, the problem with this is that, if I need other request handlers to do the same thing, I would need to copy paste this peace of code all over the place. And I don&#8217;t like that. We can do this bit more elegantly by using <a href="http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Decorators">Python decorators</a>. By writing a decorator we can avoid duplicating the same code to every request handler. This is how the same example will look using a decorator.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> tornado.<span style="color: black;">web</span>
<span style="color: #ff7700;font-weight:bold;">import</span> tornado.<span style="color: black;">decorators</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> RequestHandler<span style="color: black;">&#40;</span>tornado.<span style="color: black;">web</span>.<span style="color: black;">RequestHandler</span><span style="color: black;">&#41;</span>:
&nbsp;
    @tornado.<span style="color: black;">decorators</span>.<span style="color: black;">wait_for</span><span style="color: black;">&#40;</span>milliseconds=<span style="color: #ff4500;">500</span><span style="color: black;">&#41;</span>                                               
    <span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:                                                            
        <span style="color: #008000;">self</span>.<span style="color: black;">set_header</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Content-Type&quot;</span>, <span style="color: #483d8b;">&quot;text/plain&quot;</span><span style="color: black;">&#41;</span>                         
        <span style="color: #008000;">self</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello, world&quot;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Looks nice and clean. Doesn&#8217;t it? Well, the complex part has been moved now to the decorator <code>tornado.decorators.wait_for</code>. Let&#8217;s look now how we can implement that.</p>
<p><code>tornado/decorators.py</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> functools <span style="color: #ff7700;font-weight:bold;">import</span> partial
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> tornado.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> asynchronous
<span style="color: #ff7700;font-weight:bold;">from</span> tornado.<span style="color: black;">ioloop</span> <span style="color: #ff7700;font-weight:bold;">import</span> IOLoop
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> wait_for<span style="color: black;">&#40;</span>milliseconds=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> _finish_request<span style="color: black;">&#40;</span>request, start_time<span style="color: black;">&#41;</span>:
        timeout = <span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - start_time<span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">1000.0</span>
	request.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>Server waited for %.3f ms&quot;</span> <span style="color: #66cc66;">%</span> timeout<span style="color: black;">&#41;</span>
        request.<span style="color: black;">finish</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> _decorator<span style="color: black;">&#40;</span>func<span style="color: black;">&#41;</span>:
	func = asynchronous<span style="color: black;">&#40;</span>func<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">def</span> _wrapper<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
            ioloop = IOLoop.<span style="color: black;">instance</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            callback = partial<span style="color: black;">&#40;</span>_finish_request, args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            ioloop.<span style="color: black;">add_timeout</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + milliseconds / <span style="color: #ff4500;">1000.0</span>, callback<span style="color: black;">&#41;</span>
            func<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> _wrapper
    <span style="color: #ff7700;font-weight:bold;">return</span> _decorator</pre></td></tr></table></div>

<p>Here I have implemented the decorator <code>wait_for</code> that takes the number of milliseconds to wait as a parameter.  Let&#8217;s start from line 13 where the actual decorator is implemented. Decorator&#8217;s parameter is always the function that is being decorated. You can think of this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">@my_decorator
<span style="color: #ff7700;font-weight:bold;">def</span> my_function<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    // do something</pre></div></div>

<p>&#8230;being same as this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> my_function<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    // do something
my_function = my_decorator<span style="color: black;">&#40;</span>my_function<span style="color: black;">&#41;</span></pre></div></div>

<p>Now, on line 14 decorate the function with Tornado&#8217;s <code>tornado.web.asynchronous</code> decorator. Just like we did in the first example. So that our request does not return before we call <code>tornado.web.RequestHandler.finish()</code>. Then on line 15 we write a wrapper method that adds the timeout and callback to <code>tornado.ioloop.IOLoop</code> and after that calls the original function that we are decorating. The trickiest part here probably is that we need to give our callback function some parameters and Tornado&#8217;s <code>add_timeout</code> method only takes the callback function as the parameter. For that we use Python&#8217;s <code>functools.partial</code> to generate the callback and give some parameters to it on line 17.</p>
<p>To conclude the blog post here is a complete example of a script that you can test this with. You need to create the <code>tornado/decorators.py</code> using the code above for this to work.</p>
<p><code>http_server.py</code></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> tornado.<span style="color: black;">httpserver</span>
<span style="color: #ff7700;font-weight:bold;">import</span> tornado.<span style="color: black;">ioloop</span>
<span style="color: #ff7700;font-weight:bold;">import</span> tornado.<span style="color: black;">web</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> tornado.<span style="color: black;">decorators</span> <span style="color: #ff7700;font-weight:bold;">import</span> wait_for
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MainHandler<span style="color: black;">&#40;</span>tornado.<span style="color: black;">web</span>.<span style="color: black;">RequestHandler</span><span style="color: black;">&#41;</span>:
&nbsp;
    @wait_for<span style="color: black;">&#40;</span>milliseconds=<span style="color: #ff4500;">500</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">set_header</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Content-Type&quot;</span>, <span style="color: #483d8b;">&quot;text/plain&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello, world&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
application = tornado.<span style="color: black;">web</span>.<span style="color: black;">Application</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;/&quot;</span>, MainHandler<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    http_server = tornado.<span style="color: black;">httpserver</span>.<span style="color: black;">HTTPServer</span><span style="color: black;">&#40;</span>application<span style="color: black;">&#41;</span>
    http_server.<span style="color: black;">listen</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">8080</span><span style="color: black;">&#41;</span>
    tornado.<span style="color: black;">ioloop</span>.<span style="color: black;">IOLoop</span>.<span style="color: black;">instance</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>If everything goes right your server should return something like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Hello, world
&nbsp;
Server waited for 500.371 ms</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.teemu.im/2010/10/03/exercise-in-python-decorators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generating random strings in Erlang</title>
		<link>http://blog.teemu.im/2009/11/07/generating-random-strings-in-erlang/</link>
		<comments>http://blog.teemu.im/2009/11/07/generating-random-strings-in-erlang/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 05:44:20 +0000</pubDate>
		<dc:creator>Teemu Harju</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.teemu.im/?p=287</guid>
		<description><![CDATA[I could not find any decent examples from the web on how to generate a random string with a certain set of characters and length in Erlang. The basic idea for such a method is to take a string of allowed characters and loop N times where the N is the length of the resulting [...]]]></description>
			<content:encoded><![CDATA[<p>I could not find any decent examples from the web on how to generate a random string with a certain set of characters and length in Erlang. The basic idea for such a method is to take a string of allowed characters and loop N times where the N is the length of the resulting string. Then at each loop we take some random character from the string that contains the required set of characters. Sounds relatively simple, right? Next we have to write this in Erlang. This is what I came up with&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #ff3c00;">get_random_string</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Length</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">AllowedChars</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
    <span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">foldl</span><span style="color: #109ab8;">&#40;</span><span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">_</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Acc</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
                        <span style="color: #109ab8;">&#91;</span><span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">nth</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff4e18;">random</span>:<span style="color: #ff3c00;">uniform</span><span style="color: #109ab8;">&#40;</span><span style="color: #fa6fff;">length</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">AllowedChars</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
                                   <span style="color: #45b3e6;">AllowedChars</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#93;</span>
                            <span style="color: #014ea4;">++</span> <span style="color: #45b3e6;">Acc</span>
                <span style="color: #186895;">end</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#93;</span><span style="color: #6bb810;">,</span> <span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">seq</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Length</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span></pre></td></tr></table></div>

<p>Ok, Erlang is not the most readable language in the world and a simple thing such as generating a random string can look pretty tedious. No worries. I&#8217;ll go through the method line by line.</p>
<p>I&#8217;m using the <code>lists:foldl</code> method here. What it does is that it goes through a list (from left to right) and calls a function that has as it&#8217;s parameter a value from that list and the result form the previous iteration. The result of the method is the result of the last call to the function. The list I give as a parameter to <code>lists:foldl</code> is a sequence of numbers from one to the length of the resulting random string. For that I use the <code>lists:seq</code> method. This is how we define how many times we loop.</p>
<p>I&#8217;ll explain the <code>fun()</code> that is the first parameter of <code>lists:foldl</code>. Here is what it looks like separate from the whole code.</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">_</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Acc</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
     <span style="color: #109ab8;">&#91;</span><span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">nth</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff4e18;">random</span>:<span style="color: #ff3c00;">uniform</span><span style="color: #109ab8;">&#40;</span><span style="color: #fa6fff;">length</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">AllowedChars</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">AllowedChars</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#93;</span>
          <span style="color: #014ea4;">++</span> <span style="color: #45b3e6;">Acc</span>
<span style="color: #186895;">end</span></pre></div></div>

<p>The first parameter of the function is the value from the given list (<code>[1, 2, 3, 4,..., N]</code>) and we don&#8217;t use it (hence the underscore). The second parameter <code>Acc</code> is called the accumulator that is the result from the previous iteration. To achieve our goal of producing random strings we use <code>lists:nth</code> and <code>random:uniform</code> method calls to pick a random character from the <code>AllowedChars</code> string. Note that the <code>lists:nth</code> returns the integer value of that character so that is why the method call is wrapped in square brackets making the result a string (in Erlang strings are lists of integers). What we do then is that we add the<code> Acc</code> (the result of the previous iteration) to the result and this way build our random string.</p>
<p>There is also a third parameter for the <code>lists:foldl</code> method that you probably have guessed already. Naturally, you also have to give the value of the accumulator for the first iteration, which in this case is empty list <code>[]</code> or empty string since strings in Erlang are actually lists.</p>
<p>Here is an example of the result that the method produces.</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;">test:<span style="color: #ff3c00;">get_random_string</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">32</span><span style="color: #6bb810;">,</span> <span style="color: #ff7800;">&quot;qwertyQWERTY1234567890&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>     
<span style="color: #ff7800;">&quot;8qttW01wQET1qRTt1r4tr2T392QY94Re&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.teemu.im/2009/11/07/generating-random-strings-in-erlang/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Automatic code reloading in Erlang</title>
		<link>http://blog.teemu.im/2009/11/03/automatic-code-reloading-in-erlang/</link>
		<comments>http://blog.teemu.im/2009/11/03/automatic-code-reloading-in-erlang/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 13:24:48 +0000</pubDate>
		<dc:creator>Teemu Harju</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[PubSubHubbub]]></category>

		<guid isPermaLink="false">http://blog.teemu.im/?p=282</guid>
		<description><![CDATA[I&#8217;ve recently got back to coding Erlang and noticed a neat module that I didn&#8217;t know existed that is probably worth writing a blog entry about. I&#8217;ve started developing a PubSubHubbub hub in Erlang called Hubbabubba and I&#8217;m using the great Mochiweb HTTP library as the HTTP server implementation. I discovered the reloader.erl module that [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently got back to coding <a href="http://www.erlang.org">Erlang</a> and noticed a neat module that I didn&#8217;t know existed that is probably worth writing a blog entry about. I&#8217;ve started developing a <a href="http://code.google.com/p/pubsubhubbub/">PubSubHubbub</a> hub in Erlang called <a href="http://github.com/tsharju/hubbabubba">Hubbabubba</a> and I&#8217;m using the great <a href="http://code.google.com/p/mochiweb/">Mochiweb HTTP library</a> as the HTTP server implementation. I discovered the <a href="http://code.google.com/p/mochiweb/source/browse/trunk/src/reloader.erl">reloader.erl module</a> that comes with Mochiweb. It automatically reloads the code when you have the application running and you modify the code (remember to compile as well). This is something that I&#8217;ve found very useful when developing with <a href="http://www.djangoproject.com/">Django</a> or <a href="http://code.google.com/appengine/docs/whatisgoogleappengine.html">AppEngine</a> and I&#8217;m really satisfied that there is a similar solution for Erlang as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teemu.im/2009/11/03/automatic-code-reloading-in-erlang/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python For Series 60 v.1.9 Released For Testing</title>
		<link>http://blog.teemu.im/2008/12/28/python-for-series-60-v19-released-for-testing/</link>
		<comments>http://blog.teemu.im/2008/12/28/python-for-series-60-v19-released-for-testing/#comments</comments>
		<pubDate>Sun, 28 Dec 2008 06:41:45 +0000</pubDate>
		<dc:creator>Teemu Harju</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Series 60]]></category>
		<category><![CDATA[s60]]></category>

		<guid isPermaLink="false">http://blog.teemu.im/?p=233</guid>
		<description><![CDATA[Python for Nokia&#8217;s Series 60 platform has been around for four years now and to be honest, not much has happened on that front during the past year. However, suddenly on 24th of December Nokia releases a version 1.9.0 of the Python for Series 60 that is a major rewrite of the whole thing and [...]]]></description>
			<content:encoded><![CDATA[<p>Python for Nokia&#8217;s Series 60 platform has been around for four years now and to be honest, not much has happened on that front during the past year. However, suddenly on 24th of December <a href="http://discussion.forum.nokia.com/forum/showthread.php?t=154215">Nokia releases a version 1.9.0 of the Python for Series 60</a> that is a major rewrite of the whole thing and comes now with Python 2.5 version of the core language. As usual, the odd-number version branch means that the release is for testing purposes only and the even-numbered 2.0 version branch should be released once it becomes more stable. <a href="http://croozeus.com/blog">Blog at Croozeus.com</a> has done pretty nice <a href="http://croozeus.com/blogs/?p=153">wrap up of the new release</a> and here are my thoughts.</p>
<blockquote><p>First of all I should mention that as I write this blog entry I&#8217;ve not yet had time to test the new Python for Series 60 release. So I apologize beforehand for all false comments I&#8217;m about to make. </p></blockquote>
<p>It seems that they are giving a lot more focus on the ease of development in this new release. Which is a really good thing I must say. What pleases me the most is that they are planning on improving the Python runtime deployment so that the end-user or the guy that is installing Python applications should not have to worry too much about having or not having the Python runtime installed on his phone. Definitely a good thing. The new release includes a packaging tool that is not part of the S60 SDK and is basically <a href="http://code.google.com/p/ensymble/">ensymble</a> with added GUI. Ensymble is an excellent tool and it is very nice to see it included in the official PyS60 release.</p>
<p>Like mentioned already, the new release includes the version 2.5 of the Python interpreter and most of it&#8217;s standard libraries. Ok, the word &#8220;most&#8221; does not sound good here. I&#8217;ll focus first on the good things. First, <strong>the new release has Expat XML parser in it</strong>. Definitely a good thing since XML is something that pretty much every application out there uses and so far people have had to make ugly regexp XML parsers or use 3rd party package of expat to be able to parse XML in their PyS60 applications. However, I would say that including <a href="http://docs.python.org/library/json.html">JSON parser</a> as the official Python 2.6 release did lately, would probably be a good idea too.</p>
<p>Also, inclusion of <a href="http://www.python.org/doc/2.5.4/lib/module-asyncore.html">asyncore</a> and <strong>more compliant socket module</strong> sounds nice. I can&#8217;t wait to try <a href="http://twistedmatrix.com/trac/">Twisted</a> on PyS60 and see how it works. One could do some crazy things with that on a mobile phone.</p>
<p>Then to the things that are not included from the standard Python 2.5 libraries. Now, I don&#8217;t have too much information on this since, like said before, I haven&#8217;t tried the new release yet. However, to my disappointment I noticed that <a href="http://www.python.org/doc/2.5.4/lib/module-sqlite3.html">sqlite3</a> is <a href="http://discussion.forum.nokia.com/forum/showpost.php?p=521671&#038;postcount=2">not included</a>. SQLite should probably be a platform component in S60 because it is kind of becoming a de facto standard in mobile platforms since both iPhone and Android platforms use it. I don&#8217;t know what is the equivalent in S60 or does such exist but having some kind of storage other than just plain text file for PyS60 applications would be extremely nice thing to have.</p>
<p>Ok, I have to try out the new PyS60 release soon. I&#8217;m hoping that I will be pleasantly surprised. I&#8217;ll definitely write more about it later.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teemu.im/2008/12/28/python-for-series-60-v19-released-for-testing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

