Source

Python 3 Patterns & Idioms / html / Comprehensions.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Comprehensions &mdash; Python 3 Patterns, Recipes and Idioms</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '1.0',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="shortcut icon" href="_static/favicon.ico"/>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
    <link rel="next" title="Coroutines & Concurrency" href="CoroutinesAndConcurrency.html" />
    <link rel="prev" title="Generators, Iterators, and Itertools" href="GeneratorsIterators.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="CoroutinesAndConcurrency.html" title="Coroutines & Concurrency"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="GeneratorsIterators.html" title="Generators, Iterators, and Itertools"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="comprehensions">
<span id="index-1"></span><h1>Comprehensions<a class="headerlink" href="#comprehensions" title="Permalink to this headline"></a></h1>
<p>History: where did they come from?</p>
<p>They require a mind shift.</p>
<p>What makes them so compelling (once you &#8216;get it&#8217;)?</p>
<p>A two-level list comprehension using <tt class="docutils literal"><span class="pre">os.walk()</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Comprehensions/os_walk_comprehension.py</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="n">restFiles</span> <span class="o">=</span> <span class="p">[</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="mf">0</span><span class="p">],</span> <span class="n">f</span><span class="p">)</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="s">&quot;.&quot;</span><span class="p">)</span>
             <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="mf">2</span><span class="p">]</span> <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s">&quot;.rst&quot;</span><span class="p">)]</span>
<span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">restFiles</span><span class="p">:</span>
    <span class="k">print</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="a-more-complex-example">
<h2>A More Complex Example<a class="headerlink" href="#a-more-complex-example" title="Permalink to this headline"></a></h2>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This will get a full description of all parts.</p>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># CodeManager.py</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">TODO: Break check into two pieces?</span>
<span class="sd">TODO: update() is still only in test mode; doesn&#39;t actually work yet.</span>

<span class="sd">Extracts, displays, checks and updates code examples in restructured text (.rst)</span>
<span class="sd">files.</span>

<span class="sd">You can just put in the codeMarker and the (indented) first line (containing the</span>
<span class="sd">file path) into your restructured text file, then run the update program to</span>
<span class="sd">automatically insert the rest of the file.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">re</span><span class="o">,</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">shutil</span><span class="o">,</span> <span class="nn">inspect</span><span class="o">,</span> <span class="nn">difflib</span>

<span class="n">restFiles</span> <span class="o">=</span> <span class="p">[</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="mf">0</span><span class="p">],</span> <span class="n">f</span><span class="p">)</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="s">&quot;.&quot;</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="s">&quot;_test&quot;</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span>
             <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="mf">2</span><span class="p">]</span> <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s">&quot;.rst&quot;</span><span class="p">)]</span>

<span class="k">class</span> <span class="nc">Languages</span><span class="p">:</span>
    <span class="s">&quot;Strategy design pattern&quot;</span>

    <span class="k">class</span> <span class="nc">Python</span><span class="p">:</span>
        <span class="n">codeMarker</span> <span class="o">=</span> <span class="s">&quot;::</span><span class="se">\n\n</span><span class="s">&quot;</span>
        <span class="n">commentTag</span> <span class="o">=</span> <span class="s">&quot;#&quot;</span>
        <span class="n">listings</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">&quot;::</span><span class="se">\n\n</span><span class="s">( {4}#.*(?:</span><span class="se">\n</span><span class="s">+ {4}.*)*)&quot;</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">Java</span><span class="p">:</span>
        <span class="n">codeMarker</span> <span class="o">=</span> <span class="s">&quot;..  code-block:: java</span><span class="se">\n\n</span><span class="s">&quot;</span>
        <span class="n">commentTag</span> <span class="o">=</span> <span class="s">&quot;//&quot;</span>
        <span class="n">listings</span> <span class="o">=</span> \
            <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">&quot;.. *code-block:: *java</span><span class="se">\n\n</span><span class="s">( {4}//.*(?:</span><span class="se">\n</span><span class="s">+ {4}.*)*)&quot;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">shift</span><span class="p">(</span><span class="n">listing</span><span class="p">):</span>
    <span class="s">&quot;Shift the listing left by 4 spaces&quot;</span>
    <span class="k">return</span> <span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mf">4</span><span class="p">:]</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&quot;    &quot;</span><span class="p">)</span> <span class="k">else</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">listing</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()]</span>

<span class="c"># TEST - makes duplicates of the rst files in a test directory to test update():</span>
<span class="n">dirs</span> <span class="o">=</span> <span class="n">set</span><span class="p">([</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&quot;_test&quot;</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">f</span><span class="p">))</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">restFiles</span><span class="p">])</span>
<span class="k">if</span> <span class="p">[</span><span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">dirs</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">d</span><span class="p">)]:</span>
    <span class="p">[</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&quot;_test&quot;</span><span class="p">,</span> <span class="n">f</span><span class="p">))</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">restFiles</span><span class="p">]</span>
<span class="n">testFiles</span> <span class="o">=</span> <span class="p">[</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="mf">0</span><span class="p">],</span> <span class="n">f</span><span class="p">)</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="s">&quot;_test&quot;</span><span class="p">)</span>
             <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="mf">2</span><span class="p">]</span> <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s">&quot;.rst&quot;</span><span class="p">)]</span>

<span class="k">class</span> <span class="nc">Commands</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Each static method can be called from the command line. Add a new static</span>
<span class="sd">    method here to add a new command to the program.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">display</span><span class="p">(</span><span class="n">language</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Print all the code listings in the .rst files.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">restFiles</span><span class="p">:</span>
            <span class="n">listings</span> <span class="o">=</span> <span class="n">language</span><span class="o">.</span><span class="n">listings</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">listings</span><span class="p">:</span> <span class="k">continue</span>
            <span class="k">print</span><span class="p">(</span><span class="s">&#39;=&#39;</span> <span class="o">*</span> <span class="mf">60</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="n">f</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="s">&#39;=&#39;</span> <span class="o">*</span> <span class="mf">60</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">l</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">listings</span><span class="p">):</span>
                <span class="k">print</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">shift</span><span class="p">(</span><span class="n">l</span><span class="p">)))</span>
                <span class="k">if</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">listings</span><span class="p">)</span> <span class="o">-</span> <span class="mf">1</span><span class="p">:</span>
                    <span class="k">print</span><span class="p">(</span><span class="s">&#39;-&#39;</span> <span class="o">*</span> <span class="mf">60</span><span class="p">)</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">extract</span><span class="p">(</span><span class="n">language</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Pull the code listings from the .rst files and write each listing into</span>
<span class="sd">        its own file. Will not overwrite if code files and .rst files disagree</span>
<span class="sd">        unless you say &quot;extract -force&quot;.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">force</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span> <span class="o">==</span> <span class="mf">3</span> <span class="ow">and</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">2</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;-force&#39;</span>
        <span class="n">paths</span> <span class="o">=</span> <span class="n">set</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">listing</span> <span class="ow">in</span> <span class="p">[</span><span class="n">shift</span><span class="p">(</span><span class="n">listing</span><span class="p">)</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">restFiles</span>
                    <span class="k">for</span> <span class="n">listing</span> <span class="ow">in</span> <span class="n">language</span><span class="o">.</span><span class="n">listings</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())]:</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">listing</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nb">len</span><span class="p">(</span><span class="n">language</span><span class="o">.</span><span class="n">commentTag</span><span class="p">):]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">paths</span><span class="p">:</span>
                <span class="k">print</span><span class="p">(</span><span class="s">&quot;ERROR: Duplicate file name: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">path</span><span class="p">)</span>
                <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">paths</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&quot;..&quot;</span><span class="p">,</span> <span class="s">&quot;code&quot;</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span>
            <span class="n">dirname</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">dirname</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">dirname</span><span class="p">):</span>
                <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">dirname</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">force</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">difflib</span><span class="o">.</span><span class="n">ndiff</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">splitlines</span><span class="p">(),</span> <span class="n">listing</span><span class="p">):</span>
                    <span class="k">if</span> <span class="n">i</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&quot;+ &quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">i</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&quot;- &quot;</span><span class="p">):</span>
                        <span class="k">print</span><span class="p">(</span><span class="s">&quot;ERROR: Existing file different from .rst&quot;</span><span class="p">)</span>
                        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Use &#39;extract -force&#39; to force overwrite&quot;</span><span class="p">)</span>
                        <span class="n">Commands</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="n">language</span><span class="p">)</span>
                        <span class="k">return</span>
            <span class="nb">file</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">listing</span><span class="p">))</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="n">language</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Ensure that external code files exist and check which external files</span>
<span class="sd">        have changed from what&#39;s in the .rst files. Generate files in the</span>
<span class="sd">        _deltas subdirectory showing what has changed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">class</span> <span class="nc">Result</span><span class="p">:</span> <span class="c"># Messenger</span>
            <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span> <span class="o">=</span> <span class="n">kwargs</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">Result</span><span class="p">(</span><span class="n">missing</span> <span class="o">=</span> <span class="p">[],</span> <span class="n">deltas</span> <span class="o">=</span> <span class="p">[])</span>
        <span class="n">listings</span> <span class="o">=</span> <span class="p">[</span><span class="n">Result</span><span class="p">(</span><span class="n">code</span> <span class="o">=</span> <span class="n">shift</span><span class="p">(</span><span class="n">code</span><span class="p">),</span> <span class="nb">file</span> <span class="o">=</span> <span class="n">f</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">restFiles</span> <span class="k">for</span> <span class="n">code</span> <span class="ow">in</span>
                    <span class="n">language</span><span class="o">.</span><span class="n">listings</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())]</span>
        <span class="n">paths</span> <span class="o">=</span> <span class="p">[</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">normpath</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&quot;..&quot;</span><span class="p">,</span> <span class="s">&quot;code&quot;</span><span class="p">,</span> <span class="n">path</span><span class="p">))</span> <span class="k">for</span> <span class="n">path</span> <span class="ow">in</span>
                    <span class="p">[</span><span class="n">listing</span><span class="o">.</span><span class="n">code</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()[</span><span class="nb">len</span><span class="p">(</span><span class="n">language</span><span class="o">.</span><span class="n">commentTag</span><span class="p">):]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
                     <span class="k">for</span> <span class="n">listing</span> <span class="ow">in</span> <span class="n">listings</span><span class="p">]]</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s">&quot;_deltas&quot;</span><span class="p">):</span>
            <span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="s">&quot;_deltas&quot;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">path</span><span class="p">,</span> <span class="n">listing</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">paths</span><span class="p">,</span> <span class="n">listings</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
                <span class="n">result</span><span class="o">.</span><span class="n">missing</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">code</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">difflib</span><span class="o">.</span><span class="n">ndiff</span><span class="p">(</span><span class="n">listing</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">code</span><span class="p">):</span>
                    <span class="k">if</span> <span class="n">i</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&quot;+ &quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">i</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&quot;- &quot;</span><span class="p">):</span>
                        <span class="n">d</span> <span class="o">=</span> <span class="n">difflib</span><span class="o">.</span><span class="n">HtmlDiff</span><span class="p">()</span>
                        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s">&quot;_deltas&quot;</span><span class="p">):</span>
                            <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="s">&quot;_deltas&quot;</span><span class="p">)</span>
                        <span class="n">html</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&quot;_deltas&quot;</span><span class="p">,</span>
                            <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">path</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">)[</span><span class="mf">0</span><span class="p">]</span> <span class="o">+</span> <span class="s">&quot;.html&quot;</span><span class="p">)</span>
                        <span class="nb">open</span><span class="p">(</span><span class="n">html</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">write</span><span class="p">(</span>
                            <span class="s">&quot;&lt;html&gt;&lt;h1&gt;Left: </span><span class="si">%s</span><span class="s">&lt;br&gt;Right: </span><span class="si">%s</span><span class="s">&lt;/h1&gt;&quot;</span> <span class="o">%</span>
                            <span class="p">(</span><span class="n">listing</span><span class="o">.</span><span class="n">file</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span> <span class="o">+</span>
                            <span class="n">d</span><span class="o">.</span><span class="n">make_file</span><span class="p">(</span><span class="n">listing</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">code</span><span class="p">))</span>
                        <span class="n">result</span><span class="o">.</span><span class="n">deltas</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Result</span><span class="p">(</span><span class="nb">file</span> <span class="o">=</span> <span class="n">listing</span><span class="o">.</span><span class="n">file</span><span class="p">,</span>
                            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">,</span> <span class="n">html</span> <span class="o">=</span> <span class="n">html</span><span class="p">,</span> <span class="n">code</span> <span class="o">=</span> <span class="n">code</span><span class="p">))</span>
                        <span class="k">break</span>
        <span class="k">if</span> <span class="n">result</span><span class="o">.</span><span class="n">missing</span><span class="p">:</span>
            <span class="k">print</span><span class="p">(</span><span class="s">&quot;Missing </span><span class="si">%s</span><span class="s"> files:</span><span class="se">\n</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span>
                  <span class="p">(</span><span class="n">language</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">missing</span><span class="p">)))</span>
        <span class="k">for</span> <span class="n">delta</span> <span class="ow">in</span> <span class="n">result</span><span class="o">.</span><span class="n">deltas</span><span class="p">:</span>
            <span class="k">print</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s"> changed in </span><span class="si">%s</span><span class="s">; see </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span>
                  <span class="p">(</span><span class="n">delta</span><span class="o">.</span><span class="n">file</span><span class="p">,</span> <span class="n">delta</span><span class="o">.</span><span class="n">path</span><span class="p">,</span> <span class="n">delta</span><span class="o">.</span><span class="n">html</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">result</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="n">language</span><span class="p">):</span> <span class="c"># Test until it is trustworthy</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Refresh external code files into .rst files.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">check_result</span> <span class="o">=</span> <span class="n">Commands</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="n">language</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">check_result</span><span class="o">.</span><span class="n">missing</span><span class="p">:</span>
            <span class="k">print</span><span class="p">(</span><span class="n">language</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="s">&quot;update aborted&quot;</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="n">changed</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="k">def</span> <span class="nf">_update</span><span class="p">(</span><span class="n">matchobj</span><span class="p">):</span>
            <span class="n">listing</span> <span class="o">=</span> <span class="n">shift</span><span class="p">(</span><span class="n">matchobj</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mf">1</span><span class="p">))</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">listing</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()[</span><span class="nb">len</span><span class="p">(</span><span class="n">language</span><span class="o">.</span><span class="n">commentTag</span><span class="p">):]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="n">filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">path</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">)[</span><span class="mf">0</span><span class="p">]</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&quot;..&quot;</span><span class="p">,</span> <span class="s">&quot;code&quot;</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span>
            <span class="n">code</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span>
            <span class="k">return</span> <span class="n">language</span><span class="o">.</span><span class="n">codeMarker</span> <span class="o">+</span> \
                <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([(</span><span class="s">&quot;    &quot;</span> <span class="o">+</span> <span class="n">line</span><span class="p">)</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">listing</span><span class="p">])</span>
        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">testFiles</span><span class="p">:</span>
            <span class="n">updated</span> <span class="o">=</span> <span class="n">language</span><span class="o">.</span><span class="n">listings</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">_update</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
            <span class="nb">open</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">updated</span><span class="p">)</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">commands</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">inspect</span><span class="o">.</span><span class="n">getmembers</span><span class="p">(</span><span class="n">Commands</span><span class="p">,</span> <span class="n">inspect</span><span class="o">.</span><span class="n">isfunction</span><span class="p">))</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">2</span> <span class="ow">or</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">commands</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Command line options:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">commands</span><span class="p">:</span>
            <span class="k">print</span><span class="p">(</span><span class="n">name</span> <span class="o">+</span> <span class="s">&quot;: &quot;</span> <span class="o">+</span> <span class="n">commands</span><span class="p">[</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">__doc__</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">language</span> <span class="ow">in</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getmembers</span><span class="p">(</span><span class="n">Languages</span><span class="p">,</span> <span class="n">inspect</span><span class="o">.</span><span class="n">isclass</span><span class="p">):</span>
            <span class="n">commands</span><span class="p">[</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">1</span><span class="p">]](</span><span class="n">language</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/cover.png" alt="Logo"/>
            </a></p>
    <font color="Red">This book is in early development; you will find parts that are incorrect &amp; incomplete.</font>
    
            <h3><a href="index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">Comprehensions</a><ul>
<li><a class="reference external" href="#a-more-complex-example">A More Complex Example</a></li>
</ul>
</li>
</ul>


            <h4>Previous topic</h4>
            <p class="topless"><a href="GeneratorsIterators.html"
                                  title="previous chapter">Generators, Iterators, and Itertools</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="CoroutinesAndConcurrency.html"
                                  title="next chapter">Coroutines &amp; Concurrency</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/Comprehensions.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
    
	  <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
	      <p style="font-size: 90%">Enter search terms or a module, class or function name.</p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
    <h4><a href="http://www.mindviewinc.com/Books/Python3Patterns/Index.php">Project Homepage</a></h4>
    <h4><a href="http://www.bitbucket.org/BruceEckel/python-3-patterns-idioms/issues/">Corrections/Suggestions</a></h4>
    <h4><a href="http://www.mindviewinc.com/Consulting/Index.php">Consulting &amp; Training</a></h4><br><br>

        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="CoroutinesAndConcurrency.html" title="Coroutines & Concurrency"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="GeneratorsIterators.html" title="Generators, Iterators, and Itertools"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Python 3 Patterns, Recipes and Idioms</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
      Last updated on Jan 22, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.
    </div>
  </body>
</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.