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

<channel>
	<title>the occasional occurrence</title>
	<atom:link href="http://blog.dowski.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dowski.com</link>
	<description>Unfortunately, Christian had a Thwart, and the Magpie stayed in play.</description>
	<pubDate>Fri, 12 Mar 2010 17:49:56 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tagged Revision Log Messages in Mercurial</title>
		<link>http://blog.dowski.com/2010/03/12/tagged-revision-log-messages-in-mercurial/</link>
		<comments>http://blog.dowski.com/2010/03/12/tagged-revision-log-messages-in-mercurial/#comments</comments>
		<pubDate>Fri, 12 Mar 2010 17:49:56 +0000</pubDate>
		<dc:creator>christian</dc:creator>
		
		<category><![CDATA[General]]></category>

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

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

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

		<guid isPermaLink="false">http://blog.dowski.com/?p=355</guid>
		<description><![CDATA[I work at YouGov and we use the Mercurial for revision control.
We tag every release in a MAJOR.MINOR.BUGFIX format.  2.32.2 for example.
Recently I wanted to get a summary of the commit log messages for each of the tagged revisions.  Here is the magic command that got me the output that I wanted:

# get [...]]]></description>
			<content:encoded><![CDATA[<p>I work at <a href="http://www.polimetrix.com">YouGov</a> and we use the <a href="http://mercurial.selenic.com/">Mercurial</a> for revision control.</p>
<p>We tag every release in a MAJOR.MINOR.BUGFIX format.  2.32.2 for example.</p>
<p>Recently I wanted to get a summary of the commit log messages for each of the tagged revisions.  Here is the magic command that got me the output that I wanted:</p>
<pre>
# get all log entries for releases tagged with a 2.31 MAJOR.MINOR
hg tags | grep 2\.31 | cut -c33-36 | xargs -L1 hg log -r
</pre>
<p>cw</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dowski.com/2010/03/12/tagged-revision-log-messages-in-mercurial/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Evolution of a Haskell Function</title>
		<link>http://blog.dowski.com/2010/03/11/evolution-of-a-haskell-function/</link>
		<comments>http://blog.dowski.com/2010/03/11/evolution-of-a-haskell-function/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 17:23:56 +0000</pubDate>
		<dc:creator>christian</dc:creator>
		
		<category><![CDATA[General]]></category>

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

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

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

		<guid isPermaLink="false">http://blog.dowski.com/?p=345</guid>
		<description><![CDATA[I'm going through <a href="http://www.realworldhaskell.org/">Real World Haskell</a> trying to get a handle on the <a href="http://www.haskell.org">Haskell</a> programming language.  <a href="http://www.python.org">Python</a> is my current language of choice, but I like to learn new programming languages too.

So last night I was going over the chapter that introduces 'let', 'where', 'case' and guards and I wanted to try them out.  I contrived a simple situation where I thought I could use them.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m going through <a href="http://book.realworldhaskell.org/">Real World Haskell</a> trying to get a handle on the <a href="http://www.haskell.org">Haskell</a> programming language.  <a href="http://www.python.org">Python</a> is my current language of choice, but I like to learn new programming languages too.</p>
<p>So last night I was going over <a href="http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html">the chapter that introduces &#8216;let&#8217;, &#8216;where&#8217;, &#8216;case&#8217; and guards</a> and I wanted to try them out.  I contrived a simple situation where I thought I could use them.</p>
<p><span id="more-345"></span></p>
<p>I made some Contestants and wanted to see if they were &#8220;valid&#8221; for some definition of the word.  I chose that they had to have a real value for age (not <code>Missing</code>) and be 30 years old or older (I&#8217;m pretty sure the game is called &#8220;Who Wants to Be a 30-year-old Computer Nerd?&#8221;).</p>
<p>Here is my implementation.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #5d478b; font-style: italic;">-- my parameterized type</span>
<span style="color: #06c; font-weight: bold;">data</span> Perhaps a = Is a
               | Missing
                 <span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Show</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- definition of a Contestant</span>
<span style="color: #06c; font-weight: bold;">data</span> Contestant = Contestant <span style="color: green;">&#123;</span>
                    name ::  <span style="color: #cccc00; font-weight: bold;">String</span>,
                    age  ::  Perhaps <span style="color: #cccc00; font-weight: bold;">Int</span>
                <span style="color: green;">&#125;</span> <span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Show</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- some contestants</span>
contestant1 = Contestant <span style="background-color: #3cb371;">&quot;Christian&quot;</span> <span style="color: green;">&#40;</span>Is <span style="color: red;">31</span><span style="color: green;">&#41;</span>
contestant2 = Contestant <span style="background-color: #3cb371;">&quot;Sarah&quot;</span> Missing
contestant3 = Contestant <span style="background-color: #3cb371;">&quot;Curtis&quot;</span> <span style="color: green;">&#40;</span>Is <span style="color: red;">6</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- the validation logic</span>
contestantValid :: Contestant -&gt; <span style="color: #cccc00; font-weight: bold;">Bool</span>
contestantValid c = 
    <span style="color: #06c; font-weight: bold;">case</span> age c <span style="color: #06c; font-weight: bold;">of</span>
        Missing      -&gt; False
        Is true_age  -&gt; oldenough true_age
            <span style="color: #06c; font-weight: bold;">where</span>
                oldenough a
                    | a &gt;= <span style="color: red;">30</span> = True
                    | a &lt; <span style="color: red;">30</span>  = False
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- some code to print the results of running the validation against contestants</span>
main = <span style="color: #06c; font-weight: bold;">do</span>
    <span style="font-weight: bold;">print</span> $ <span style="color: green;">&#40;</span>contestant1, contestantValid contestant1<span style="color: green;">&#41;</span>
    <span style="font-weight: bold;">print</span> $ <span style="color: green;">&#40;</span>contestant2, contestantValid contestant2<span style="color: green;">&#41;</span>
    <span style="font-weight: bold;">print</span> $ <span style="color: green;">&#40;</span>contestant3, contestantValid contestant3<span style="color: green;">&#41;</span></pre></div></div>

<p>I was pretty proud of myself.  I used &#8216;case&#8217;, &#8216;where&#8217; and guards (not to mention parameterized types - I reimplemented Maybe/Just/Nothing as Perhaps/Is/Missing).</p>
<p>But man, it seemed like a lot of code to do something simple.</p>
<p>As I was falling asleep I realized how I could simplify it.  I could use pattern matching to easily handle the <code>Missing</code> case.  It would also let me pull the <code>age</code> out of a Contestant with ease too.</p>
<p>Here is the <code>contestantValid</code> function updated to reflect that.</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="haskell">contestantValid :: Contestant -&gt; <span style="color: #cccc00; font-weight: bold;">Bool</span>
contestantValid <span style="color: green;">&#40;</span>Contestant _ Missing<span style="color: green;">&#41;</span> = False
contestantValid <span style="color: green;">&#40;</span>Contestant _ <span style="color: green;">&#40;</span>Is age<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
                                | age &gt;= <span style="color: red;">30</span> = True
                                | age &lt; <span style="color: red;">30</span>  = False</pre></div></div>

<p>Quite a bit simpler.  I patted myself on the back.  It was less fancy than the first version, but at least I had some guards in there.</p>
<p>Then it hit me.  I didn&#8217;t need the guards.</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="haskell">contestantValid :: Contestant -&gt; <span style="color: #cccc00; font-weight: bold;">Bool</span>
contestantValid <span style="color: green;">&#40;</span>Contestant _ Missing<span style="color: green;">&#41;</span> = False
contestantValid <span style="color: green;">&#40;</span>Contestant _ <span style="color: green;">&#40;</span>Is age<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> = age &gt;= <span style="color: red;">30</span></pre></div></div>

<p>Well, so much for &#8216;where&#8217;, &#8216;case&#8217; and guards.  It sure is a boring function now, but I think the process of how I got there is interesting.</p>
<p>cw</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dowski.com/2010/03/11/evolution-of-a-haskell-function/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Link: PiCloud Overview</title>
		<link>http://blog.dowski.com/2010/03/11/link-picloud-overview/</link>
		<comments>http://blog.dowski.com/2010/03/11/link-picloud-overview/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 14:50:31 +0000</pubDate>
		<dc:creator>christian</dc:creator>
		
		<category><![CDATA[General]]></category>

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

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

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

		<guid isPermaLink="false">http://blog.dowski.com/?p=341</guid>
		<description><![CDATA[Here&#8217;s a great overview of using PiCloud that goes beyond &#8220;hello world&#8221; type stuff.
For those of you who don&#8217;t know, PiCloud is a cloud computing platform for Python that aims to simplify the task of running code in &#8220;the cloud.&#8221;
cw
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a <a href="http://chmullig.com/2010/03/picloud-introduction/">great overview of using PiCloud</a> that goes beyond &#8220;hello world&#8221; type stuff.</p>
<p>For those of you who don&#8217;t know, <a href="http://www.picloud.com">PiCloud</a> is a cloud computing platform for Python that aims to simplify the task of running code in &#8220;the cloud.&#8221;</p>
<p>cw</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dowski.com/2010/03/11/link-picloud-overview/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Aubrey</title>
		<link>http://blog.dowski.com/2010/01/06/aubrey/</link>
		<comments>http://blog.dowski.com/2010/01/06/aubrey/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 17:04:41 +0000</pubDate>
		<dc:creator>christian</dc:creator>
		
		<category><![CDATA[Family]]></category>

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

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

		<guid isPermaLink="false">http://blog.dowski.com/?p=333</guid>
		<description><![CDATA[Our family is so excited and blessed to welcome Aubrey into the world.

She was born this morning at 7:11am EST.  She weighed in at 8lbs. 1oz. and is 19&#8243; long.  Mom and baby are doing well.  I&#8217;m doing great!  
cw
]]></description>
			<content:encoded><![CDATA[<p>Our family is so excited and blessed to welcome Aubrey into the world.</p>
<p><img src="http://lh4.ggpht.com/_9gjL_waMKiA/S0TABTqKQ8I/AAAAAAAAAtM/GuBiQeMKm80/s400/IMG_3210.JPG" alt="Baby Aubrey" /></p>
<p>She was born this morning at 7:11am EST.  She weighed in at 8lbs. 1oz. and is 19&#8243; long.  Mom and baby are doing well.  I&#8217;m doing great! <img src='http://blog.dowski.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>cw</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dowski.com/2010/01/06/aubrey/feed/</wfw:commentRss>
		</item>
		<item>
		<title>On Tree Houses and Software</title>
		<link>http://blog.dowski.com/2009/11/12/on-tree-houses-and-software/</link>
		<comments>http://blog.dowski.com/2009/11/12/on-tree-houses-and-software/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 04:43:46 +0000</pubDate>
		<dc:creator>christian</dc:creator>
		
		<category><![CDATA[General]]></category>

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

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

		<guid isPermaLink="false">http://blog.dowski.com/?p=309</guid>
		<description><![CDATA[From the ShopTalk blog:
The Minimum Viable Tree House 
A case-study in what not to do with your software project.
]]></description>
			<content:encoded><![CDATA[<p>From the <a href="http://shoptalkapp.com/">ShopTalk</a> blog:</p>
<p><a href="http://shoptalkapp.com/blog/2009/11/11/the-minimum-viable-tree-house">The Minimum Viable Tree House</a> </p>
<p>A case-study in what not to do with your software project.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dowski.com/2009/11/12/on-tree-houses-and-software/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Beautiful Coroutines</title>
		<link>http://blog.dowski.com/2009/10/20/beautiful-coroutines/</link>
		<comments>http://blog.dowski.com/2009/10/20/beautiful-coroutines/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 21:47:01 +0000</pubDate>
		<dc:creator>christian</dc:creator>
		
		<category><![CDATA[General]]></category>

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

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

		<guid isPermaLink="false">http://blog.dowski.com/?p=306</guid>
		<description><![CDATA[Some guy on some other blog wrote this.  His name sounds familiar &#8230;
Beautiful Coroutines: Cooperative Concurrency in Python using Diesel
cw
]]></description>
			<content:encoded><![CDATA[<p>Some guy on some other blog wrote this.  His name sounds familiar &#8230;</p>
<p><a href="http://shoptalkapp.com/blog/2009/10/20/beautiful-coroutines">Beautiful Coroutines: Cooperative Concurrency in Python using Diesel</a></p>
<p>cw</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dowski.com/2009/10/20/beautiful-coroutines/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Diesel</title>
		<link>http://blog.dowski.com/2009/09/23/diesel/</link>
		<comments>http://blog.dowski.com/2009/09/23/diesel/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 17:20:13 +0000</pubDate>
		<dc:creator>christian</dc:creator>
		
		<category><![CDATA[General]]></category>

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

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

		<guid isPermaLink="false">http://blog.dowski.com/?p=301</guid>
		<description><![CDATA[I&#8217;ve been working with some friends on getting a startup off the ground.  We just released one of the core libraries that our software is built on.  
Announcing Diesel!
cw
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with some friends on getting a <a href="http://shoptalkapp.com">startup</a> off the ground.  We just released one of the core libraries that our software is built on.  </p>
<p><a href="http://shoptalkapp.com/blog/2009/9/23/announcing-diesel">Announcing Diesel!</a></p>
<p>cw</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dowski.com/2009/09/23/diesel/feed/</wfw:commentRss>
		</item>
		<item>
		<title>What I did this past weekend</title>
		<link>http://blog.dowski.com/2009/09/21/what-i-did-this-past-weekend/</link>
		<comments>http://blog.dowski.com/2009/09/21/what-i-did-this-past-weekend/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 02:48:25 +0000</pubDate>
		<dc:creator>christian</dc:creator>
		
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blog.dowski.com/?p=297</guid>
		<description><![CDATA[This past weekend, I enriched some soil.  I know it sounds cool, but really, it was freakin&#8217; awesome.  
I started with light brown dusty dirt in a &#8220;flower bed&#8221;.  It might have even been tainted with nuclear waste.  Nothing grows in it.  Ants prefer to make their nests in the [...]]]></description>
			<content:encoded><![CDATA[<p>This past weekend, I enriched some soil.  I know it sounds cool, but really, it was freakin&#8217; awesome.  </p>
<p>I started with light brown dusty dirt in a &#8220;flower bed&#8221;.  It might have even been tainted with nuclear waste.  Nothing grows in it.  Ants prefer to make their nests in the concrete sidewalk adjacent to it.</p>
<p>So I started in on the dirt.  What did I start in on it with?  A real-deal pick ax.  Oh.  Yeah.  I didn&#8217;t let up either.  OK, I did let up, a few times in fact.  That dirt was hard.  Harder than the hands of any of the Cleveland Browns&#8217; wide receivers.  <strong>That hard.</strong></p>
<p>After I was done with the pick ax, I started in with the shovel.  I loaded the dirt into a wheel barrow.  I pushed the wheel barrow into the woods.  I dumped the dirt.  I proceeded to iterate over these steps until at least a ton (weight) of the dirt was in a pile in the woods.  </p>
<p>If I were a retired guy prone to exaggeration and I was watching me, I&#8217;m pretty sure I would have said &#8220;That guy must have hauled twenty, maybe thirty tons of dirt!  He was speed shiftin&#8217;!&#8221;</p>
<p>Let&#8217;s take stock of the situation.  In the front yard I have &#8230; less dirt.  In the woods I have &#8230; more dirt.  Aha, but also in the woods, I have my secret weapon.  An even bigger pile of leaves, from last fall.  They have been lying in wait, decomposing, for just such a moment as this.</p>
<p>Now I haul wheel barrows full of dirt/decayed leaves to the front.  Load after load.  Iterations.  Piles of dirt/decayed leaves.  I spread the dirt/decayed leaves in the &#8220;flower bed&#8221;.</p>
<p>It used to be light brown, almost sand colored.  Now it is brown like coffee grounds - or decomposed leaves - and it is enriched.</p>
<p>cw</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dowski.com/2009/09/21/what-i-did-this-past-weekend/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Crushed</title>
		<link>http://blog.dowski.com/2009/06/27/crushed/</link>
		<comments>http://blog.dowski.com/2009/06/27/crushed/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 05:48:52 +0000</pubDate>
		<dc:creator>christian</dc:creator>
		
		<category><![CDATA[General]]></category>

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

		<guid isPermaLink="false">http://blog.dowski.com/?p=288</guid>
		<description><![CDATA[&#8220;Julio!&#8221;
There was no answer. 
Though drenched with sweat, the summer heat was no match for the desperation of a father for his son.  Racing through the field, the leaves on the cornstalks met Gustavo&#8217;s skin like slender green blades.  He didn&#8217;t even notice the pain as he closed the gap between himself and [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;Julio!&#8221;</p>
<p>There was no answer. </p>
<p>Though drenched with sweat, the summer heat was no match for the desperation of a father for his son.  Racing through the field, the leaves on the cornstalks met Gustavo&#8217;s skin like slender green blades.  He didn&#8217;t even notice the pain as he closed the gap between himself and where he had last seen his son.</p>
<p>As he burst free from field where he toiled, he nearly toppled over the children.  Julio&#8217;s playmates were frantic.  Tears streamed down their faces carrying the the dust of the pasture from their eyes. Gustavo hardly halted for a second.</p>
<p>He had already heard their cries from a long distance off, and he had heard the cry of his son.</p>
<p>The pasture wasn&#8217;t far now.  Gustavo&#8217;s heart filled with a mixture of fear, love and rage as his eyes settled on the dark shape ahead beside the fence row.</p>
<p>Cavernous black eyes laced with red veins met the eyes of the overwhelmed father.  The now giant bull that he had raised from a calf stood above the limp, bloodied figure lying in the dust.</p>
<p>&#8230;</p>
<p>&#8220;Father!&#8221;</p>
<p>The cry was one of desperation.  The thunder of the hooves behind him surely drowned out his small voice.  He glanced to his right and for a brief moment felt relief at the sight of his friends sliding under the fence to safety.</p>
<p>&#8220;This shouldn&#8217;t be,&#8221; the primal thoughts that raced through Julio&#8217;s mind told him.  The animal must have broken free from its paddock and entered the previously empty pasture that was the children&#8217;s playground for the day.</p>
<p>It happened suddenly.  The great horn hooked his light frame and sent him hurtling through the hazy air.  The scene of the pasture swirled around him.  Then the crunch of his back against the fencepost and the taste of blood and dirt in his mouth.</p>
<p>Before he could recover the hooves and horns of the great beast met him with blow after crushing blow.  No more cries he made, only the involuntary sounds of air being forced from his now-beaten body.</p>
<p>The pain blanketed him like the heat from the midday sun blanketed the countryside.</p>
<p>&#8220;I&#8217;m finished,&#8221; he barely thought as consciousness mercifully began to depart him.  Darkness was closing in all around.</p>
<p>A terrible sound tore across the pasture and reached the ears of the fading child.  It was familiar to him.  Again it sounded, cutting through the darkness of his consciousness like a sickle through wheat.  Rather than adding to his misery, the deafening sound comforted Julio.</p>
<p>His father had arrived.</p>
<p>&#8230;</p>
<p>Before the first shells were finished dancing in the dust near his feet, Gustavo unleashed another round upon the treasured animal, now turned murderer.  It reared away in fear and pain and stumbled to the ground a short distance from the child.  The nearby ground shook as the great beast fell to the dust.</p>
<p>Casting his weapon aside, Gustavo cleared the fence and was at once by the side of his son.</p>
<p>&#8230;</p>
<p>Julio felt the whole world begin to shake, as visions of the farm where he lived flashed before his eyes.  With his final act of consciousness before slipping into the darkness, he realized that he was in the arms of his father, who was rushing him off to undoubted safety.  </p>
<p>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dowski.com/2009/06/27/crushed/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Odd Old-Style vs. New-Style Class Behavior</title>
		<link>http://blog.dowski.com/2009/05/21/odd-old-style-vs-new-style-class-behavior/</link>
		<comments>http://blog.dowski.com/2009/05/21/odd-old-style-vs-new-style-class-behavior/#comments</comments>
		<pubDate>Thu, 21 May 2009 14:55:20 +0000</pubDate>
		<dc:creator>christian</dc:creator>
		
		<category><![CDATA[General]]></category>

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

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

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

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

		<guid isPermaLink="false">http://blog.dowski.com/?p=279</guid>
		<description><![CDATA[So we have some older Python code at work that uses old-style classes.  We usually try and bring those up to date when we encounter them.
The other day one of the developers did that and one of our tests started failing.  A simple change from:

class Foo:
    # stuff here

to:

class Foo&#40;object&#41;:
 [...]]]></description>
			<content:encoded><![CDATA[<p>So we have some older Python code at work that uses old-style classes.  We usually try and bring those up to date when we encounter them.</p>
<p>The other day one of the developers did that and one of our tests started failing.  A simple change from:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">class</span> Foo:
    <span style="color: #808080; font-style: italic;"># stuff here</span></pre></div></div>

<p>to:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">class</span> Foo<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># stuff here</span></pre></div></div>

<p>was all that happened.</p>
<p>Here is some code that encapsulates the problem and runs with some interesting results:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Two nearly identical classes with quite different behavior.&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> LameContainerOld:
    <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>._items = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'bar'</span>:<span style="color: #483d8b;">'test'</span><span style="color: black;">&#125;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__getitem__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._items<span style="color: black;">&#91;</span>name<span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__getattr__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, attr<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._items, attr<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> LameContainerNew<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>._items = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'bar'</span>:<span style="color: #483d8b;">'test'</span><span style="color: black;">&#125;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__getitem__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._items<span style="color: black;">&#91;</span>name<span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__getattr__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, attr<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._items, attr<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> cls <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span>LameContainerOld, LameContainerNew<span style="color: black;">&#93;</span>:
        container = cls<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Testing&quot;</span>, cls
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            <span style="color: #483d8b;">'foo'</span> <span style="color: #ff7700;font-weight:bold;">in</span> container
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>Membership in %s raised %r!&quot;</span> % <span style="color: black;">&#40;</span>container.__class__, e<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>Membership in %s worked!&quot;</span> % container.__class__
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>%s&quot;</span> % container.<span style="color: #0000cd;">__getitem__</span></pre></div></div>

<p>Here is some output from running that:</p>
<pre>
Testing __main__.LameContainerOld
        Membership in __main__.LameContainerOld worked!
        &lt;bound method LameContainerOld.__getitem__ of {'bar': 'test'}&gt;
Testing &lt;class '__main__.LameContainerNew'&gt;
        Membership in &lt;class '__main__.LameContainerNew'&gt; raised KeyError(0,)!
        &lt;bound method LameContainerNew.__getitem__ of &lt;__main__.LameContainerNew object at 0xb7d1c1ac&gt;&gt;
</pre>
<p>From what I can tell, when a membership test happens on the old-style instance, a membership test is done on <code>self._items</code> and it returns <code>False</code>.  When the membership test happens on the new-style instance, it tries to treat it like a sequence and calls the <code>__getitem__</code> method with index 0.</p>
<p>Does that seem like a correct analysis?  Does anyone know why the behavior is different there?  </p>
<p>Also look at the output for printing <code>container.__getitem__</code>.  Isn&#8217;t <code>__getattr__</code> only supposed to be called when the attribute isn&#8217;t present on the instance?  Why does it return the <code>__getitem__</code> method of <code>self._items</code> for the old-style instance then?</p>
<p>Very puzzling.</p>
<p>cw</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dowski.com/2009/05/21/odd-old-style-vs-new-style-class-behavior/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
