<?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>depth first search &#187; python</title>
	<atom:link href="http://www.depthfirstsearch.net/blog/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.depthfirstsearch.net/blog</link>
	<description>“We can only see a short distance ahead, but we can see plenty there that needs to be done.&#34;</description>
	<lastBuildDate>Fri, 03 Feb 2012 04:22:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>In a Pickle</title>
		<link>http://www.depthfirstsearch.net/blog/2011/10/25/in-a-pickle/</link>
		<comments>http://www.depthfirstsearch.net/blog/2011/10/25/in-a-pickle/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 22:17:58 +0000</pubDate>
		<dc:creator>JS</dc:creator>
				<category><![CDATA[computing]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.depthfirstsearch.net/blog/?p=2590</guid>
		<description><![CDATA[I like IPython, but I&#8217;m not sure I understand it, and my lack of understanding has caused me some pain recently when a pickle file (Python object serialization for the uninitiated) would not open. I&#8217;m not even sure I&#8217;ve figured out how to reproduce the problem, but I did find a very simple example of [...]]]></description>
			<content:encoded><![CDATA[<p>I like IPython, but I&#8217;m not sure I understand it, and my lack of understanding has caused me some pain recently when a pickle file (Python object serialization for the uninitiated) would not open. I&#8217;m not even sure I&#8217;ve figured out how to reproduce the problem, but I did find a very simple example of some code that runs fine in Python but not IPython:</p>
<p><script src="https://gist.github.com/1314491.js?file=ipickle.py"></script></p>
<p>Running this with</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python ipickle.py</pre></div></div>

<p> gives:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">stober<span style="color: #7a0874; font-weight: bold;">&#91;</span>~<span style="color: #000000; font-weight: bold;">/</span>Dropbox<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>gist<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>laptop<span style="color: #7a0874; font-weight: bold;">&#125;</span>$ python ipickle.py
<span style="color: #000000; font-weight: bold;">&lt;</span>__main__.A instance at 0x10cc37bd8<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>Running the same thing with IPython</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ipython ipickle.py</pre></div></div>

<p> produces decidedly different output:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">&nbsp;
stober<span style="color: #7a0874; font-weight: bold;">&#91;</span>~<span style="color: #000000; font-weight: bold;">/</span>Dropbox<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>gist<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>laptop<span style="color: #7a0874; font-weight: bold;">&#125;</span>$ ipython ipickle.py 
<span style="color: #660033;">---------------------------------------------------------------------------</span>
AttributeError                            Traceback <span style="color: #7a0874; font-weight: bold;">&#40;</span>most recent call <span style="color: #c20cb9; font-weight: bold;">last</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>stober<span style="color: #000000; font-weight: bold;">/</span>Dropbox<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>gist<span style="color: #000000; font-weight: bold;">/</span>ipickle.py <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">&lt;</span>module<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
     <span style="color: #000000;">16</span>     pickle.dump<span style="color: #7a0874; font-weight: bold;">&#40;</span>A<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>, open<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;A.pck&quot;</span>,<span style="color: #ff0000;">&quot;w&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
     <span style="color: #000000;">17</span> 
---<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">18</span>     a = pickle.load<span style="color: #7a0874; font-weight: bold;">&#40;</span>open<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;A.pck&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
     <span style="color: #000000;">19</span>     print a
&nbsp;
<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>Frameworks<span style="color: #000000; font-weight: bold;">/</span>Python.framework<span style="color: #000000; font-weight: bold;">/</span>Versions<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2.7</span><span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>python2.7<span style="color: #000000; font-weight: bold;">/</span>pickle.pyc <span style="color: #000000; font-weight: bold;">in</span> load<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">file</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
   <span style="color: #000000;">1376</span> 
   <span style="color: #000000;">1377</span> def load<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">file</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>:
-<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">1378</span>     <span style="color: #7a0874; font-weight: bold;">return</span> Unpickler<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">file</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.load<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
   <span style="color: #000000;">1379</span> 
   <span style="color: #000000;">1380</span> def loads<span style="color: #7a0874; font-weight: bold;">&#40;</span>str<span style="color: #7a0874; font-weight: bold;">&#41;</span>:
&nbsp;
<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>Frameworks<span style="color: #000000; font-weight: bold;">/</span>Python.framework<span style="color: #000000; font-weight: bold;">/</span>Versions<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2.7</span><span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>python2.7<span style="color: #000000; font-weight: bold;">/</span>pickle.pyc <span style="color: #000000; font-weight: bold;">in</span> load<span style="color: #7a0874; font-weight: bold;">&#40;</span>self<span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #000000;">856</span>             <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #000000;">1</span>:
    <span style="color: #000000;">857</span>                 key = <span style="color: #c20cb9; font-weight: bold;">read</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
--<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">858</span>                 dispatch<span style="color: #7a0874; font-weight: bold;">&#91;</span>key<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>self<span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #000000;">859</span>         except _Stop, stopinst:
    <span style="color: #000000;">860</span>             <span style="color: #7a0874; font-weight: bold;">return</span> stopinst.value
&nbsp;
<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>Frameworks<span style="color: #000000; font-weight: bold;">/</span>Python.framework<span style="color: #000000; font-weight: bold;">/</span>Versions<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2.7</span><span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>python2.7<span style="color: #000000; font-weight: bold;">/</span>pickle.pyc <span style="color: #000000; font-weight: bold;">in</span> load_inst<span style="color: #7a0874; font-weight: bold;">&#40;</span>self<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   <span style="color: #000000;">1067</span>         module = self.readline<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>:-<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
   <span style="color: #000000;">1068</span>         name = self.readline<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>:-<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
-<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">1069</span>         klass = self.find_class<span style="color: #7a0874; font-weight: bold;">&#40;</span>module, name<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   <span style="color: #000000;">1070</span>         self._instantiate<span style="color: #7a0874; font-weight: bold;">&#40;</span>klass, self.marker<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
   <span style="color: #000000;">1071</span>     dispatch<span style="color: #7a0874; font-weight: bold;">&#91;</span>INST<span style="color: #7a0874; font-weight: bold;">&#93;</span> = load_inst
&nbsp;
<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>Frameworks<span style="color: #000000; font-weight: bold;">/</span>Python.framework<span style="color: #000000; font-weight: bold;">/</span>Versions<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2.7</span><span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>python2.7<span style="color: #000000; font-weight: bold;">/</span>pickle.pyc <span style="color: #000000; font-weight: bold;">in</span> find_class<span style="color: #7a0874; font-weight: bold;">&#40;</span>self, module, name<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   <span style="color: #000000;">1124</span>         __import__<span style="color: #7a0874; font-weight: bold;">&#40;</span>module<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   <span style="color: #000000;">1125</span>         mod = sys.modules<span style="color: #7a0874; font-weight: bold;">&#91;</span>module<span style="color: #7a0874; font-weight: bold;">&#93;</span>
-<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">1126</span>         klass = getattr<span style="color: #7a0874; font-weight: bold;">&#40;</span>mod, name<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   <span style="color: #000000;">1127</span>         <span style="color: #7a0874; font-weight: bold;">return</span> klass
   <span style="color: #000000;">1128</span> 
&nbsp;
AttributeError: <span style="color: #ff0000;">'FakeModule'</span> object has no attribute <span style="color: #ff0000;">'A'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.depthfirstsearch.net/blog/2011/10/25/in-a-pickle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reversing a Dictionary</title>
		<link>http://www.depthfirstsearch.net/blog/2011/05/10/reversing-a-dictionary/</link>
		<comments>http://www.depthfirstsearch.net/blog/2011/05/10/reversing-a-dictionary/#comments</comments>
		<pubDate>Tue, 10 May 2011 18:19:10 +0000</pubDate>
		<dc:creator>JS</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.depthfirstsearch.net/blog/?p=2491</guid>
		<description><![CDATA[For some reason the code I&#8217;m writing requires a lot of mappings between different kinds of values, and I find myself trying to manipulate dictionaries in many ways. I wrote a simple function that reverses a dictionary: Then I remembered the setdefault method and was able to create a more concise function: Of course if [...]]]></description>
			<content:encoded><![CDATA[<p>For some reason the code I&#8217;m writing requires a lot of mappings between different kinds of values, and I find myself trying to manipulate dictionaries in many ways. I wrote a simple function that reverses a dictionary:</p>
<p><script src="https://gist.github.com/965024.js"> </script></p>
<p>Then I remembered the setdefault method and was able to create a more concise function:</p>
<p><script src="https://gist.github.com/965019.js"> </script></p>
<p>Of course if your dictionary is 1-1, you probably don&#8217;t want to introduce sets into the reverse version, so I created a function that composes nicely with the previous function:</p>
<p><script src="https://gist.github.com/965026.js"> </script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.depthfirstsearch.net/blog/2011/05/10/reversing-a-dictionary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Worse than Failure</title>
		<link>http://www.depthfirstsearch.net/blog/2010/01/19/worse-than-failure/</link>
		<comments>http://www.depthfirstsearch.net/blog/2010/01/19/worse-than-failure/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 20:35:25 +0000</pubDate>
		<dc:creator>JS</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.depthfirstsearch.net/blog/?p=1894</guid>
		<description><![CDATA[The shorthand &#8220;WTF&#8221; is common among developers. It does not actually stand for &#8220;worse than failure&#8221; but I run a clean ship here people. [Hint: let's just say that WTF is a somewhat crude interrogative. Sample usage: "Dude, WTF?" or, if you prefer, comic form.] I just spent two days looking for a bug in [...]]]></description>
			<content:encoded><![CDATA[<p>The shorthand &#8220;WTF&#8221; is common among developers. It does not actually stand for &#8220;<a href="http://thedailywtf.com/Info/About.aspx">worse than failure</a>&#8221; but I run a clean ship here people. [Hint: let's just say that WTF is a somewhat crude interrogative. Sample usage: "Dude, WTF?" or, if you prefer, <a href="http://www.osnews.com/images/comics/wtfm.jpg">comic form</a>.]</p>
<p>I just spent two days looking for a bug in my code whose fix was simply replacing</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">A = A + np.<span style="color: black;">dot</span><span style="color: black;">&#40;</span>features, features - env.<span style="color: black;">gamma</span> <span style="color: #66cc66;">*</span> newfeatures<span style="color: black;">&#41;</span></pre></div></div>

<p>with</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">A = A + np.<span style="color: black;">outer</span><span style="color: black;">&#40;</span>features, features - env.<span style="color: black;">gamma</span> <span style="color: #66cc66;">*</span> newfeatures<span style="color: black;">&#41;</span></pre></div></div>

<p>In my defense, I didn&#8217;t spend two full days looking for this mistake, but the process was so demoralizing that I couldn&#8217;t find much willpower to do productive work when I wasn&#8217;t actively debugging.</p>
<p>Takeaway lesson, triple check your linear algebra. If you get something wrong, you&#8217;re likely to have a hard time figuring out why, or worse, you may mistake the junk for the correct answer. [Aside: note that I was really burned by the fact that '+' is overloaded in this case, and so is valid for both matrix, matrix addition and scalar, matrix addition. Despite the scorch marks, I do like this feature.]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.depthfirstsearch.net/blog/2010/01/19/worse-than-failure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Computing All Shortest Paths in Python</title>
		<link>http://www.depthfirstsearch.net/blog/2009/12/03/computing-all-shortest-paths-in-python/</link>
		<comments>http://www.depthfirstsearch.net/blog/2009/12/03/computing-all-shortest-paths-in-python/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 19:27:34 +0000</pubDate>
		<dc:creator>JS</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.depthfirstsearch.net/blog/?p=1744</guid>
		<description><![CDATA[A research problem I&#8217;m working on involves Isomap, a method of dimensionality reduction that requires computing all shortest paths over relatively large graphs. In interpreted languages like Matlab this is often done through the use of the Floyd-Warshall algorithm, a simple dynamic programming approach to computing all shortest paths. In the reference Isomap code this [...]]]></description>
			<content:encoded><![CDATA[<p>A research problem I&#8217;m working on involves <a href="http://waldron.stanford.edu/~isomap/">Isomap</a>, a method of dimensionality reduction that requires computing all shortest paths over relatively large graphs. In interpreted languages like Matlab this is often done through the use of the <a href="http://en.wikipedia.org/wiki/Floyd-Warshall">Floyd-Warshall algorithm</a>, a simple <img src='http://s.wordpress.com/latex.php?latex=O%28n%5E3%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='O(n^3)' title='O(n^3)' class='latex' /> dynamic programming approach to computing all shortest paths. In the reference Isomap code this algorithm takes only three lines:</p>

<div class="wp_syntax"><div class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">for</span> k=<span style="color: #33f;">1</span>:N
    D = <span style="color: #0000FF;">min</span><span style="color: #080;">&#40;</span>D,<span style="color: #0000FF;">repmat</span><span style="color: #080;">&#40;</span>D<span style="color: #080;">&#40;</span>:,k<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">1</span> N<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>+<span style="color: #0000FF;">repmat</span><span style="color: #080;">&#40;</span>D<span style="color: #080;">&#40;</span>k,:<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#91;</span>N <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div>

<p>The benefits of Floyd-Warshall are two-fold for languages like Matlab. The first is that the implementation is short and simple. The second is that the implementation can employ a number of linear algebra primitives that are highly optimized in languages like Matlab. In the code snippet above the repmat, addition, and min operations are all implemented as low-level highly optimized <a href="http://en.wikipedia.org/wiki/LAPACK">library calls</a>. This means that Floyd-Warshall, despite the non-optimal runtime, is often the fastest Matlab approach to computing all shortest paths even on reasonably large problem sizes.</p>
<p>In my work, I&#8217;ve reimplemented Isomap in Python (which will soon appear in my algorithms gallery). My implementation of Floyd-Warshall uses <a href="http://numpy.scipy.org/">NumPy</a> for access to similarly optimized low-level linear algebra routines. Here&#8217;s my original Python code snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
    adj = np.<span style="color: black;">minimum</span><span style="color: black;">&#40;</span> adj, np.<span style="color: black;">add</span>.<span style="color: black;">outer</span><span style="color: black;">&#40;</span>adj<span style="color: black;">&#91;</span>:,k<span style="color: black;">&#93;</span>,adj<span style="color: black;">&#91;</span>k,:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">return</span> adj</pre></div></div>

<p>Note that I used outer products in my code, instead of the repmat approach in the original Isomap implementation. I decided to see if using tile, the NumPy equivalent or repmat, would result in an even faster Floyd-Warshall implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
    adj = np.<span style="color: black;">minimum</span><span style="color: black;">&#40;</span>adj, np.<span style="color: black;">tile</span><span style="color: black;">&#40;</span>adj<span style="color: black;">&#91;</span>:,k<span style="color: black;">&#93;</span>.<span style="color: black;">reshape</span><span style="color: black;">&#40;</span>-<span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + np.<span style="color: black;">tile</span><span style="color: black;">&#40;</span>adj<span style="color: black;">&#91;</span>k,:<span style="color: black;">&#93;</span>,<span style="color: black;">&#40;</span>n,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">return</span> adj</pre></div></div>

<p>To test these two approaches, I generated a few random graphs on 1000 nodes and computed all shortest paths several times (to account for variations in CPU usage). To my delight, it turns out that my original implementation ran an average of 30 seconds faster (42.8 seconds)  than the approach using tile (72.7 seconds). Though more experimentation might be required to make this comparison definitive, I am trying to uncover possible reasons for the performance difference, and in particular why the Matlab code uses repmat instead of outer products (I have yet to compare performance of different Matlab versions of this algorithm).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.depthfirstsearch.net/blog/2009/12/03/computing-all-shortest-paths-in-python/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Metaclass Magic</title>
		<link>http://www.depthfirstsearch.net/blog/2008/10/10/metaclass-magic/</link>
		<comments>http://www.depthfirstsearch.net/blog/2008/10/10/metaclass-magic/#comments</comments>
		<pubDate>Sat, 11 Oct 2008 02:43:39 +0000</pubDate>
		<dc:creator>JS</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[economics]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.depthfirstsearch.net/blog/?p=667</guid>
		<description><![CDATA[So today I found myself trying to figure out how to pickle objects that are based on dynamically constructed classes. This is probably the simplest instance of metaclass magic in Python &#8212; the use of the &#8216;type&#8217; builtin, a metaclass. A metaclass is a class whose objects are themselves class specifications. People use metaclasses to [...]]]></description>
			<content:encoded><![CDATA[<p>So today I found myself trying to figure out how to pickle objects that are based on dynamically constructed classes. This is probably the simplest instance of metaclass magic in Python &#8212; the use of the &#8216;type&#8217; builtin, a metaclass. A metaclass is a class whose objects are themselves class specifications. People use metaclasses to modify the default way classes are built, or in my case to build classes dynamically (say from a soup of mixins).</p>
<p>Anyway, the following code ran into problems.</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;">pickle</span>
&nbsp;
classes = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
classes<span style="color: black;">&#91;</span><span style="color: #483d8b;">'ClassName'</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">type</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'ClassName'</span>, <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
obj = classes<span style="color: black;">&#91;</span><span style="color: #483d8b;">'ClassName'</span><span style="color: black;">&#93;</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">pickle</span>.<span style="color: black;">dump</span><span style="color: black;">&#40;</span>obj,<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'test.pck'</span>,<span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Attempting to run this code gives:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">Traceback <span style="color: black;">&#40;</span>most recent call last<span style="color: black;">&#41;</span>:
  File <span style="color: #483d8b;">&quot;/tmp/py17808UNS&quot;</span>, line <span style="color: #ff4500;">8</span>, <span style="color: #ff7700;font-weight:bold;">in</span>
    <span style="color: #dc143c;">pickle</span>.<span style="color: black;">dump</span><span style="color: black;">&#40;</span>obj,<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'test.pck'</span>,<span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  File <span style="color: #483d8b;">&quot;/usr/lib/python2.5/pickle.py&quot;</span>, line <span style="color: #ff4500;">1362</span>, <span style="color: #ff7700;font-weight:bold;">in</span> dump
    Pickler<span style="color: black;">&#40;</span><span style="color: #008000;">file</span>, protocol<span style="color: black;">&#41;</span>.<span style="color: black;">dump</span><span style="color: black;">&#40;</span>obj<span style="color: black;">&#41;</span>
  File <span style="color: #483d8b;">&quot;/usr/lib/python2.5/pickle.py&quot;</span>, line <span style="color: #ff4500;">224</span>, <span style="color: #ff7700;font-weight:bold;">in</span> dump
    <span style="color: #008000;">self</span>.<span style="color: black;">save</span><span style="color: black;">&#40;</span>obj<span style="color: black;">&#41;</span>
  File <span style="color: #483d8b;">&quot;/usr/lib/python2.5/pickle.py&quot;</span>, line <span style="color: #ff4500;">331</span>, <span style="color: #ff7700;font-weight:bold;">in</span> save
    <span style="color: #008000;">self</span>.<span style="color: black;">save_reduce</span><span style="color: black;">&#40;</span>obj=obj, <span style="color: #66cc66;">*</span>rv<span style="color: black;">&#41;</span>
  File <span style="color: #483d8b;">&quot;/usr/lib/python2.5/pickle.py&quot;</span>, line <span style="color: #ff4500;">401</span>, <span style="color: #ff7700;font-weight:bold;">in</span> save_reduce
    save<span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span>
  File <span style="color: #483d8b;">&quot;/usr/lib/python2.5/pickle.py&quot;</span>, line <span style="color: #ff4500;">286</span>, <span style="color: #ff7700;font-weight:bold;">in</span> save
    f<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, obj<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># Call unbound method with explicit self</span>
  File <span style="color: #483d8b;">&quot;/usr/lib/python2.5/pickle.py&quot;</span>, line <span style="color: #ff4500;">562</span>, <span style="color: #ff7700;font-weight:bold;">in</span> save_tuple
    save<span style="color: black;">&#40;</span>element<span style="color: black;">&#41;</span>
  File <span style="color: #483d8b;">&quot;/usr/lib/python2.5/pickle.py&quot;</span>, line <span style="color: #ff4500;">286</span>, <span style="color: #ff7700;font-weight:bold;">in</span> save
    f<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, obj<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># Call unbound method with explicit self</span>
  File <span style="color: #483d8b;">&quot;/usr/lib/python2.5/pickle.py&quot;</span>, line <span style="color: #ff4500;">748</span>, <span style="color: #ff7700;font-weight:bold;">in</span> save_global
    <span style="color: black;">&#40;</span>obj, module, name<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
PicklingError: Can<span style="color: #483d8b;">'t pickle : it'</span>s <span style="color: #ff7700;font-weight:bold;">not</span> found <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #dc143c;">__main__</span>.<span style="color: black;">ClassName</span></pre></div></div>

<p>So what was I trying to do? I&#8217;ve implemented a learning agent as an object and constructed a bunch of <a href="http://en.wikipedia.org/wiki/Mixins">mixins</a> that specify variations of the training algorithm, reward function, and various other modifiable agent properties. What I want to do is take the cross product of all the variations, testing each on the <a href="http://www.cs.utexas.edu/facilities/accommodations/condor/">cluster</a> here at UT. Of course I want to pickle each agent so that I can inspect the resulting performance later without having to rerun a thousand experiments.</p>
<p>I only have a small number of variations, so explicitly naming all the mixin combinations isn&#8217;t too onerous, but I am curious as to whether lack of serialization inhibits the use of mixins in Python programming generally.</p>
<p>Or maybe I&#8217;m just programming orthogonal to the norm.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.depthfirstsearch.net/blog/2008/10/10/metaclass-magic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python Segfaults</title>
		<link>http://www.depthfirstsearch.net/blog/2008/09/24/python-segfaults/</link>
		<comments>http://www.depthfirstsearch.net/blog/2008/09/24/python-segfaults/#comments</comments>
		<pubDate>Wed, 24 Sep 2008 18:23:25 +0000</pubDate>
		<dc:creator>JS</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.depthfirstsearch.net/blog/?p=607</guid>
		<description><![CDATA[Python is an interpreted language, and the interpreter is solid enough that you wouldn&#8217;t expect to encounter the following: stober&#91;~&#93;&#123;landmine&#125;$ python test.py Segmentation fault Pretty scary stuff. So what was I running? #!/lusr/bin/python from opencv import highgui from opencv import cv &#160; writer = highgui.cvCreateVideoWriter&#40;&#34;test.mpg&#34;, highgui.CV_FOURCC&#40;'m','p','g','1'&#41;, 30.0, cv.cvSize&#40;100,100&#41;, True&#41; 99.9% of the time the Python [...]]]></description>
			<content:encoded><![CDATA[<p>Python is an interpreted language, and the interpreter is solid enough that you wouldn&#8217;t expect to encounter the following:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">stober<span style="color: #7a0874; font-weight: bold;">&#91;</span>~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>landmine<span style="color: #7a0874; font-weight: bold;">&#125;</span>$ python test.py
Segmentation fault</pre></div></div>

<p>Pretty scary stuff.  So what was I running?</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/lusr/bin/python</span>
<span style="color: #ff7700;font-weight:bold;">from</span> opencv <span style="color: #ff7700;font-weight:bold;">import</span> highgui
<span style="color: #ff7700;font-weight:bold;">from</span> opencv <span style="color: #ff7700;font-weight:bold;">import</span> cv
&nbsp;
writer = highgui.<span style="color: black;">cvCreateVideoWriter</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;test.mpg&quot;</span>, highgui.<span style="color: black;">CV_FOURCC</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'m'</span>,<span style="color: #483d8b;">'p'</span>,<span style="color: #483d8b;">'g'</span>,<span style="color: #483d8b;">'1'</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">30.0</span>, cv.<span style="color: black;">cvSize</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span>,<span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">True</span><span style="color: black;">&#41;</span></pre></div></div>

<p>99.9% of the time the Python interpreter is not at fault, so you can safetly bet the problem has something to do with a library, preferably some library that is</p>
<ol>
<li>not in part of the standard libraries</li>
<li>not written in pure Python.</li>
</ol>
<p>The Python bindings for <a href="http://opencvlibrary.sourceforge.net/">OpenCV</a> fit both of these criteria perfectly. So the question now is, what do I do about it?</p>
<p>A major advantage of open source is that, if you are so inclined, you may be able to figure out a solution directly. If you spend all day in Python, you may have forgotten about an old friend from your distant past &#8212; <em>gdb</em>.</p>
<p>With a debuggable OpenCV library at my disposal, here what my gdb session looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">This GDB was configured <span style="color: #c20cb9; font-weight: bold;">as</span> <span style="color: #ff0000;">&quot;i686-pc-linux-gnu&quot;</span>.
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #c20cb9; font-weight: bold;">file</span> python
Reading symbols from <span style="color: #000000; font-weight: bold;">/</span>lusr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python...done.
Using host libthread_db library <span style="color: #ff0000;">&quot;/lib/tls/i686/cmov/libthread_db.so.1&quot;</span>.
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">break</span> cvCreateVideoWriter
Function <span style="color: #ff0000;">&quot;cvCreateVideoWriter&quot;</span> not defined.
Make breakpoint pending on future shared library load? <span style="color: #7a0874; font-weight: bold;">&#40;</span>y or <span style="color: #7a0874; font-weight: bold;">&#91;</span>n<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> y
Breakpoint <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>cvCreateVideoWriter<span style="color: #7a0874; font-weight: bold;">&#41;</span> pending.
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> run test.py
Starting program: <span style="color: #000000; font-weight: bold;">/</span>lusr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python test.py
<span style="color: #7a0874; font-weight: bold;">&#91;</span>Thread debugging using libthread_db enabled<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>New Thread 0xb7dee6c0 <span style="color: #7a0874; font-weight: bold;">&#40;</span>LWP <span style="color: #000000;">32307</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
Breakpoint <span style="color: #000000;">2</span> at 0xb65b1a50: <span style="color: #c20cb9; font-weight: bold;">file</span> cvcap.cpp, line <span style="color: #000000;">250</span>.
Pending breakpoint <span style="color: #ff0000;">&quot;cvCreateVideoWriter&quot;</span> resolved
<span style="color: #7a0874; font-weight: bold;">&#91;</span>Switching to Thread 0xb7dee6c0 <span style="color: #7a0874; font-weight: bold;">&#40;</span>LWP <span style="color: #000000;">32307</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
&nbsp;
Breakpoint <span style="color: #000000;">2</span>, cvCreateVideoWriter <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">filename</span>=0xb7d85634 <span style="color: #ff0000;">&quot;test.mpg&quot;</span>, <span style="color: #007800;">fourcc</span>=<span style="color: #000000;">23556205</span>, <span style="color: #007800;">fps</span>=<span style="color: #000000;">30</span>, <span style="color: #007800;">frameSize</span>=<span style="color: #7a0874; font-weight: bold;">&#123;</span>width = <span style="color: #000000;">100</span>, height = <span style="color: #000000;">100</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>, <span style="color: #007800;">is_color</span>=<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> at cvcap.cpp:<span style="color: #000000;">250</span>
<span style="color: #000000;">250</span>                                                 double fps, CvSize frameSize, int is_color <span style="color: #7a0874; font-weight: bold;">&#41;</span>
Current language:  auto; currently <span style="color: #c20cb9; font-weight: bold;">c++</span>
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> n
<span style="color: #000000;">256</span>             <span style="color: #000000; font-weight: bold;">if</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span>fourcc <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #000000; font-weight: bold;">!</span>fps<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000;">271</span>                     result = cvCreateVideoWriter_FFMPEG<span style="color: #7a0874; font-weight: bold;">&#40;</span>filename, fourcc, fps, frameSize, is_color<span style="color: #7a0874; font-weight: bold;">&#41;</span>;
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> s
cvCreateVideoWriter_FFMPEG <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">filename</span>=0xb7d85634 <span style="color: #ff0000;">&quot;test.mpg&quot;</span>, <span style="color: #007800;">fourcc</span>=<span style="color: #000000;">23556205</span>, <span style="color: #007800;">fps</span>=<span style="color: #000000;">30</span>, <span style="color: #007800;">frameSize</span>=<span style="color: #7a0874; font-weight: bold;">&#123;</span>width = <span style="color: #000000;">100</span>, height = <span style="color: #000000;">100</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>, <span style="color: #007800;">isColor</span>=<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> at cvcap_ffmpeg.cpp:<span style="color: #000000;">1274</span>
<span style="color: #000000;">1274</span>                                               CvSize frameSize, int isColor <span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> n
<span style="color: #000000;">1276</span>        CvVideoWriter_FFMPEG<span style="color: #000000; font-weight: bold;">*</span> writer = new CvVideoWriter_FFMPEG;
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> n
<span style="color: #000000;">692</span>         CvVideoWriter_FFMPEG<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> init<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000;">1277</span>        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #7a0874; font-weight: bold;">&#40;</span> writer-<span style="color: #000000; font-weight: bold;">&gt;</span>open<span style="color: #7a0874; font-weight: bold;">&#40;</span> filename, fourcc, fps, frameSize, isColor <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> n
&nbsp;
Program received signal SIGSEGV, Segmentation fault.
0xb76f3e9f <span style="color: #000000; font-weight: bold;">in</span> codec_get_id <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> from <span style="color: #000000; font-weight: bold;">/</span>u<span style="color: #000000; font-weight: bold;">/</span>robot<span style="color: #000000; font-weight: bold;">/</span>python<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>libavformat.so.52
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>I&#8217;ll omit the circuitous debugging path that resulted. Some strategic breakpoint settings later, I found out that the expression</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">highgui.<span style="color: black;">FOUR_CC</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'m'</span>,<span style="color: #483d8b;">'p'</span>,<span style="color: #483d8b;">'g'</span>,<span style="color: #483d8b;">'1'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>led to the segfault. I never got to the root cause, but using the gdb session as intuition, and flipping through the code, I discovered that highgui has a</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">highgui.<span style="color: black;">CV_FOURCC_DEFAULT</span></pre></div></div>

<p>field which lets the function cvCreateVideoWriter select the proper encoding based on the filename.</p>
<p>With the workaround, here&#8217;s what I get:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">stober<span style="color: #7a0874; font-weight: bold;">&#91;</span>~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>landmine<span style="color: #7a0874; font-weight: bold;">&#125;</span>$ python test.py
Output <span style="color: #666666; font-style: italic;">#0, mpeg, to 'test.mpg':</span>
    Stream <span style="color: #666666; font-style: italic;">#0.0: Video: mpeg1video (hq), yuv420p, 100x100, q=2-31, 640 kb/s, 30.00 tb(c)</span>
swig<span style="color: #000000; font-weight: bold;">/</span>python detected a memory leak of <span style="color: #7a0874; font-weight: bold;">type</span> <span style="color: #ff0000;">'CvVideoWriter *'</span>, no destructor found.</pre></div></div>

<p>Still some line noise, but no segfault! So what are the takeaways?</p>
<ol>
<li>Well, if you do a lot of work with open source libraries, be sure you are comfortable building debuggable versions of those libraries.</li>
<li> Also, become familiar with using gdb to debug dynamically loaded libraries. If you use Python with a lot of imported shared libraries, you&#8217;ll need to know how to do so eventually.</li>
<li> Finally, have patience. I omitted the better part of an afternoon&#8217;s worth of faulty assumptions, misleading code paths, and general confusion in the above description. Learn to manage your frustration as you learn to navigate unfamiliar code. Both skills are invaluable.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.depthfirstsearch.net/blog/2008/09/24/python-segfaults/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>OpenCV and Python</title>
		<link>http://www.depthfirstsearch.net/blog/2008/09/22/opencv-and-python/</link>
		<comments>http://www.depthfirstsearch.net/blog/2008/09/22/opencv-and-python/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 20:04:34 +0000</pubDate>
		<dc:creator>JS</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[opencv]]></category>

		<guid isPermaLink="false">http://www.depthfirstsearch.net/blog/?p=595</guid>
		<description><![CDATA[You may have come across the following issue with OpenCV Python bindings: image = cv.cvCreateImage&#40;size, cv.IPL_DEPTH_8U, 3&#41; image.imageData = data # set some data Trying to set the &#8220;data&#8221; of an OpenCV image directly fails. The problem is in the underlying SWIG code that attempts to make the imageData field available for writing at the [...]]]></description>
			<content:encoded><![CDATA[<p>You may have come across the following issue with OpenCV Python bindings:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">image = cv.<span style="color: black;">cvCreateImage</span><span style="color: black;">&#40;</span>size, cv.<span style="color: black;">IPL_DEPTH_8U</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
image.<span style="color: black;">imageData</span> = data <span style="color: #808080; font-style: italic;"># set some data</span></pre></div></div>

<p>Trying to set the &#8220;data&#8221; of an OpenCV image directly fails. The problem is in the underlying SWIG code that attempts to make the imageData field available for writing at the python level. You can see in <a href="http://opencvlibrary.cvs.sourceforge.net/opencvlibrary/opencv/interfaces/swig/python/imagedata.i?r1=1.3&amp;r2=1.4">CVS</a> that the code gets fixed <strong>immediately after</strong> the 1.0 release.</p>
<p>But wait! Doesn&#8217;t OpenCV come with an adaptors.py that is supposed to provide methods for translating between various Python data types (NumPy &lt; =&gt; PIL &lt; =&gt; IplImage) ?</p>
<p>A quick look at the code:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> PIL2Ipl<span style="color: black;">&#40;</span><span style="color: #008000;">input</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Converts a PIL image to the OpenCV/IPL CvMat data format.
&nbsp;
    Supported input image formats are:
        RGB
        L
        F
    &quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span><span style="color: #008000;">input</span>, PIL.<span style="color: black;">Image</span>.<span style="color: black;">Image</span><span style="color: black;">&#41;</span>:
       <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">TypeError</span>, <span style="color: #483d8b;">'must be called with PIL.Image.Image!'</span>
&nbsp;
    size = cv.<span style="color: black;">cvSize</span><span style="color: black;">&#40;</span><span style="color: #008000;">input</span>.<span style="color: black;">size</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, <span style="color: #008000;">input</span>.<span style="color: black;">size</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># mode dictionary:</span>
    <span style="color: #808080; font-style: italic;"># (pil_mode : (ipl_depth, ipl_channels, color model, channel Seq)</span>
    mode_list = <span style="color: black;">&#123;</span>
        <span style="color: #483d8b;">&quot;RGB&quot;</span> : <span style="color: black;">&#40;</span>cv.<span style="color: black;">IPL_DEPTH_8U</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>,
        <span style="color: #483d8b;">&quot;L&quot;</span>   : <span style="color: black;">&#40;</span>cv.<span style="color: black;">IPL_DEPTH_8U</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>,
        <span style="color: #483d8b;">&quot;F&quot;</span>   : <span style="color: black;">&#40;</span>cv.<span style="color: black;">IPL_DEPTH_32F</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: black;">&#125;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> mode_list.<span style="color: black;">has_key</span><span style="color: black;">&#40;</span><span style="color: #008000;">input</span>.<span style="color: black;">mode</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span>, <span style="color: #483d8b;">'unknown or unsupported input mode'</span>
&nbsp;
    modes = mode_list<span style="color: black;">&#91;</span><span style="color: #008000;">input</span>.<span style="color: black;">mode</span><span style="color: black;">&#93;</span>
&nbsp;
    result = cv.<span style="color: black;">cvCreateImage</span><span style="color: black;">&#40;</span>
        size,
        modes<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, <span style="color: #808080; font-style: italic;"># depth</span>
        modes<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>  <span style="color: #808080; font-style: italic;"># channels</span>
        <span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># set imageData</span>
    result.<span style="color: black;">imageData</span>=<span style="color: #008000;">input</span>.<span style="color: black;">tostring</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#FAIL!</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> result</pre></div></div>

<p>As you can see, adaptors.py tries to set imageData as well, and so fails to convert from any format into IplImage. Your options are to work from the repository version of OpenCV or to back port the change from the repository to your local installation. Both require compiling from source and so as you may imagine, neither case is convenient when working with libcv as installed from your favorite package manager of choice.</p>
<p>A third option is to back port the changes from the repository to your own custom swig module whose only goal is to implement a function that sets the imageData field properly. That&#8217;s the approach I took, and though it took a considerable amount of time to implement (because I don&#8217;t really understand SWIG), the end result did not require many lines of code. Here&#8217;s my iplimage.i file:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">%</span>module iplimage
<span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>
<span style="color: #339933;">#include &quot;cv.h&quot;</span>
<span style="color: #993333;">void</span> set<span style="color: #009900;">&#40;</span>CvMat <span style="color: #339933;">*</span>self<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>string<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">%</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> set<span style="color: #009900;">&#40;</span>CvMat <span style="color: #339933;">*</span>self<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>string<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And here&#8217;s iplimage.c copied with minor changes directly out of the repository for OpenCV:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;cv.h&quot;</span>
&nbsp;
<span style="color: #993333;">void</span> set<span style="color: #009900;">&#40;</span>CvMat <span style="color: #339933;">*</span> self<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>string<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> depth <span style="color: #339933;">=</span> CV_MAT_DEPTH<span style="color: #009900;">&#40;</span>self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>type<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> cn <span style="color: #339933;">=</span> CV_MAT_CN<span style="color: #009900;">&#40;</span>self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>type<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> step <span style="color: #339933;">=</span> self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>step <span style="color: #339933;">?</span> self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>step <span style="color: #339933;">:</span> CV_ELEM_SIZE<span style="color: #009900;">&#40;</span>self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>type<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>cols<span style="color: #339933;">;</span>
	<span style="color: #993333;">long</span> line<span style="color: #339933;">;</span>
	<span style="color: #993333;">long</span> pixel<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>depth <span style="color: #339933;">==</span> CV_8U <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> cn<span style="color: #339933;">==</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// RGB case</span>
		<span style="color: #666666; font-style: italic;">// The data is reordered beause OpenCV uses BGR instead of RGB</span>
&nbsp;
		<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> line <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>rows<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>line<span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>pixel <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> pixel <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>cols<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>pixel<span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// In OpenCV the beginning of the lines are aligned</span>
				<span style="color: #666666; font-style: italic;">// to 4 Bytes. So use step instead of cols.</span>
				<span style="color: #993333;">long</span> position <span style="color: #339933;">=</span> line<span style="color: #339933;">*</span>step <span style="color: #339933;">+</span> pixel<span style="color: #339933;">*</span><span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
				<span style="color: #993333;">long</span> sourcepos <span style="color: #339933;">=</span> line<span style="color: #339933;">*</span>self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>cols<span style="color: #339933;">*</span><span style="color: #0000dd;">3</span> <span style="color: #339933;">+</span> pixel<span style="color: #339933;">*</span><span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
				self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data.<span style="color: #202020;">ptr</span><span style="color: #009900;">&#91;</span>position  <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> string<span style="color: #009900;">&#91;</span>sourcepos<span style="color: #339933;">+</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data.<span style="color: #202020;">ptr</span><span style="color: #009900;">&#91;</span>position<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> string<span style="color: #009900;">&#91;</span>sourcepos<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data.<span style="color: #202020;">ptr</span><span style="color: #009900;">&#91;</span>position<span style="color: #339933;">+</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> string<span style="color: #009900;">&#91;</span>sourcepos  <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>depth <span style="color: #339933;">==</span> CV_8U <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> cn<span style="color: #339933;">==</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Grayscale 8bit case</span>
&nbsp;
		<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> line <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>rows<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>line<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// In OpenCV the beginning of the lines are aligned</span>
			<span style="color: #666666; font-style: italic;">// to 4 Bytes. So use step instead of cols.</span>
			memcpy
				<span style="color: #009900;">&#40;</span>
				 self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data.<span style="color: #202020;">ptr</span> <span style="color: #339933;">+</span> line<span style="color: #339933;">*</span>step<span style="color: #339933;">,</span>
				 string <span style="color: #339933;">+</span> line<span style="color: #339933;">*</span>self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>cols<span style="color: #339933;">,</span>
				 step
				<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> depth <span style="color: #339933;">==</span> CV_32F <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// float (32bit) case</span>
		<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> line <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>rows<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>line<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// here we don not have to care about alignment as the Floats are</span>
			<span style="color: #666666; font-style: italic;">// as long as the alignment</span>
			memcpy
				<span style="color: #009900;">&#40;</span>
				 self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data.<span style="color: #202020;">ptr</span> <span style="color: #339933;">+</span> line<span style="color: #339933;">*</span>step<span style="color: #339933;">,</span>
				 string <span style="color: #339933;">+</span> line<span style="color: #339933;">*</span>self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>cols<span style="color: #339933;">*</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				 step
				<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> depth <span style="color: #339933;">==</span> CV_64F <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// double (64bit) case</span>
		<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> line <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>rows<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>line<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// here we don not have to care about alignment as the Floats are</span>
			<span style="color: #666666; font-style: italic;">// as long as the alignment</span>
			memcpy
				<span style="color: #009900;">&#40;</span>
				 self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data.<span style="color: #202020;">ptr</span> <span style="color: #339933;">+</span> line<span style="color: #339933;">*</span>step<span style="color: #339933;">,</span>
				 string <span style="color: #339933;">+</span> line<span style="color: #339933;">*</span>self<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>cols<span style="color: #339933;">*</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				 step
				<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>An example makefile that works on my system:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">CFLAGS</span>= <span style="color: #000000; font-weight: bold;">`</span>pkg-config <span style="color: #660033;">--cflags</span> opencv<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">LDFLAGS</span>= <span style="color: #000000; font-weight: bold;">`</span>pkg-config <span style="color: #660033;">--libs</span> opencv<span style="color: #000000; font-weight: bold;">`</span> -L.
<span style="color: #007800;">CXX</span>=<span style="color: #c20cb9; font-weight: bold;">g++</span>
&nbsp;
_iplimage.so: iplimage.c
	swig <span style="color: #660033;">-python</span> iplimage.i
	$<span style="color: #7a0874; font-weight: bold;">&#40;</span>CXX<span style="color: #7a0874; font-weight: bold;">&#41;</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span>CFLAGS<span style="color: #7a0874; font-weight: bold;">&#41;</span> -I<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>include<span style="color: #000000; font-weight: bold;">/</span>python2.5 <span style="color: #660033;">-c</span> iplimage.c iplimage_wrap.c
	$<span style="color: #7a0874; font-weight: bold;">&#40;</span>CXX<span style="color: #7a0874; font-weight: bold;">&#41;</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span>LDFLAGS<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #660033;">-shared</span> iplimage.o iplimage_wrap.o <span style="color: #660033;">-o</span> _iplimage.so</pre></div></div>

<p>If you manage to build the module, you can then invoke it from python using a call to <em>iplimage.set(image,data)</em> to set the imageData field for real. You can use the default OpenCV Python bindings that come with your package manager and simply install this small additional module. Hopefully, we&#8217;ll see a new release of OpenCV soon that negates the need for any of these acrobatics.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.depthfirstsearch.net/blog/2008/09/22/opencv-and-python/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>On Python</title>
		<link>http://www.depthfirstsearch.net/blog/2008/01/19/on-python/</link>
		<comments>http://www.depthfirstsearch.net/blog/2008/01/19/on-python/#comments</comments>
		<pubDate>Sun, 20 Jan 2008 04:21:10 +0000</pubDate>
		<dc:creator>JS</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.depthfirstsearch.net/2008/01/19/on-python/</guid>
		<description><![CDATA[I no longer choose to use Python. One day I decided to spend a couple of hours parallelizing some code for a new dual core machine. Than I discovered the GIL. From the Python list circa 2004: I've said it before. One day enough people will think that the GIL is a problem big enough [...]]]></description>
			<content:encoded><![CDATA[<p>I no longer choose to use Python. One day I decided to spend a couple of hours parallelizing some code for a new dual core machine. Than I discovered the GIL. From the Python list circa 2004:</p>
<blockquote>
<pre><a href="http://mail.python.org/pipermail/python-list/2004-October/286885.html">I've said it before.  One day enough people will think that the GIL is
a problem big enough to warrant a solution, e.g., when the majority of
systems where CPython runs have more than one CPU.  Until then we have
to go back to early 90s programming and use IPC (interprocess
communication) to scale applications that want to run PURE python code
on more than one CPU.</a></pre>
</blockquote>
<p>That day has come to pass.</p>
<p>UPDATE: Though the lack of true threads is still a concern, I&#8217;ve found working with the latest iteration of numpy to be much easier than alternatives like Matlab or R. The truth: syntax matters.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.depthfirstsearch.net/blog/2008/01/19/on-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parallel Python</title>
		<link>http://www.depthfirstsearch.net/blog/2007/09/16/parallel-python/</link>
		<comments>http://www.depthfirstsearch.net/blog/2007/09/16/parallel-python/#comments</comments>
		<pubDate>Mon, 17 Sep 2007 03:50:58 +0000</pubDate>
		<dc:creator>JS</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[!concurrency]]></category>
		<category><![CDATA[!perl]]></category>
		<category><![CDATA[rpc is slow]]></category>

		<guid isPermaLink="false">http://www.depthfirstsearch.net/2007/09/16/parallel-python/</guid>
		<description><![CDATA[I once tried to parallelize some Python code to take advantage of multiple processors, so I learned about the Global Interpreter Lock the hard way. Threads are supposed to allow for concurrency without protection. If you want concurrency and protection, use processes. The GIL is just a half-baked half measure between these two easy to [...]]]></description>
			<content:encoded><![CDATA[<p>I once tried to parallelize some Python code to take advantage of multiple processors, so I learned about the Global Interpreter Lock the hard way. Threads are supposed to allow for concurrency without protection. If you want concurrency and protection, use processes. The GIL is just a half-baked half measure between these two easy to state abstractions. That was the beginning of my path away from Python.</p>
<p>Anyway, for those still interested: <a href="http://www.artima.com/forums/flat.jsp?forum=106&amp;thread=214303">Parallel Python</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.depthfirstsearch.net/blog/2007/09/16/parallel-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Continuation Passing Style &#8211; Twisted</title>
		<link>http://www.depthfirstsearch.net/blog/2007/06/26/continuation-passing-style-twisted/</link>
		<comments>http://www.depthfirstsearch.net/blog/2007/06/26/continuation-passing-style-twisted/#comments</comments>
		<pubDate>Wed, 27 Jun 2007 01:25:52 +0000</pubDate>
		<dc:creator>JS</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[cps]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[twisted]]></category>

		<guid isPermaLink="false">http://www.depthfirstsearch.net/?p=282</guid>
		<description><![CDATA[Is deferred a use of continuation passing style? In a sense. continuation is a representation of some of the execution state of a program Adding deferred callbacks defines a sequence of continuations. At each step, the deferred process calls the next function in the callback sequence using the data returned from the previous call. It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Is <a href="http://twistedmatrix.com/projects/core/documentation/howto/defer.html">deferred</a> a use of continuation passing style? In a sense.</p>
<blockquote><p><a href="http://en.wikipedia.org/wiki/Continuations"><strong>continuation</strong> is a representation of some of the execution state of a program</a></p></blockquote>
<p>Adding deferred callbacks defines a sequence of continuations. At each step, the deferred process calls the next function in the callback sequence using the data returned from the previous call. It&#8217;s not true CPS since the callbacks themselves don&#8217;t take continuations as parameters.</p>
<p>I like to think of it as CPS-light. Completely useless &#8211; until you need it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.depthfirstsearch.net/blog/2007/06/26/continuation-passing-style-twisted/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 1/46 queries in 1.599 seconds using disk: basic
Object Caching 676/777 objects using disk: basic

Served from: www.depthfirstsearch.net @ 2012-02-05 06:39:38 -->
