<?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>Ståle&#039;s weblog &#187; Python</title>
	<atom:link href="http://blog.staale.org/tag/python/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.staale.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 18 Oct 2010 07:43:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Euler problem 8 solved in Scala and Python</title>
		<link>http://blog.staale.org/2009/08/euler-problem-8-solved-in-scala-and-python.html</link>
		<comments>http://blog.staale.org/2009/08/euler-problem-8-solved-in-scala-and-python.html#comments</comments>
		<pubDate>Wed, 12 Aug 2009 07:10:23 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Euler]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=128</guid>
		<description><![CDATA[Problem 8 in project Euler is: Find the greatest product of five consecutive digits in the 1000-digit number. The first step is just to copy and paste the big block off numbers into a new file. I preserved line breaks and everything in my file, and called it Euler008-data.txt. Since I day to day program [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://projecteuler.net/index.php?section=problems&amp;id=8">Problem 8</a> in project Euler is:</p>
<blockquote><p>Find the greatest product of five consecutive digits in the 1000-digit number.</p></blockquote>
<p>The first step is just to copy and paste the big block off numbers into a new file. I preserved line breaks and everything in my file, and called it Euler008-data.txt.</p>
<p>Since I day to day program in Python, and have been pretty happy with how Python handles file reading, I wanted to compare the Scala code for this to the equivalent Python code. I first present the Python solution for extracting the number into 1 long string:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">digit = <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Euler008-data.txt&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Pretty short one-liner. I first open the file and read the entire content, giving me a single big string with linebreaks. I then use the default split method which splits on whitespace to create an array of strings, one for each line. Then finally that list is joined with nothing between the numbers. The result is one big number of strings.</p>
<p>Next I need a method for calculating the product of the digits in a string, I will use this method on each segment of 5 numbers in the digit string:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> prodDigits<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
    prod = <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> d <span style="color: #ff7700;font-weight:bold;">in</span> n: prod <span style="color: #66cc66;">*</span>= <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> prod</pre></td></tr></table></div>

<p>Her it&#8217;s just a matter of iterating over the characters in the string, converting each to an int and multiplying them together.</p>
<p>Finally we need to test each subset of 5 digits. I include here the complete program for finding the solution.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> prodDigits<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
        prod = <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> d <span style="color: #ff7700;font-weight:bold;">in</span> n: prod <span style="color: #66cc66;">*</span>= <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> prod
    digit = <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Euler008-data.txt&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    maxval = <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #008000;">len</span><span style="color: black;">&#40;</span>digit<span style="color: black;">&#41;</span>-<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>:
        maxval = <span style="color: #008000;">max</span><span style="color: black;">&#40;</span>maxval, prodDigits<span style="color: black;">&#40;</span>digit<span style="color: black;">&#91;</span>n:n+<span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> maxval</pre></td></tr></table></div>

<p>Now for the Scala solution. I import and use the scala.io.Source.fromFile method for extracting the data from the file. The fromFile returns a list of characters, for the content off a file. To convert this into one big digit string, I just filter out all the newlines, then turn the list into a string with mkString:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> digit <span style="color: #000080;">=</span> fromFile<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Euler008-data.txt&quot;</span><span style="color: #F78811;">&#41;</span> filter <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_!=</span><span style="color: #6666FF;">'<span style="color: #0000ff; font-weight: bold;">\n</span>'</span><span style="color: #F78811;">&#41;</span> mkString</pre></td></tr></table></div>

<p>Next we need the method for converting a string to the product of its digits. To do that, I map the string from a list of chars to a list of ints, by substracting &#8217;0&#8242; from each value, then I fold that list using the multiply operator:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> prodDigits<span style="color: #F78811;">&#40;</span>n<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span> /<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>n map <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>-<span style="color: #6666FF;">'0'</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_*_</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>Thanks to Scalas functional nature, this is a much shorter function than the same in Python. I add an extra step in scala, where I convert the digit string into a list of parts, each beeing 1 subset of 5 strings from the digit string:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> parts <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>n <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> until digit.<span style="color: #000000;">length</span>-<span style="color: #F78811;">5</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">yield</span> digit.<span style="color: #000000;">substring</span><span style="color: #F78811;">&#40;</span>n,n+<span style="color: #F78811;">5</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>The final step is to map all the parts into products, then find the max value, here is the complete program:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">io</span>.<span style="color: #000000;">Source</span>.<span style="color: #000000;">fromFile</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> Euler008 <span style="color: #0000ff; font-weight: bold;">extends</span> Application <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> prodDigits<span style="color: #F78811;">&#40;</span>n<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span> /<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>n map <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>-<span style="color: #6666FF;">'0'</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_*_</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> digit <span style="color: #000080;">=</span> fromFile<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Euler008-data.txt&quot;</span><span style="color: #F78811;">&#41;</span> filter <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_!=</span><span style="color: #6666FF;">'<span style="color: #0000ff; font-weight: bold;">\n</span>'</span><span style="color: #F78811;">&#41;</span> mkString
    <span style="color: #0000ff; font-weight: bold;">val</span> parts <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>n <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> until digit.<span style="color: #000000;">length</span>-<span style="color: #F78811;">5</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">yield</span> digit.<span style="color: #000000;">substring</span><span style="color: #F78811;">&#40;</span>n,n+<span style="color: #F78811;">5</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> max <span style="color: #000080;">=</span> parts map prodDigits reduceLeft<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">&gt;</span>b<span style="color: #F78811;">&#41;</span> a <span style="color: #0000ff; font-weight: bold;">else</span> b<span style="color: #F78811;">&#41;</span>
    println<span style="color: #F78811;">&#40;</span>max<span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>I think Scala is great for simple file handling (there are other methods to turn files into a list of strings as well), and the product method gets pretty small thanks to functional programming. The actual runtime is faster for the Python program though. Partly because of the JavaVM startup overhead, and also because I didn&#8217;t create a list of substrings in Python, which is an extra 995 object creations.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2009/08/euler-problem-8-solved-in-scala-and-python.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A GWT request que</title>
		<link>http://blog.staale.org/2009/07/a-gwt-request-que.html</link>
		<comments>http://blog.staale.org/2009/07/a-gwt-request-que.html#comments</comments>
		<pubDate>Fri, 17 Jul 2009 11:02:18 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=112</guid>
		<description><![CDATA[I have gotten the opportunity to work a bit with GWT likely, which is a technology I really like. And I am happy that I am now able to try it out more, and learn how to use it. One off the things I needed to work, was to request data from a different server, [...]]]></description>
			<content:encoded><![CDATA[<p>I have gotten the opportunity to work a bit with GWT likely, which is a technology I really like. And I am happy that I am now able to try it out more, and learn how to use it. One off the things I needed to work, was to request data from a different server, as I integrate with Python Django on the back end. I could create a forwarding servlet to handle the communication to deal with the same origin policy off browsers, but I have instead opted for using a Script tag approach that I found from the <a href="http://code.google.com/p/google-web-toolkit-doc-1-5/wiki/FAQ_JSONFeedsFromOtherDomain">GWT faq</a> and <a href="http://giantflyingsaucer.com/blog/?p=126">elsewhere</a>.</p>
<p>However, I had one job that resulted in over 20 additional request jobs. So I wanted a system that throttled this, and only kept a limited amount of requests live at the same time. So I needed a solution a bit beyond what I had in the examples.</p>
<h3>Circumventing the SOP when fetching JSON data</h3>
<p>The way you circumvent the SOP in browsers, is to use script tags. You add a script tag to the document with the url for the JSON service, and you also add a parameter for which callback method the service should use. The service then creates the json data, and wraps it in &#8220;callback(&lt;json-data&gt;)&#8221;, so when the page loads, that method will get invoked, giving you the data. This does of course require the server you are communicating with to support this method of data fetching.</p>
<h3>Server side Django views</h3>
<p>To ease the creation off the JSON views in Python, I created a decorator that, when applied to a method, would wrap it such that it only needed to return something that could be json encoded. Then encode and send the result. The actual code for this relies on en extension off the DecoratorBase I posted about <a href="http://blog.staale.org/2009/03/python-decorators.html">here</a>, so I will not post it here. Maybe I can update this in a seperate post.</p>
<h3>Easy request building</h3>
<p>To make it easier to code the requests, I used the builder pattern to create JsonRequestJob objects. The JsonRequrestJob objects are rather simple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> JsonRequestJob<span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> url<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> params<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> AsyncDataCallback<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> callback<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> JsonRequestJob<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url, <span style="color: #003399;">String</span> params, AsyncDataCallback<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">url</span> <span style="color: #339933;">=</span> url<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">params</span> <span style="color: #339933;">=</span> params<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">callback</span> <span style="color: #339933;">=</span> callback<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And here is the builder:</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
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> JsonRequestBuilder <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> url<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> StringBuilder params <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> JsonRequestBuilder<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">url</span> <span style="color: #339933;">=</span> url<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> JsonRequestBuilder param<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key, <span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        params.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'='</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> exec<span style="color: #009900;">&#40;</span>AsyncDataCallback<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject<span style="color: #339933;">&gt;</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        jobs.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> JsonRequestJob<span style="color: #009900;">&#40;</span>url, params.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, callback<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        dispatchJob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To actually create a builder, I have a DataFetcher class (which contains the above 2 classes), and it has a get method for creating a builder. The DataFetcher also need configuration in the form off a base url. This is set up by including a script tag in the root element:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;script language=&quot;javascript&quot;&gt;
&nbsp;
var dataUrlBase = &quot;http://localhost:8000/etc&quot;
&nbsp;
&lt;script&gt;</pre></td></tr></table></div>

<p>And here is the get method. Keep in mind that the function argument here refers to which view/function I am calling on the Python Django side off things:</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="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #003399;">String</span> getDataUrlBase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
    return $wnd[&quot;dataUrlBase&quot;]
}-*/</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> JsonRequestBuilder get<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> function<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> JsonRequestBuilder<span style="color: #009900;">&#40;</span>getDataUrlBase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>function<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;.json?callback=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Finally, the actual DataFetcher needs to have a que for adding and dequing jobs, as well as an implementation that actually creates and inserts the script tag, sets up the callback method to call when data is loaded, as well as ensuring the max number of requests is kept at a minimum.</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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DataFetcher <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Queue<span style="color: #339933;">&lt;</span>JsonRequestJob<span style="color: #339933;">&gt;</span> jobs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkedList<span style="color: #339933;">&lt;</span>JsonRequestJob<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> MAX_JOB_COUNT <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> jobCount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> requestCount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> dispatchJob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>jobCount <span style="color: #339933;">&gt;=</span> MAX_JOB_COUNT <span style="color: #339933;">||</span> jobs.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        jobCount<span style="color: #339933;">++;</span>
        <span style="color: #000000; font-weight: bold;">final</span> JsonRequestJob job <span style="color: #339933;">=</span> jobs.<span style="color: #006633;">poll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        makeJSONRequest<span style="color: #009900;">&#40;</span>job.<span style="color: #006633;">url</span>, job.<span style="color: #006633;">params</span>, job.<span style="color: #006633;">callback</span>, requestCount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        requestCount <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>requestCount <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">5000</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> makeJSONRequest<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url, <span style="color: #003399;">String</span> params, AsyncDataCallback<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> handler, <span style="color: #000066; font-weight: bold;">int</span> requestId<span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
    var callback = &quot;callback&quot; + requestId;
&nbsp;
    var script = document.createElement(&quot;script&quot;);
    script.setAttribute(&quot;src&quot;, url+callback+params);
    script.setAttribute(&quot;type&quot;, &quot;text/javascript&quot;);
&nbsp;
    $wnd[callback] = function(jsonObj) {
        $wnd[callback + &quot;done&quot;] = true;
        @DataFetcher::dispatchJSON(Lcom/google/gwt/core/client/JavaScriptObject;LAsyncDataCallback;)(jsonObj, handler);
    }
&nbsp;
    setTimeout(function() {
        if (!$wnd[callback + &quot;done&quot;]) {
            @DataFetcher::dispatchJSON(Lcom/google/gwt/core/client/JavaScriptObject;LAsyncDataCallback;)(null, handler);
        }
&nbsp;
        // cleanup
        $wnd.document.getElementsByTagName(&quot;head&quot;)[0].removeChild(script);
        delete $wnd[callback];
        delete $wnd[callback + &quot;done&quot;];
    }, 1000);
&nbsp;
    $wnd.document.getElementsByTagName(&quot;head&quot;)[0].appendChild(script);
&nbsp;
    }-*/</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> dispatchJSON<span style="color: #009900;">&#40;</span>JavaScriptObject jsonObj, AsyncDataCallback<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> handler<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        jobCount<span style="color: #339933;">--;</span>
        dispatchJob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        handler.<span style="color: #006633;">handleData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span> jsonObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Thankfully, JavaScript client side is singlethreaded, so we don&#8217;t have to worry about race conditions or synchronizations. If we needed to do that, this could could obviously break, as it has race conditions in relation to the increment/decrement and checking off the jobCount variable. Also notice that there is a timeout added that checks that the page is loaded within 1 second. If it doesn&#8217;t, null is sent to the callback, and the actual callback method is removed from the page.</p>
<p>The code is not entirely complete. The AsyncDataCallback interface is missing (but it&#8217;s tiny and easy to implement). </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2009/07/a-gwt-request-que.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python decorators</title>
		<link>http://blog.staale.org/2009/03/python-decorators.html</link>
		<comments>http://blog.staale.org/2009/03/python-decorators.html#comments</comments>
		<pubDate>Fri, 20 Mar 2009 15:08:15 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Decorators]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=48</guid>
		<description><![CDATA[How to create class decorators in Python that are relatively transparent as decorators for the outside world.]]></description>
			<content:encoded><![CDATA[<p>I have a project at work where in Python, where I use decorators to wrap functionality for views. I hit on a snag when I used 2 decorators on 1 function. I like using class decorators, I read some recommendations regarding that, and it fits better with my Java upbringing <img src='http://blog.staale.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . The 2 decorators I needed to apply to the same function looked like this (__call__ method omitted for brevity):</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;">class</span> AutoTemplate<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, func<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">func</span> = func
        pathElems = func.__module__.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;.&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>+<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;%s.html&quot;</span><span style="color: #66cc66;">%</span>func.__name__<span style="color: black;">&#93;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">template</span> = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span> pathElems<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> AccessRequired<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, func<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">func</span> = func</pre></td></tr></table></div>

<p>In addition, I used the functions actual name in the urls.py file to generate the url tuple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> createUrl<span style="color: black;">&#40;</span>method<span style="color: black;">&#41;</span>:
    name = method.<span style="color: black;">func</span>.<span style="color: black;">func_name</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^%s.html$'</span><span style="color: #66cc66;">%</span>name, method, <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>, name<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>So the finished product after decorators where applied needed to work for that. Because the decorators resulted in a class rather than a function being assigned to the the view, this didn&#8217;t work properly.</p>
<p>With a little help from <a href="http://stackoverflow.com/questions/666216/decorator-classes-in-python">Stackoverflow</a> however, I came upon a good solution, that makes it really transparent that a function is actually decorated:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> DecoratorBase<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, func<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">func</span> = func
        <span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: #008000;">dir</span><span style="color: black;">&#40;</span>func<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> - <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: #008000;">dir</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
            <span style="color: #008000;">setattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, n, <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>func, n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> AutoTemplate<span style="color: black;">&#40;</span>DecoratorBase<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">pass</span>
<span style="color: #ff7700;font-weight:bold;">class</span> AccessRequired<span style="color: black;">&#40;</span>DecoratorBase<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">pass</span></pre></td></tr></table></div>

<p>This does not work for decorators requiring parameters, I will see if I can find a solution for that if I need it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2009/03/python-decorators.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

