<?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>Algorithm.co.il &#187; Python</title>
	<atom:link href="http://www.algorithm.co.il/blogs/category/programming/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm.co.il/blogs</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Tue, 21 Jun 2011 20:37:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Python Module Usage Stats &#8211; Feb 2011</title>
		<link>http://www.algorithm.co.il/blogs/programming/python/python-module-usage-stats/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/python/python-module-usage-stats/#comments</comments>
		<pubDate>Mon, 28 Feb 2011 13:56:04 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[module usage]]></category>
		<category><![CDATA[Python packages]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=670</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python/python-module-usage-stats/' addthis:title='Python Module Usage Stats &#8211; Feb 2011'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Here are the top 30 "base modules", ordered by number of PyPI projects importing them. These results are based on 11,204 packages download from PyPI. Explanations, full results and code to generate them are available below. <a href="http://www.algorithm.co.il/blogs/programming/python/python-module-usage-stats/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python/python-module-usage-stats/' addthis:title='Python Module Usage Stats &#8211; Feb 2011' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python/python-module-usage-stats/' addthis:title='Python Module Usage Stats &#8211; Feb 2011'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div><p>Here are the top 30 &#8220;base modules&#8221;, ordered by number of PyPI projects importing them. These results are based on 11,204 packages download from PyPI. Explanations, full results and code to generate them are available below.</p>
<h3>Results</h3>
<p><a href="http://www.algorithm.co.il/blogs/wp-content/uploads/2011/02/module_stats.png"><img class="alignnone size-medium wp-image-672" title="Top 30 imported Python modules, Feb 2011" src="http://www.algorithm.co.il/blogs/wp-content/uploads/2011/02/module_stats.png" alt="" /></a><br />
(click to enlarge)</p>
<p><a href="http://www.algorithm.co.il/sitecode/module_stats_results.zip">Full results are available</a> (see Methodology to understand what they mean exactly).</p>
<h3>Discussion</h3>
<p>Some interesting tidbits and comparisons:</p>
<ul>
<li>It seems django has gained &#8220;some popularity&#8221;. Zope is very high up on the list, and plone is at 42 with 907 projects importing it.</li>
<li> The number of projects importing unittest is somewhat depressing, especially relative to setuptools which is impressive. <del>That might be because setuptools is somewhat a prerequisite to appear on PyPI (practically speaking), while unittest is not.</del> (Edit: corrected by Michael Foord in a comment)</li>
<li> optparse with 1875 vs. getopt with 515.</li>
<li> cPickle with 690 vs. pickle with 598.</li>
<li> simplejson with 760 vs. json with 593.</li>
</ul>
<p>I invite you all to find out more interesting pieces of information by going over the results. I bet there&#8217;s a lot more knowledge to be gained from this.</p>
<h3>Background</h3>
<p>Back in 2007 I wrote a small script that counted module imports in python code. I used it to generate statistics for Python modules. A week or two ago I had an idea to repeat that experiment &#8211; and see the difference between 2007 and 2011. I also thought of a small hypothesis to test: since django became very popular, I&#8217;d expect it to be very high up on the list.</p>
<p>I started working with my old code, and decided that I should update it. Looking for imports in Python code is not as simple as it seems. I considered using the tokenize and parser modules, but decided against that. Using parser would make my code version dependent and by the time I thought of tokenize, I had the complicated part already worked out. By the complicated part I mean of course the big regexps I used ;)</p>
<h3>Methodology</h3>
<p><strong>Input</strong>: PyPI and a source distribution of the Python2.7 standard library. I wrote a small script (cheese_getter.py) to fetch python modules. It does it by reading the PyPI index page, and then using easy_install to fetch each module. Since currently there are a bit less than 13k modules in PyPI, this took some time.</p>
<p><strong>Parsing</strong>: I wrote a relatively simple piece of code to find &#8220;import x&#8221; and &#8220;from x import y&#8221; statements in code. This is much more tricky than it seems: statements such as &#8220;from x import a,b&#8221;, &#8220;from . import bla&#8221; and</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> bla <span style="color: #ff7700;font-weight:bold;">import</span> \
               some_module\
               some_module2</pre></div></div>

<p>should all be supported. In order to achieve uniformity, I converted each import statement to a series of dotted modules. So for example, &#8220;import a.b&#8221; will yield &#8220;a&#8221; and &#8220;a.b&#8221;, and &#8220;from b import c,d&#8221; will yield &#8220;b&#8221;, &#8220;b.c&#8221;, and &#8220;b.d&#8221;.</p>
<p><strong>Processing</strong>: I created three result types:</p>
<ol>
<li>total number of imports</li>
<li>total number of packages importing the module</li>
<li>total number of packages importing the module, only for the first module mentioned in a dotted module name, e.g. not &#8220;a.b&#8221;, only &#8220;a&#8221;.</li>
</ol>
<p>I believe the third is the most informative, although there are interesting things to learn from the others as well.</p>
<p><strong>Code</strong>: <a href="http://www.algorithm.co.il/sitecode/module_stats_code.zip">Full code is available</a>. Peer reviews and independent reports are welcome :)</p>
<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python/python-module-usage-stats/' addthis:title='Python Module Usage Stats &#8211; Feb 2011' ><a href="http://addthis.com/bookmark.php?v=250&amp;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">|</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/python/python-module-usage-stats/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Small programming challenge No. 6 &#8211; nblocks</title>
		<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-6-nblocks/</link>
		<comments>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-6-nblocks/#comments</comments>
		<pubDate>Mon, 24 Jan 2011 23:22:44 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Challenges]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[blocks]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[nblocks]]></category>
		<category><![CDATA[percentile]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=622</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-6-nblocks/' addthis:title='Small programming challenge No. 6 &#8211; nblocks'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>I came up with this challenge when I had to write a function to divide a sequence to percentiles. I needed this to calculate some statistics over trips for plnnr.com. &#8220;This sounds trivial&#8221; I thought, and reached for my simple &#8230; <a href="http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-6-nblocks/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-6-nblocks/' addthis:title='Small programming challenge No. 6 &#8211; nblocks' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I came up with this challenge when I had to write a function to divide a sequence to percentiles. I needed this to calculate some statistics over trips for <a href="http://plnnr.com/">plnnr.com</a>. &#8220;This sounds trivial&#8221; I thought, and reached for my simple blocks function:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> blocks<span style="color: black;">&#40;</span>seq, block_len<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;blocks(range(5),2) -&amp;gt; [[0, 1], [2, 3], [4]]&quot;&quot;&quot;</span>
    seq_len = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> seq_len<span style="color: #66cc66;">%</span>block_len == <span style="color: #ff4500;">0</span>:
        num_blocks = seq_len/block_len
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        num_blocks = <span style="color: #ff4500;">1</span> + <span style="color: black;">&#40;</span>seq_len/block_len<span style="color: black;">&#41;</span>
&nbsp;
    result = <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span><span style="color: black;">&#93;</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>num_blocks<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> idx, obj <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>:
        result<span style="color: black;">&#91;</span>idx/block_len<span style="color: black;">&#93;</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>obj<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> result</pre></div></div>

<p>So I set block_len to len(seq)/10 and called blocks(seq, block_len). Unfortunately, according to the docs of blocks (which I wrote&#8230;), when there is extra data, a &#8220;partial&#8221; block is added &#8211; which is exactly what we don&#8217;t want when calculating percentiles.<br />
<strong>Instead, the behavior we want is nblocks(seq, number_of_blocks), for example: nblocks(range(10), 3) -&gt; [0, 1, 2], [3, 4, 5], [6, 7, 8, 9].</strong></p>
<p>This is a surprisingly hard to write function, or rather, harder than you&#8217;d expect. I&#8217;ll be especially glad if someone writes it elegantly. My solution works well enough, but isn&#8217;t the prettiest.</p>
<p>So, you have the definition &#8211; let&#8217;s see if you can do better than me. Once enough solutions are presented, I will present my own.</p>
<p><u>IMPORTANT EDIT: the required signature is <strong>nblocks(seq, num_blocks).</strong></u> So for seq(range(10), 3), the return value should be 3 blocks, with the last one having an extra item. As a general rule, the extra items should be spread as evenly as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-6-nblocks/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>One-liner Guitar Tuner in Python</title>
		<link>http://www.algorithm.co.il/blogs/programming/python/one-liner-guitar-tuner-in-python/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/python/one-liner-guitar-tuner-in-python/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 21:04:32 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Sound]]></category>
		<category><![CDATA[guitar]]></category>
		<category><![CDATA[guitar tuner]]></category>
		<category><![CDATA[one-liner]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=611</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python/one-liner-guitar-tuner-in-python/' addthis:title='One-liner Guitar Tuner in Python'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>On windows, assuming imports are free: import winsound winsound.Beep&#40;220*&#40;&#40;2**&#40;1/12.0&#41;&#41;**7&#41;, 2000&#41; But that&#8217;s just because I like to tune to E. If you prefer a more &#8220;natural looking&#8221; note, you can use A: winsound.Beep&#40;110, 1000&#41; But why choose at all when &#8230; <a href="http://www.algorithm.co.il/blogs/programming/python/one-liner-guitar-tuner-in-python/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python/one-liner-guitar-tuner-in-python/' addthis:title='One-liner Guitar Tuner in Python' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.algorithm.co.il/sitecode/guitar.jpg" alt="Guitar" style="border: 2px solid black;"/></p>
<p>On windows, assuming imports are free:</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;">winsound</span>
<span style="color: #dc143c;">winsound</span>.<span style="color: black;">Beep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">220</span><span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">**</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>/<span style="color: #ff4500;">12.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">2000</span><span style="color: black;">&#41;</span></pre></div></div>

<p>But that&#8217;s just because I like to tune to E. If you prefer a more &#8220;natural looking&#8221; note, you can use A:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #dc143c;">winsound</span>.<span style="color: black;">Beep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">110</span>, <span style="color: #ff4500;">1000</span><span style="color: black;">&#41;</span></pre></div></div>

<p>But why choose at all when you can go for all of them?</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#91;</span><span style="color: #dc143c;">winsound</span>.<span style="color: black;">Beep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">220</span><span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">**</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>/<span style="color: #ff4500;">12.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span>i<span style="color: black;">&#41;</span>, <span style="color: #ff4500;">500</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: black;">&#91;</span><span style="color: #ff4500;">7</span>, <span style="color: #ff4500;">2</span>, -<span style="color: #ff4500;">2</span>, -<span style="color: #ff4500;">7</span>, -<span style="color: #ff4500;">12</span>, -<span style="color: #ff4500;">17</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span></pre></div></div>

<p><small>Image by <a href="http://www.flickr.com/photos/keela84/8075445/">Keela84</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/python/one-liner-guitar-tuner-in-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Visualizing Data Using the Hilbert Curve</title>
		<link>http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/</link>
		<comments>http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 06:57:30 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Fractals]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[hilbert curve]]></category>
		<category><![CDATA[PIL]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=525</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/' addthis:title='Visualizing Data Using the Hilbert Curve'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Some time ago, a coworker asked me to help him visualize some data. He had a very long series (many millions) of data points, and he thought that plotting a pixel for each one would visualize it well, so he &#8230; <a href="http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/' addthis:title='Visualizing Data Using the Hilbert Curve' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Some time ago, a coworker asked me to help him visualize some data. He had a very long series (many millions) of data points, and he thought that plotting a pixel for each one would visualize it well, so he asked for my help.<br />
I installed Python &#038; PIL on his machine, and not too long after, he had the image plotted. The script looked something like:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">data_points = get_data_points<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
n =  <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>data_points<span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span>
&nbsp;
image = Image<span style="color: black;">&#40;</span><span style="color: #483d8b;">'1'</span>, <span style="color: black;">&#40;</span>n, n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> idx, pt <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>data_points<span style="color: black;">&#41;</span>:
	image.<span style="color: black;">putpixel</span><span style="color: black;">&#40;</span>pt, <span style="color: black;">&#40;</span>idx/n, idx<span style="color: #66cc66;">%</span>n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
image.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'bla.png'</span>, <span style="color: #483d8b;">'png'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Easy enough to do. Well, easy enough if you have enough memory to handle very large data sets. Luckily enough, we had just enough memory for this script &#038; data series, and we were happy. The image was generated, and everything worked fine.</p>
<p>Still, we wanted to improve on that. One problem with this visualization is that two horizontally adjacent pixels don’t have anything to do with each other. Remembering xkcd’s &#8220;<a href="http://xkcd.com/195/">Map of the Internet</a>&#8220;, I decided to use the <a href="http://en.wikipedia.org/wiki/Hilbert_curve">Hilbert Curve</a>. I started with wikipedia&#8217;s version of the code for the Python turtle and changed it to generate a string of instructions of where to put pixels. On the way I improved the time complexity by changing it to have only two recursion calls instead of four. (It can probably be taken down to one by the way, I leave that as a challenge to the reader :)</p>
<p>Unfortunately, at this point we didn’t have enough memory to hold all of those instructions, so I changed it into a generator. Now it was too slow. I cached the lower levels of the recursion, and now it worked in reasonable time (about 3-5 minutes), with reasonable memory requirements (no OutOfMemory exceptions). Of course, I&#8217;m skipping a bit of exceptions and debugging along the way. Still, it was relatively straightforward.</p>
<p>Writing the generator wasn&#8217;t enough &#8211; there were still pixels to draw! It took a few more minutes to write a simple &#8220;turtle&#8221;, that walks the generated hilbert curve.<br />
Now, we were ready:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">hilbert = Hilbert<span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: black;">log</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>data_points<span style="color: black;">&#41;</span>, <span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> pt <span style="color: #ff7700;font-weight:bold;">in</span> data_points:
    x,y = hilbert.<span style="color: black;">next</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    image.<span style="color: black;">putpixel</span><span style="color: black;">&#40;</span>pt, <span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>A few minutes later, the image was generated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fuzz-Testing With Nose</title>
		<link>http://www.algorithm.co.il/blogs/programming/python/fuzz-testing-with-nose/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/python/fuzz-testing-with-nose/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 07:49:01 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[fuzzing]]></category>
		<category><![CDATA[nose]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=587</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python/fuzz-testing-with-nose/' addthis:title='Fuzz-Testing With Nose'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>A few days ago, I found a in my website, plnnr.com. The bug was in a new feature I added to the algorithm. The first thing I did was write a small unit-test to reproduce the bug. With that unit-test &#8230; <a href="http://www.algorithm.co.il/blogs/programming/python/fuzz-testing-with-nose/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python/fuzz-testing-with-nose/' addthis:title='Fuzz-Testing With Nose' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>A few days ago, I found a in my website, <a href="http://plnnr.com">plnnr.com</a>. The bug was in a new feature I added to the algorithm. The first thing I did was write a small unit-test to reproduce the bug. With that unit-test in hand, I then worked to fix the bug, and got this unit-test to pass.</p>
<p>As I previously persumed this feature to be (relatively :) bug free, I decided that more testing was in order. This time however, a single test-case would not be enough &#8211; I needed to make sure that the trip-generation algorithm works in many cases. Enter <a href="http://en.wikipedia.org/wiki/Fuzz_testing">fuzzing</a>.</p>
<p>Plnnr.com generates trips according to trip preferences. Why not generate the trip preferences with a fuzzer, and then check if the planning algorithm chokes on them? While fuzzing is usually used to generate invalid input with the goal of causing the program to crash, in this case I&#8217;m generating valid input with the goal of causing the planning algorithm to fail.</p>
<p>Usually fuzzing is done with one of two techniques &#8211; exhaustive fuzzing, that goes systematically (possibly selectively) over the input space and random fuzzing, which picks inputs at random &#8211; or &#8220;somewhat&#8221; randomly. In my case, the input space consists of &#8220;world data&#8221; &#8211; locations of attractions, restaurants, etc, and trip preferences &#8211; intensity, required attractions, and so on. Since the input space is so large and &#8220;unstructured&#8221;, I found it much easier to go with random fuzzing.</p>
<p>In each test-case, I will generate a &#8220;random world&#8221;, and random trip preferences for that world.<br />
Here is some sample code that shows how this might look:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">trip_prefs.<span style="color: black;">num_days</span> = <span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
trip_prefs.<span style="color: black;">intensity</span> = <span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> randbit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    trip_prefs.<span style="color: black;">schedule_lunch</span> = <span style="color: #008000;">True</span></pre></div></div>

<p>Where randbit is defined like so:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> randbit<span style="color: black;">&#40;</span>prob = <span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">random</span>.<span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span>  prob</pre></div></div>

<p>This is all very well, but tests need to be reproducible. If a fuzzer-generated test case fails and I can&#8217;t recreate it to analyze the error and later verify that it is fixed, it isn&#8217;t of much use. To solve this issue, the input generation function receives some value, and sets the random seed with this parameter. Now, generating test cases is just a matter of generating a sequence of random values. Here is my code to do that:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> FuzzTestBase<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    __test__ = <span style="color: #008000;">False</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> run_single_fuzz<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, random_seed<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">pass</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> fuzz_test<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">random</span>.<span style="color: black;">seed</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        random_seeds = <span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">random</span>.<span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</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>NUM_FUZZ_TESTS<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> seed <span style="color: #ff7700;font-weight:bold;">in</span> random_seeds:
            <span style="color: #ff7700;font-weight:bold;">yield</span> <span style="color: #008000;">self</span>.<span style="color: black;">run_single_fuzz</span>, seed</pre></div></div>

<p>FuzzTestBase is a base-class for actual test classes. Each test class just needs to define its own version of run_single_fuzz, and in it call random.seed(random_seed) and log random_seed.</p>
<p>This code uses <a href="http://somethingaboutorange.com/mrl/projects/nose/0.11.3/">nose</a>&#8216;s ability to test generators: it assumes that a test generator yields test functions and their parameters.</p>
<p>A few interesting issues:<br />
* I generate the random seeds beforehand, so that calling random.seed() in the actual test case doesn&#8217;t affect the seed sequence.<br />
* Originally I used just random.random() as a seed instead of str(random.random()). The problem with that is that this way it&#8217;s not reproducible. random.random() returns a floating point value x, for which usually x != eval(str(x)):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">In <span style="color: black;">&#91;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span>: x = <span style="color: #dc143c;">random</span>.<span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">11</span><span style="color: black;">&#93;</span>: x == <span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">11</span><span style="color: black;">&#93;</span>: <span style="color: #008000;">False</span></pre></div></div>

<p>Even though x == eval(repr(x)) for that case, there&#8217;s still room for error. Unlike floating point numbers, it&#8217;s harder to go wrong with string equality. So str(random.random()) is just a cheap way to generate random strings.</p>
<p>I&#8217;d recommend that if your testing mostly consists of selected test cases based on what you think is possible user behavior, you might want to add some fuzzed inputs. I originally started the fuzz-testing described in this blog-post to better test for a specific bug. After adding the fuzz-testing, I found another bug I didn&#8217;t know was there. This just goes to show how useful fuzzing is as a testing tool. The fact that it&#8217;s so easy to implement is just a bonus.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/python/fuzz-testing-with-nose/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Pyweb-il Presentation on Optimization Slides</title>
		<link>http://www.algorithm.co.il/blogs/programming/pyweb-il-oresentation-on-optimization-slides/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/pyweb-il-oresentation-on-optimization-slides/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 18:31:50 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[presentation slides]]></category>
		<category><![CDATA[pyweb-il]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=570</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/pyweb-il-oresentation-on-optimization-slides/' addthis:title='Pyweb-il Presentation on Optimization Slides'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Last Monday I gave a presentation in pywebil on optimization, that&#8217;s loosely based on my blog post on the same subject. Here are the slides for that presentation.<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/pyweb-il-oresentation-on-optimization-slides/' addthis:title='Pyweb-il Presentation on Optimization Slides' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Last Monday I gave a presentation in <a href="http://groups.google.com/group/pyweb-il">pywebil</a> on optimization, that&#8217;s loosely based on my <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/10-python-optimization-tips-and-issues/">blog post on the same subject</a>. Here are the <a href="http://www.algorithm.co.il/sitecode/optimization.pdf">slides for that presentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/pyweb-il-oresentation-on-optimization-slides/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple SQLObject DB Migration how-to</title>
		<link>http://www.algorithm.co.il/blogs/programming/simple-sqlobject-db-migration-how-to/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/simple-sqlobject-db-migration-how-to/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 05:50:58 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[database migration]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[sqlobject]]></category>
		<category><![CDATA[turbogears]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=507</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/simple-sqlobject-db-migration-how-to/' addthis:title='Simple SQLObject DB Migration how-to'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>I&#8217;ve been using sqlobject for plnnr.com for quite some time now. So far my experience with it has been positive. Although I&#8217;ll probably change ORM when I move to django, for now it stays. While it stays, I need to &#8230; <a href="http://www.algorithm.co.il/blogs/programming/simple-sqlobject-db-migration-how-to/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/simple-sqlobject-db-migration-how-to/' addthis:title='Simple SQLObject DB Migration how-to' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using sqlobject for <a href="http://plnnr.com">plnnr.com</a> for quite some time now. So far my experience with it has been positive. Although I&#8217;ll probably change ORM when I move to django, for now it stays. While it stays, I need to be able to upgrade my schema to add features.<br />
SQLObject already has a tool for the job, sqlobject-admin. There are instructions on how to use it, but I found them unsatisfactory.<br />
(By the way, both django&#8217;s ORM and sqlalchemy also have tools for that, django-south and sqlalchemy-migrate respectively.)</p>
<p>So here is how I use sqlobject-admin to do migrations. Note that if you&#8217;re using turbogears 1.0, you would probably be using tg-admin. In that case, bear in mind that tg-admin just simplifies the job for you by adding various standard parameters, but apart from that, the idea stays the same.<br />
Notes:<br />
* I wrote these instructions on a windows machine. On linux machines it should be almost the same, but might require tweaking.<br />
* I used a specific db URI in the examples. You can change it to whatever you want.<br />
* I once had to tweak the main sqlobject-admin file to add the current dir to sys.path. YMMV.</p>
<p><strong>1. Example project:</strong><br />
Let&#8217;s setup a project that uses sqlobject. We&#8217;ll create a single file, &#8216;main.py&#8217; with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> sqlobject
&nbsp;
sqlobject.<span style="color: black;">sqlhub</span>.<span style="color: black;">processConnection</span> = sqlobject.<span style="color: black;">connectionForURI</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'sqlite:/D|/work/sotest/sotest.sqlite'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyThing<span style="color: black;">&#40;</span>sqlobject.<span style="color: black;">SQLObject</span><span style="color: black;">&#41;</span>:
    bla = sqlobject.<span style="color: black;">StringCol</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This is about as simple as I could get it with sqlobject.</p>
<p><strong>2. Starting to use sqlobject-admin</strong><br />
Sqlobject-admin has quite a bit of bureaucracy to go through before you get everything to work right. For a simple project, I cheat (i.e. fake an egg :), and do the following:<br />
a. Create a directory in your project called sqlobject-history<br />
b. If your project name is sotest, create a directory inside your project called sotest.egg-info<br />
c. Inside that dir create a file called sqlobject.txt<br />
d. Inside that file write:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">db_module=main
history_dir=$base/sqlobject-history</pre></div></div>

<p>(note that the main here is the name of the module we created earlier).</p>
<p><strong>3. Start using sqlobject-admin</strong><br />
This will be the workflow with sqlobject-admin:<br />
1. Have the creation sql for the current code version.<br />
2. Update your code<br />
3. Generate the creation sql for the new code version, *without updating the db*<br />
4. Create an upgrade script using the diff between the versions<br />
5. Use the upgrade script.</p>
<p>More specifically:<br />
1. First time &#8211; do:</p>
<blockquote><p>sqlobject-admin record &#8211;egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite</p></blockquote>
<p>2. To see that everything works, do:</p>
<blockquote><p>sqlobject-admin list &#8211;egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite</p></blockquote>
<p>and:</p>
<blockquote><p>sqlobject-admin status &#8211;egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite</p></blockquote>
<p>3. Update your database definition (in the Python file). For example, change the contents of main.py to:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> sqlobject
&nbsp;
sqlobject.<span style="color: black;">sqlhub</span>.<span style="color: black;">processConnection</span> = sqlobject.<span style="color: black;">connectionForURI</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'sqlite:/D|/work/sotest/sotest.sqlite'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyThing<span style="color: black;">&#40;</span>sqlobject.<span style="color: black;">SQLObject</span><span style="color: black;">&#41;</span>:
    bla = sqlobject.<span style="color: black;">StringCol</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    bla2 = sqlobject.<span style="color: black;">StringCol</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>4. Here is the critical part. Do</p>
<blockquote><p>sqlobject-admin record &#8211;egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite &#8211;no-db-record</p></blockquote>
<p>In the sqlobject-history directory there should be now two subdirectories, for each version. Let&#8217;s call the old version X and the new version Y. In the old version directory create a file:<br />
upgrade_sqlite_Y.sql (where Y is the new version&#8217;s name).<br />
In this file, write down the sql to add the bla2 column to the MyThing table. You can use the creation sql commands in the respective versions&#8217; directories to write it.</p>
<p>(note: if we used &#8211;edit we would get an editor opened, and if the edited file has any content when you close it, it will be saved as the upgrade script. I don&#8217;t like using this method. Note that if you&#8217;re on windows you&#8217;ll have to fix sqlobject-admin to open your editor, as the command it uses works only on linux machines.)</p>
<p>5. run<br />
<blockquote>sqlobject-admin upgrade &#8211;egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite</p></blockquote>
<p>6. Make sure everything is OK with sqlobject-admin status.</p>
<p><strong>3. After using the upgrade script</strong><br />
You can use the same upgrade script for other instances of your project. Just make sure that you have the versions numbers correct, and the first version recorded in the database.</p>
<p>I hope this will be useful for someone using sqlobject, I know I needed this kind of how-to. If you have any questions, feel free to ask them in comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/simple-sqlobject-db-migration-how-to/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The mathematics behind the solution for Challenge No. 5</title>
		<link>http://www.algorithm.co.il/blogs/challenges/the-mathematics-behind-the-solution-for-challenge-no-5/</link>
		<comments>http://www.algorithm.co.il/blogs/challenges/the-mathematics-behind-the-solution-for-challenge-no-5/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 23:49:15 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Challenges]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[base systems]]></category>
		<category><![CDATA[factorial]]></category>
		<category><![CDATA[Knuth]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[permutations]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=500</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/challenges/the-mathematics-behind-the-solution-for-challenge-no-5/' addthis:title='The mathematics behind the solution for Challenge No. 5'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>If you take a look at the various solutions people proposed for the last challenge of generating a specific permutation, you&#8217;ll see that they are very similar. Most of them are based on some form of div-mod usage. The reason &#8230; <a href="http://www.algorithm.co.il/blogs/challenges/the-mathematics-behind-the-solution-for-challenge-no-5/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/challenges/the-mathematics-behind-the-solution-for-challenge-no-5/' addthis:title='The mathematics behind the solution for Challenge No. 5' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>If you take a look at the various solutions people proposed for the <a href="http://www.algorithm.co.il/blogs/index.php/programming/small-programming-challenge-no-5-generating-a-permutation/">last challenge of generating a specific permutation</a>, you&#8217;ll see that they are very similar. Most of them are based on some form of div-mod usage. The reason this is so, is because all of these solutions are using the <a href="http://en.wikipedia.org/wiki/Factorial_base">Factorial Base</a>.</p>
<p>What does that mean?<br />
Note that we usually encounter div-mods when we want to find the representation of a number in a certain base. That should already pique your interest. Now consider that a base&#8217;s digits need not have the same weight. For example, consider how we count the number of seconds since the start of the week:</p>
<p>seconds of the last minute, A (at most 60-1)<br />
minutes of the last hour, B (at most 60-1)<br />
hours of the last day, C (at most (24-1)<br />
days of the last week, D (at most 7-1)</p>
<p>So given A, B, C, D, we would say that the number of seconds is:<br />
A + 60*B + 24*C + 7*D. This certainly looks like a base transformation. To go back, we would use divmod.</p>
<p>The factorial base is just the same, with the numbers n, n-1, &#8230; 1. Note that in the factorial base, you can only represent a finite number of numbers &#8211; n!. This should not be surprising &#8211; this is what we set out to do in the first place!<br />
The thing that I found really amazing about this is that all the people to whom I posed this challenge came up with almost the same &#8220;way&#8221; of solving it.</p>
<p>Other interesting curiosities regarding bases can be found in Knuth&#8217;s book, &#8220;The Art of Computer Programming&#8221;, volume 2, Section 4.1.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/challenges/the-mathematics-behind-the-solution-for-challenge-no-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>And now for something completely different</title>
		<link>http://www.algorithm.co.il/blogs/humour/and-now-for-something-completely-different/</link>
		<comments>http://www.algorithm.co.il/blogs/humour/and-now-for-something-completely-different/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 18:28:42 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Humour]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[clip]]></category>
		<category><![CDATA[humor]]></category>
		<category><![CDATA[python zen]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=484</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/humour/and-now-for-something-completely-different/' addthis:title='And now for something completely different'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>My friend Yuval whom you might already know from the comments here, apparently composed music for the Python Zen. It made me laugh today, and as it&#8217;s been a long day, I thought it&#8217;s worth sharing here. Especially as it &#8230; <a href="http://www.algorithm.co.il/blogs/humour/and-now-for-something-completely-different/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/humour/and-now-for-something-completely-different/' addthis:title='And now for something completely different' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>My friend <a href="http://uberpython.wordpress.com/">Yuval</a> whom you might already know from the comments here, apparently composed music for the <a href="http://www.python.org/dev/peps/pep-0020/">Python Zen</a>. It made me laugh today, and as it&#8217;s been a long day, I thought it&#8217;s worth sharing here. Especially as it is Python related.</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/kYB72Qa6F9I&#038;hl=en&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/kYB72Qa6F9I&#038;hl=en&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/humour/and-now-for-something-completely-different/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Leaky method references</title>
		<link>http://www.algorithm.co.il/blogs/programming/python/leaky-method-references/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/python/leaky-method-references/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 08:39:25 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Gotcha]]></category>
		<category><![CDATA[memory leak]]></category>
		<category><![CDATA[__del__]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=472</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python/leaky-method-references/' addthis:title='Leaky method references'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>After reading my last post regarding __del__, you should know that __del__ + reference cycle = leak. Let&#8217;s say that you do need to use __del__, so you decide to avoid reference cycles. You write your code in such a &#8230; <a href="http://www.algorithm.co.il/blogs/programming/python/leaky-method-references/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python/leaky-method-references/' addthis:title='Leaky method references' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>After reading my last post <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/python-gotchas-no-2-garbage-collection-oddities/">regarding __del__</a>, you should know that __del__ + reference cycle = leak.<br />
Let&#8217;s say that you do need to use __del__, so you decide to avoid reference cycles. You write your code in such a way as to use the minimum necessary cycles, and for the ones that remain you use the weakref module.</p>
<p>You might still have cycles where you don&#8217;t expect it &#8211; in references to methods.<br />
Consider the following piece of code. Can you spot the reference cycle?</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> A<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> f<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
a = A<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
a.<span style="color: black;">g</span> = a.<span style="color: black;">f</span></pre></div></div>

<p>This code has the following reference cycle: a -> a.g -> a.f -> a.<br />
How come?<br />
When you call a.f like so: &#8220;a.f()&#8221; <strong>two</strong> things happen:<br />
1. A.f is bounded to a<br />
2. The bounded A.f is called with the first parameter getting the bounded value.</p>
<p>You may consider that &#8220;a.f&#8221; is syntactic sugar for the partial function application, A.f gets a as a first argument but doesn&#8217;t get called yet.</p>
<p>When you use &#8220;a.g = a.f&#8221; what actually happens is a holding a reference to a bounded method, which holds a reference to a.</p>
<p>An idiom that uses these cycles is implementing state machines. Consider the following example code:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> MyMachine<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><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">next_func</span> = <span style="color: #008000;">self</span>.<span style="color: black;">state_a</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">input</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">input</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">next_func</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> state_a<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'a: '</span>, value
        <span style="color: #008000;">self</span>.<span style="color: black;">next_func</span> = <span style="color: #008000;">self</span>.<span style="color: black;">state_b</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> state_b<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'b: '</span>, value
        <span style="color: #008000;">self</span>.<span style="color: black;">next_func</span> = <span style="color: #008000;">self</span>.<span style="color: black;">state_a</span></pre></div></div>

<p>Of course, my code was a bit more complicated than that, but the basic idea remains. (My code usually created some kind of function table in __init__ used to lookup the next function, and lookups happened outside &#8220;state functions&#8221;). I&#8217;ve seen many state machine recipes include method references &#8211; and rightly so. It&#8217;s a clear and easy way to code a state machine. (For example, <a href="http://code.activestate.com/recipes/574430/">this state machine recipe</a>).<br />
Be careful though &#8211; once you add __del__ to these simple recipes you might end up with a memory leak.</p>
<p>Short note: I was going to publish this post a few days ago, but <a href="http://www.kylev.com/2009/11/03/finding-my-first-python-reference-cycle/">kylev</a> beat me to it. This just goes to show that other people encountered this kind of cycle.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/python/leaky-method-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

