<?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"
	>

<channel>
	<title>GTSystem Tech Blog</title>
	<atom:link href="http://www.gtsystem.eu/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gtsystem.eu/blog</link>
	<description>Just another WordPress weblog</description>
	<pubDate>Tue, 06 Dec 2011 23:15:38 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
	<language>en</language>
			<item>
		<title>Bottle decorator for validating query parameters</title>
		<link>http://www.gtsystem.eu/blog/2011/11/bottle-decorator-for-validate-query-parameters/</link>
		<comments>http://www.gtsystem.eu/blog/2011/11/bottle-decorator-for-validate-query-parameters/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 00:13:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.gtsystem.eu/blog/?p=46</guid>
		<description><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
digg_url = 'http://www.gtsystem.eu/blog/2011/11/bottle-decorator-for-validate-query-parameters/';
digg_bgcolor = '';
digg_skin = '';
digg_window = '';
digg_title = 'Bottle decorator for validating query parameters';
digg_bodytext = '';
digg_media = '';
digg_topic = '';
//-->
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</div>
Bottle is my favorite python web framework, it is lightweight and fast. Those are good qualities, especially when you need to write web services.
However it misses some nice features of other &#8220;bigger&#8221; framework. One of them is a nice way to handle and validate query parameters.
To make an example, lets suppose that your route function [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
digg_url = 'http://www.gtsystem.eu/blog/2011/11/bottle-decorator-for-validate-query-parameters/';
digg_bgcolor = '';
digg_skin = '';
digg_window = '';
digg_title = 'Bottle decorator for validating query parameters';
digg_bodytext = '';
digg_media = '';
digg_topic = '';
//-->
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</div>
<p><a href="http://bottlepy.org">Bottle</a> is my favorite python web framework, it is lightweight and fast. Those are good qualities, especially when you need to write web services.<br />
However it misses some nice features of other &#8220;bigger&#8221; framework. One of them is a nice way to handle and validate query parameters.<br />
To make an example, lets suppose that your route function expect the GET parameters a, b and c. In bottle you will have to write something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> bottle <span style="color: #ff7700;font-weight:bold;">import</span> route, request
&nbsp;
@route<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/test/'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">test</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    a = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>request.<span style="color: black;">GET</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;a&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    b = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>request.<span style="color: black;">GET</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;b&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    c = request.<span style="color: black;">GET</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;c&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">upper</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># logic go here</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;ok&quot;</span></pre></div></div>

<p>As you can see we have tree line of boilerplate code that will reduce the focus from the real logic of the function. The situation will be further complicated if we want to have some of this parameter required and others optional with a fallback default value and if we want to handle the errors in a proper way.</p>
<p>While one of my web service requires to get some parameters from the query string, I decide to write down this decorator:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;&quot;&quot;
Copyright (c) 2011, Giuseppe Tribulato.
License: MIT (see http://www.opensource.org/licenses/mit-license.php for details)
&quot;&quot;&quot;</span>
<span style="color: #ff7700;font-weight:bold;">from</span> bottle <span style="color: #ff7700;font-weight:bold;">import</span> route, request
<span style="color: #ff7700;font-weight:bold;">import</span> functools
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">inspect</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> checkParams<span style="color: black;">&#40;</span><span style="color: #66cc66;">**</span><span style="color: #dc143c;">types</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> decorate<span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span>:
        farg, _, _, def_params = <span style="color: #dc143c;">inspect</span>.<span style="color: black;">getargspec</span><span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> def_params <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>: def_params = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        farg = farg<span style="color: black;">&#91;</span>:<span style="color: #008000;">len</span><span style="color: black;">&#40;</span>farg<span style="color: black;">&#41;</span> - <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>def_params<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
        param_info = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>par, ptype, par <span style="color: #ff7700;font-weight:bold;">in</span> farg<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> par, ptype <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">types</span>.<span style="color: black;">iteritems</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
        @functools.<span style="color: black;">wraps</span><span style="color: black;">&#40;</span>f<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>kargs<span style="color: black;">&#41;</span>:
            getparam = request.<span style="color: black;">GET</span>.<span style="color: black;">get</span>
            <span style="color: #ff7700;font-weight:bold;">for</span> par, ptype, required <span style="color: #ff7700;font-weight:bold;">in</span> param_info:
                value = getparam<span style="color: black;">&#40;</span>par<span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> value: <span style="color: #808080; font-style: italic;"># None or empty str </span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> required:
                        error = <span style="color: #483d8b;">&quot;%s() requires the parameter %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>wrapper.__name__, par<span style="color: black;">&#41;</span>
                        <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">TypeError</span><span style="color: black;">&#40;</span>error<span style="color: black;">&#41;</span>
                    <span style="color: #ff7700;font-weight:bold;">continue</span>
                <span style="color: #ff7700;font-weight:bold;">try</span>:
                    kargs<span style="color: black;">&#91;</span>par<span style="color: black;">&#93;</span> = ptype<span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">except</span>:
                    error = <span style="color: #483d8b;">&quot;Cannot convert parameter %s to %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>par, ptype.__name__<span style="color: black;">&#41;</span>
                    <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span><span style="color: black;">&#40;</span>error<span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">return</span> f<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kargs<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> wrapper
    <span style="color: #ff7700;font-weight:bold;">return</span> decorate</pre></div></div>

<p>To see how it works, lets rewrite the previous example using <code>checkParams</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">string</span>
&nbsp;
@route<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/test/'</span><span style="color: black;">&#41;</span>
@checkParams<span style="color: black;">&#40;</span>a = <span style="color: #008000;">int</span>, b = <span style="color: #008000;">bool</span>, c = <span style="color: #dc143c;">string</span>.<span style="color: black;">upper</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">test</span><span style="color: black;">&#40;</span>a, b, c<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>a = a, b = b, c = c<span style="color: black;">&#41;</span></pre></div></div>

<p>Inside <code>checkParams</code> we are declaring that we want to handle the GET parameters <code>a</code>, <code>b</code> and <code>c</code> with the provided conversion functions and we expect such parameters to be passed to our decorated function <code>test</code>.</p>
<p>We can test it, pointing the browser to our server (in my case on localhost:8080) with the following url:</p>
<pre>http://localhost:8080/test/?a=10&#038;b=1&#038;c=test</pre>
<p>this will output</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;TEST&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>The parameters were parsed, converted and passed to our test function as expected. I define all the three parameters of the function <code>test</code> as required. Lets take a look to what happens removing one of the parameters from the query string:</p>
<pre>http://localhost:8080/test/?a=10&#038;b=1</pre>
<p>if you set <code>bottle.debug(True)</code> this will output:</p>
<pre>
Internal Server Error

TypeError('test() requires the parameter c',)
</pre>
<p>If I want to define <code>c</code> as optional, I can give to it a default value:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;">@route<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/test/'</span><span style="color: black;">&#41;</span>
@checkParams<span style="color: black;">&#40;</span>a = <span style="color: #008000;">int</span>, b = <span style="color: #008000;">bool</span>, c = <span style="color: #dc143c;">string</span>.<span style="color: black;">upper</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">test</span><span style="color: black;">&#40;</span>a, b, c = <span style="color: #483d8b;">&quot;default&quot;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>a = a, b = b, c = c<span style="color: black;">&#41;</span></pre></div></div>

<p>This time we get a valid response:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;default&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finally lets try what happens if we pass an invalid type to <code>a</code>:</p>
<pre>http://localhost:8080/test/?a=no&#038;b=1</pre>
<p>this will output:</p>
<pre>
Internal Server Error

ValueError('Cannot convert parameter a to int',)
</pre>
<p>That&#8217;s it! I hope that this snippet can be useful to someone.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gtsystem.eu/blog/2011/11/bottle-decorator-for-validate-query-parameters/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Improved performance of Python 3.1</title>
		<link>http://www.gtsystem.eu/blog/2009/08/improved-performance-of-python-31/</link>
		<comments>http://www.gtsystem.eu/blog/2009/08/improved-performance-of-python-31/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 14:41:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.gtsystem.eu/blog/?p=39</guid>
		<description><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
digg_url = 'http://www.gtsystem.eu/blog/2009/08/improved-performance-of-python-31/';
digg_bgcolor = '';
digg_skin = '';
digg_window = '';
digg_title = 'Improved performance of Python 3.1';
digg_bodytext = '';
digg_media = '';
digg_topic = '';
//-->
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</div>
In my previous post (six months ago) I claim about the bad performance of Python 3.0. The first py3k version contained a new I/O library written totally in python that was the main cause of such problems.
Now Python 3.1 present a totally new I/O Library rewritten in c for maximum performance. As promised, I re-execute all [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
digg_url = 'http://www.gtsystem.eu/blog/2009/08/improved-performance-of-python-31/';
digg_bgcolor = '';
digg_skin = '';
digg_window = '';
digg_title = 'Improved performance of Python 3.1';
digg_bodytext = '';
digg_media = '';
digg_topic = '';
//-->
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</div>
<p>In my <a href="2008/12/python-30-io-performance-issues/" target="_blank">previous post</a> (six months ago) I claim about the bad performance of Python 3.0. The first py3k version contained a new I/O library written totally in python that was the main cause of <a href="http://bugs.python.org/issue4561" target="_blank">such problems</a>.</p>
<p>Now Python 3.1 present a totally new I/O Library rewritten in c for maximum performance. As promised, I re-execute all my previus tests with the latest python version (3.1.1).</p>
<p>The next graph show the results, look at my <a href="2008/12/python-30-io-performance-issues/" target="_blank">previous post</a> for details on the tests.</p>
<p><div id="attachment_40" class="wp-caption alignnone" style="width: 510px"><a href="http://www.gtsystem.eu/blog/wp-content/uploads/2009/08/pytestgraph.jpg"><img class="size-full wp-image-40" title="pytestgraph" src="http://www.gtsystem.eu/blog/wp-content/uploads/2009/08/pytestgraph.jpg" alt="Different python version compared for performance" width="500" height="310" /></a><p class="wp-caption-text">Different python version compared for performance</p></div></p>
<p><strong>Text Read/Write</strong></p>
<p>First you can see that the incredible bad text read issue is now solved. Python 3.1 is now much much faster, but it remain 2.42x slower than python 2.5. This is probably due to the text decoding phase. Text writing is now comparable to python 2.5.</p>
<p><strong>Binary Read/Write</strong></p>
<p>The rewritten I/O module is now able to read binary data faster than before. The same is not true for the writing performance that remain slow as Python 3.0.</p>
<p><strong>Print</strong></p>
<p>Another problem that I noticed in python 3.0 was the very bad performance on the new print function compared to the old print statement. In this test, Python 3.1 is now &#8220;only&#8221; 5 times slower than Python 2.5.</p>
<p>In general a good work was done to optimize py3k. I think that for general applications, py3k is now perfectly usable.</p>
<p>Many thanks to the python development team!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gtsystem.eu/blog/2009/08/improved-performance-of-python-31/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Python 3.0 I/O performance issues</title>
		<link>http://www.gtsystem.eu/blog/2008/12/python-30-io-performance-issues/</link>
		<comments>http://www.gtsystem.eu/blog/2008/12/python-30-io-performance-issues/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 07:55:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.gtsystem.eu/blog/?p=15</guid>
		<description><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
digg_url = 'http://www.gtsystem.eu/blog/2008/12/python-30-io-performance-issues/';
digg_bgcolor = '';
digg_skin = '';
digg_window = '';
digg_title = 'Python 3.0 I/O performance issues';
digg_bodytext = '';
digg_media = '';
digg_topic = '';
//-->
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</div>
Python 3.0 (aka Python 3000) was released about a week ago.
In the what&#8217;s new the python team claim:
The net result of the 3.0 generalizations is that Python 3.0 runs the pystone benchmark around 10% slower than Python 2.5. Most likely the biggest cause is the removal of special-casing for small integers. There’s room for improvement, [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
digg_url = 'http://www.gtsystem.eu/blog/2008/12/python-30-io-performance-issues/';
digg_bgcolor = '';
digg_skin = '';
digg_window = '';
digg_title = 'Python 3.0 I/O performance issues';
digg_bodytext = '';
digg_media = '';
digg_topic = '';
//-->
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</div>
<p>Python 3.0 (aka Python 3000) was released about a week ago.</p>
<p>In the <a href="http://docs.python.org/3.0/whatsnew/3.0.html">what&#8217;s new</a> the python team claim:</p>
<blockquote><p>The net result of the 3.0 generalizations is that Python 3.0 runs the pystone benchmark around 10% slower than Python 2.5. Most likely the biggest cause is the removal of special-casing for small integers. There’s room for improvement, but it will happen after 3.0 is released!</p></blockquote>
<p>In this week however, many people notice big performance differences in the new version versus the old 2.5/2.6 series related to I/O.</p>
<p>Since the read and write performance are very important for my work (data analysis), I did some tests in my laptop (MacBook Pro 2.4Ghz).</p>
<p>I installed from source code Python 2.5.2 and Python 3.0. Similar script has been compared by running them 5 times and taking average times.</p>
<h3>Binary Read Test</h3>
<p>Loading of a big file (156Mb) into memory in one step.</p>
<table border="0">
<tbody>
<tr>
<td>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!python2.5</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;bigfile.txt&quot;</span>,<span style="color: #483d8b;">&quot;rb&quot;</span><span style="color: black;">&#41;</span>
start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
stop = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%.3f sec&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>stop-start<span style="color: black;">&#41;</span></pre></div></div>

</td>
<td>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!python3.0</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;bigfile.txt&quot;</span>,<span style="color: #483d8b;">&quot;rb&quot;</span><span style="color: black;">&#41;</span>
start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
stop = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%.3f sec&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>stop-start<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

</td>
</tr>
<tr>
<td>Avg: 0.459 sec</td>
<td>Avg: 0.761 sec (<strong>+66%</strong>)</td>
</tr>
</tbody>
</table>
<p><span id="more-15"></span></p>
<h3>Text Read Test</h3>
<p>Iterating over a big file (156Mb) line by line.</p>
<table border="0">
<tbody>
<tr>
<td>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;bigfile.txt&quot;</span>,<span style="color: #483d8b;">&quot;r&quot;</span><span style="color: black;">&#41;</span>
start = <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: #ff7700;font-weight:bold;">for</span> l <span style="color: #ff7700;font-weight:bold;">in</span> f:
	<span style="color: #ff7700;font-weight:bold;">pass</span>
stop = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%.3f sec&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>stop-start<span style="color: black;">&#41;</span></pre></div></div>

</td>
<td>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;bigfile.txt&quot;</span>,<span style="color: #483d8b;">&quot;rt&quot;</span><span style="color: black;">&#41;</span>
start = <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: #ff7700;font-weight:bold;">for</span> l <span style="color: #ff7700;font-weight:bold;">in</span> f:
	<span style="color: #ff7700;font-weight:bold;">pass</span>
stop = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%.3f sec&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>stop-start<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

</td>
</tr>
<tr>
<td>Avg: 0.713 sec</td>
<td>Avg: 35.709 sec (<strong>+4,909%</strong>)</td>
</tr>
</tbody>
</table>
<p> </p>
<h3>Binary Write Test</h3>
<p>This test measure writing time for binary data 100Mb.</p>
<table border="0">
<tbody>
<tr>
<td>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
l = <span style="color: #483d8b;">&quot;A&quot;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">1024</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">1024</span>
&nbsp;
f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;writefile.txt&quot;</span>,<span style="color: #483d8b;">&quot;wb&quot;</span><span style="color: black;">&#41;</span>
start = <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: #ff7700;font-weight:bold;">for</span> i <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;">100</span><span style="color: black;">&#41;</span>:
	f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span>
stop = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%.3f sec&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>stop-start<span style="color: black;">&#41;</span></pre></div></div>

</td>
<td>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
l = <span style="color: #483d8b;">&quot;A&quot;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">1024</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">1024</span>
l=l.<span style="color: black;">encode</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;iso-8859-1&quot;</span><span style="color: black;">&#41;</span>
f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;writefile.txt&quot;</span>,<span style="color: #483d8b;">&quot;wb&quot;</span><span style="color: black;">&#41;</span>
start = <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: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>:
	f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span>
stop = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%.3f sec&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>stop-start<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

</td>
</tr>
<tr>
<td>Avg: 2.501 sec</td>
<td>2.572 sec (<strong>+3%</strong>)</td>
</tr>
</tbody>
</table>
<p> </p>
<h3>Text Write Test</h3>
<p>Write performance for text files.</p>
<table border="0">
<tbody>
<tr>
<td>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
l = <span style="color: #483d8b;">&quot;*&quot;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">1024</span> + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;writefile.txt&quot;</span>,<span style="color: #483d8b;">&quot;w&quot;</span><span style="color: black;">&#41;</span>
start = <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: #ff7700;font-weight:bold;">for</span> i <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;">100000</span><span style="color: black;">&#41;</span>:
	f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span>
stop = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%.3f sec&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>stop-start<span style="color: black;">&#41;</span></pre></div></div>

</td>
<td>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
l = <span style="color: #483d8b;">&quot;*&quot;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">1024</span> + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;writefile.txt&quot;</span>,<span style="color: #483d8b;">&quot;wt&quot;</span><span style="color: black;">&#41;</span>
start = <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: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">100000</span><span style="color: black;">&#41;</span>:
	f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span>
stop = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%.3f sec&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>stop-start<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

</td>
</tr>
<tr>
<td>Avg: 2.564 sec</td>
<td>Avg: 5.315 (<strong>+107%</strong>)</td>
</tr>
</tbody>
</table>
<p> </p>
<h3>Print</h3>
<p>From Python 3.0 &#8220;print&#8221; became a function. Previously it was a statement.  What does it means? From a syntax point of view, are necessary small changes (for example add brackets around parameters). But the real bad news are on the execution time. In this test I compare the execution of the statement/function without parameters.</p>
<table border="0">
<tbody>
<tr>
<td>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">timeit</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #dc143c;">cmd</span>=<span style="color: #483d8b;">'for i in xrange(1000000): print '</span>
t=<span style="color: #dc143c;">timeit</span>.<span style="color: black;">Timer</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span><span style="color: black;">&#41;</span>.<span style="color: #dc143c;">timeit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%.3f sec<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> t<span style="color: black;">&#41;</span></pre></div></div>

</td>
<td>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">timeit</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #dc143c;">cmd</span>=<span style="color: #483d8b;">'for i in range(1000000): print()'</span>
t=<span style="color: #dc143c;">timeit</span>.<span style="color: black;">Timer</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span><span style="color: black;">&#41;</span>.<span style="color: #dc143c;">timeit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%.3f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> t<span style="color: black;">&#41;</span></pre></div></div>

</td>
</tr>
<tr>
<td>Avg: 0.230 sec</td>
<td>Avg: 10.956 sec (<strong>+4,655%</strong>)</td>
</tr>
</tbody>
</table>
<p> </p>
<p>In the next python release (3.0.1), is expected some speed improvement on many of the previous tests. I will redo my test when this new version will be available.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gtsystem.eu/blog/2008/12/python-30-io-performance-issues/feed/</wfw:commentRss>
		</item>
		<item>
		<title>iPhone SDK 2.2 Localizable.strings bug</title>
		<link>http://www.gtsystem.eu/blog/2008/12/iphone-sdk-22-localizablestrings-bug/</link>
		<comments>http://www.gtsystem.eu/blog/2008/12/iphone-sdk-22-localizablestrings-bug/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 18:12:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Xcode]]></category>

		<category><![CDATA[Localizable]]></category>

		<guid isPermaLink="false">http://www.gtsystem.eu/blog/?p=5</guid>
		<description><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
digg_url = 'http://www.gtsystem.eu/blog/2008/12/iphone-sdk-22-localizablestrings-bug/';
digg_bgcolor = '';
digg_skin = '';
digg_window = '';
digg_title = 'iPhone SDK 2.2 Localizable.strings bug';
digg_bodytext = '';
digg_media = '';
digg_topic = '';
//-->
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</div>
In the latest release of the iPhone SDK 2.2 (9m2621) there is an issue concerning the localization procedure.
When the input string file is utf-8 and &#8220;Strings file Output Encoding&#8221; is set to binary, the resulting file isn&#8217;t Localizable.string but Localizable.string.XXXX where XXXX is a pid number.
To correct this issue you can patch the ruby script copystrings located [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
digg_url = 'http://www.gtsystem.eu/blog/2008/12/iphone-sdk-22-localizablestrings-bug/';
digg_bgcolor = '';
digg_skin = '';
digg_window = '';
digg_title = 'iPhone SDK 2.2 Localizable.strings bug';
digg_bodytext = '';
digg_media = '';
digg_topic = '';
//-->
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</div>
<p>In the latest release of the iPhone SDK 2.2 (9m2621) there is an issue concerning the localization procedure.</p>
<p>When the input string file is utf-8 and &#8220;Strings file Output Encoding&#8221; is set to binary, the resulting file isn&#8217;t Localizable.string but Localizable.string.XXXX where XXXX is a pid number.</p>
<p>To correct this issue you can patch the ruby script copystrings located in /Developer/Library/Xcode/Plug-ins/CoreBuildTasks.xcplugin/Contents/Resources/copystrings.</p>
<p>Simply change line 134 from:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">system</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'plutil -convert binary1 -s -o &quot;'</span> <span style="color:#006600; font-weight:bold;">+</span> OPTIONS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:OutputDir</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/'</span> <span style="color:#006600; font-weight:bold;">+</span> 
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">basename</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'&quot; -- &quot;'</span> <span style="color:#006600; font-weight:bold;">+</span> path <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'&quot;'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>to</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">system</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'plutil -convert binary1 -s -o &quot;'</span> <span style="color:#006600; font-weight:bold;">+</span> OPTIONS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:OutputDir</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/'</span> <span style="color:#006600; font-weight:bold;">+</span> 
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">basename</span><span style="color:#006600; font-weight:bold;">&#40;</span>path.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.#{Process.pid}&quot;</span>,<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'&quot; -- &quot;'</span> <span style="color:#006600; font-weight:bold;">+</span> path <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'&quot;'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gtsystem.eu/blog/2008/12/iphone-sdk-22-localizablestrings-bug/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

