<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Trey Hunner]]> - Python</title>
  <link href="http://treyhunner.com/atom.xml" rel="self"/>
  <link href="http://treyhunner.com/"/>
  <updated>2017-01-01T16:49:28-08:00</updated>
  <id>http://treyhunner.com/</id>
  <author>
    <name><![CDATA[Trey Hunner]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[The Iterator Protocol: How for Loops Work in Python]]></title>
    <link href="http://treyhunner.com/2016/12/python-iterator-protocol-how-for-loops-work/"/>
    <updated>2016-12-28T11:00:00-08:00</updated>
    <id>http://treyhunner.com/2016/12/python-iterator-protocol-how-for-loops-work</id>
    <content type="html"><![CDATA[<p>We&rsquo;re interviewing for a job and our interviewer has asked us to remove all <code>for</code> loops from a block of code.  They then mentioned something about iterators and cackled maniacally while rapping their fingers on the table.  We&rsquo;re nervous and frustrated about being assigned this ridiculous task, but we&rsquo;ll try our best.</p>

<p>To understand how to loop without a <code>for</code> loop, we&rsquo;ll need to discover what makes <code>for</code> loops tick.</p>

<p>We&rsquo;re about to learn how <code>for</code> loops work in Python.  Along the way we&rsquo;ll need to learn about iterables, iterators, and the iterator protocol.  Let&rsquo;s loop. ➿</p>

<h2>Looping with indexes: a failed attempt</h2>

<p>We might initially try to remove our <code>for</code> loops by using a traditional looping idiom from the world of C: <a href="http://treyhunner.com/2016/04/how-to-loop-with-indexes-in-python/">looping with indexes</a>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;blue&quot;</span><span class="p">,</span> <span class="s">&quot;purple&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'><span class="k">while</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">colors</span><span class="p">):</span>
</span><span class='line'>    <span class="k">print</span><span class="p">(</span><span class="n">colors</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
</span><span class='line'>    <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
</span></code></pre></td></tr></table></div></figure>


<p>This works on lists, but it fails on sets:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='pycon'><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">colors</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;blue&quot;</span><span class="p">,</span> <span class="s">&quot;purple&quot;</span><span class="p">}</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="k">while</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">colors</span><span class="p">):</span>
</span><span class='line'><span class="gp">... </span>    <span class="k">print</span><span class="p">(</span><span class="n">colors</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
</span><span class='line'><span class="gp">... </span>    <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
</span><span class='line'><span class="gp">...</span>
</span><span class='line'><span class="gt">Traceback (most recent call last):</span>
</span><span class='line'>  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">2</span>, in <span class="n">&lt;module&gt;</span>
</span><span class='line'><span class="gr">TypeError</span>: <span class="n">&#39;set&#39; object does not support indexing</span>
</span></code></pre></td></tr></table></div></figure>


<p>This approach only works on <a href="https://docs.python.org/3/glossary.html#term-sequence">sequences</a>, which are data types that have indexes from <code>0</code> to one less than their length.  Lists, strings, and tuples are sequences.  Dictionaries, sets, and many other <em>iterables</em> are not <em>sequences</em>.</p>

<p>We&rsquo;ve been instructed to implement a looping construct that works on <em>all iterables</em>, not just sequences.</p>

<h2>Iterables: what are they?</h2>

<p>In the Python world, an <strong>iterable</strong> is any object that <strong>you can loop over with a for loop</strong>.</p>

<p><a href="https://docs.python.org/3/glossary.html#term-iterable">Iterables</a> are not always indexable, they don&rsquo;t always have lengths, and they&rsquo;re not always finite.</p>

<p>Here&rsquo;s an <em>infinite</em> iterable which provides every multiple of 5 as you loop over it:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">count</span>
</span><span class='line'><span class="n">multiples_of_five</span> <span class="o">=</span> <span class="n">count</span><span class="p">(</span><span class="n">step</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>When we were using <code>for</code> loops, we could have looped over the beginning of this iterable like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">multiples_of_five</span><span class="p">:</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">n</span> <span class="o">&gt;</span> <span class="mi">100</span><span class="p">:</span>
</span><span class='line'>        <span class="k">break</span>
</span><span class='line'>    <span class="k">print</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>If we removed the <code>break</code> condition from that <code>for</code> loop, it would go on printing forever.</p>

<p>So iterables can be infinitely long: which means that we can&rsquo;t always convert an iterable to a <code>list</code> (or any other sequence) before we loop over it.  We need to somehow ask our iterable for each item of our iterable individually, the same way our <code>for</code> loop works.</p>

<h2>Iterables &amp; Iterators</h2>

<p>Okay we&rsquo;ve defined <em>iterable</em>, but how do iterables actually work in Python?</p>

<p>All <a href="https://docs.python.org/3/glossary.html#term-iterable">iterables</a> can be passed to the built-in <code>iter</code> function to get an <strong>iterator</strong> from them.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='pycon'><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="nb">iter</span><span class="p">([</span><span class="s">&#39;some&#39;</span><span class="p">,</span> <span class="s">&#39;list&#39;</span><span class="p">])</span>
</span><span class='line'><span class="go">&lt;list_iterator object at 0x7f227ad51128&gt;</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="nb">iter</span><span class="p">({</span><span class="s">&#39;some&#39;</span><span class="p">,</span> <span class="s">&#39;set&#39;</span><span class="p">})</span>
</span><span class='line'><span class="go">&lt;set_iterator object at 0x7f227ad32b40&gt;</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="nb">iter</span><span class="p">(</span><span class="s">&#39;some string&#39;</span><span class="p">)</span>
</span><span class='line'><span class="go">&lt;str_iterator object at 0x7f227ad51240&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>That&rsquo;s an interesting fact but&hellip; what&rsquo;s an <em>iterator</em>?</p>

<p>Iterators have exactly one job: return the &ldquo;next&rdquo; item in our iterable.  They&rsquo;re sort of like <a href="https://en.wikipedia.org/wiki/Tally_counter">tally counters</a>, but they don&rsquo;t have a reset button and instead of giving the next number they give the next item in our iterable.</p>

<p>You can get an iterator from <em>any</em> iterable:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='pycon'><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">iterator</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="s">&#39;hi&#39;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>And iterators can be passed to <code>next</code> to get their next item:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='pycon'><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">iterator</span><span class="p">)</span>
</span><span class='line'><span class="go">&#39;h&#39;</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">iterator</span><span class="p">)</span>
</span><span class='line'><span class="go">&#39;i&#39;</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">iterator</span><span class="p">)</span>
</span><span class='line'><span class="gt">Traceback (most recent call last):</span>
</span><span class='line'>  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
</span><span class='line'><span class="gr">StopIteration</span>
</span></code></pre></td></tr></table></div></figure>


<p>So <a href="https://docs.python.org/3/glossary.html#term-iterator">iterators</a> can be passed to the built-in <code>next</code> function to get the next item from them and if there is no next item (because we reached the end), a <code>StopIteration</code> exception will be raised.</p>

<h2>Iterators are also iterables</h2>

<p>So calling <code>iter</code> on an <em>iterable</em> gives us an iterator.  And calling <code>next</code> on an <em>iterator</em> gives us the next item or raises a <code>StopIteration</code> exception if there aren&rsquo;t any more items.</p>

<p>There&rsquo;s actually a bit more to it than that though.  You can pass iterators to the built-in <code>iter</code> function to get themselves back.  That means that iterators are also iterables.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='pycon'><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">iterator</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="s">&#39;hi&#39;</span><span class="p">)</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">iterator2</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">iterator</span><span class="p">)</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">iterator</span> <span class="ow">is</span> <span class="n">iterator2</span>
</span><span class='line'><span class="go">True</span>
</span></code></pre></td></tr></table></div></figure>


<p>That fact leads to some interesting consequences that we don&rsquo;t have time to go into right now.  We&rsquo;ll save that discussion for a future learning adventure&hellip;</p>

<h2>The Iterator Protocol</h2>

<p>The <strong>iterator protocol</strong> is a fancy term meaning &ldquo;how iterables actually work in Python&rdquo;.</p>

<p>Let&rsquo;s redefine iterables from Python&rsquo;s perspective.</p>

<p>Iterables:</p>

<ol>
<li>Can be passed to the <code>iter</code> function to get an iterator for them.</li>
<li>There is no 2.  That&rsquo;s <em>really</em> all that&rsquo;s needed to be an iterable.</li>
</ol>


<p>Iterators:</p>

<ol>
<li>Can be passed to the <code>next</code> function which gives their next item or raises <code>StopIteration</code></li>
<li>Return themselves when passed to the <code>iter</code> function.</li>
</ol>


<p>The inverse of these statements should also hold true.  Which means:</p>

<ol>
<li>Anything that can be passed to <code>iter</code> without an error is an iterable.</li>
<li>Anything that can be passed to <code>next</code> without an error (except for <code>StopIteration</code>) is an iterator.</li>
<li>Anything that returns itself when passed to <code>iter</code> is an iterator.</li>
</ol>


<h2>Looping with iterators</h2>

<p>With what we&rsquo;ve learned about iterables and iterators, we should now be able to recreate a <code>for</code> loop without actually using a <code>for</code> loop.</p>

<p>This <code>while</code> loop manually loops over some <code>iterable</code>, printing out each item as it goes:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">print_each</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span>
</span><span class='line'>    <span class="n">iterator</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span>
</span><span class='line'>    <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
</span><span class='line'>        <span class="k">try</span><span class="p">:</span>
</span><span class='line'>            <span class="n">item</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">iterator</span><span class="p">)</span>
</span><span class='line'>        <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
</span><span class='line'>            <span class="k">break</span>  <span class="c"># Iterator exhausted: stop the loop</span>
</span><span class='line'>        <span class="k">else</span><span class="p">:</span>
</span><span class='line'>            <span class="k">print</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>We can call this function with any iterable and it will loop over it:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='pycon'><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">print_each</span><span class="p">({</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">})</span>
</span><span class='line'><span class="go">1</span>
</span><span class='line'><span class="go">2</span>
</span><span class='line'><span class="go">3</span>
</span></code></pre></td></tr></table></div></figure>


<p>The above function is essentially the same as this one which uses a <code>for</code> loop:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">print_each</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
</span><span class='line'>        <span class="k">print</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This <code>for</code> loop is automatically doing what we were doing manually: calling <code>iter</code> to get an iterator and then calling <code>next</code> over and over until a <code>StopIteration</code> exception is raised.</p>

<p>The iterator protocol is used by <code>for</code> loops, tuple unpacking, and all built-in functions that work on generic iterables.  Using the iterator protocol (either manually or automatically) is the only universal way to loop over any iterable in Python.</p>

<h2>For loops: more complex than they seem</h2>

<p>We&rsquo;re now ready to complete the very silly task our interviewer assigned to us.  We&rsquo;ll remove all <code>for</code> loops from our code by manually using <code>iter</code> and <code>next</code> to loop over iterables.  What did we learn in exploring this task?</p>

<p>Everything you can loop over is an <strong>iterable</strong>.  Looping over iterables works via getting an <strong>iterator</strong> from an iterable and then repeatedly asking the iterator for the next item.</p>

<p>The way iterators and iterables work is called the <strong>iterator protocol</strong>.  List comprehensions, tuple unpacking, <code>for</code> loops, and all other forms of iteration rely on the iterator protocol.</p>

<p>I&rsquo;ll explore iterators more in future articles.  For now know that iterators are hiding behind the scenes of all iteration in Python.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Check Whether All Items Match a Condition in Python]]></title>
    <link href="http://treyhunner.com/2016/11/check-whether-all-items-match-a-condition-in-python/"/>
    <updated>2016-11-29T09:45:00-08:00</updated>
    <id>http://treyhunner.com/2016/11/check-whether-all-items-match-a-condition-in-python</id>
    <content type="html"><![CDATA[<p>In this article, we&rsquo;re going to look at a common programming pattern and discuss how we can refactor our code when we notice this pattern. 🏗</p>

<p>We&rsquo;ll be discussing how to make code with this shape a little more descriptive:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">all_good</span> <span class="o">=</span> <span class="bp">True</span>
</span><span class='line'><span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
</span><span class='line'>    <span class="k">if</span> <span class="ow">not</span> <span class="n">condition</span><span class="p">(</span><span class="n">item</span><span class="p">):</span>
</span><span class='line'>        <span class="n">all_good</span> <span class="o">=</span> <span class="bp">False</span>
</span><span class='line'>        <span class="k">break</span>
</span></code></pre></td></tr></table></div></figure>


<h2>An Example: Primality</h2>

<p>Here&rsquo;s a function that checks whether a given number is prime by trying to divide it by all numbers below it:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>        <span class="k">if</span> <span class="n">candidate</span> <span class="o">%</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span><span class='line'>            <span class="k">return</span> <span class="bp">False</span>
</span><span class='line'>    <span class="k">return</span> <span class="bp">True</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>Note</strong>: a <a href="http://stackoverflow.com/questions/5811151/why-do-we-check-upto-the-square-root-of-a-prime-number-to-determine-if-it-is-pri#5811176">square root</a> makes this faster and our code breaks below <code>2</code> but we&rsquo;ll ignore those issues here</p>

<p>This function:</p>

<ol>
<li>loops from 2 to the given number</li>
<li>returns <code>False</code> as soon as a divisor is found</li>
<li>returns <code>True</code> if no divisor was found</li>
</ol>


<p>This primality check is asking &ldquo;do any numbers evenly divide the candidate number&rdquo;.</p>

<p>Note that this function <strong>returns as soon as it finds a divisor</strong>, so it <em>only</em> iterates all the way through the number range when the candidate number is prime.</p>

<p>Let&rsquo;s take a look at how we can rewrite this function using <code>all</code>.</p>

<h2>What&rsquo;s <code>all</code>?</h2>

<p>Python has a built-in function <code>all</code> that returns <code>True</code> if all items are <strong>truthy</strong></p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='pycon'><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="nb">all</span><span class="p">([</span><span class="s">&#39;hello, &#39;</span><span class="n">there</span><span class="s">&#39;])</span>
</span><span class='line'><span class="go">True</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="nb">all</span><span class="p">([</span><span class="s">&#39;hello, &#39;</span><span class="n">there</span><span class="s">&#39;, &#39;&#39;])</span>
</span><span class='line'><span class="go">False</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="nb">all</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
</span><span class='line'><span class="go">True</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="nb">all</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
</span><span class='line'><span class="go">False</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can think of truthy as meaning non-empty or non-zero (Python chat on <a href="https://www.crowdcast.io/e/truthiness">truthiness</a>).  For our purposes, we&rsquo;ll treat it as pretty much the same as <code>True</code>.</p>

<p>The <code>all</code> built-in function is equivalent to this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">all</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
</span><span class='line'>        <span class="k">if</span> <span class="ow">not</span> <span class="n">element</span><span class="p">:</span>
</span><span class='line'>            <span class="k">return</span> <span class="bp">False</span>
</span><span class='line'>    <span class="k">return</span> <span class="bp">True</span>
</span></code></pre></td></tr></table></div></figure>


<p>Notice the similarity between <code>all</code> and our <code>is_prime</code> function?  Our <code>is_prime</code> function is similar, but they&rsquo;re not quite the same structure.</p>

<p>The <code>all</code> function checks for the truthiness of <code>element</code>, but we need something a little more than that: we need to check a condition on each element (whether it&rsquo;s a divsior).</p>

<h2>Using <code>all</code></h2>

<p>Our original <code>is_prime</code> function looks like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>        <span class="k">if</span> <span class="n">candidate</span> <span class="o">%</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span><span class='line'>            <span class="k">return</span> <span class="bp">False</span>
</span><span class='line'>    <span class="k">return</span> <span class="bp">True</span>
</span></code></pre></td></tr></table></div></figure>


<p>If we want to use <code>all</code> in this function, we need an iterable (like a list) to pass to <code>all</code>.</p>

<p>If we wanted to be really silly, we could make such a list of boolean values like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>  <span class="n">divisibility</span> <span class="o">=</span> <span class="p">[]</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>        <span class="k">if</span> <span class="n">candidate</span> <span class="o">%</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span><span class='line'>            <span class="n">divisibility</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
</span><span class='line'>        <span class="k">else</span><span class="p">:</span>
</span><span class='line'>            <span class="n">divisibility</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
</span><span class='line'>  <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="n">divisibility</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>We could simplify this function like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>  <span class="n">divisibility</span> <span class="o">=</span> <span class="p">[]</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>      <span class="n">divisibility</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">candidate</span> <span class="o">%</span> <span class="n">n</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'>  <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="n">divisibility</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>I know this is probably doesn&rsquo;t seem like progress, but bear with me for a few more steps&hellip;</p>

<h2>List comprehensions</h2>

<p>If you&rsquo;re familiar with list comprehensions, this code structure might look a little familiar.  We&rsquo;re creating one iterable from another which is exactly what list comprehensions are good for.</p>

<p>Let&rsquo;s copy-paste our way into a list comprehension (see my article on <a href="http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/">how to write list comprehensions</a>):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>  <span class="n">divisibility</span> <span class="o">=</span> <span class="p">[</span>
</span><span class='line'>      <span class="n">candidate</span> <span class="o">%</span> <span class="n">n</span> <span class="o">!=</span> <span class="mi">0</span>
</span><span class='line'>      <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">candidate</span><span class="p">)</span>
</span><span class='line'>  <span class="p">]</span>
</span><span class='line'>  <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="n">divisibility</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>That&rsquo;s quite a bit shorter, but there&rsquo;s a problem: we&rsquo;re <strong>building up an entire list just to loop over it once</strong>!</p>

<p>This is less efficient than our original approach, which only looped all the way when <code>candidate</code> was prime.</p>

<p>Let&rsquo;s fix this inefficiency by turning our list comprehension into a generator expression.</p>

<h2>Generator expressions</h2>

<p>A generator expression is like a list comprehension, but instead of making a list it makes a <strong>generator</strong> (Python chat on <a href="https://www.crowdcast.io/e/generators">generators</a>).</p>

<p>A generator is a <strong>lazy iterable</strong>: generators don&rsquo;t compute the items they contain until you loop over them.  We&rsquo;ll see what that means in a moment.</p>

<p>We can turn our list comprehension into a generator expression by changing the brackets to parentheses:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>  <span class="n">divisibility</span> <span class="o">=</span> <span class="p">(</span>
</span><span class='line'>      <span class="n">candidate</span> <span class="o">%</span> <span class="n">n</span> <span class="o">!=</span> <span class="mi">0</span>
</span><span class='line'>      <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">candidate</span><span class="p">)</span>
</span><span class='line'>  <span class="p">)</span>
</span><span class='line'>  <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="n">divisibility</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now our code doesn&rsquo;t create a list to loop over.  Instead it provides us with a generator that allows us to compute the divisibility of each number one-by-one.</p>

<p>We can make this code even more readable by putting that generator expression inside the function call (notice that we can drop the second set of parentheses):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>  <span class="k">return</span> <span class="nb">all</span><span class="p">(</span>
</span><span class='line'>      <span class="n">candidate</span> <span class="o">%</span> <span class="n">n</span> <span class="o">!=</span> <span class="mi">0</span>
</span><span class='line'>      <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">candidate</span><span class="p">)</span>
</span><span class='line'>    <span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that because our generator is lazy, we stop computing divisibilities as soon as our <code>all</code> function finds a divisible number.  So we end up calculating <code>candidate % n != 0</code> only as many times as we did in our original function.</p>

<h2>Recap</h2>

<p>So we started with a <code>for</code> loop, an <code>if</code> statement, a <code>return</code> statement for stopping once we find a divisor, and a <code>return</code> statement for the case where our number had no divisors (when it&rsquo;s prime).</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>        <span class="k">if</span> <span class="n">candidate</span> <span class="o">%</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span><span class='line'>            <span class="k">return</span> <span class="bp">False</span>
</span><span class='line'>    <span class="k">return</span> <span class="bp">True</span>
</span></code></pre></td></tr></table></div></figure>


<p>We turned all that into a generator expression passed to the <code>all</code> function.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">all</span><span class="p">(</span>
</span><span class='line'>        <span class="n">candidate</span> <span class="o">%</span> <span class="n">n</span> <span class="o">!=</span> <span class="mi">0</span>
</span><span class='line'>        <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">candidate</span><span class="p">)</span>
</span><span class='line'>    <span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>I prefer this second approach (a generator expression with <code>all</code>) because I find it <strong>more descriptive</strong>.</p>

<p>We&rsquo;re checking to see whether &ldquo;all numbers in a range are not divisors of our candidate number&rdquo;.  That sounds quite a bit more like English to me than &ldquo;loop over all numbers in a range and return False if a divisor is found otherwise return True&rdquo;.</p>

<p>If you don&rsquo;t find the behavior of <code>all</code> intuitive, you might find it easier to understand (and more English-like) when used with <code>if</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">if</span> <span class="nb">all</span><span class="p">(</span><span class="n">condition</span><span class="p">(</span><span class="n">item</span><span class="p">)</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">):</span>
</span><span class='line'>    <span class="n">message</span> <span class="o">=</span> <span class="s">&quot;All good&quot;</span>
</span><span class='line'><span class="k">else</span><span class="p">:</span>
</span><span class='line'>    <span class="n">message</span> <span class="o">=</span> <span class="s">&quot;Bad value found&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can always reformat your code to use an <code>if</code> statement if you find it more readable.</p>

<h2><code>any</code> or <code>all</code></h2>

<p>We&rsquo;ve been working with the <code>all</code> function, but I haven&rsquo;t mentioned it&rsquo;s counterpart: the <code>any</code> function.  Let&rsquo;s take a look at how <code>all</code> and <code>any</code> compare.</p>

<p>These two expressions:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">all_good</span> <span class="o">=</span> <span class="nb">all</span><span class="p">(</span>
</span><span class='line'>    <span class="n">condition</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">things</span>
</span><span class='line'><span class="p">)</span>
</span><span class='line'><span class="n">some_bad</span> <span class="o">=</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span>
</span><span class='line'>    <span class="n">condition</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">things</span>
</span><span class='line'><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Are equivalent to these two expressions (because of <a href="https://en.wikipedia.org/wiki/De_Morgan%27s_laws">DeMorgan&rsquo;s Laws</a>):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">all_good</span> <span class="o">=</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span>
</span><span class='line'>    <span class="ow">not</span> <span class="n">condition</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">things</span>
</span><span class='line'><span class="p">)</span>
</span><span class='line'><span class="n">some_bad</span> <span class="o">=</span> <span class="nb">any</span><span class="p">(</span>
</span><span class='line'>    <span class="ow">not</span> <span class="n">condition</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">things</span>
</span><span class='line'><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>So this code:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">all</span><span class="p">(</span>
</span><span class='line'>        <span class="n">candidate</span> <span class="o">%</span> <span class="n">n</span> <span class="o">!=</span> <span class="mi">0</span>
</span><span class='line'>        <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">candidate</span><span class="p">)</span>
</span><span class='line'>    <span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Is feature-identical to this code:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">candidate</span><span class="p">):</span>
</span><span class='line'>    <span class="k">return</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span>
</span><span class='line'>        <span class="n">candidate</span> <span class="o">%</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span>
</span><span class='line'>        <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">candidate</span><span class="p">)</span>
</span><span class='line'>    <span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Both of them stop as soon as they find a divisor.</p>

<p>I find the use of <code>all</code> more readable here, but I wanted to mention that <code>any</code> would work just as well.</p>

<h2>Cheat sheet for refactoring with <code>any</code> and <code>all</code></h2>

<p>All that explanation above was valuable, but how can we use this new knowledge to refactor our own code?  Here&rsquo;s a cheat sheet for you.</p>

<p>Anytime you see code like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">all_good</span> <span class="o">=</span> <span class="bp">True</span>
</span><span class='line'><span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
</span><span class='line'>    <span class="k">if</span> <span class="ow">not</span> <span class="n">condition</span><span class="p">(</span><span class="n">item</span><span class="p">):</span>
</span><span class='line'>        <span class="n">all_good</span> <span class="o">=</span> <span class="bp">False</span>
</span><span class='line'>        <span class="k">break</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can replace that code with this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">all_good</span> <span class="o">=</span> <span class="nb">all</span><span class="p">(</span>
</span><span class='line'>    <span class="n">condition</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">iterable</span>
</span><span class='line'><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Anytime you see code like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">any_good</span> <span class="o">=</span> <span class="bp">False</span>
</span><span class='line'><span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">condition</span><span class="p">(</span><span class="n">item</span><span class="p">):</span>
</span><span class='line'>        <span class="n">any_good</span> <span class="o">=</span> <span class="bp">True</span>
</span><span class='line'>        <span class="k">break</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can replace it with this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">any_good</span> <span class="o">=</span> <span class="nb">any</span><span class="p">(</span>
</span><span class='line'>    <span class="n">condition</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">iterable</span>
</span><span class='line'><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that <code>break</code> is used in the code above because we&rsquo;re not returning from a function.  Using <code>return</code> (like we did in <code>is_prime</code>) is another way to stop our loop early.</p>

<p>Python&rsquo;s <code>any</code> and <code>all</code> functions were <em>made</em> for use with generator expressions (discussion <a href="https://mail.python.org/pipermail/python-dev/2005-March/thread.html#52010">here</a> and <a href="https://mail.python.org/pipermail/python-dev/2005-March/thread.html#52010">here</a>).  You can use <code>any</code> and <code>all</code> without generator expressions, but I don&rsquo;t find a need for that as often.</p>

<p><strong>Quick note</strong>: <code>any(item == 'something' for item in iterable)</code> is the same as <code>'something' in iterable</code>.  Don&rsquo;t use <code>all</code>/<code>any</code> for checking containment, use <code>in</code>.</p>

<h2>Conclusion: code style in a process</h2>

<p>As you discover new Python idioms and new language features are invented, your code style will evolve.  Your preferred code style may never stop evolving.  Code style is not concrete: it&rsquo;s a process.</p>

<p>I hope I&rsquo;ve inspired you to embrace the use of <code>any</code>/<code>all</code> with generator expressions for improved readability and code clarity.</p>

<p>Have a question about code style?  Have a thought about <code>any</code>, <code>all</code>, and generator expressions?  Please <a href="http://twitter.com/treyhunner">tweet me</a>, <a href="mailto:hello@truthful.technology">email me</a>, or comment below. 😄</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Weekly Python Chat: Live From PyCon]]></title>
    <link href="http://treyhunner.com/2016/05/weekly-python-chat-live-from-pycon/"/>
    <updated>2016-05-23T09:00:00-07:00</updated>
    <id>http://treyhunner.com/2016/05/weekly-python-chat-live-from-pycon</id>
    <content type="html"><![CDATA[<p><strong>TL;DR</strong>: If you&rsquo;ve never been to PyCon and are curious what the big deal is about, sign up for the <a href="http://ccst.io/e/pycon">PyCon Day 1 live chat</a> and <a href="http://ccst.io/e/sprints">PyCon Sprints live chat</a>.</p>

<p>I have been holding live webcasts every week for almost 2 months now.  I started this trend after my <a href="http://treyhunner.com/2016/03/regular-expressions-in-python-webinar/">regular expressions webinar</a> in March.  I soon came up with a name and made a website for these <a href="https://twitter.com/treyhunner/status/720758046117343236">weekly python chat</a> events.  Now there&rsquo;s also a <a href="https://twitter.com/PythonChat">Twitter account</a> and a <a href="https://www.facebook.com/PythonChat">Facebook page</a>.</p>

<h2>Guest speakers and other experimentation</h2>

<p>I&rsquo;ve really enjoyed holding these events.  The audience participation has been great: ample questions and plenty of helpful chat adding on to the discussion and occasionally correcting my mistakes.</p>

<p>I&rsquo;ve been experimenting with the chat format by bringing in guest speakers the last couple weeks and I plan to introduce more general topics occasionally in the future.</p>

<h2>Web chats about PyCon, live from PyCon</h2>

<p>Next week I&rsquo;ll be continuing my experimentation by hosting two Weekly Python Chat events <strong>live from PyCon</strong>.</p>

<p>The first chat next week will be during the first day of PyCon.  I will likely be in the hallway accompanied by a couple other Python friends.  We&rsquo;ll answer your questions about what there is to do at PyCon, how it&rsquo;s different from other cons, and why we go.</p>

<p>The second chat will be during the first day of the sprints.  We&rsquo;ll chat about who the sprints are for, how new contributors can get involved with the sprints, and what makes the sprints rewarding.</p>

<h2>I want to convince you to join me at PyCon 2017</h2>

<p><img src="http://treyhunner.com/images/pycon-badge.jpg"></p>

<p>If you&rsquo;ve never attended the sprints, sign up for the second chat to ask your questions and state your concerns.  Hopefully I can convince you to stay for the sprints next time.</p>

<p>If you&rsquo;ve never been to PyCon, sign up for both chats and voice your questions and concerns in each.  I will address your questions and concerns, even if you can&rsquo;t make the live event.  <strong>Both chats will be recorded</strong> and you can re-watch them afterward.</p>

<h3>Chat 1: Live from Day 1 of PyCon</h3>

<p><strong>Monday</strong> May 30, 2016 at <strong>3:30pm</strong> PDT</p>

<p>We&rsquo;ll discuss <strong>what PyCon is all about</strong>.</p>

<p><strong><a href="http://ccst.io/e/pycon">Sign up here to attend the PyCon Day 1 live chat</a></strong></p>

<h3>Chat 2: Live from the PyCon sprints</h3>

<p><strong>Thursday</strong> June 2, 2016 at <strong>11:00am</strong> PDT</p>

<p>We&rsquo;ll chat about <strong>how the sprints work</strong>.</p>

<p><strong><a href="http://ccst.io/e/sprints">Sign up here to attend the PyCon Sprints live chat</a></strong></p>

<h2>Get in touch</h2>

<p>Have questions?  Want to share your PyCon experiences during the live chat?  Going to PyCon next week and want to meet up?  <a href="mailto:tr%65y%40&#116;%72%75&#116;%68%66u&#108;&#46;&#116;&#101;c%68&#110;%6flo%67%79">Contact me</a>!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to Loop With Indexes in Python]]></title>
    <link href="http://treyhunner.com/2016/04/how-to-loop-with-indexes-in-python/"/>
    <updated>2016-04-25T09:00:00-07:00</updated>
    <id>http://treyhunner.com/2016/04/how-to-loop-with-indexes-in-python</id>
    <content type="html"><![CDATA[<p>If you&rsquo;re moving to Python from C or Java, you might be confused by Python&rsquo;s <code>for</code> loops.  <strong>Python doesn&rsquo;t actually have for loops</strong>&hellip; at least not the same kind of <code>for</code> loop that C-based languages have.  Python&rsquo;s <code>for</code> loops are actually <a href="https://en.wikipedia.org/wiki/Foreach_loop">foreach loops</a>.</p>

<p>In this article I&rsquo;ll compare Python&rsquo;s <code>for</code> loops to those of other languages and discuss the usual ways we solve common problems with <code>for</code> loops in Python.</p>

<h2>For loops in other languages</h2>

<p>Before we look at Python&rsquo;s loops, let&rsquo;s take a look at a for loop in JavaScript:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;red&quot;</span><span class="p">,</span> <span class="s2">&quot;green&quot;</span><span class="p">,</span> <span class="s2">&quot;blue&quot;</span><span class="p">,</span> <span class="s2">&quot;purple&quot;</span><span class="p">];</span>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">colors</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">colors</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This JavaScript loop looks nearly identical in C/C++ and Java.</p>

<p>In this loop we:</p>

<ol>
<li>Set a counter variable <code>i</code> to 0</li>
<li>Check if the counter is less than the array length</li>
<li>Execute the code in the loop <em>or</em> exit the loop if the counter is too high</li>
<li>Increment the counter variable by 1</li>
</ol>


<h2>Looping in Python</h2>

<p>Now let&rsquo;s talk about loops in Python.  First we&rsquo;ll look at two slightly more familiar looping methods and then we&rsquo;ll look at the idiomatic way to loop in Python.</p>

<h3>while</h3>

<p>If we wanted to mimic the behavior of our traditional C-style <code>for</code> loop in Python, we could use a <code>while</code> loop:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;blue&quot;</span><span class="p">,</span> <span class="s">&quot;purple&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'><span class="k">while</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">colors</span><span class="p">):</span>
</span><span class='line'>    <span class="k">print</span><span class="p">(</span><span class="n">colors</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
</span><span class='line'>    <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
</span></code></pre></td></tr></table></div></figure>


<p>This involves the same 4 steps as the <code>for</code> loops in other languages (note that we&rsquo;re setting, checking, and incrementing <code>i</code>) but it&rsquo;s not quite as compact.</p>

<p>This method of looping in Python is very uncommon.</p>

<h3>range of length</h3>

<p>I often see new Python programmers attempt to recreate traditional <code>for</code> loops in a slightly more creative fashion in Python:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;blue&quot;</span><span class="p">,</span> <span class="s">&quot;purple&quot;</span><span class="p">]</span>
</span><span class='line'><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">colors</span><span class="p">)):</span>
</span><span class='line'>    <span class="k">print</span><span class="p">(</span><span class="n">colors</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
</span></code></pre></td></tr></table></div></figure>


<p>This first creates a range corresponding to the indexes in our list (<code>0</code> to <code>len(colors) - 1</code>).  We can loop over this range using Python&rsquo;s for-in loop (really a <a href="https://en.wikipedia.org/wiki/Foreach_loop">foreach</a>).</p>

<p>This provides us with the index of each item in our <code>colors</code> list, which is the same way that C-style <code>for</code> loops work.  To get the actual color, we use <code>colors[i]</code>.</p>

<h3>for-in: the usual way</h3>

<p>Both the while loop and range-of-len methods rely on looping over indexes.  But we don&rsquo;t actually care about the indexes: we&rsquo;re only using these indexes for the purpose of retrieving elements from our list.</p>

<p>Because we don&rsquo;t actually care about the indexes in our loop, there is <strong>a much simpler method of looping</strong> we can use:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;blue&quot;</span><span class="p">,</span> <span class="s">&quot;purple&quot;</span><span class="p">]</span>
</span><span class='line'><span class="k">for</span> <span class="n">color</span> <span class="ow">in</span> <span class="n">colors</span><span class="p">:</span>
</span><span class='line'>    <span class="k">print</span><span class="p">(</span><span class="n">color</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>So instead of retrieving the item indexes and looking up each element, we can just loop over our list using a plain for-in loop.</p>

<p>The other two methods we discussed are sometimes referred to as <a href="https://en.wikipedia.org/wiki/Anti-pattern">anti-patterns</a> because they are programming patterns which are widely considered unidiomatic.</p>

<h2>What if we need indexes?</h2>

<p>What if we actually need the indexes?  For example, let&rsquo;s say we&rsquo;re printing out president names along with their numbers (based on list indexes).</p>

<h3>range of length</h3>

<p>We could use <code>range(len(our_list))</code> and then lookup the index like before:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">presidents</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;Washington&quot;</span><span class="p">,</span> <span class="s">&quot;Adams&quot;</span><span class="p">,</span> <span class="s">&quot;Jefferson&quot;</span><span class="p">,</span> <span class="s">&quot;Madison&quot;</span><span class="p">,</span> <span class="s">&quot;Monroe&quot;</span><span class="p">,</span> <span class="s">&quot;Adams&quot;</span><span class="p">,</span> <span class="s">&quot;Jackson&quot;</span><span class="p">]</span>
</span><span class='line'><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">presidents</span><span class="p">)):</span>
</span><span class='line'>    <span class="k">print</span><span class="p">(</span><span class="s">&quot;President {}: {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">presidents</span><span class="p">[</span><span class="n">i</span><span class="p">]))</span>
</span></code></pre></td></tr></table></div></figure>


<p>But there&rsquo;s a more idiomatic way to accomplish this task: use the <code>enumerate</code> function.</p>

<h3>enumerate</h3>

<p>Python&rsquo;s built-in <code>enumerate</code> function allows us to loop over a list and retrieve both the index and the value of each item in the list:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">presidents</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;Washington&quot;</span><span class="p">,</span> <span class="s">&quot;Adams&quot;</span><span class="p">,</span> <span class="s">&quot;Jefferson&quot;</span><span class="p">,</span> <span class="s">&quot;Madison&quot;</span><span class="p">,</span> <span class="s">&quot;Monroe&quot;</span><span class="p">,</span> <span class="s">&quot;Adams&quot;</span><span class="p">,</span> <span class="s">&quot;Jackson&quot;</span><span class="p">]</span>
</span><span class='line'><span class="k">for</span> <span class="n">num</span><span class="p">,</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">presidents</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
</span><span class='line'>    <span class="k">print</span><span class="p">(</span><span class="s">&quot;President {}: {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>enumerate</code> function gives us an iterable where each element is a tuple that contains the index of the item and the original item value.</p>

<p>This function is meant for solving the task of:</p>

<ol>
<li>Accessing each item in a list (or another iterable)</li>
<li>Also getting the index of each item accessed</li>
</ol>


<p>So whenever we need item indexes while looping, we should think of <code>enumerate</code>.</p>

<p><strong>Note</strong>: the <code>start=1</code> option to <code>enumerate</code> here is optional.  If we didn&rsquo;t specify this, we&rsquo;d start counting at <code>0</code> by default.</p>

<h2>What if we need to loop over multiple things?</h2>

<p>Often when we use list indexes, it&rsquo;s to look something up in another list.</p>

<h3>enumerate</h3>

<p>For example, here we&rsquo;re looping over two lists at the same time using indexes to look up corresponding elements:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;blue&quot;</span><span class="p">,</span> <span class="s">&quot;purple&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">ratios</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">]</span>
</span><span class='line'><span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">color</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">colors</span><span class="p">):</span>
</span><span class='line'>    <span class="n">ratio</span> <span class="o">=</span> <span class="n">ratios</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
</span><span class='line'>    <span class="k">print</span><span class="p">(</span><span class="s">&quot;{}% {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ratio</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="n">color</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that we only need the index in this scenario because we&rsquo;re using it to lookup elements at the same index in our second list.  What we really want is to loop over two lists simultaneously: the indexes just provide a means to do that.</p>

<h3>zip</h3>

<p>We don&rsquo;t actually care about the index when looping here.  Our real goal is to loop over two lists at once.  This need is common enough that there&rsquo;s a special built-in function just for this.</p>

<p>Python&rsquo;s <code>zip</code> function allows us to <strong>loop over multiple lists at the same time</strong>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;blue&quot;</span><span class="p">,</span> <span class="s">&quot;purple&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">ratios</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">]</span>
</span><span class='line'><span class="k">for</span> <span class="n">color</span><span class="p">,</span> <span class="n">ratio</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">colors</span><span class="p">,</span> <span class="n">ratios</span><span class="p">):</span>
</span><span class='line'>    <span class="k">print</span><span class="p">(</span><span class="s">&quot;{}% {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ratio</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="n">color</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>zip</code> function takes multiple lists and returns an iterable that provides a tuple of the corresponding elements of each list as we loop over it.</p>

<p>Note that <code>zip</code> with different size lists will stop after the shortest list runs out of items.  You may want to look into <a href="https://docs.python.org/3/library/itertools.html#itertools.zip_longest">itertools.zip_longest</a> if you need different behavior.  Also note that <code>zip</code> in Python 2 returns a list but <code>zip</code> in Python 3 returns a lazy iterable.  In Python 2, <code>itertools.izip</code> is equivalent to the newer Python 3 <code>zip</code> function.</p>

<h2>Looping cheat sheet</h2>

<p>Here&rsquo;s a very short looping cheat sheet that might help you remember the preferred construct for each of these three looping scenarios.</p>

<p>Loop over a single list with a regular for-in:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">numbers</span><span class="p">:</span>
</span><span class='line'>    <span class="k">print</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Loop over multiple lists at the same time with <code>zip</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">for</span> <span class="n">header</span><span class="p">,</span> <span class="n">rows</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">headers</span><span class="p">,</span> <span class="n">columns</span><span class="p">):</span>
</span><span class='line'>    <span class="k">print</span><span class="p">(</span><span class="s">&quot;{}: {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">header</span><span class="p">,</span> <span class="s">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">rows</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Loop over a list while keeping track of indexes with <code>enumerate</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">for</span> <span class="n">num</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">lines</span><span class="p">):</span>
</span><span class='line'>    <span class="k">print</span><span class="p">(</span><span class="s">&quot;{0:03d}: {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">line</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<h2>In Summary</h2>

<p>If you find yourself tempted to use <code>range(len(my_list))</code> or a loop counter, think about whether you can reframe your problem to allow usage of <code>zip</code> or <code>enumerate</code> (or a combination of the two).</p>

<p>In fact, if you find yourself reaching for <code>enumerate</code>, think about whether you actually need indexes at all.  It&rsquo;s quite rare to need indexes in Python.</p>

<ol>
<li>If you need to loop over multiple lists at the same time, use <code>zip</code></li>
<li>If you only need to loop over a single list just use a for-in loop</li>
<li>If you need to loop over a list and you need item indexes, use <code>enumerate</code></li>
</ol>


<p>If you find yourself struggling to figure out the best way to loop, try using the cheat sheet above.</p>

<p>For more a more detailed explanation of the fundamentals of looping in Python, see Ned Batchelder&rsquo;s <a href="http://nedbatchelder.com/text/iter.html">Loop Like a Native</a> presentation.</p>

<p>Thanks <a href="http://lost-theory.org/">Steven Kryskalla</a> and <a href="http://purplediane.github.io/">Diane Chen</a> for proof-reading this post.</p>

<p><strong>Update</strong>: If you&rsquo;re interested in learning how to make your own Python objects that can be looped over, you may want to watch the <a href="https://www.crowdcast.io/e/operator-overloading">operator overloading chat</a> I held on April 30, 2016.</p>

<p>Happy looping!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Webinar: Regular Expressions in Python]]></title>
    <link href="http://treyhunner.com/2016/03/regular-expressions-in-python-webinar/"/>
    <updated>2016-03-15T15:07:32-07:00</updated>
    <id>http://treyhunner.com/2016/03/regular-expressions-in-python-webinar</id>
    <content type="html"><![CDATA[<p>Don&rsquo;t understand how regular expressions <strong>work</strong>?</p>

<p>Having trouble making your regular expressions <strong>readable</strong>?</p>

<p>I&rsquo;m doing a 90 minute webinar to show you how to write <strong>readable regular expressions</strong>.</p>

<p>You can <a href="http://regex.eventbrite.com/?aff=blog">sign up here</a>.</p>

<p><strong>May 2015 Update</strong>: the webinar was recorded and you can watch it by signing up at the same link.  The second half of this webinar event will be held this Saturday May 7 at 9am Pacific Time and is also accessible from the same link.</p>

<h2>What&rsquo;s a webinar?</h2>

<p>Is this like a seminar?  Aren&rsquo;t seminars boring?  Seminars can be boring but I&rsquo;m hoping this webinar will be fun.</p>

<p>&ldquo;Webinar&rdquo; doesn&rsquo;t sound cool, but I prefer it over &ldquo;Wwworkshop&rdquo;.</p>

<p>This will basically be like <strong>an online workshop</strong>.  I&rsquo;ll demonstrate some concepts through live coding and explanations and we&rsquo;ll take a couple breaks to work through exercises together.  There will be a chat room so we can discuss the concepts and share our answers for each of the exercises.</p>

<p>I do plan to do more of these so if you have a better suggestion for what to call this kind of thing than &ldquo;webinar&rdquo;, I&rsquo;m all ears.</p>

<h2>What will we learn?</h2>

<p>We&rsquo;ll learn about using regular expressions for validating text and for searching within text.  We&rsquo;ll cover the basic regular expression syntax and how to use use regular expressions in Python in particular.</p>

<p>Most importantly, we will discuss <strong>how to make your regular expressions readable</strong>.</p>

<p>I will be reviewing the basics of regular expressions, so if you&rsquo;ve never used regular expressions before you should be able to follow along.</p>

<h2>Will there be any follow-up to this?</h2>

<p>Yes!  I&rsquo;m planning a second half to this webinar which will also be 90 minutes long.  I&rsquo;ll announce the date and time to attendees of part 1.</p>

<p>There is more to regular expressions than validation and searching.</p>

<p>Here are some things we will not cover in part 1, but which I hope to go over in part 2:</p>

<ul>
<li>Substitutions</li>
<li>Data normalization</li>
<li>Greediness</li>
<li>Lookahead/lookbehind</li>
</ul>


<h2>When is this?</h2>

<p>March 26 at 9am Pacific Time.</p>

<p>That&rsquo;s:</p>

<ul>
<li>11am in Chicago</li>
<li>12pm in New York</li>
<li>4pm in London</li>
<li>5pm in Berlin</li>
<li>6pm in Kyiv</li>
<li>7pm in Moscow</li>
<li>9:30pm in Bengaluru</li>
<li>midnight in Perth</li>
</ul>


<p><a href="http://www.timeanddate.com/worldclock/fixedtime.html?msg=Regular+Expressions+in+Python&amp;iso=20160326T09&amp;p1=770&amp;ah=1&amp;am=30">Find out what time that is in your time zone</a></p>

<h2>How much does this cost?</h2>

<p>$500.</p>

<p>Just kidding.</p>

<p>It&rsquo;s free.</p>

<h2>Where can I sign up?</h2>

<p>You can sign up at <a href="http://regex.eventbrite.com/?aff=blog">regex.eventbrite.com</a>.</p>

<p>I set the registration cap at 100 and as of this writing, 49 tickets have been claimed.</p>

<p>I did a similar event (on list comprehensions) for PyLadies Remote in the past and it went well.</p>

<p>I plan to do more of these kinds of events in the future.  I&rsquo;ll announce events to my email list first.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The Idiomatic Way to Merge Dictionaries in Python]]></title>
    <link href="http://treyhunner.com/2016/02/how-to-merge-dictionaries-in-python/"/>
    <updated>2016-02-23T10:00:00-08:00</updated>
    <id>http://treyhunner.com/2016/02/how-to-merge-dictionaries-in-python</id>
    <content type="html"><![CDATA[<p>Have you ever wanted to combine two or more dictionaries in Python?</p>

<p>There are multiple ways to solve this problem: some are awkward, some are inaccurate, and most require multiple lines of code.</p>

<p>Let&rsquo;s walk through the different ways of solving this problem and discuss which is the most <a href="https://docs.python.org/3/glossary.html#term-pythonic">Pythonic</a>.</p>

<h2>Our Problem</h2>

<p>Before we can discuss solutions, we need to clearly define our problem.</p>

<p>Our code has two dictionaries: <code>user</code> and <code>defaults</code>.  We want to merge these two dictionaries into a new dictionary called <code>context</code>.</p>

<p>We have some requirements:</p>

<ol>
<li><code>user</code> values should override <code>defaults</code> values in cases of duplicate keys</li>
<li>keys in <code>defaults</code> and <code>user</code> may be any valid keys</li>
<li>the values in <code>defaults</code> and <code>user</code> can be anything</li>
<li><code>defaults</code> and <code>user</code> should not change during the creation of <code>context</code></li>
<li>updates made to <code>context</code> should never alter <code>defaults</code> or <code>user</code></li>
</ol>


<p><strong>Note</strong>: In 5, we&rsquo;re focused on updates to the dictionary, not contained objects.  For concerns about mutability of nested objects, we should look into <a href="https://docs.python.org/3/library/copy.html#copy.deepcopy">copy.deepcopy</a>.</p>

<p>So we want something like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='pycon'><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">user</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;name&#39;</span><span class="p">:</span> <span class="s">&quot;Trey&quot;</span><span class="p">,</span> <span class="s">&#39;website&#39;</span><span class="p">:</span> <span class="s">&quot;http://treyhunner.com&quot;</span><span class="p">}</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;name&#39;</span><span class="p">:</span> <span class="s">&quot;Anonymous User&quot;</span><span class="p">,</span> <span class="s">&#39;page_name&#39;</span><span class="p">:</span> <span class="s">&quot;Profile Page&quot;</span><span class="p">}</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">context</span> <span class="o">=</span> <span class="n">merge_dicts</span><span class="p">(</span><span class="n">defaults</span><span class="p">,</span> <span class="n">user</span><span class="p">)</span>  <span class="c"># magical merge function</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">context</span>
</span><span class='line'><span class="go">{&#39;website&#39;: &#39;http://treyhunner.com&#39;, &#39;name&#39;: &#39;Trey&#39;, &#39;page_name&#39;: &#39;Profile Page&#39;}</span>
</span></code></pre></td></tr></table></div></figure>


<p>We&rsquo;ll also consider whether a solution is Pythonic.  This is a very subjective and often illusory measure.  Here are a few of the particular criteria we will use:</p>

<ul>
<li>The solution should be concise but not terse</li>
<li>The solution should be readable but not overly verbose</li>
<li>The solution should be one line if possible so it can be written inline if needed</li>
<li>The solution should not be needlessly inefficient</li>
</ul>


<h2>Possible Solutions</h2>

<p>Now that we&rsquo;ve defined our problem, let&rsquo;s discuss some possible solutions.</p>

<p>We&rsquo;re going to walk through a number of methods for merging dictionaries and discuss which of these methods is the most accurate and which is the most idiomatic.</p>

<h3>Multiple update</h3>

<p>Here&rsquo;s one of the simplest ways to merge our dictionaries:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">context</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'><span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">defaults</span><span class="p">)</span>
</span><span class='line'><span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">user</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here we&rsquo;re making an empty dictionary and using the <a href="https://docs.python.org/3.5/library/stdtypes.html#dict.update">update</a> method to add items from each of the other dictionaries.  Notice that we&rsquo;re adding <code>defaults</code> first so that any common keys in <code>user</code> will override those in <code>defaults</code>.</p>

<p>All five of our requirements were met so this is <strong>accurate</strong>.  This solution takes three lines of code and cannot be performed inline, but it&rsquo;s pretty clear.</p>

<p>Score:</p>

<ul>
<li>Accurate: yes</li>
<li>Idiomatic: fairly, but it would be nicer if it could be inlined</li>
</ul>


<h3>Copy and update</h3>

<p>Alternatively, we could copy <code>defaults</code> and update the copy with <code>user</code>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">context</span> <span class="o">=</span> <span class="n">defaults</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
</span><span class='line'><span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">user</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This solution is only slightly different from the previous one.</p>

<p>For this particular problem, I prefer this solution of copying the <code>defaults</code> dictionary to make it clear that <code>defaults</code> represents default values.</p>

<p>Score:</p>

<ul>
<li>Accurate: yes</li>
<li>Idiomatic: yes</li>
</ul>


<h3>Dictionary constructor</h3>

<p>We could also pass our dictionary to the <code>dict</code> constructor which will also copy the dictionary for us:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">context</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">defaults</span><span class="p">)</span>
</span><span class='line'><span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">user</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This solution is very similar to the previous one, but it&rsquo;s a little bit less explicit.</p>

<p>Score:</p>

<ul>
<li>Accurate: yes</li>
<li>Idiomatic: somewhat, though I&rsquo;d prefer the first two solutions over this</li>
</ul>


<h3>Keyword arguments hack</h3>

<p>You may have seen this clever answer before, <a href="http://stackoverflow.com/a/39858/98187">possibly on StackOverflow</a>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">context</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">defaults</span><span class="p">,</span> <span class="o">**</span><span class="n">user</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is just one line of code.  That&rsquo;s kind of cool.  However, this solution is a little hard to understand.</p>

<p>Beyond readability, there&rsquo;s an even bigger problem: <strong>this solution is wrong.</strong></p>

<p>The keys must be strings.  In Python 2 (with the CPython interpreter) we can get away with non-strings as keys, but don&rsquo;t be fooled: this is a hack that only works by accident in Python 2 using the standard CPython runtime.</p>

<p>Score:</p>

<ul>
<li>Accurate: no.  Requirement 2 is not met (keys may be any valid key)</li>
<li>Idiomatic: no.  This is a hack.</li>
</ul>


<h3>Dictionary comprehension</h3>

<p>Just because we can, let&rsquo;s try doing this with a dictionary comprehension:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">context</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="n">v</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="p">[</span><span class="n">defaults</span><span class="p">,</span> <span class="n">user</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This works, but this is a little hard to read.</p>

<p>If we have an unknown number of dictionaries this might be a good idea, but we&rsquo;d probably want to break our comprehension over multiple lines to make it more readable.  In our case of two dictionaries, this doubly-nested comprehension is a little much.</p>

<p>Score:</p>

<ul>
<li>Accurate: yes</li>
<li>Idiomatic: arguably not</li>
</ul>


<h3>Concatenate items</h3>

<p>What if we get a <code>list</code> of items from each dictionary, concatenate them, and then create a new dictionary from that?</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">context</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">defaults</span><span class="o">.</span><span class="n">items</span><span class="p">())</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="n">user</span><span class="o">.</span><span class="n">items</span><span class="p">()))</span>
</span></code></pre></td></tr></table></div></figure>


<p>This actually works.  We know that the <code>user</code> keys will win out over <code>defaults</code> because those keys come at the end of our concatenated list.</p>

<p>In Python 2 we actually don&rsquo;t need the <code>list</code> conversions, but we&rsquo;re working in Python 3 here (you are on Python 3, right?).</p>

<p>Score:</p>

<ul>
<li>Accurate: yes</li>
<li>Idiomatic: not particularly, there&rsquo;s a bit of repetition</li>
</ul>


<h3>Union items</h3>

<p>In Python 3, <code>items</code> is a <code>dict_items</code> object, which is a quirky object that supports union operations.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">context</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">defaults</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="o">|</span> <span class="n">user</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
</span></code></pre></td></tr></table></div></figure>


<p>That&rsquo;s kind of interesting.  But <strong>this is not accurate</strong>.</p>

<p>Requirement 1 (<code>user</code> should &ldquo;win&rdquo; over <code>defaults</code>) fails because the union of two <code>dict_items</code> objects is a <a href="https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset">set</a> of key-value pairs and sets are unordered so duplicate keys may resolve in an <em>unpredictable</em> way.</p>

<p>Requirement 3 (the values can be anything) fails because sets require their items to be <a href="https://docs.python.org/3/glossary.html#term-hashable">hashable</a> so both the keys <em>and values</em> in our key-value tuples must be hashable.</p>

<p>Side note: I&rsquo;m not sure why the union operation is even allowed on <code>dict_items</code> objects.  What is this good for?</p>

<p>Score:</p>

<ul>
<li>Accurate: no, requirements 1 and 3 fail</li>
<li>Idiomatic: no</li>
</ul>


<h3>Chain items</h3>

<p>So far the most idiomatic way we&rsquo;ve seen to perform this merge in a single line of code involves creating two lists of items, concatenating them, and forming a dictionary.</p>

<p>We can join our items together more succinctly with <a href="https://docs.python.org/3/library/itertools.html#itertools.chain">itertools.chain</a>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">chain</span>
</span><span class='line'><span class="n">context</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">chain</span><span class="p">(</span><span class="n">defaults</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span> <span class="n">user</span><span class="o">.</span><span class="n">items</span><span class="p">()))</span>
</span></code></pre></td></tr></table></div></figure>


<p>This works well and may be more efficient than creating two unnecessary lists.</p>

<p>Score:</p>

<ul>
<li>Accurate: yes</li>
<li>Idiomatic: fairly, but those <code>items</code> calls seem slightly redundant</li>
</ul>


<h3>ChainMap</h3>

<p>A <a href="https://docs.python.org/3/library/collections.html#collections.ChainMap">ChainMap</a> allows us to create a new dictionary without even looping over our initial dictionaries (well <em>sort of</em>, we&rsquo;ll discuss this):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">ChainMap</span>
</span><span class='line'><span class="n">context</span> <span class="o">=</span> <span class="n">ChainMap</span><span class="p">({},</span> <span class="n">user</span><span class="p">,</span> <span class="n">defaults</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>A <code>ChainMap</code> groups dictionaries together into a proxy object (a &ldquo;view&rdquo;); lookups query each provided dictionary until a match is found.</p>

<p>This code raises a few questions.</p>

<h4>Why did we put <code>user</code> before <code>defaults</code>?</h4>

<p>We ordered our arguments this way to ensure requirement 1 was met.  The dictionaries are searched in order, so <code>user</code> returns matches before <code>defaults</code>.</p>

<h4>Why is there an empty dictionary before <code>user</code>?</h4>

<p>This is for requirement 5.  Changes to <code>ChainMap</code> objects affect the first dictionary provided and we don&rsquo;t want <code>user</code> to change so we provided an empty dictionary first.</p>

<h4>Does this actually give us a dictionary?</h4>

<p>A <code>ChainMap</code> object is <strong>not a dictionary</strong> but it is a <strong>dictionary-like</strong> mapping.  We may be okay with this if our code practices <a href="https://docs.python.org/3/glossary.html#term-duck-typing">duck typing</a>, but we&rsquo;ll need to inspect the features of <code>ChainMap</code> to be sure.  Among other features, <code>ChainMap</code> objects are coupled to their <a href="https://gist.github.com/treyhunner/2abe2617ea029504ef8e">underlying dictionaries</a> and they handle <a href="https://gist.github.com/treyhunner/5260810b4cced03359d9">removing items</a> in an interesting way.</p>

<p>Score:</p>

<ul>
<li>Accurate: possibly, we&rsquo;ll need to consider our use cases</li>
<li>Idiomatic: yes if we decide this suits our use case</li>
</ul>


<h3>Dictionary from ChainMap</h3>

<p>If we really want a dictionary, we could convert our <code>ChainMap</code> to a dictionary:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">context</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">ChainMap</span><span class="p">(</span><span class="n">user</span><span class="p">,</span> <span class="n">defaults</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>It&rsquo;s a little odd that <code>user</code> must come before <code>defaults</code> in this code whereas this order was flipped in most of our other solutions.  Outside of that oddity, this code is fairly simple and should be clear enough for our purposes.</p>

<p>Score:</p>

<ul>
<li>Accurate: yes</li>
<li>Idiomatic: yes</li>
</ul>


<h3>Dictionary concatenation</h3>

<p>What if we simply concatenate our dictionaries?</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">context</span> <span class="o">=</span> <span class="n">defaults</span> <span class="o">+</span> <span class="n">user</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is cool, but it <strong>isn&rsquo;t valid</strong>.  This was discussed in a <a href="https://mail.python.org/pipermail/python-ideas/2015-February/031748.html">python-ideas thread</a> last year.</p>

<p>Some of the concerns brought up in this thread include:</p>

<ul>
<li>Maybe <code>|</code> makes more sense than <code>+</code> because dictionaries are like sets</li>
<li>For duplicate keys, should the left-hand side or right-hand side win?</li>
<li>Should there be an <code>updated</code> built-in instead (kind of like <a href="https://docs.python.org/3/library/functions.html#sorted">sorted</a>)?</li>
</ul>


<p>Score:</p>

<ul>
<li>Accurate: no. This doesn&rsquo;t work.</li>
<li>Idiomatic: no. This doesn&rsquo;t work.</li>
</ul>


<h3>Dictionary unpacking</h3>

<p>If you&rsquo;re using Python 3.5, thanks to <a href="https://www.python.org/dev/peps/pep-0448/">PEP 448</a>, there&rsquo;s a new way to merge dictionaries:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">context</span> <span class="o">=</span> <span class="p">{</span><span class="o">**</span><span class="n">defaults</span><span class="p">,</span> <span class="o">**</span><span class="n">user</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is simple and Pythonic.  There are quite a few symbols, but it&rsquo;s fairly clear that the output is a dictionary at least.</p>

<p>This is functionally equivalent to our very first solution where we made an empty dictionary and populated it with all items from <code>defaults</code> and <code>user</code> in turn.  All of our requirements are met and this is likely the simplest solution we&rsquo;ll ever get.</p>

<p>Score:</p>

<ul>
<li>Accurate: yes</li>
<li>Idiomatic: yes</li>
</ul>


<h2>Summary</h2>

<p>There are a number of ways to combine multiple dictionaries, but there are few elegant ways to do this with just one line of code.</p>

<p>If you&rsquo;re using Python 3.5, this is the one obvious way to solve this problem:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">context</span> <span class="o">=</span> <span class="p">{</span><span class="o">**</span><span class="n">defaults</span><span class="p">,</span> <span class="o">**</span><span class="n">user</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you are not yet using Python 3.5, you&rsquo;ll need to review the solutions above to determine which is the most appropriate for your needs.</p>

<p><strong>Note</strong>: For those of you particularly concerned with performance, I also measured the <a href="https://gist.github.com/treyhunner/f35292e676efa0be1728">performance of these different dictionary merging methods</a>.</p>

<p>If you&rsquo;re interested in deep-merging this dictionary (merging a dictionary of dictionaries for example), check out <a href="https://gist.github.com/mahmoud/db02d16ac89fa401b968">this deep merging technique</a> from Mahmoud Hashemi.</p>

<p><strong>Update</strong>: If you&rsquo;re interested in learning more about the new features of <code>*</code> and <code>**</code> in Python 3.5 and their history you may want to watch the <a href="https://www.crowdcast.io/e/unpacking">Packing &amp; Unpacking Operators chat</a> I held on April 23, 2016.</p>

<p><br>I teach Python for a living.  If you like my teaching style and your team is interested in <strong><a href="http://truthful.technology/">Python training</a></strong>, please <a href="mailto:hello@truthful.technology">contact me</a>!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Python List Comprehensions: Explained Visually]]></title>
    <link href="http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/"/>
    <updated>2015-12-01T10:30:00-08:00</updated>
    <id>http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color</id>
    <content type="html"><![CDATA[<p>Sometimes a programming design pattern becomes common enough to warrant its own special syntax.  Python&rsquo;s <a href="https://docs.python.org/3/tutorial/datastructures.html#tut-listcomps">list comprehensions</a> are a prime example of such a syntactic sugar.</p>

<p>List comprehensions in Python are great, but mastering them can be tricky because they don&rsquo;t solve a new problem: they just provide a new syntax to solve an existing problem.</p>

<p>Let&rsquo;s learn what list comprehensions are and how to identify when to use them.</p>

<p><strong>Update</strong>: I held a 1 hour <a href="http://ccst.io/e/list-comprehensions">video chat about list comprehensions</a> which extends the material in this article.  If you want more after reading this post, check out the recording.</p>

<h2>What are list comprehensions?</h2>

<p>List comprehensions are a tool for transforming one list (any <a href="https://docs.python.org/3/glossary.html#term-iterable">iterable</a> actually) into another list.  During this transformation, elements can be conditionally included in the new list and each element can be transformed as needed.</p>

<p>If you&rsquo;re familiar with functional programming, you can think of list comprehensions as syntactic sugar for a <code>filter</code> followed by a <code>map</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='pycon'><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">doubled_odds</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">,</span> <span class="n">numbers</span><span class="p">))</span>
</span><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">doubled_odds</span> <span class="o">=</span> <span class="p">[</span><span class="n">n</span> <span class="o">*</span> <span class="mi">2</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">numbers</span> <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you&rsquo;re not familiar with functional programming, don&rsquo;t worry: I&rsquo;ll explain using <code>for</code> loops.</p>

<h2>From loops to comprehensions</h2>

<p>Every list comprehension can be rewritten as a <code>for</code> loop but not every <code>for</code> loop can be rewritten as a list comprehension.</p>

<p>The key to understanding when to use list comprehensions is to practice identifying problems that <em>smell</em> like list comprehensions.</p>

<p>If you can rewrite your code to look <em>just like this <code>for</code> loop</em>, you can also rewrite it as a list comprehension:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">new_things</span> <span class="o">=</span> <span class="p">[]</span>
</span><span class='line'><span class="k">for</span> <span class="n">ITEM</span> <span class="ow">in</span> <span class="n">old_things</span><span class="p">:</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">condition_based_on</span><span class="p">(</span><span class="n">ITEM</span><span class="p">):</span>
</span><span class='line'>        <span class="n">new_things</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&quot;something with &quot;</span> <span class="o">+</span> <span class="n">ITEM</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can rewrite the above <code>for</code> loop as a list comprehension like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">new_things</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;something with &quot;</span> <span class="o">+</span> <span class="n">ITEM</span> <span class="k">for</span> <span class="n">ITEM</span> <span class="ow">in</span> <span class="n">old_things</span> <span class="k">if</span> <span class="n">condition_based_on</span><span class="p">(</span><span class="n">ITEM</span><span class="p">)]</span>
</span></code></pre></td></tr></table></div></figure>


<h2>List Comprehensions: The Animated Movie™</h2>

<p>That&rsquo;s great, but how did we do that?</p>

<p>We <strong>copy-pasted</strong> our way from a <code>for</code> loop to a list comprehension.</p>

<p><img src="http://treyhunner.com/images/list-comprehension-condition.gif"></p>

<p>Here&rsquo;s the order we copy-paste in:</p>

<ol>
<li>Copy the variable assignment for our new empty list (line 3)</li>
<li>Copy the expression that we&rsquo;ve been <code>append</code>-ing into this new list (line 6)</li>
<li>Copy the <code>for</code> loop line, excluding the final <code>:</code> (line 4)</li>
<li>Copy the <code>if</code> statement line, also without the <code>:</code> (line 5)</li>
</ol>


<p>We&rsquo;ve now copied our way from this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">numbers</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]</span>
</span><span class='line'>
</span><span class='line'><span class="n">doubled_odds</span> <span class="o">=</span> <span class="p">[]</span>
</span><span class='line'><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">numbers</span><span class="p">:</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
</span><span class='line'>        <span class="n">doubled_odds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">n</span> <span class="o">*</span> <span class="mi">2</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>To this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">numbers</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]</span>
</span><span class='line'>
</span><span class='line'><span class="n">doubled_odds</span> <span class="o">=</span> <span class="p">[</span><span class="n">n</span> <span class="o">*</span> <span class="mi">2</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">numbers</span> <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<h2>List Comprehensions: Now in Color</h2>

<p>Let&rsquo;s use colors to highlight what&rsquo;s going on.</p>

<pre class="colored-comprehension">
<span class="new-collection">doubled_odds</span> = <span class="collection-type">[]</span>
<span class="for-loop">for <span class="item">n</span> in <span class="old-collection">numbers</span></span>:
    <span class="conditional-clause">if <span class="condition">n % 2 == 1</span></span>:
        <span class="new-collection">doubled_odds</span>.append(<span class="item-mutation">n * 2</span>)
</pre>




<pre class="colored-comprehension">
<span class="new-collection">doubled_odds</span> = <span class="collection-type">[</span><span class="item-mutation">n * 2</span> <span class="for-loop">for <span class="item">n</span> in <span class="old-collection">numbers</span></span><span class="collection-type"> <span class="conditional-clause">if <span class="condition">n % 2 == 1</span></span>]</span>
</pre>


<p>We copy-paste from a <code>for</code> loop into a list comprehension by:</p>

<ol>
<li>Copying the <span class="new-collection">variable assignment</span> for our <span class="collection-type">new empty list</span></li>
<li>Copying <span class="item-mutation">the expression that we&rsquo;ve been <code>append</code>-ing</span> into this new list</li>
<li>Copying <span class="for-loop">the <code>for</code> loop line</span>, excluding the final <code>:</code></li>
<li>Copying <span class="conditional-clause">the <code>if</code> statement line</span>, also without the <code>:</code></li>
</ol>


<h2>Unconditional Comprehensions</h2>

<p>But what about comprehensions that don&rsquo;t have a conditional clause (that <code>if SOMETHING</code> part at the end)?  These loop-and-append <code>for</code> loops are even simpler than the loop-and-conditionally-append ones we&rsquo;ve already covered.</p>

<p>A <code>for</code> loop that doesn&rsquo;t have an <code>if</code> statement:</p>

<pre class="colored-comprehension">
<span class="new-collection">doubled_numbers</span> = <span class="collection-type">[]</span>
<span class="for-loop">for <span class="item">n</span> in <span class="old-collection">numbers</span></span>:
    <span class="new-collection">doubled_numbers</span>.append(<span class="item-mutation">n * 2</span>)
</pre>


<p>That same code written as a comprehension:</p>

<pre class="colored-comprehension">
<span class="new-collection">doubled_numbers</span> = <span class="collection-type">[</span><span class="item-mutation">n * 2</span> <span class="for-loop">for <span class="item">n</span> in <span class="old-collection">numbers</span></span><span class="collection-type">]</span>
</pre>


<p>Here&rsquo;s the transformation animated:</p>

<p><img src="http://treyhunner.com/images/list-comprehension-no-condition.gif"></p>

<p>We can copy-paste our way from a simple loop-and-append <code>for</code> loop by:</p>

<ol>
<li>Copying the <span class="new-collection">variable assignment</span> for our <span class="collection-type">new empty list</span> (line 3)</li>
<li>Copying <span class="item-mutation">the expression that we&rsquo;ve been <code>append</code>-ing</span> into this new list (line 5)</li>
<li>Copying <span class="for-loop">the <code>for</code> loop line</span>, excluding the final <code>:</code> (line 4)</li>
</ol>


<h2>Nested Loops</h2>

<p>What about list comprehensions with nested looping?&hellip; 😦</p>

<p>Here&rsquo;s a <code>for</code> loop that flattens a matrix (a list of lists):</p>

<pre class="colored-comprehension">
<span class="new-collection">flattened</span> = <span class="collection-type">[]</span>
<span class="for-loop">for <span class="item">row</span> in <span class="old-collection">matrix</span></span>:
    <span class="nested-for-loop">for <span class="item">n</span> in <span class="old-collection">row</span></span>:
        <span class="new-collection">flattened</span>.append(<span class="item-mutation">n</span>)
</pre>


<p>Here&rsquo;s a list comprehension that does the same thing:</p>

<pre class="colored-comprehension">
<span class="new-collection">flattened</span> = <span class="collection-type">[</span><span class="item-mutation">n</span> <span class="for-loop">for <span class="item">row</span> in <span class="old-collection">matrix</span></span><span class="collection-type"> <span class="nested-for-loop">for <span class="item">n</span> in <span class="old-collection">row</span></span><span class="collection-type">]</span>
</pre>


<p>Nested loops in list comprehensions do not read like English prose.</p>

<p><strong>Note:</strong> My brain wants to write this list comprehension as:</p>

<pre class="colored-comprehension">
<span class="new-collection">flattened</span> = <span class="collection-type">[</span><span class="item-mutation">n</span> <span class="nested-for-loop">for <span class="item">n</span> in <span class="old-collection">row</span></span><span class="collection-type"> <span class="for-loop">for <span class="item">row</span> in <span class="old-collection">matrix</span></span><span class="collection-type">]</span>
</pre>


<p><strong>But that&rsquo;s not right!</strong>  I&rsquo;ve mistakenly flipped the <code>for</code> loops here.  The correct version is the one above.</p>

<p>When working with nested loops in list comprehensions remember that <strong>the <code>for</code> clauses remain in the same order</strong> as in our original <code>for</code> loops.</p>

<h2>Other Comprehensions</h2>

<p>This same principle applies to <a href="https://docs.python.org/3/tutorial/datastructures.html#sets">set comprehensions</a> and <a href="https://docs.python.org/3/tutorial/datastructures.html#dictionaries">dictionary comprehensions</a>.</p>

<p>Code that creates a set of all the first letters in a sequence of words:</p>

<pre class="colored-comprehension">
<span class="new-collection">first_letters</span> = <span class="collection-type">set()</span>
<span class="for-loop">for <span class="item">w</span> in <span class="old-collection">words</span></span>:
    <span class="new-collection">first_letters</span>.add(<span class="item-mutation">w[0]</span>)
</pre>


<p>That same code written as a set comprehension:</p>

<pre class="colored-comprehension">
<span class="new-collection">first_letters</span> = <span class="collection-type">{</span><span class="item-mutation">w[0]</span> <span class="for-loop">for <span class="item">w</span> in <span class="old-collection">words</span></span><span class="collection-type">}</span>
</pre>


<p>Code that makes a new dictionary by swapping the keys and values of the original one:</p>

<pre class="colored-comprehension">
<span class="new-collection">flipped</span> = <span class="collection-type">{}</span>
<span class="for-loop">for <span class="item">key, value</span> in <span class="old-collection">original.items()</span></span>:
    <span class="new-collection">flipped</span>[<span class="item-mutation">value</span>] = <span class="item-mutation">key</span>
</pre>


<p>That same code written as a dictionary comprehension:</p>

<pre class="colored-comprehension">
<span class="new-collection">flipped</span> = <span class="collection-type">{</span><span class="item-mutation">value</span>: <span class="item-mutation">key</span> <span class="for-loop">for <span class="item">key, value</span> in <span class="old-collection">original.items()</span></span><span class="collection-type">}</span>
</pre>


<h2>Readability Counts</h2>

<p>Did you find the above list comprehensions hard to read?  I often find longer list comprehensions very difficult to read when they&rsquo;re written on one line.</p>

<p>Remember that <a href="https://docs.python.org/3/reference/lexical_analysis.html#implicit-line-joining">Python allows line breaks</a> between brackets and braces.</p>

<h3>List comprehension</h3>

<p>Before</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">doubled_odds</span> <span class="o">=</span> <span class="p">[</span><span class="n">n</span> <span class="o">*</span> <span class="mi">2</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">numbers</span> <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>After</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">doubled_odds</span> <span class="o">=</span> <span class="p">[</span>
</span><span class='line'>    <span class="n">n</span> <span class="o">*</span> <span class="mi">2</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">numbers</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span>
</span><span class='line'><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Nested loops in list comprehension</h3>

<p>Before</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">flattened</span> <span class="o">=</span> <span class="p">[</span><span class="n">n</span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">matrix</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">row</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>After</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">flattened</span> <span class="o">=</span> <span class="p">[</span>
</span><span class='line'>    <span class="n">n</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">matrix</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">row</span>
</span><span class='line'><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Dictionary comprehension</h3>

<p>Before</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">flipped</span> <span class="o">=</span> <span class="p">{</span><span class="n">value</span><span class="p">:</span> <span class="n">key</span> <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">original</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
</span></code></pre></td></tr></table></div></figure>


<p>After</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">flipped</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">value</span><span class="p">:</span> <span class="n">key</span>
</span><span class='line'>    <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">original</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that we are not adding line breaks arbitrarily: we&rsquo;re breaking between each of the lines of code we copy-pasted to make these comprehension.  Our line breaks occur where color changes occur in the colorized versions.</p>

<h2>Learn with me</h2>

<p>I did a <a href="https://www.youtube.com/watch?v=u-mhKtC1Xh4">class on list comprehensions</a> with <a href="http://remote.pyladies.com/">PyLadies Remote</a> recently.</p>

<p>If you&rsquo;d like to watch me walk through an explanation of any of the above topics, check out the video:</p>

<ol>
<li><a href="https://youtu.be/u-mhKtC1Xh4?t=3m30s">list comprehensions</a></li>
<li><a href="https://youtu.be/u-mhKtC1Xh4?t=35m05s">generator expressions</a></li>
<li><a href="https://youtu.be/u-mhKtC1Xh4?t=44m44s">set comprehensions</a></li>
<li><a href="https://youtu.be/u-mhKtC1Xh4?t=47m44s">dictionary comprehensions</a></li>
</ol>


<h2>Summary</h2>

<p>When struggling to write a comprehension, don&rsquo;t panic.  Start with a <code>for</code> loop first and copy-paste your way into a comprehension.</p>

<p>Any <code>for</code> loop that looks like this:</p>

<pre class="colored-comprehension">
<span class="new-collection">new_things</span> = <span class="collection-type">[]</span>
<span class="for-loop">for <span class="item">ITEM</span> in <span class="old-collection">old_things</span></span>:
    <span class="conditional-clause">if <span class="condition">condition_based_on(ITEM)</span></span>:
        <span class="new-collection">new_things</span>.append(<span class="item-mutation">"something with " + ITEM</span>)
</pre>


<p>Can be rewritten into a list comprehension like this:</p>

<pre class="colored-comprehension">
<span class="new-collection">new_things</span> = <span class="collection-type">[</span><span class="item-mutation">"something with " + ITEM</span> <span class="for-loop">for <span class="item">ITEM</span> in <span class="old-collection">old_things</span></span><span class="collection-type"> <span class="conditional-clause">if <span class="condition">condition_based_on(ITEM)</span></span>]</span>
</pre>


<p>If you can nudge a <code>for</code> loop until it looks like the ones above, you can rewrite it as a list comprehension.</p>

<p>This article was based on my Intro to Python class.  If you&rsquo;re interested in chatting about my <a href="http://truthful.technology/">Python training services</a>, <a href="mailto:hello@truthful.technology">drop me a line</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Counting Things in Python: A History]]></title>
    <link href="http://treyhunner.com/2015/11/counting-things-in-python/"/>
    <updated>2015-11-09T11:30:00-08:00</updated>
    <id>http://treyhunner.com/2015/11/counting-things-in-python</id>
    <content type="html"><![CDATA[<p>Sometimes the <a href="http://nedbatchelder.com/blog/201011/pythonic.html">Pythonic</a> way to solve a problem changes over time.  As Python has evolved, so has the Pythonic way to count list items.</p>

<p>Let&rsquo;s look at different techniques for counting the number of times things appear in a list.  While analyzing these techniques, we will <em>only</em> be looking at code style.  We&rsquo;ll worry about performance later.</p>

<p>We will need some historical context to understand these different techniques.  Fortunately we live in the <code>__future__</code> and we have a time machine.  Let&rsquo;s jump in our DeLorean and head to 1997.</p>

<h2>if Statement</h2>

<p>It&rsquo;s January 1, 1997 and we&rsquo;re using Python 1.4.  We have a list of colors and we&rsquo;d love to know how many times each color occurs in this list.  Let&rsquo;s use <a href="https://docs.python.org/release/1.4/lib/node13.html">a dictionary</a>!</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;black&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">color_counts</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'><span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">colors</span><span class="p">:</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">color_counts</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="n">c</span><span class="p">):</span>
</span><span class='line'>        <span class="n">color_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="n">color_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
</span><span class='line'>    <span class="k">else</span><span class="p">:</span>
</span><span class='line'>        <span class="n">color_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>Note:</strong> we&rsquo;re not using <code>+=</code> because augmented assignment won&rsquo;t be added until <a href="https://www.python.org/dev/peps/pep-0203/">Python 2.0</a> and we&rsquo;re not using the <code>c in color_counts</code> idiom because that won&rsquo;t be invented until <a href="https://docs.python.org/release/2.2/lib/typesmapping.html">Python 2.2</a>!</p>

<p>After running this we&rsquo;ll see that our <code>color_counts</code> dictionary now contains the counts of each color in our list:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='pycon'><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">color_counts</span>
</span><span class='line'><span class="go">{&#39;brown&#39;: 3, &#39;yellow&#39;: 2, &#39;green&#39;: 1, &#39;black&#39;: 1, &#39;red&#39;: 1}</span>
</span></code></pre></td></tr></table></div></figure>


<p>That was pretty simple.  We just looped through each color, checked if it was in the dictionary, added the color if it wasn&rsquo;t, and incremented the count if it was.</p>

<p>We could also write this as:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;black&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">color_counts</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'><span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">colors</span><span class="p">:</span>
</span><span class='line'>    <span class="k">if</span> <span class="ow">not</span> <span class="n">color_counts</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="n">c</span><span class="p">):</span>
</span><span class='line'>        <span class="n">color_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'>    <span class="n">color_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="n">color_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
</span></code></pre></td></tr></table></div></figure>


<p>This might be a little slower on sparse lists (lists with lots of non-repeating colors) because it executes two statements instead of one, but we&rsquo;re not worried about performance, we&rsquo;re worried about code style.  After some thought, we decide to stick with this new version.</p>

<h2>try Block</h2>

<p>It&rsquo;s January 2, 1997 and we&rsquo;re still using Python 1.4.  We woke up this morning with a sudden realization: our code is practicing &ldquo;Look Before You Leap&rdquo; (<a href="https://docs.python.org/2/glossary.html#term-lbyl">LBYL</a>) when we should be practicing &ldquo;Easier to Ask Forgiveness, Than Permission&rdquo; (<a href="https://docs.python.org/2/glossary.html#term-eafp">EAFP</a>) because EAFP is more Pythonic.  Let&rsquo;s refactor our code to use a try-except block:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;black&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">color_counts</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'><span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">colors</span><span class="p">:</span>
</span><span class='line'>    <span class="k">try</span><span class="p">:</span>
</span><span class='line'>        <span class="n">color_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="n">color_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
</span><span class='line'>    <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
</span><span class='line'>        <span class="n">color_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now our code attempts to increment the count for each color and if the color isn&rsquo;t in the dictionary, a <code>KeyError</code> will be raised and we will instead set the color count to 1 for the color.</p>

<h2>get Method</h2>

<p>It&rsquo;s January 1, 1998 and we&rsquo;ve upgraded to Python 1.5.  We&rsquo;ve decided to refactor our code to use the <a href="https://docs.python.org/release/1.5/lib/node13.html">new <code>get</code> method on dictionaries</a>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;black&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">color_counts</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'><span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">colors</span><span class="p">:</span>
</span><span class='line'>    <span class="n">color_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="n">color_counts</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now our code loops through each color, gets the current count for the color from the dictionary, defaulting this count to <code>0</code>, adds <code>1</code> to the count, and sets the dictionary key to this new value.</p>

<p>It&rsquo;s cool that this is all one line of code, but we&rsquo;re not entirely sure if this is more Pythonic.  We decide this might be too clever so we revert this change.</p>

<h2>setdefault</h2>

<p>It&rsquo;s January 1, 2001 and we&rsquo;re now using Python 2.0!  We&rsquo;ve heard that <a href="https://docs.python.org/release/2.0/lib/typesmapping.html">dictionaries have a <code>setdefault</code> method now</a> and we decide to refactor our code to use this new method.  We also decide to use the new <code>+=</code> augmented assignment operator:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;black&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">color_counts</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'><span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">colors</span><span class="p">:</span>
</span><span class='line'>    <span class="n">color_counts</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'>    <span class="n">color_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>setdefault</code> method is being called on every loop, regardless of whether it&rsquo;s needed, but this does seem a little more readable.  We decide that this is more Pythonic than our previous solutions and commit our change.</p>

<h2>fromkeys</h2>

<p>It&rsquo;s January 1, 2004 and we&rsquo;re using Python 2.3.  We&rsquo;ve heard about a <a href="https://docs.python.org/release/2.3/lib/typesmapping.html">new <code>fromkeys</code> class method</a> on dictionaries for constructing dictionaries from a list of keys.  We refactor our code to use this new method:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;black&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">color_counts</span> <span class="o">=</span> <span class="nb">dict</span><span class="o">.</span><span class="n">fromkeys</span><span class="p">(</span><span class="n">colors</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'><span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">colors</span><span class="p">:</span>
</span><span class='line'>    <span class="n">color_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
</span></code></pre></td></tr></table></div></figure>


<p>This creates a new dictionary using our colors as keys, with all values set to <code>0</code> initially.  This allows us to increment each key without worrying whether it has been set.  We&rsquo;ve removed the need for any checking or exception handling which seems like an improvement.  We decide to keep this change.</p>

<h2>Comprehension and set</h2>

<p>It&rsquo;s January 1, 2005 and we&rsquo;re using Python 2.4.  We realize that we could solve our counting problem using sets (<a href="https://docs.python.org/release/2.3/lib/module-sets.html">released in Python 2.3</a> and made into <a href="https://docs.python.org/release/2.4/lib/types-set.html">a built-in in 2.4</a>) and list comprehensions (<a href="https://www.python.org/dev/peps/pep-0202/">released in Python 2.0</a>).  After further thought, we remember that <a href="https://www.python.org/dev/peps/pep-0289/">generator expressions</a> were also just released in Python 2.4 and we decide to use one of those instead of a list comprehension:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;black&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">color_counts</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">c</span><span class="p">,</span> <span class="n">colors</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">c</span><span class="p">))</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">set</span><span class="p">(</span><span class="n">colors</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>Note</strong>: we didn&rsquo;t use a dictionary comprehension because those won&rsquo;t be invented until <a href="https://www.python.org/dev/peps/pep-0274/">Python 2.7</a>.</p>

<p>This works.  It&rsquo;s one line of code.  But is it Pythonic?</p>

<p>We remember the <a href="https://www.python.org/dev/peps/pep-0020/">Zen of Python</a>, which <a href="https://mail.python.org/pipermail/python-list/1999-June/001951.html">started in a python-list email thread</a> and was <a href="http://svn.python.org/view/python/tags/r221/Lib/this.py?revision=25249&amp;view=markup">snuck into Python 2.2.1</a>.  We type <code>import this</code> at our REPL:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='pycon'><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">this</span>
</span><span class='line'><span class="go">The Zen of Python, by Tim Peters</span>
</span><span class='line'>
</span><span class='line'><span class="go">Beautiful is better than ugly.</span>
</span><span class='line'><span class="go">Explicit is better than implicit.</span>
</span><span class='line'><span class="go">Simple is better than complex.</span>
</span><span class='line'><span class="go">Complex is better than complicated.</span>
</span><span class='line'><span class="go">Flat is better than nested.</span>
</span><span class='line'><span class="go">Sparse is better than dense.</span>
</span><span class='line'><span class="go">Readability counts.</span>
</span><span class='line'><span class="go">Special cases aren&#39;t special enough to break the rules.</span>
</span><span class='line'><span class="go">Although practicality beats purity.</span>
</span><span class='line'><span class="go">Errors should never pass silently.</span>
</span><span class='line'><span class="go">Unless explicitly silenced.</span>
</span><span class='line'><span class="go">In the face of ambiguity, refuse the temptation to guess.</span>
</span><span class='line'><span class="go">There should be one-- and preferably only one --obvious way to do it.</span>
</span><span class='line'><span class="go">Although that way may not be obvious at first unless you&#39;re Dutch.</span>
</span><span class='line'><span class="go">Now is better than never.</span>
</span><span class='line'><span class="go">Although never is often better than *right* now.</span>
</span><span class='line'><span class="go">If the implementation is hard to explain, it&#39;s a bad idea.</span>
</span><span class='line'><span class="go">If the implementation is easy to explain, it may be a good idea.</span>
</span><span class='line'><span class="go">Namespaces are one honking great idea -- let&#39;s do more of those!</span>
</span></code></pre></td></tr></table></div></figure>


<p>Our code is <em>more complex</em> (<strong>O(n<sup>2</sup>)</strong> instead of <strong>O(n)</strong>), <em>less beautiful</em>, and <em>less readable</em>.  That change was a fun experiment, but this one-line solution is <strong>less Pythonic</strong> than what we already had.  We decide to revert this change.</p>

<h2>defaultdict</h2>

<p>It&rsquo;s January 1, 2007 and we&rsquo;re using Python 2.5.  We&rsquo;ve just found out that <a href="https://docs.python.org/release/2.5/lib/defaultdict-objects.html"><code>defaultdict</code> is in the standard library</a> now.  This should allow us to set <code>0</code> as the default value in our dictionary.  Let&rsquo;s refactor our code to count using a <code>defaultdict</code> instead:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span>
</span><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;black&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">color_counts</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
</span><span class='line'><span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">colors</span><span class="p">:</span>
</span><span class='line'>    <span class="n">color_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
</span></code></pre></td></tr></table></div></figure>


<p>That <code>for</code> loop is so simple now!  This is almost certainly more Pythonic.</p>

<p>We realize that our <code>color_counts</code> variable does act differently, however it <em>does</em> inherit from <code>dict</code> and supports all the same mapping functionality.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='pycon'><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">color_counts</span>
</span><span class='line'><span class="go">defaultdict(&lt;type &#39;int&#39;&gt;, {&#39;brown&#39;: 3, &#39;yellow&#39;: 2, &#39;green&#39;: 1, &#39;black&#39;: 1, &#39;red&#39;: 1})</span>
</span></code></pre></td></tr></table></div></figure>


<p>Instead of converting <code>color_counts</code> to a <code>dict</code>, we&rsquo;ll assume the rest of our code practices <a href="https://docs.python.org/2/glossary.html#term-duck-typing">duck typing</a> and leave this dict-like object as-is.</p>

<h2>Counter</h2>

<p>It&rsquo;s January 1, 2011 and we&rsquo;re using Python 2.7.  We&rsquo;ve been told that our <code>defaultdict</code> code is no longer the most Pythonic way to count colors.  <a href="https://docs.python.org/2.7/library/collections.html#collections.Counter">A <code>Counter</code> class was included in the standard library</a> in Python 2.7 and it does all of the work for us!</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">Counter</span>
</span><span class='line'><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;red&quot;</span><span class="p">,</span> <span class="s">&quot;green&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;yellow&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;brown&quot;</span><span class="p">,</span> <span class="s">&quot;black&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">color_counts</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">colors</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Could this get any simpler?  This must be the most Pythonic way.</p>

<p>Like <code>defaultdict</code>, this returns a dict-like object (a <code>dict</code> subclass actually), which should be good enough for our purposes, so we&rsquo;ll stick with it.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='pycon'><span class='line'><span class="gp">&gt;&gt;&gt; </span><span class="n">color_counts</span>
</span><span class='line'><span class="go">Counter({&#39;brown&#39;: 3, &#39;yellow&#39;: 2, &#39;green&#39;: 1, &#39;black&#39;: 1, &#39;red&#39;: 1})</span>
</span></code></pre></td></tr></table></div></figure>


<h2>After thought: Performance</h2>

<p>Notice that we didn&rsquo;t focus on efficiency for these solutions.  Most of these solutions have the same time complexity (<code>O(n)</code> in big O notation) but runtimes could vary based on the Python implementation.</p>

<p>While performance isn&rsquo;t our main concern, <a href="https://gist.github.com/treyhunner/0987601f960a5617a1be">I did measure the run-times on CPython 3.5.0</a>.  It&rsquo;s interesting to see how each implementation changes in relative efficiency based on the density of color names in the list.</p>

<h2>Conclusion</h2>

<p>Per the <a href="https://www.python.org/dev/peps/pep-0020/">Zen of Python</a>, &ldquo;there should be one&ndash; and preferably only one&ndash; obvious way to do it&rdquo;.  This is an aspirational message.  There isn&rsquo;t always one obvious way to do it.  The &ldquo;obvious&rdquo; way can vary by time, need, and level of expertise.</p>

<p><strong>&ldquo;Pythonic&rdquo; is a relative term.</strong></p>

<h3>Related Resources</h3>

<ul>
<li><a href="http://www.wefearchange.org/2010/06/import-this-and-zen-of-python.html">import this and the Zen of Python</a>: Zen of Python trivia borrowed from this post</li>
<li><a href="https://www.youtube.com/watch?v=AZDWveIdqjY">Permission or Forgiveness</a>: Alex Martelli discusses Grace Hopper&rsquo;s EAFP</li>
<li><a href="https://codefisher.org/catch/blog/2015/04/22/python-how-group-and-count-dictionaries/">Python How To: Group and Count with Dictionaries</a>: while writing this post, I discovered this related article</li>
</ul>


<h3>Credits</h3>

<p>Thanks to <a href="http://brianschrader.com/">Brian Schrader</a> and <a href="http://stackoverflow.com/users/400617/davidism">David Lord</a> for proof-reading this post and <a href="http://micah.bigprob.net/">Micah Denbraver</a> for actually <a href="https://gist.github.com/macro1/9b364612ee3907df4179">testing out these solutions</a> on the correct versions of Python.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The Many Flavors of mock.patch]]></title>
    <link href="http://treyhunner.com/2014/10/the-many-flavors-of-mock-dot-patch/"/>
    <updated>2014-10-13T21:00:00-07:00</updated>
    <id>http://treyhunner.com/2014/10/the-many-flavors-of-mock-dot-patch</id>
    <content type="html"><![CDATA[<p>I write a lot of unit tests.  Unfortunately, my code often requires monkey patching to be properly unit tested.  I frequently use the <code>patch</code> function from <a href="http://www.voidspace.org.uk/">Michael Foord&rsquo;s</a> <a href="https://pypi.python.org/pypi/mock/">mock</a> library (now available in Python 3.4 as <a href="https://docs.python.org/3.4/library/unittest.mock.html#module-unittest.mock">unittest.mock</a>) to monkey patch my code.</p>

<p>While chatting with other users of <code>patch</code>, I realized that everyone seems to have their own favorite way to use it.  In this post I will discuss the ways I use patch.</p>

<h3>Decorator</h3>

<p>patch can be used as a method decorator:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">mock</span> <span class="kn">import</span> <span class="n">patch</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">MyModelTest</span><span class="p">:</span>
</span><span class='line'>    <span class="nd">@patch</span><span class="p">(</span><span class="s">&#39;mylib.utils.other_func&#39;</span><span class="p">)</span>
</span><span class='line'>    <span class="k">def</span> <span class="nf">test_some_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other_func</span><span class="p">):</span>
</span><span class='line'>        <span class="n">other_func</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s">&quot;MY STRING&quot;</span>
</span><span class='line'>        <span class="k">assert</span> <span class="n">some_func</span><span class="p">(</span><span class="s">&quot;my string&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="s">&quot;MY STRING&quot;</span>
</span><span class='line'>        <span class="n">other_func</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">(</span><span class="s">&quot;my string&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>or as a class decorator:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">mock</span> <span class="kn">import</span> <span class="n">patch</span>
</span><span class='line'>
</span><span class='line'><span class="nd">@patch</span><span class="p">(</span><span class="s">&#39;mylib.utils.other_func&#39;</span><span class="p">)</span>
</span><span class='line'><span class="k">class</span> <span class="nc">MyModelTest</span><span class="p">:</span>
</span><span class='line'>    <span class="k">def</span> <span class="nf">test_some_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other_func</span><span class="p">):</span>
</span><span class='line'>        <span class="n">other_func</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s">&quot;MY STRING&quot;</span>
</span><span class='line'>        <span class="k">assert</span> <span class="n">some_func</span><span class="p">(</span><span class="s">&quot;my string&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="s">&quot;MY STRING&quot;</span>
</span><span class='line'>        <span class="n">other_func</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">(</span><span class="s">&quot;my string&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>I use patch as a decorator when I have a function I want patched during my whole test.  I tend not to use patch as a class decorator and I&rsquo;ll explain why below.</p>

<p><a href="https://github.com/treyhunner/pep438/blob/cdb57e2cb1c3053255a0caf2a5ebb64672da661c/test_pep438.py#L79">Decorator example</a></p>

<h3>Context Manager</h3>

<p>patch can be used as a context manager:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">mock</span> <span class="kn">import</span> <span class="n">patch</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">MyModelTest</span><span class="p">:</span>
</span><span class='line'>    <span class="k">def</span> <span class="nf">test_some_func</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class='line'>        <span class="n">other_func</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s">&quot;MY STRING&quot;</span>
</span><span class='line'>        <span class="k">with</span> <span class="n">patch</span><span class="p">(</span><span class="s">&#39;mylib.utils.other_func&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">other_func</span><span class="p">:</span>
</span><span class='line'>            <span class="k">assert</span> <span class="n">some_func</span><span class="p">(</span><span class="s">&quot;my string&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="s">&quot;MY STRING&quot;</span>
</span><span class='line'>        <span class="n">other_func</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">(</span><span class="s">&quot;my string&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>I prefer to use patch as a context manager when I want to patch a function for only part of a test.  I do not use patch as a context manager when I want a function patched for an entire test.</p>

<p><a href="https://github.com/treyhunner/pep438/blob/cdb57e2cb1c3053255a0caf2a5ebb64672da661c/test_pep438.py#L46">Context manager example</a></p>

<h3>Manually using start and stop</h3>

<p>patch can also be used to manually patch/unpatch using <code>start</code> and <code>stop</code> methods:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">mock</span> <span class="kn">import</span> <span class="n">patch</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">MyModelTest</span><span class="p">:</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">other_func_patch</span> <span class="o">=</span> <span class="n">patch</span><span class="p">(</span><span class="s">&#39;mylib.utils.other_func&#39;</span><span class="p">)</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">other_func</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">other_func_patch</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">other_func</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s">&quot;MY STRING&quot;</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">other_func_patch</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nf">test_some_func</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class='line'>        <span class="k">assert</span> <span class="n">some_func</span><span class="p">(</span><span class="s">&quot;my string&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="s">&quot;MY STRING&quot;</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">other_func</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">(</span><span class="s">&quot;my string&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>I prefer to use patch using start/stop when I need a function to be patched for every function in a test class.</p>

<p>This is probably the most common way I use patch in my tests.  I often group my tests into test classes where each method is focused around testing the same function.  Therefore I will usually want the same functions/objects patched for every test method.</p>

<p>I noted above that I prefer not to use class decorators to solve this problem.  Instead, I prefer to use test class attributes to store references to patched functions instead of accepting patch arguments on every test method with decorators.  I find this more <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>.</p>

<p><strong>Warning:</strong> One of the primary benefits of the decorator/context manager forms of patch is that they handle clean up for you.  Whenever you call <code>start</code> to setup your patch object, <em>always</em> remember to call <code>stop</code> to clean it up.  Otherwise you&rsquo;ll have a monkey patched function/object for the rest of your running program.</p>

<p><a href="https://github.com/treyhunner/pep438/blob/cdb57e2cb1c3053255a0caf2a5ebb64672da661c/test_pep438.py#L128">start and stop example</a></p>

<h3>Summary</h3>

<p>Patch can be used:</p>

<ol>
<li>as a method or class decorator</li>
<li>as a context manager</li>
<li>using start and stop methods</li>
</ol>


<p>I prefer my tests to be readable, <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>, and easy to modify.  I tend to use start/stop methods for that reason, but I also frequently use patch method decorators and sometimes use patch context managers.  It&rsquo;s useful to know the different flavors of <code>patch</code> because your favorite flavor may not always be the most suitable one for the problem at hand.</p>

<p>Did I miss a flavor?  Want to let me know which flavor you prefer and why?  Please comment below.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[CSS Classes and Django Form Fields]]></title>
    <link href="http://treyhunner.com/2014/09/adding-css-classes-to-django-form-fields/"/>
    <updated>2014-09-30T11:00:00-07:00</updated>
    <id>http://treyhunner.com/2014/09/adding-css-classes-to-django-form-fields</id>
    <content type="html"><![CDATA[<p>Django forms provide input validation and HTML form field generation.  They also integrate nicely with models.  However, Django itself does not allow one-off customizations of form-generated HTML.</p>

<p>In this post I will discuss a method for customizing the HTML generated by Django form fields, with the specific goal of adding custom CSS classes to Django form fields.</p>

<p>Here&rsquo;s a Django form definition:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">AuthenticationForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
</span><span class='line'>    <span class="n">username</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">254</span><span class="p">)</span>
</span><span class='line'>    <span class="n">password</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">widget</span><span class="o">=</span><span class="n">forms</span><span class="o">.</span><span class="n">PasswordInput</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here&rsquo;s the form used in a template:</p>

<pre><code>{{ form.as_p }}
</code></pre>

<h2>The Problem</h2>

<p>We&rsquo;re using <a href="http://getbootstrap.com/">Bootstrap</a> and we want to add an <code>input-lg</code> CSS class onto our username field to make it really big.</p>

<h2>The Solution(s)</h2>

<p>There are many ways to solve this problem.  I will discuss some solutions I dislike before I discuss my preferred solution.</p>

<h3>Using a form widget attribute</h3>

<p>We could add a <code>class</code> attribute to our Django form field:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">AuthenticationForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
</span><span class='line'>    <span class="n">username</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span>
</span><span class='line'>        <span class="n">max_length</span><span class="o">=</span><span class="mi">254</span><span class="p">,</span>
</span><span class='line'>        <span class="n">widget</span><span class="o">=</span><span class="n">forms</span><span class="o">.</span><span class="n">TextInput</span><span class="p">(</span><span class="n">attrs</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;class&#39;</span><span class="p">:</span> <span class="s">&quot;input-lg&quot;</span><span class="p">}),</span>
</span><span class='line'>    <span class="p">)</span>
</span><span class='line'>    <span class="n">password</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">widget</span><span class="o">=</span><span class="n">forms</span><span class="o">.</span><span class="n">PasswordInput</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>I dislike this approach because it requires including presentation rules in our back-end code.  This class attribute is used exclusively by our CSS and/or JavaScript and should therefore live in Django templates, not in Python code.</p>

<h3>Using django-floppyforms</h3>

<p>If we&rsquo;re using <a href="https://django-floppyforms.readthedocs.org/">django-floppyforms</a> we could include logic in our <code>floppyforms/attrs.html</code> template to add specific classes based on a context variable (<a href="https://github.com/grundleborg/django-floppyforms-bootstrap3/blob/9ab0261eb8ae0c939e4ad01066716e445357cb95/floppyforms_bootstrap3/templates/floppyforms/attrs.html">example</a>).  Here&rsquo;s an example:</p>

<pre><code>{% for name, value in attrs.items %} {{ name }}{% if value != True %}="{{ value }}{% if name == "class" %} {{ extra_classes }}{% endif %}"{% endfor %}
</code></pre>

<p>This should work but it&rsquo;s ugly and in general I do not enjoy maintaining heavy logic in my templates.</p>

<p><aside>Aside: there is currently an <a href="https://github.com/gregmuellegger/django-floppyforms/issues/99">open issue</a> on django-floppyforms discussing how this could be added as a feature to the library.</aside></p>

<h3>Using django-widget-tweaks</h3>

<p>I prefer to solve this problem with <a href="https://pypi.python.org/pypi/django-widget-tweaks">django-widget-tweaks</a>.</p>

<p>The django-widget-tweaks library provides two solutions to this problem:</p>

<ol>
<li><code>add_class</code> template filter</li>
<li><code>render_field</code> template tag.</li>
</ol>


<h4>The add_class template filter</h4>

<p><a href="http://kmike.ru/pages/about/">Mikhail Korobov</a> originally created the <a href="https://pypi.python.org/pypi/django-widget-tweaks">django-widget-tweaks</a> library in 2011.  It started as a series of template filters for modifying form field attributes from your Django templates.</p>

<p>Here&rsquo;s an example usage of the <code>add_class</code> filter for adding a CSS class to our form field:</p>

<pre><code>{% load widget_tweaks %}
&lt;p&gt;
    {{ form.username|add_class:"input-lg" }}
    {{ form.username.errors }}
&lt;/p&gt;
&lt;p&gt;
    {{ form.password }}
    {{ form.password.errors }}
&lt;/p&gt;
</code></pre>

<p>I find this solution both easy to read and easy to maintain.</p>

<h4>The render_field template tag</h4>

<p>I discovered <a href="https://pypi.python.org/pypi/django-widget-tweaks">django-widget-tweaks</a> shortly after <a href="http://kmike.ru/pages/about/">Mikhail</a> created it.  I appreciated his solution for this problem, but I wanted a more HTML-like syntax for my form field customizations.  I created the <code>render_field</code> template tag to satisfy that desire.</p>

<p>With the <code>render_field</code> tag you can add attributes to form fields with a much more HTML-like syntax:</p>

<pre><code>{% load widget_tweaks %}
&lt;p&gt;
    {% render_field form.username class+="input-lg" %}
    {{ form.username.errors }}
&lt;/p&gt;
&lt;p&gt;
    {% render_field form.password %}
    {{ form.password.errors }}
&lt;/p&gt;
</code></pre>

<p>As a bonus, with <code>render_field</code> we can also set a CSS class for erroneous and required form fields.  See <a href="https://pypi.python.org/pypi/django-widget-tweaks#render-field">the documentation</a> for more details.</p>

<h2>Conclusion</h2>

<p>I have not had a chance to use <a href="https://django-floppyforms.readthedocs.org/">django-floppyforms</a> yet, but I expect that <a href="https://pypi.python.org/pypi/django-widget-tweaks">django-widget-tweaks</a> and django-floppyforms would integrate well together.</p>

<p>I am on the lookout for new solutions to this problem, but django-widget-tweaks has served me well so far.  I have used it for three years now it remains one of my go-to libraries for new Django projects.</p>

<p>How do you add CSS classes do your Django form fields?  If you have another solution please leave a comment below.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Supporting Both Django 1.7 and South]]></title>
    <link href="http://treyhunner.com/2014/03/migrating-to-django-1-dot-7/"/>
    <updated>2014-03-27T13:05:00-07:00</updated>
    <id>http://treyhunner.com/2014/03/migrating-to-django-1-dot-7</id>
    <content type="html"><![CDATA[<p>Have an open source Django app with South migrations?  Adding support for Django 1.7 might be a little painful.  In this post I will discuss the difficulty of supporting Django 1.7 while maintaining South migrations for users of Django 1.6 and below.</p>

<p>Django 1.7 uses the <code>migrations</code> sub-package in your app for database migrations and South relies on the same package.  Unfortunately, you can&rsquo;t store both packages in the same place.  At first glance, it seems we cannot support both Django 1.7 and previous versions of Django using South.  However, as I explain below, we can support both at once.</p>

<h2>Assessing your options</h2>

<p>In order to support both Django 1.7 and Django 1.6 with South we can rename the <code>migrations</code> package and instruct users to reference the new package in their settings module.  We can do this with the <a href="https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-MIGRATION_MODULES">MIGRATION_MODULES</a> or <a href="http://south.readthedocs.org/en/latest/settings.html#south-migration-modules">SOUTH_MIGRATION_MODULES</a> settings.  There are three options:</p>

<ol>
<li>Move existing <code>migrations</code> directory to <code>south_migrations</code> and create Django 1.7 migrations in <code>migrations</code> package</li>
<li>Create new Django 1.7 migrations package in <code>django_migrations</code> directory and leave existing South migrations package</li>
<li>Move existing <code>migrations</code> directory to <code>south_migrations</code> and create Django 1.7 migrations in <code>django_migrations</code> directory</li>
</ol>


<p>The first option requires existing users either switch to Django 1.7 or update their settings module before upgrading to the new version of your app.  The second option requires all Django 1.7 users to customize their settings module to properly install your app.  The third option requires everyone (both Django 1.7 and South users) to update their settings module.</p>

<p>Out of those options I prefer the first one.  When you eventually drop support for South, you will probably want your Django 1.7 migrations to live in the <code>migrations</code> directory.  If you don&rsquo;t force that switch now, you would eventually need to break backwards-compatibility or maintain two duplicate migrations directories.</p>

<p>So our plan is to move the South migrations to <code>south_migrations</code> and create Django 1.7 migrations.  An example with the <a href="https://github.com/treyhunner/django-email-log">django-email-log</a> app:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>git mv email_log/migrations email_log/south_migrations
</span><span class='line'><span class="nv">$ </span>python manage.py makemigrations email_log
</span><span class='line'><span class="nv">$ </span>git add email_log/migrations
</span></code></pre></td></tr></table></div></figure>


<h2>Breaking South support</h2>

<p>If you move <code>migrations</code> to <code>south_migrations</code> and make a Django 1.7 <code>migrations</code> package, what happens to existing users with South?</p>

<p>Your new app upgrade will break backwards compatibility for South users and you want to make sure they <em>know</em> they need to make a change immediately after upgrading.  Users should see a loud and clear error message instructing them what they need to do.  This can be done by hijacking their use of the <strong>migrate</strong> command with South.</p>

<p>Existing users will run <strong>migrate</strong> when upgrading your app.  If they don&rsquo;t migrate immediately, they will when they notice a problem and realize they need to run <strong>migrate</strong>.  Upon migrating, we want to show a clear error message telling the user what to do.</p>

<h2>Failing loudly and with a clear error message</h2>

<p>When South looks for app migrations it will import our <code>migrations</code> package.  Our <code>migrations</code> package contains Django 1.7 migrations, which South won&rsquo;t understand.  So we want to make sure that if our <code>migrations</code> package is imported either Django 1.7 is installed or a proper error message is displayed.  Upon importing this package, we can check for the presence of the new <code>django.db.migrations</code> module and if not found we will raise an exception with a descriptive error message.</p>

<p>For example, this is the code I plan to add to the <code>email_log/migrations/__init__.py</code> file for <a href="https://github.com/treyhunner/django-email-log">django-email-log</a> to add Django 1.7 support:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="sd">&quot;&quot;&quot;</span>
</span><span class='line'><span class="sd">Django migrations for email_log app</span>
</span><span class='line'>
</span><span class='line'><span class="sd">This package does not contain South migrations.  South migrations can be found</span>
</span><span class='line'><span class="sd">in the ``south_migrations`` package.</span>
</span><span class='line'><span class="sd">&quot;&quot;&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="n">SOUTH_ERROR_MESSAGE</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;</span><span class="se">\n</span><span class="s"></span>
</span><span class='line'><span class="s">For South support, customize the SOUTH_MIGRATION_MODULES setting like so:</span>
</span><span class='line'>
</span><span class='line'><span class="s">    SOUTH_MIGRATION_MODULES = {</span>
</span><span class='line'><span class="s">        &#39;email_log&#39;: &#39;email_log.south_migrations&#39;,</span>
</span><span class='line'><span class="s">    }</span>
</span><span class='line'><span class="s">&quot;&quot;&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="c"># Ensure the user is not using Django 1.6 or below with South</span>
</span><span class='line'><span class="k">try</span><span class="p">:</span>
</span><span class='line'>    <span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">migrations</span>  <span class="c"># noqa</span>
</span><span class='line'><span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
</span><span class='line'>    <span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="kn">import</span> <span class="n">ImproperlyConfigured</span>
</span><span class='line'>    <span class="k">raise</span> <span class="n">ImproperlyConfigured</span><span class="p">(</span><span class="n">SOUTH_ERROR_MESSAGE</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now when we run <strong>migrate</strong> with Django 1.6 and South, we&rsquo;ll see the following exception raised:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">django</span><span class="o">.</span><span class="n">core</span><span class="o">.</span><span class="n">exceptions</span><span class="o">.</span><span class="n">ImproperlyConfigured</span><span class="p">:</span>
</span><span class='line'>
</span><span class='line'><span class="n">For</span> <span class="n">South</span> <span class="n">support</span><span class="p">,</span> <span class="n">customize</span> <span class="n">the</span> <span class="n">SOUTH_MIGRATION_MODULES</span> <span class="n">setting</span> <span class="n">like</span> <span class="n">so</span><span class="p">:</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">SOUTH_MIGRATION_MODULES</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>        <span class="s">&#39;email_log&#39;</span><span class="p">:</span> <span class="s">&#39;email_log.south_migrations&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Conclusion</h2>

<p>This breaks backwards compatibility, but our users should immediately understand what has broken and how to fix it.  Remember to upgrade the major number of your package version to note this backwards-incompatible change.</p>

<p>I would love to hear your thoughts about this approach in the comments below.  Let me know if you have other ideas about how to handle supporting Django 1.7 migrations and South at the same time.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[TDD With Django Tutorial]]></title>
    <link href="http://treyhunner.com/2013/11/tdd-with-django-workshop/"/>
    <updated>2013-11-04T00:00:00-08:00</updated>
    <id>http://treyhunner.com/2013/11/tdd-with-django-workshop</id>
    <content type="html"><![CDATA[<p>I helped host a free Test-Driven Django Web Development workshop on <time date="2013-11-02">Saturday November 2</time> with <a href="http://pythonsd.org/">San Diego Python</a>.  We created a series of tutorials demonstrating how to create a Django-powered blog while practicing test-driven development.  The <a href="http://python.org/psf/">Python Software Foundation</a> sponsored the event and the <a href="http://aicenterca.com/">Ansir Innovation Center</a> provided a venue.</p>

<p>You can find the tutorials at <a href="http://bit.ly/pysd-tdd">http://bit.ly/pysd-tdd</a> .  The tutorials are provided under a <a href="https://creativecommons.org/licenses/by-sa/3.0/">CC BY-SA license</a> so you can reuse and modify them for your own purposes.</p>

<p>Tutorial markdown files and working source code may be found on <a href="https://github.com/pythonsd/test-driven-django-development">Github</a>.  We plan to improve and extend these tutorials for a future workshop.  If you have ideas for improvements/additions or if you notice a bug, please submit an issue or open a pull request.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Visual Integration Tests for Django]]></title>
    <link href="http://treyhunner.com/2013/10/visual-integration-tests-for-django/"/>
    <updated>2013-10-03T15:19:00-07:00</updated>
    <id>http://treyhunner.com/2013/10/visual-integration-tests-for-django</id>
    <content type="html"><![CDATA[<p>I recently added a new type of test to my testing arsenal: visual tests.  Visual tests ensure the CSS, markup, and JavaScript produce a webpage that looks right.</p>

<h2>Visual testing frameworks</h2>

<p>Visual testing tools compare screenshots to ensure tested webpages look pixel perfect.  Capturing webpage screenshots requires a full-featured web browser to render CSS and execute JavaScript.  All three of the visual testing tools I found rely on Selenium or PhantomJS for rendering.</p>

<h3>PhantomCSS</h3>

<p><a href="https://github.com/Huddle/PhantomCSS">PhantomCSS</a> uses PhantomJS for screenshot differencing.  PhantomCSS won&rsquo;t integrate directly with the Django live server or your Python test suite, so if you want to run a visual integration test, you&rsquo;d need to manually start and stop the test server between tests.  I might eventually try out PhantomCSS for CSS unit tests, but I wanted to visually test my full website so I needed integration with the Django live server.</p>

<h3>Django-casper</h3>

<p><a href="https://github.com/dobarkod/django-casper">Django-casper</a> uses Django live server tests to execute CasperJS test files (which use PhantomJS) to compare screenshots.  Each test requires an additional Python test which references a JavaScript file that executes the navigation and screenshotting code.  I found this approach messy and difficult to setup.</p>

<h3>Needle</h3>

<p>The <a href="https://github.com/bfirsh/needle">needle</a> Python library uses Selenium to navigate your website and screenshot rendered pages.  Unfortunately needle has poor test coverage, a seemingly failing test suite, and no change log.  Despite these shortcomings, I went with needle for my visual integration tests because it got the job done.</p>

<h2>Django and Needle</h2>

<p>I used the following mixin to integrate the Django live server with needle.  I used PhantomJS, but Firefox or another Selenium web driver should work as well.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="kn">from</span> <span class="nn">django.test</span> <span class="kn">import</span> <span class="n">LiveServerTestCase</span>
</span><span class='line'><span class="kn">from</span> <span class="nn">needle.cases</span> <span class="kn">import</span> <span class="n">NeedleTestCase</span>
</span><span class='line'><span class="kn">from</span> <span class="nn">selenium.webdriver</span> <span class="kn">import</span> <span class="n">PhantomJS</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">DjangoNeedleTestCase</span><span class="p">(</span><span class="n">NeedleTestCase</span><span class="p">,</span> <span class="n">LiveServerTestCase</span><span class="p">):</span>
</span><span class='line'>
</span><span class='line'>    <span class="sd">&quot;&quot;&quot;Needle test case for use with Django live server&quot;&quot;&quot;</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">driver</span> <span class="o">=</span> <span class="n">PhantomJS</span>
</span><span class='line'>
</span><span class='line'>    <span class="nd">@classmethod</span>
</span><span class='line'>    <span class="k">def</span> <span class="nf">get_web_driver</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
</span><span class='line'>        <span class="k">return</span> <span class="nb">type</span><span class="p">(</span><span class="s">&#39;NeedleWebDriver&#39;</span><span class="p">,</span> <span class="p">(</span><span class="n">NeedleWebDriverMixin</span><span class="p">,</span> <span class="n">cls</span><span class="o">.</span><span class="n">driver</span><span class="p">),</span> <span class="p">{})()</span>
</span></code></pre></td></tr></table></div></figure>


<p>Unfortunately the above code only works with the version of needle on Github.  The PyPI version does not yet include the <code>NeedleWebDriverMixin</code> (which I contributed recently for Django support).  I have created <a href="https://github.com/bfirsh/needle/issues/13">an issue</a> suggesting a new PyPI release be made to resolve this problem.</p>

<h2>Room for improvement</h2>

<p>Currently I only run my visual tests manually.  Visual tests are very brittle and occasionally they just break without any changes.  If I manage to stabilize my visual tests so that they pass consistently on different platforms, I may run them during continuous integration.</p>

<p>Do you have another solution for visual integration testing?  Let me know in the comments.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Extensible JSON Encoder Using Single-dispatch Functions]]></title>
    <link href="http://treyhunner.com/2013/09/singledispatch-json-serializer/"/>
    <updated>2013-09-27T00:00:00-07:00</updated>
    <id>http://treyhunner.com/2013/09/singledispatch-json-serializer</id>
    <content type="html"><![CDATA[<p>Single-dispatch generic functions will be added to Python 3.4 (as proposed in <a href="http://www.python.org/dev/peps/pep-0443/">PEP 443</a>).  When reading about single-dispatch functions I immediately thought of the difficulties I&rsquo;ve had with custom JSON encoders.  Below I explain why custom JSON encoders can complicate your application and how single-dispatch functions could be used to create a simpler JSON encoder.</p>

<h2>JSON Encoding using the json library</h2>

<p>With the current Python <code>json</code> library, using an extensible JSON encoder in your generic application may require some/all of the following:</p>

<ul>
<li>Allowing specification of custom encoder class by client applications</li>
<li>Overriding the default JSON encoder class (or a client-specified one) for any further extensions</li>
<li>Passing JSON encoder classes into other serialization libraries used by your application</li>
</ul>


<h3>Combining JSON encoders</h3>

<p>If you need to compose two custom JSON encoders specified in two different packages, you may need to:</p>

<ul>
<li>Use multiple inheritance and hope the encoders play nicely together</li>
<li>Duplicate code from one of the packages and create a new serializer with single inheritance</li>
<li>Monkey patch one or both of the libraries</li>
</ul>


<h2>JSON encoder using single-dispatch generic functions</h2>

<p>I created a wrapper around the <code>json</code> library to make a JSON encoder using single-dispatch generic functions.  Here&rsquo;s how to use it:</p>

<div><script src='https://gist.github.com/6734816.js?file=example.py'></script>
<noscript><pre><code>from decimal import Decimal

from json_singledispatch import encode


@encode.register(set)
def encode_set(obj):
    return encode(list(obj))


@encode.register(Decimal)
def encode_decimal(obj):
    return encode(str(obj))


print encode({&#39;key&#39;: &quot;value&quot;})
print encode({5, 6})
print encode(Decimal(&quot;5.6&quot;))</code></pre></noscript></div>


<p>As you can see, it&rsquo;s fairly easy to extend the encoder to understand serialization rules for new data types.</p>

<p>The impementation is fairly simple, albeit a hack:</p>

<div><script src='https://gist.github.com/6734816.js?file=json_singledispatch.py'></script>
<noscript><pre><code>import json
from singledispatch import singledispatch


class _CustomEncoder(json.JSONEncoder):
    def default(self, obj):
        for type_, handler in encode.registry.items():
            if isinstance(obj, type_) and type_ is not object:
                return handler(obj)
        return super(_CustomEncoder, self).default(obj)


@singledispatch
def encode(obj, **kwargs):
    return json.dumps(obj, cls=_CustomEncoder, **kwargs)</code></pre></noscript></div>


<p>This code is intended as a proof-of-concept to demonstrate the power of single-dispatch generic functions.  Feel free to use it however you like.</p>

<h2>Related Links</h2>

<ul>
<li><a href="http://lukasz.langa.pl/8/single-dispatch-generic-functions/">What single-dispatch generic functios mean for you</a></li>
<li><a href="http://julien.danjou.info/blog/2013/python-3.4-single-dispatch-generic-function">Python 3.4 single dispatch, a step into generic functions</a></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Log All Outgoing Emails in Django]]></title>
    <link href="http://treyhunner.com/2013/05/django-email-log/"/>
    <updated>2013-05-20T00:00:00-07:00</updated>
    <id>http://treyhunner.com/2013/05/django-email-log</id>
    <content type="html"><![CDATA[<p>Ever needed to determine whether an email was sent from a Django project?  I
made a Django application that does exactly that: <a href="https://github.com/treyhunner/django-email-log">django-email-log</a>.</p>

<p>I got the idea from <a href="http://stackoverflow.com/a/7553759/98187">a StackOverflow answer</a> and I decided to make a real
application out of it.  All emails are stored in a single model which can
easily be viewed, searched, sorted, and filtered from the admin site.</p>

<p>I used test-driven development when making the app and I baked in Python 3
support from the beginning.  I found the process of TDD for a standalone
Python package fairly easy and enjoyable.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Django-simple-history Is Back]]></title>
    <link href="http://treyhunner.com/2013/05/django-simple-history/"/>
    <updated>2013-05-01T00:00:00-07:00</updated>
    <id>http://treyhunner.com/2013/05/django-simple-history</id>
    <content type="html"><![CDATA[<p>I wrote <a href="http://treyhunner.com/2011/09/django-and-model-history/">a post</a> over a year ago about recording a history of changes for Django model instances.  I evaluated three different Django packages to record model history.  My favorite of the options, django-simple-history, was abandoned and development continued through multiple forks.</p>

<p>I recently attempted to revive <a href="https://github.com/treyhunner/django-simple-history">django-simple-history</a>.  I added tests, put it <a href="https://pypi.python.org/pypi/django-simple-history/">on PyPI</a>, and made it easier to use with newer versions of Django.  I moved my fork of the project to git and Github, added Travis and Coveralls support for continuous integration and code coverage tracking, and noted future features on the issue tracker.</p>

<p>Soon after I started writing tests for the project I received feature requests, issues, pull requests, and emails with words of encouragement.  I appreciate all of the help I&rsquo;ve had while reviving the project.  I plan to remain responsive to the suggestions for my fork of the code.  If you&rsquo;d like to help out with the project please feel free to submit an issue, make a pull request, or comment on the code commits on the <a href="https://github.com/treyhunner/django-simple-history">Github page</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Random Name Generator in Python]]></title>
    <link href="http://treyhunner.com/2013/02/random-name-generator/"/>
    <updated>2013-02-17T00:00:00-08:00</updated>
    <id>http://treyhunner.com/2013/02/random-name-generator</id>
    <content type="html"><![CDATA[<p>I&rsquo;ve used multiple websites to generate random names for my test data when
running manual or automated QA tests.</p>

<p>Since discovering DuckDuckGo&rsquo;s <a href="https://duckduckgo.com/?q=random+word">random word</a> generation, I&rsquo;ve
<a href="https://duckduckhack.uservoice.com/forums/5168-ideas-for-duckduckgo-instant-answer-plugins/suggestions/2850418-random-name">hoped</a> that someone would make a DuckDuckGo plugin to generate
random names also.</p>

<p>I didn&rsquo;t want to write my own DuckDuckGo plugin yet so I made a Python-powered
command line tool instead using <a href="https://www.census.gov/genealogy/www/data/1990surnames/index.html">1990 Census data</a>.</p>

<p>The program is called <code>names</code> and can be found on <a href="https://github.com/treyhunner/names">Github</a> and <a href="http://pypi.python.org/pypi/names/0.1">PyPI</a>.</p>

<h3>It&rsquo;s really simple</h3>

<p>It&rsquo;s basically just one file currently that&rsquo;s <a href="https://github.com/treyhunner/names/blob/f99542dc21f48aa82da4406f8ce408e92639430d/names/__init__.py">about 40 lines long</a>.
There is only one feature available from the command line currently: generate a
single random full name.  There&rsquo;s a few more features if importing as a Python
package: generate random last name or generate random first name (with or
without specifying gender), generate random full name (also without or without
gender).</p>

<p>The random name picker relies on the cumulative frequencies listed in the
included Census data files.  Here&rsquo;s the steps that are taken:
1. A random floating point number is chosen between 0.0 and 90.0
2. Name file lines are iterated through until a cumulative frequency is found
that is less than the randomly generated number
3. The name on that line is chosen and returned (or printed out)</p>

<h3>Examples</h3>

<p>Here&rsquo;s how you use it from the command line:</p>

<pre><code>$ names
Kara Lopes
</code></pre>

<p>Here&rsquo;s how you use it as a Python package:</p>

<pre><code>&gt;&gt;&gt; import names
&gt;&gt;&gt; names.get_full_name()
u'Patricia Halford'
&gt;&gt;&gt; names.get_full_name(gender='male')
u'Patrick Keating'
&gt;&gt;&gt; names.get_first_name()
'Bernard'
&gt;&gt;&gt; names.get_first_name(gender='female')
'Christina'
&gt;&gt;&gt; names.get_last_name()
'Szczepanek'
</code></pre>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Django and Model History]]></title>
    <link href="http://treyhunner.com/2011/09/django-and-model-history/"/>
    <updated>2011-09-29T00:00:00-07:00</updated>
    <id>http://treyhunner.com/2011/09/django-and-model-history</id>
    <content type="html"><![CDATA[<p><strong>May 2013 Update:</strong> <a href="http://treyhunner.com/2013/05/django-simple-history/">django-simple-history is back</a></p>

<p>Recently I had a need to store a snapshot of every state of particular model instance in a Django application.  I basically needed version control for the rows in my database tables.  When searching for applications that provided this feature, which I call <strong>model history</strong>, I found <a href="http://djangopackages.com/grids/g/model-audit/">many different approaches</a> but few good comparisons of them.  In an attempt to fill that void, I&rsquo;m going to detail some of my findings here.</p>

<h3>django-reversion</h3>

<p>The <a href="https://github.com/etianen/django-reversion">django-reversion</a> application was started in 2008 by Dave Hall.  Reversion uses only one table to store data for all version-tracked models.  Each version of a model adds a new row to this table, using a JSON object representing the model state.  Models can be reverted to previous versions from the admin interface.  This single-table version structure makes django-reversion very easy to install and to uninstall, but it also creates <a href="http://groups.google.com/group/django-reversion/browse_thread/thread/922b4e42d9577e0b">problems when model fields are changed</a>.</p>

<h3>django-revisions</h3>

<p>The <a href="https://github.com/stdbrouw/django-revisions">django-revisions</a> application was created by Stijn Debrouwere in 2010 because the existing Django model history applications at the time were abandoned or suffered from fundamental design problems.  Revisions uses a model history method called same-table versioning (<a href="http://stdbrouw.github.com/django-revisions/design.html">design details outlined here</a>).  Same-table versioning adds a few fields to each version-tracked model which allows it to record the most recent version of each model as well as old versions in the original model table.  Model changes are simplified because they change all versions at once and no new tables need to be added to use revisions (just new fields on existing tables).  The only problem I found with revisions was that it does not currently support database-level uniqueness constraints.  Adding <code>unique=True</code> to a model field or a <code>unique_together</code> Meta attribute will result in an error.  Currently uniqueness constraints must be specified in a separate way for Revisions to honor them when saving models.</p>

<h3>django-simple-history</h3>

<p>The <a href="https://bitbucket.org/q/django-simple-history/">django-simple-history</a> application was based on code originally written by Marty Alchin, author of Pro Django.  Marty Alchin posted <a href="https://code.djangoproject.com/wiki/AuditTrail">AuditTrail</a> on the Django trac wiki in 2007 and later revised and republished the code in his book Pro Django in 2008, renaming it to HistoricalRecords.  Corey Bertram created django-simple-history from this code and put it online in 2010.</p>

<p>Simple History works by creating a separate &ldquo;historical&rdquo; model for each model that requires an audit trail and storing a snapshot of each changed model instance in that historical model.  For example, a Book model would have a HistoricalBook created from it that would store a new HistoricalBook instance every time a Book instance was changed.  Collisions are avoided by disabling uniqueness constraints and model schema changes are accepted by automatically changing historical models as well.  This method comes at the cost of creating an extra table in the database for each model that needs history.</p>

<h3>My conclusions</h3>

<p>When testing these three applications myself, I immediately eliminated django-reversion because I needed to allow easy model schema changes for my project.  I found that both django-revisions and django-simple-history worked well with schema migrations through <a href="http://south.aeracode.org/docs/about.html">South</a> (which I use on everything).  Django-revisions worked better for data migrations in South (due to only needing to change one model), but the uniqueness constraint problems with django-revisions would have been problematic for some of my models.  So eventually I settled on <a href="https://bitbucket.org/q/django-simple-history/">django-simple-history</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Encrypted Private Keys in Django]]></title>
    <link href="http://treyhunner.com/2010/12/encrypted-private-keys-in-django/"/>
    <updated>2010-12-11T00:00:00-08:00</updated>
    <id>http://treyhunner.com/2010/12/encrypted-private-keys-in-django</id>
    <content type="html"><![CDATA[<p>Uniquely identifiable URLs are necessary for many web applications.  For example, a website that provides book reviews may identify the URL of a specific book like this: <strong>www.example.com/books/8839/</strong>.  The easiest way to identify entities in Django is to use the unique primary key of each object, which by default is an auto-incremented positive integer.</p>

<p>Revealing the primary key of an entity is often not desirable.  An astute visitor of the website mentioned above may be able to guess information from the URL such as how many book reviews are available on the website or how old specific reviews are.</p>

<p>The code snippet below demonstrates one way to use a unique but cryptic identifier for an object without needing to change the way primary keys are generated.  There are two notable extensions to the basic Django Model in the below code:</p>

<ol>
<li>The encrypted_pk and encrypted_id model properties return an AES-encrypted version of the primary key as a 13 character base-36 string.</li>
<li>The get method of the default manager can be queried with an encrypted primary key by using the keyword argument encrypted_pk.</li>
</ol>


<p>Feel free to use this code however you want.</p>

<div><script src='https://gist.github.com/735861.js'></script>
<noscript><pre><code># This code is under the MIT license.
# Inspired by this StackOverflow question:
http://stackoverflow.com/questions/3295405/creating-django-objects-with-a-random-primary-key

import struct
from Crypto.Cipher import DES
from django.db import models


def base36encode(number):
    &quot;&quot;&quot;Encode number to string of alphanumeric characters (0 to z). (Code taken from Wikipedia).&quot;&quot;&quot;
    if not isinstance(number, (int, long)):
        raise TypeError(&#39;number must be an integer&#39;)
    if number &lt; 0:
        raise ValueError(&#39;number must be positive&#39;)

    alphabet = &#39;0123456789abcdefghijklmnopqrstuvwxyz&#39;
    base36 = &#39;&#39;
    while number:
        number, i = divmod(number, 36)
        base36 = alphabet[i] + base36

    return base36 or alphabet[0]


def base36decode(numstr):
    &quot;&quot;&quot;Convert a base-36 string (made of alphanumeric characters) to its numeric value.&quot;&quot;&quot;
    return int(numstr,36)


class EncryptedPKModelManager(models.Manager):
    &quot;&quot;&quot;This manager allows models to be identified based on their encrypted_pk value.&quot;&quot;&quot;
    def get(self, *args, **kwargs):
        encrypted_pk = kwargs.pop(&#39;encrypted_pk&#39;, None)
        if encrypted_pk:
            # If found, decrypt encrypted_pk argument and set pk argument to the appropriate value
            kwargs[&#39;pk&#39;] = struct.unpack(&#39;&lt;Q&#39;, self.model.encryption_obj.decrypt(
                struct.pack(&#39;&lt;Q&#39;, base36decode(encrypted_pk))
            ))[0]
        return super(EncryptedPKModelManager, self).get(*args, **kwargs)


class EncryptedPKModel(models.Model):
    &quot;&quot;&quot;Adds encrypted_pk property to children which returns the encrypted value of the primary key.&quot;&quot;&quot;
    encryption_obj = DES.new(&#39;8charkey&#39;) # This 8 character secret key should be changed!

    def __init__(self, *args, **kwargs):
        super(EncryptedPKModel, self).__init__(*args, **kwargs)
        setattr(
            self.__class__,
            &quot;encrypted_%s&quot; % (self._meta.pk.name,),
            property(self.__class__._encrypted_pk)
        )

    def _encrypted_pk(self):
        return base36encode(struct.unpack(&#39;&lt;Q&#39;, self.encryption_obj.encrypt(
            str(struct.pack(&#39;&lt;Q&#39;, self.pk))
        ))[0])

    encrypted_pk = property(_encrypted_pk)

    class Meta:
        abstract = True


class ExampleModelManager(EncryptedPKModelManager):
    pass


class ExampleModel(EncryptedPKModel):
    objects = ExampleModelManager()
    example_field = models.CharField(max_length=32)


# Example usage:
# example_instance = ExampleModel.objects.get(pk=1)
# url_pk = example_instance.encrypted_pk
# ExampleModel.objects.get(encrypted_pk=url_pk)
</code></pre></noscript></div>



]]></content>
  </entry>
  
</feed>
