Mathieu D. avatar Mathieu D. committed 5426ffd

rebuild

Comments (0)

Files changed (82)

+# Sphinx build info version 1
+# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
+config: 5f2ef99f1e761f3cd9ee3406f236b62d
+tags: fbb0d17656682115ca4d033fb2f83ba1

html/AppFrameworks.html.orig

+
+
+<!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>Building Application Frameworks &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_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/underscore.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <link rel="shortcut icon" href="_static/favicon.ico"/>
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
+    <link rel="next" title="Fronting for an Implementation" href="Fronting.html" />
+    <link rel="prev" title="The Singleton" href="Singleton.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="Fronting.html" title="Fronting for an Implementation"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="Singleton.html" title="The Singleton"
+             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="building-application-frameworks">
+<h1>Building Application Frameworks<a class="headerlink" href="#building-application-frameworks" title="Permalink to this headline">¶</a></h1>
+<p>An application framework allows you to inherit from a class or set of classes
+and create a new application, reusing most of the code in the existing classes
+and overriding one or more methods in order to customize the application to your
+needs. A fundamental concept in the application framework is the <em>Template
+Method</em> which is typically hidden beneath the covers and drives the application
+by calling the various methods in the base class (some of which you have
+overridden in order to create the application).</p>
+<p>For example, whenever you create an applet you&#8217;re using an application
+framework: you inherit from <strong>JApplet</strong> and then override <strong>init( )</strong>. The
+applet mechanism (which is a <em>Template Method</em>) does the rest by drawing the
+screen, handling the event loop, resizing, etc.</p>
+<div class="section" id="template-method">
+<h2>Template Method<a class="headerlink" href="#template-method" title="Permalink to this headline">¶</a></h2>
+<p>An important characteristic of the <em>Template Method</em> is that it is defined in
+the base class and cannot be changed. It&#8217;s sometimes a <strong>private</strong> method but
+it&#8217;s virtually always <strong>final</strong>. It calls other base-class methods (the ones you
+override) in order to do its job, but it is usually called only as part of an
+initialization process (and thus the client programmer isn&#8217;t necessarily able to
+call it directly):</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># AppFrameworks/TemplateMethod.py</span>
+<span class="c"># Simple demonstration of Template Method.</span>
+
+<span class="k">class</span> <span class="nc">ApplicationFramework</span><span class="p">:</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="bp">self</span><span class="o">.</span><span class="n">__templateMethod</span><span class="p">()</span>
+    <span class="k">def</span> <span class="nf">__templateMethod</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <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="mi">5</span><span class="p">):</span>
+            <span class="bp">self</span><span class="o">.</span><span class="n">customize1</span><span class="p">()</span>
+            <span class="bp">self</span><span class="o">.</span><span class="n">customize2</span><span class="p">()</span>
+
+<span class="c"># Create an &quot;application&quot;:</span>
+<span class="k">class</span> <span class="nc">MyApp</span><span class="p">(</span><span class="n">ApplicationFramework</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">customize1</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Nudge, nudge, wink, wink! &quot;</span><span class="p">,)</span>
+    <span class="k">def</span> <span class="nf">customize2</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Say no more, Say no more!&quot;</span><span class="p">)</span>
+
+<span class="n">MyApp</span><span class="p">()</span>
+</pre></div>
+</div>
+<p>The base-class constructor is responsible for performing the necessary
+initialization and then starting the &#8220;engine&#8221; (the template method) that runs
+the application (in a GUI application, this &#8220;engine&#8221; would be the main event
+loop). The client programmer simply provides definitions for <strong>customize1( )</strong>
+and <strong>customize2( )</strong> and the &#8220;application&#8221; is ready to run.</p>
+<p>We&#8217;ll see <em>Template Method</em> numerous other times throughout the book.</p>
+</div>
+<div class="section" id="exercises">
+<h2>Exercises<a class="headerlink" href="#exercises" title="Permalink to this headline">¶</a></h2>
+<ol class="arabic">
+<li><p class="first">Create a framework that takes a list of file names on the command line. It
+opens each file except the last for reading, and the last for writing. The
+framework will process each input file using an undetermined policy and
+write the output to the last file. Inherit to customize this framework to
+create two separate applications:</p>
+<blockquote>
+<div><ol class="arabic simple">
+<li>Converts all the letters in each file to uppercase.</li>
+<li>Searches the files for words given in the first file.</li>
+</ol>
+</div></blockquote>
+</li>
+</ol>
+</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 internal" href="#">Building Application Frameworks</a><ul>
+<li><a class="reference internal" href="#template-method">Template Method</a></li>
+<li><a class="reference internal" href="#exercises">Exercises</a></li>
+</ul>
+</li>
+</ul>
+
+
+  <h4>Previous topic</h4>
+  <p class="topless"><a href="Singleton.html"
+                        title="previous chapter">The Singleton</a></p>
+  <h4>Next topic</h4>
+  <p class="topless"><a href="Fronting.html"
+                        title="next chapter">Fronting for an Implementation</a></p>
+  <h3>This Page</h3>
+  <ul class="this-page-menu">
+    <li><a href="_sources/AppFrameworks.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" />
+      <input type="submit" value="Go" />
+      <input type="hidden" name="check_keywords" value="yes" />
+      <input type="hidden" name="area" value="default" />
+    </form>
+    <p class="searchtip" 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"
+             >index</a></li>
+        <li class="right" >
+          <a href="Fronting.html" title="Fronting for an Implementation"
+             >next</a> |</li>
+        <li class="right" >
+          <a href="Singleton.html" title="The Singleton"
+             >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 08, 2012.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
+    </div>
+  </body>
+</html>

html/CanonicalScript.html.orig

+
+
+<!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>A Canonical Form for Command-Line Programs &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_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/underscore.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <link rel="shortcut icon" href="_static/favicon.ico"/>
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
+    <link rel="next" title="Messenger/Data Transfer Object" href="Messenger.html" />
+    <link rel="prev" title="Discovering the Details About Your Platform" href="MachineDiscovery.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="Messenger.html" title="Messenger/Data Transfer Object"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="MachineDiscovery.html" title="Discovering the Details About Your Platform"
+             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="a-canonical-form-for-command-line-programs">
+<span id="index-0"></span><h1>A Canonical Form for Command-Line Programs<a class="headerlink" href="#a-canonical-form-for-command-line-programs" title="Permalink to this headline">¶</a></h1>
+<p>Creating Python programs for command-line use involves a certain amount of
+repetitious coding, which can often be left off or forgotten. Here is a form
+which includes everthing.</p>
+<p>Note that if you are using Windows, you can add Python programs to your &#8220;File
+New&#8221; menu and automatically include the above text in the new file. <a class="reference external" href="http://articles.techrepublic.com.com/5100-10878_11-5034852.html">This
+article</a>
+shows you how. Other operating systems have their own automation features.</p>
+</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>
+    
+
+  <h4>Previous topic</h4>
+  <p class="topless"><a href="MachineDiscovery.html"
+                        title="previous chapter">Discovering the Details About Your Platform</a></p>
+  <h4>Next topic</h4>
+  <p class="topless"><a href="Messenger.html"
+                        title="next chapter">Messenger/Data Transfer Object</a></p>
+  <h3>This Page</h3>
+  <ul class="this-page-menu">
+    <li><a href="_sources/CanonicalScript.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" />
+      <input type="submit" value="Go" />
+      <input type="hidden" name="check_keywords" value="yes" />
+      <input type="hidden" name="area" value="default" />
+    </form>
+    <p class="searchtip" 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"
+             >index</a></li>
+        <li class="right" >
+          <a href="Messenger.html" title="Messenger/Data Transfer Object"
+             >next</a> |</li>
+        <li class="right" >
+          <a href="MachineDiscovery.html" title="Discovering the Details About Your Platform"
+             >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 08, 2012.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
+    </div>
+  </body>
+</html>

html/ChangeInterface.html.orig

+
+
+<!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>Changing the Interface &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_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/underscore.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <link rel="shortcut icon" href="_static/favicon.ico"/>
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
+    <link rel="next" title="Table-Driven Code: Configuration Flexibility" href="TableDriven.html" />
+    <link rel="prev" title="Function Objects" href="FunctionObjects.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="TableDriven.html" title="Table-Driven Code: Configuration Flexibility"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="FunctionObjects.html" title="Function Objects"
+             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="changing-the-interface">
+<h1>Changing the Interface<a class="headerlink" href="#changing-the-interface" title="Permalink to this headline">¶</a></h1>
+<p>Sometimes the problem that you&#8217;re solving is as simple as &#8220;I don&#8217;t have the
+interface that I want.&#8221; Two of the patterns in <em>Design Patterns</em> solve this
+problem: <em>Adapter</em> takes one type and produces an interface to some other type.
+<em>Façade</em> creates an interface to a set of classes, simply to provide a more
+comfortable way to deal with a library or bundle of resources.</p>
+<div class="section" id="adapter">
+<h2>Adapter<a class="headerlink" href="#adapter" title="Permalink to this headline">¶</a></h2>
+<p>When you&#8217;ve got <em>this</em>, and you need <em>that</em>, <em>Adapter</em> solves the problem. The
+only requirement is to produce a <em>that</em>, and there are a number of ways you can
+accomplish this adaptation:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># ChangeInterface/Adapter.py</span>
+<span class="c"># Variations on the Adapter pattern.</span>
+
+<span class="k">class</span> <span class="nc">WhatIHave</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span>
+    <span class="k">def</span> <span class="nf">h</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span>
+
+<span class="k">class</span> <span class="nc">WhatIWant</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span>
+
+<span class="k">class</span> <span class="nc">ProxyAdapter</span><span class="p">(</span><span class="n">WhatIWant</span><span class="p">):</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="n">whatIHave</span><span class="p">):</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">whatIHave</span> <span class="o">=</span> <span class="n">whatIHave</span>
+
+    <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="c"># Implement behavior using</span>
+        <span class="c"># methods in WhatIHave:</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">whatIHave</span><span class="o">.</span><span class="n">g</span><span class="p">()</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">whatIHave</span><span class="o">.</span><span class="n">h</span><span class="p">()</span>
+
+<span class="k">class</span> <span class="nc">WhatIUse</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">op</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">whatIWant</span><span class="p">):</span>
+        <span class="n">whatIWant</span><span class="o">.</span><span class="n">f</span><span class="p">()</span>
+
+<span class="c"># Approach 2: build adapter use into op():</span>
+<span class="k">class</span> <span class="nc">WhatIUse2</span><span class="p">(</span><span class="n">WhatIUse</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">op</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">whatIHave</span><span class="p">):</span>
+        <span class="n">ProxyAdapter</span><span class="p">(</span><span class="n">whatIHave</span><span class="p">)</span><span class="o">.</span><span class="n">f</span><span class="p">()</span>
+
+<span class="c"># Approach 3: build adapter into WhatIHave:</span>
+<span class="k">class</span> <span class="nc">WhatIHave2</span><span class="p">(</span><span class="n">WhatIHave</span><span class="p">,</span> <span class="n">WhatIWant</span><span class="p">):</span>
+    <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">g</span><span class="p">()</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">h</span><span class="p">()</span>
+
+<span class="c"># Approach 4: use an inner class:</span>
+<span class="k">class</span> <span class="nc">WhatIHave3</span><span class="p">(</span><span class="n">WhatIHave</span><span class="p">):</span>
+    <span class="k">class</span> <span class="nc">InnerAdapter</span><span class="p">(</span><span class="n">WhatIWant</span><span class="p">):</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="n">outer</span><span class="p">):</span>
+            <span class="bp">self</span><span class="o">.</span><span class="n">outer</span> <span class="o">=</span> <span class="n">outer</span>
+        <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+            <span class="bp">self</span><span class="o">.</span><span class="n">outer</span><span class="o">.</span><span class="n">g</span><span class="p">()</span>
+            <span class="bp">self</span><span class="o">.</span><span class="n">outer</span><span class="o">.</span><span class="n">h</span><span class="p">()</span>
+
+    <span class="k">def</span> <span class="nf">whatIWant</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">return</span> <span class="n">WhatIHave3</span><span class="o">.</span><span class="n">InnerAdapter</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
+
+<span class="n">whatIUse</span> <span class="o">=</span> <span class="n">WhatIUse</span><span class="p">()</span>
+<span class="n">whatIHave</span> <span class="o">=</span> <span class="n">WhatIHave</span><span class="p">()</span>
+<span class="n">adapt</span><span class="o">=</span> <span class="n">ProxyAdapter</span><span class="p">(</span><span class="n">whatIHave</span><span class="p">)</span>
+<span class="n">whatIUse2</span> <span class="o">=</span> <span class="n">WhatIUse2</span><span class="p">()</span>
+<span class="n">whatIHave2</span> <span class="o">=</span> <span class="n">WhatIHave2</span><span class="p">()</span>
+<span class="n">whatIHave3</span> <span class="o">=</span> <span class="n">WhatIHave3</span><span class="p">()</span>
+<span class="n">whatIUse</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="n">adapt</span><span class="p">)</span>
+<span class="c"># Approach 2:</span>
+<span class="n">whatIUse2</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="n">whatIHave</span><span class="p">)</span>
+<span class="c"># Approach 3:</span>
+<span class="n">whatIUse</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="n">whatIHave2</span><span class="p">)</span>
+<span class="c"># Approach 4:</span>
+<span class="n">whatIUse</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="n">whatIHave3</span><span class="o">.</span><span class="n">whatIWant</span><span class="p">())</span>
+</pre></div>
+</div>
+<p>I&#8217;m taking liberties with the term &#8220;proxy&#8221; here, because in <em>Design Patterns</em>
+they assert that a proxy must have an identical interface with the object that
+it is a surrogate for. However, if you have the two words together: &#8220;proxy
+adapter,&#8221; it is perhaps more reasonable.</p>
+</div>
+<div class="section" id="facade">
+<h2>Façade<a class="headerlink" href="#facade" title="Permalink to this headline">¶</a></h2>
+<p>A general principle that I apply when I&#8217;m casting about trying to mold
+requirements into a first-cut object is &#8220;If something is ugly, hide it inside an
+object.&#8221; This is basically what <em>Façade</em> accomplishes. If you have a rather
+confusing collection of classes and interactions that the client programmer
+doesn&#8217;t really need to see, then you can create an interface that is useful for
+the client programmer and that only presents what&#8217;s necessary.</p>
+<p>Façade is often implemented as singleton abstract factory. Of course, you can
+easily get this effect by creating a class containing <strong>static</strong> factory
+methods:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># ChangeInterface/Facade.py</span>
+<span class="k">class</span> <span class="nc">A</span><span class="p">:</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="n">x</span><span class="p">):</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">B</span><span class="p">:</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="n">x</span><span class="p">):</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">C</span><span class="p">:</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="n">x</span><span class="p">):</span> <span class="k">pass</span>
+
+<span class="c"># Other classes that aren&#39;t exposed by the</span>
+<span class="c"># facade go here ...</span>
+
+<span class="k">class</span> <span class="nc">Facade</span><span class="p">:</span>
+    <span class="k">def</span> <span class="nf">makeA</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">return</span> <span class="n">A</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
+    <span class="n">makeA</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">makeA</span><span class="p">)</span>
+    <span class="k">def</span> <span class="nf">makeB</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">return</span> <span class="n">B</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
+    <span class="n">makeB</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">makeB</span><span class="p">)</span>
+    <span class="k">def</span> <span class="nf">makeC</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">return</span> <span class="n">C</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
+    <span class="n">makeC</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">makeC</span><span class="p">)</span>
+
+<span class="c"># The client programmer gets the objects</span>
+<span class="c"># by calling the static methods:</span>
+<span class="n">a</span> <span class="o">=</span> <span class="n">Facade</span><span class="o">.</span><span class="n">makeA</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
+<span class="n">b</span> <span class="o">=</span> <span class="n">Facade</span><span class="o">.</span><span class="n">makeB</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
+<span class="n">c</span> <span class="o">=</span> <span class="n">Facade</span><span class="o">.</span><span class="n">makeC</span><span class="p">(</span><span class="mf">1.0</span><span class="p">);</span>
+</pre></div>
+</div>
+<p>[rewrite this section using research from Larman&#8217;s book]</p>
+<p>Example for Facade (?): my &#8220;nicer&#8221; version of the XML library.</p>
+</div>
+<div class="section" id="exercises">
+<h2>Exercises<a class="headerlink" href="#exercises" title="Permalink to this headline">¶</a></h2>
+<ol class="arabic simple">
+<li>Create an adapter class that automatically loads a two-dimensional array of
+objects into a dictionary as key-value pairs.</li>
+</ol>
+</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 internal" href="#">Changing the Interface</a><ul>
+<li><a class="reference internal" href="#adapter">Adapter</a></li>
+<li><a class="reference internal" href="#facade">Façade</a></li>
+<li><a class="reference internal" href="#exercises">Exercises</a></li>
+</ul>
+</li>
+</ul>
+
+
+  <h4>Previous topic</h4>
+  <p class="topless"><a href="FunctionObjects.html"
+                        title="previous chapter">Function Objects</a></p>
+  <h4>Next topic</h4>
+  <p class="topless"><a href="TableDriven.html"
+                        title="next chapter">Table-Driven Code: Configuration Flexibility</a></p>
+  <h3>This Page</h3>
+  <ul class="this-page-menu">
+    <li><a href="_sources/ChangeInterface.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" />
+      <input type="submit" value="Go" />
+      <input type="hidden" name="check_keywords" value="yes" />
+      <input type="hidden" name="area" value="default" />
+    </form>
+    <p class="searchtip" 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"
+             >index</a></li>
+        <li class="right" >
+          <a href="TableDriven.html" title="Table-Driven Code: Configuration Flexibility"
+             >next</a> |</li>
+        <li class="right" >
+          <a href="FunctionObjects.html" title="Function Objects"
+             >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 08, 2012.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
+    </div>
+  </body>
+</html>

html/Comprehensions.html.orig

+
+
+<!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_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/underscore.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <link rel="shortcut icon" href="_static/favicon.ico"/>
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
+    <link rel="next" title="Coroutines, Concurrency &amp; Distributed Systems" 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 &amp; Distributed Systems"
+             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-0"></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>Comprehensions are constructs that allow sequences to be built from other sequences. Python 2.0 introduced list comprehensions and Python 3.0 comes with dictionary and set comprehensions.</p>
+<div class="section" id="list-comprehensions">
+<h2>List Comprehensions<a class="headerlink" href="#list-comprehensions" title="Permalink to this headline">¶</a></h2>
+<p>A list comprehension consists of the following parts:</p>
+<ul class="simple">
+<li>An Input Sequence.</li>
+<li>A Variable representing members of the input sequence.</li>
+<li>An Optional Predicate expression.</li>
+<li>An Output Expression producing elements of the output list from members of the Input Sequence that satisfy the predicate.</li>
+</ul>
+<p>Say we need to obtain a list of all the integers in a sequence and then square them:</p>
+<div class="highlight-python"><pre>a_list = [1, ‘4’, 9, ‘a’, 0, 4]
+
+squared_ints = [ e**2 for e in a_list if type(e) == types.IntType ]
+
+print squared_ints
+# [ 1, 81, 0, 16 ]</pre>
+</div>
+<img alt="_images/listComprehensions.gif" src="_images/listComprehensions.gif" />
+<ul class="simple">
+<li>The iterator part iterates through each member <strong>e</strong> of the input sequence <strong>a_list</strong>.</li>
+<li>The predicate checks if the member is an integer.</li>
+<li>If the member is an integer then it is passed to the output expression, squared, to become a member of the output list.</li>
+</ul>
+<p>Much the same results can be achieved using the built in functions, <strong>map</strong>, <strong>filter</strong> and the anonymous <strong>lambda</strong> function.</p>
+<p>The filter function applies a predicate to a sequence:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="nb">type</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="o">==</span> <span class="n">types</span><span class="o">.</span><span class="n">IntType</span><span class="p">,</span> <span class="n">a_list</span><span class="p">)</span>
+</pre></div>
+</div>
+<p>Map modifies each member of a sequence:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">e</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="n">a_list</span><span class="p">)</span>
+</pre></div>
+</div>
+<p>The two can be combined:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">e</span><span class="p">:</span> <span class="n">e</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">e</span><span class="p">:</span> <span class="nb">type</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="o">==</span> <span class="n">types</span><span class="o">.</span><span class="n">IntType</span><span class="p">,</span> <span class="n">a_list</span><span class="p">))</span>
+</pre></div>
+</div>
+<p>The above example involves function calls to <strong>map</strong>, <strong>filter</strong>, <strong>type</strong> and two calls to <strong>lambda</strong>. Function calls in Python are expensive. Furthermore the input sequence is traversed through twice and an intermediate list is produced by filter.</p>
+<p>The list comprehension is enclosed within a list so, it is immediately evident that a list is being produced. There is only one function call to <strong>type</strong> and no call to the cryptic <strong>lambda</strong> instead the list comprehension uses a conventional iterator, an expression and an if expression for the optional predicate.</p>
+</div>
+<div class="section" id="nested-comprehensions">
+<h2>Nested Comprehensions<a class="headerlink" href="#nested-comprehensions" title="Permalink to this headline">¶</a></h2>
+<p>An identity matrix of size n is an n by n square matrix with ones on the main diagonal and zeros elsewhere. A 3 by 3 identity matrix is:</p>
+<img alt="_images/idMatrix.png" src="_images/idMatrix.png" />
+<p>In python we can represent such a matrix by a list of lists, where each sub-list represents a row. A 3 by 3 matrix would be represented by the following list:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span> <span class="p">[</span> <span class="mi">1</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="p">[</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span> <span class="p">],</span>
+  <span class="p">[</span> <span class="mi">0</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="p">]</span>
+</pre></div>
+</div>
+<p>The above matrix can be generated by the following comprehension:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span> <span class="p">[</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">item_idx</span> <span class="o">==</span> <span class="n">row_idx</span> <span class="k">else</span> <span class="mi">0</span> <span class="k">for</span> <span class="n">item_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="p">]</span> <span class="k">for</span> <span class="n">row_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="p">]</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="techniques">
+<h2>Techniques<a class="headerlink" href="#techniques" title="Permalink to this headline">¶</a></h2>
+<p>Using <tt class="docutils literal"><span class="pre">zip()</span></tt> and dealing with two or more elements at a time:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">=</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">all_names</span><span class="p">,</span> <span class="bp">self</span><span class="p">)]</span>
+</pre></div>
+</div>
+<p>Multiple types (auto unpacking of a tuple):</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="n">f</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">f</span><span class="p">),</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">cls</span><span class="o">.</span><span class="n">all_slots</span><span class="p">,</span> <span class="n">values</span><span class="p">)]</span>
+</pre></div>
+</div>
+<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="mi">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="mi">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>
+<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="mi">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="mi">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="mi">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="mi">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="nb">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="mi">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="mi">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="mi">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="mi">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="mi">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="mi">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="mi">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="mi">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="nb">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="mi">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="mi">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="mi">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="mi">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="mi">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="mi">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="mi">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="mi">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="mi">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="mi">1</span><span class="p">]](</span><span class="n">language</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="set-comprehensions">
+<h2>Set Comprehensions<a class="headerlink" href="#set-comprehensions" title="Permalink to this headline">¶</a></h2>
+<p>Set comprehensions allow sets to be constructed using the same principles as list comprehensions, the only difference is that resulting sequence is a set.</p>
+<p>Say we have a list of names. The list can contain names which only differ in the case used to represent them,   duplicates and names consisting of only one character. We are only interested in names longer then one character and wish to represent all names in the same format: The first letter should be capitalised, all other characters should be lower case.</p>
+<p>Given the list:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="n">names</span> <span class="o">=</span> <span class="p">[</span> <span class="s">&#39;Bob&#39;</span><span class="p">,</span> <span class="s">&#39;JOHN&#39;</span><span class="p">,</span> <span class="s">&#39;alice&#39;</span><span class="p">,</span> <span class="s">&#39;bob&#39;</span><span class="p">,</span> <span class="s">&#39;ALICE&#39;</span><span class="p">,</span> <span class="s">&#39;J&#39;</span><span class="p">,</span> <span class="s">&#39;Bob&#39;</span> <span class="p">]</span>
+</pre></div>
+</div>
+<p>We require the set:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="p">{</span> <span class="s">&#39;Bob&#39;</span><span class="p">,</span> <span class="s">&#39;John&#39;</span><span class="p">,</span> <span class="s">&#39;Alice&#39;</span> <span class="p">}</span>
+</pre></div>
+</div>
+<p>Note the new syntax for denoting a set. Members are enclosed in curly braces.</p>
+<p>The following set comprehension accomplishes this:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="p">{</span> <span class="n">name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span> <span class="o">+</span> <span class="n">name</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">names</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="p">}</span>
+</pre></div>
+</div>
+</div>
+<div class="section" id="dictionary-comprehensions">
+<h2>Dictionary Comprehensions<a class="headerlink" href="#dictionary-comprehensions" title="Permalink to this headline">¶</a></h2>
+<p>Say we have a dictionary the keys of which are characters and the values of which map to the number of times that character appears in some text. The dictionary currently distinguishes between upper and lower case characters.</p>
+<p>We require a dictionary in which the occurrences of upper and lower case characters are combined:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="n">mcase</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;a&#39;</span><span class="p">:</span><span class="mi">10</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">:</span> <span class="mi">34</span><span class="p">,</span> <span class="s">&#39;A&#39;</span><span class="p">:</span> <span class="mi">7</span><span class="p">,</span> <span class="s">&#39;Z&#39;</span><span class="p">:</span><span class="mi">3</span><span class="p">}</span>
+
+<span class="n">mcase_frequency</span> <span class="o">=</span> <span class="p">{</span> <span class="n">k</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="p">:</span> <span class="n">mcase</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">k</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="n">mcase</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">k</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="mi">0</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">mcase</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span> <span class="p">}</span>
+
+<span class="c"># mcase_frequency == {&#39;a&#39;: 17, &#39;z&#39;: 3, &#39;b&#39;: 34}</span>
+</pre></div>
+</div>
+<div class="admonition note">
+<p class="first admonition-title">Note</p>
+<p class="last">Contributions by Michael Charlton, 3/23/09</p>
+</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 internal" href="#">Comprehensions</a><ul>
+<li><a class="reference internal" href="#list-comprehensions">List Comprehensions</a></li>
+<li><a class="reference internal" href="#nested-comprehensions">Nested Comprehensions</a></li>
+<li><a class="reference internal" href="#techniques">Techniques</a></li>
+<li><a class="reference internal" href="#a-more-complex-example">A More Complex Example</a></li>
+<li><a class="reference internal" href="#set-comprehensions">Set Comprehensions</a></li>
+<li><a class="reference internal" href="#dictionary-comprehensions">Dictionary Comprehensions</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, Concurrency &amp; Distributed Systems</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" />
+      <input type="submit" value="Go" />
+      <input type="hidden" name="check_keywords" value="yes" />
+      <input type="hidden" name="area" value="default" />
+    </form>
+    <p class="searchtip" 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"
+             >index</a></li>
+        <li class="right" >
+          <a href="CoroutinesAndConcurrency.html" title="Coroutines, Concurrency &amp; Distributed Systems"
+             >next</a> |</li>
+        <li class="right" >
+          <a href="GeneratorsIterators.html" title="Generators, Iterators, and Itertools"
+             >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 08, 2012.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
+    </div>
+  </body>
+</html>

html/Contributors.html.orig

+
+
+<!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>Contributors &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_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/underscore.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <link rel="shortcut icon" href="_static/favicon.ico"/>
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
+    <link rel="next" title="ToDo List" href="ToDo.html" />
+    <link rel="prev" title="Python 3 Patterns, Recipes and Idioms" href="index.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="ToDo.html" title="ToDo List"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="index.html" title="Python 3 Patterns, Recipes and Idioms"
+             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="contributors">
+<h1>Contributors<a class="headerlink" href="#contributors" title="Permalink to this headline">¶</a></h1>
+<p>List of contributors.</p>
+<div class="admonition note">
+<p class="first admonition-title">Note</p>
+<p class="last">This needs some thought. I want to include everyone who makes a
+contribution, but I&#8217;d also like to indicate people who have made
+larger contributions &#8211; there&#8217;s no automatic way to do this now
+that we have moved to BitBucket and are using Wikis to allow people
+to make contributions more simply in the beginning.</p>
+</div>
+<div class="section" id="thanks-to">
+<h2>Thanks To<a class="headerlink" href="#thanks-to" title="Permalink to this headline">¶</a></h2>
+<blockquote>
+<div><ul class="simple">
+<li>BitBucket.org and the creators of Mercurial</li>
+<li>Creator(s) of Sphinx</li>
+<li>And of course, Guido and the team for their incessant improvement of Python,
+especially for taking the risk in breaking backward compatibility in Python
+3.0 to refactor the language.</li>
+</ul>
+</div></blockquote>
+<hr class="docutils" />
+<div class="admonition-todo admonition" id="index-0">
+<p class="first admonition-title">Todo</p>
+<p>Yarko (example label of ToDo):</p>
+<ul class="last simple">
+<li>update CSS styles for todo&#8217;s &amp; todo lists;</li>
+<li>look at <a class="reference external" href="http://sphinx.pocoo.org/ext/coverage.html">http://sphinx.pocoo.org/ext/coverage.html</a> for example.</li>
+<li>Autogenerated ToDoLists do not appear in LaTeX output - debug, fix;</li>
+<li>DONE:
+- ToDo does not appear to be created by make dependencies (it&#8217;s autogenerated);
+- update Makefile to always re-generate todo lists;</li>
+</ul>
+</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 internal" href="#">Contributors</a><ul>
+<li><a class="reference internal" href="#thanks-to">Thanks To</a></li>
+</ul>
+</li>
+</ul>
+
+
+  <h4>Previous topic</h4>
+  <p class="topless"><a href="index.html"
+                        title="previous chapter">Python 3 Patterns, Recipes and Idioms</a></p>
+  <h4>Next topic</h4>
+  <p class="topless"><a href="ToDo.html"
+                        title="next chapter">ToDo List</a></p>
+  <h3>This Page</h3>
+  <ul class="this-page-menu">
+    <li><a href="_sources/Contributors.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" />
+      <input type="submit" value="Go" />
+      <input type="hidden" name="check_keywords" value="yes" />
+      <input type="hidden" name="area" value="default" />
+    </form>
+    <p class="searchtip" 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"
+             >index</a></li>
+        <li class="right" >
+          <a href="ToDo.html" title="ToDo List"
+             >next</a> |</li>
+        <li class="right" >
+          <a href="index.html" title="Python 3 Patterns, Recipes and Idioms"
+             >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 08, 2012.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
+    </div>
+  </body>
+</html>

html/CoroutinesAndConcurrency.html.orig

+
+
+<!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>Coroutines, Concurrency &amp; Distributed Systems &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_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/underscore.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <link rel="shortcut icon" href="_static/favicon.ico"/>
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
+    <link rel="next" title="Jython" href="Jython.html" />
+    <link rel="prev" title="Comprehensions" href="Comprehensions.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="Jython.html" title="Jython"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="Comprehensions.html" title="Comprehensions"
+             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="coroutines-concurrency-distributed-systems">
+<span id="index-0"></span><h1>Coroutines, Concurrency &amp; Distributed Systems<a class="headerlink" href="#coroutines-concurrency-distributed-systems" title="Permalink to this headline">¶</a></h1>
+<p>[[ Will probably need to expand this to multiple chapters:</p>
+<ol class="arabic simple">
+<li>Concurrency Concepts</li>
+<li>Coroutines</li>
+<li>Processes</li>
+<li>Threads</li>
+</ol>
+<p>(this isn&#8217;t final; may need different organization or finer grained. However, it should start
+with simpler concepts and progress to the more difficult ones, as above.)</p>
+<p>]]</p>
+<p>Primary focus should be on:</p>
+<ol class="arabic simple">
+<li>Using <tt class="docutils literal"><span class="pre">yield</span></tt> to create coroutines</li>
+<li>Using the new <tt class="docutils literal"><span class="pre">multiprocessing</span></tt> module</li>
+</ol>
+<p>and then showing some alternative techniques.</p>
+<p>foo bar <tt class="xref py py-func docutils literal"><span class="pre">input()</span></tt> baz.</p>
+<div class="section" id="the-gil">
+<h2>The GIL<a class="headerlink" href="#the-gil" title="Permalink to this headline">¶</a></h2>
+<p>The GIL prevents context switches from
+happening in the middle of C code. Basically, it makes any C
+code into a critical section, except when that C code explicitly releases
+the GIL. This greatly simplifies the task of writing extension
+modules as well the Python core.</p>
+<p>The designers of Python made a design decision
+that extension writers would not have to take care of locking.
+Thus, Python is intended to be simple/easy to integrate with any C
+library.  In order to remove the GIL, you&#8217;d have to go into all existing
+C code and write explicit locking/unlocking code, and you&#8217;d have to do this with
+every new C library as well.</p>
+<p>[[ Description of how it supports/impacts reference-counted garbage collection]]</p>
+</div>
+<div class="section" id="multiprocessing">
+<h2>Multiprocessing<a class="headerlink" href="#multiprocessing" title="Permalink to this headline">¶</a></h2>
+<p>Example by Michele Simionato in comp lang python.
+Here is an example of using multiprocessing (which is included
+in Python 2.6 and easy_installable in older Python versions)
+to print a spin bar while a computation is running:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">time</span>
+<span class="kn">import</span> <span class="nn">multiprocessing</span>
+<span class="n">DELAY</span> <span class="o">=</span> <span class="mf">0.1</span>
+<span class="n">DISPLAY</span> <span class="o">=</span> <span class="p">[</span> <span class="s">&#39;|&#39;</span><span class="p">,</span> <span class="s">&#39;/&#39;</span><span class="p">,</span> <span class="s">&#39;-&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">&#39;</span> <span class="p">]</span>
+<span class="k">def</span> <span class="nf">spinner_func</span><span class="p">(</span><span class="n">before</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">after</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">):</span>
+    <span class="n">write</span><span class="p">,</span> <span class="n">flush</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span>
+    <span class="n">pos</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
+    <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
+        <span class="n">pos</span> <span class="o">=</span> <span class="p">(</span><span class="n">pos</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="nb">len</span><span class="p">(</span><span class="n">DISPLAY</span><span class="p">)</span>
+        <span class="n">msg</span> <span class="o">=</span> <span class="n">before</span> <span class="o">+</span> <span class="n">DISPLAY</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">+</span> <span class="n">after</span>
+        <span class="n">write</span><span class="p">(</span><span class="n">msg</span><span class="p">);</span> <span class="n">flush</span><span class="p">()</span>
+        <span class="n">write</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\x08</span><span class="s">&#39;</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">msg</span><span class="p">))</span>
+        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">DELAY</span><span class="p">)</span>
+<span class="k">def</span> <span class="nf">long_computation</span><span class="p">():</span>
+    <span class="c"># emulate a long computation</span>
+    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
+<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
+    <span class="n">spinner</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">Process</span><span class="p">(</span>
+        <span class="bp">None</span><span class="p">,</span> <span class="n">spinner_func</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;Please wait ... &#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">))</span>
+    <span class="n">spinner</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
+    <span class="k">try</span><span class="p">:</span>
+        <span class="n">long_computation</span><span class="p">()</span>
+        <span class="k">print</span> <span class="s">&#39;Computation done&#39;</span>
+    <span class="k">finally</span><span class="p">:</span>
+        <span class="n">spinner</span><span class="o">.</span><span class="n">terminate</span><span class="p">()</span>
+</pre></div>
+</div>
+<p>On the Erlang mail list, four years ago, Erlang expert Joe Armstrong posted this:</p>
+<blockquote>
+<div><p>In Concurrency Oriented (CO) programming you concentrate on the concurrency and the messages between the processes. There is no sharing of data.</p>
+<p>[A program] should be thought of thousands of little black boxes all doing things in parallel - these black boxes can send and receive messages. Black boxes can detect errors in other black boxes - that&#8217;s all.
+...
+Erlang uses a simple functional language inside the [black boxes] - this is not particularly interesting - <em>any</em> language that does the job would do - the important bit is the concurrency.</p>
+</div></blockquote>
+<p>On the Squeak mail list in 1998, Alan Kay had this to say:</p>
+<blockquote>
+<div><p>...Smalltalk is not only NOT its syntax or the class library, it is not even about classes. I&#8217;m sorry that I long ago coined the term &#8220;objects&#8221; for this topic because it gets many people to focus on the lesser idea.</p>
+<p>The big idea is &#8220;messaging&#8221; &#8211; that is what the kernal of Smalltalk/Squeak is all about... The key in making great and growable systems is much more to design how its modules communicate rather than what their internal properties and behaviors should be. Think of the internet &#8211; to live, it (a) has to allow many different kinds of ideas and realizations that are beyond any single standard and (b) to allow varying degrees of safe interoperability between these ideas.</p>
+<p>If you focus on just messaging &#8211; and realize that a good metasystem can late bind the various 2nd level architectures used in objects &#8211; then much of the language-, UI-, and OS based discussions on this thread are really quite moot.</p>
+</div></blockquote>
+</div>
+<div class="section" id="further-reading">
+<h2>Further Reading<a class="headerlink" href="#further-reading" title="Permalink to this headline">¶</a></h2>
+<blockquote>
+<div><p><a class="reference external" href="http://guidewiredevelopment.wordpress.com/2008/10/06/a-more-clearly-stated-version-of-my-argument/">This article</a>
+argues that large-scale parallelism &#8211; which is what
+<tt class="docutils literal"><span class="pre">multiprocessing</span></tt> supports &#8211; is the more important problem to solve, and
+that functional languages don&#8217;t help that much with this problem.</p>
+<p><a class="reference external" href="http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/">http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/</a></p>
+</div></blockquote>
+</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 internal" href="#">Coroutines, Concurrency &amp; Distributed Systems</a><ul>
+<li><a class="reference internal" href="#the-gil">The GIL</a></li>
+<li><a class="reference internal" href="#multiprocessing">Multiprocessing</a></li>
+<li><a class="reference internal" href="#further-reading">Further Reading</a></li>
+</ul>
+</li>
+</ul>
+
+
+  <h4>Previous topic</h4>
+  <p class="topless"><a href="Comprehensions.html"
+                        title="previous chapter">Comprehensions</a></p>
+  <h4>Next topic</h4>
+  <p class="topless"><a href="Jython.html"
+                        title="next chapter">Jython</a></p>
+  <h3>This Page</h3>
+  <ul class="this-page-menu">
+    <li><a href="_sources/CoroutinesAndConcurrency.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" />
+      <input type="submit" value="Go" />
+      <input type="hidden" name="check_keywords" value="yes" />
+      <input type="hidden" name="area" value="default" />
+    </form>
+    <p class="searchtip" 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"
+             >index</a></li>
+        <li class="right" >
+          <a href="Jython.html" title="Jython"
+             >next</a> |</li>
+        <li class="right" >
+          <a href="Comprehensions.html" title="Comprehensions"
+             >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 08, 2012.
+      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
+    </div>
+  </body>
+</html>

html/Decorator.html.orig

+
+
+<!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>Decorator: Dynamic Type Selection &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_INDEX: false,
+        FILE_SUFFIX: '.html',
+        HAS_SOURCE:  true
+      };
+    </script>
+    <script type="text/javascript" src="_static/jquery.js"></script>
+    <script type="text/javascript" src="_static/underscore.js"></script>
+    <script type="text/javascript" src="_static/doctools.js"></script>
+    <link rel="shortcut icon" href="_static/favicon.ico"/>
+    <link rel="top" title="Python 3 Patterns, Recipes and Idioms" href="index.html" />
+    <link rel="next" title="Iterators: Decoupling Algorithms from Containers" href="Iterators.html" />
+    <link rel="prev" title="StateMachine" href="StateMachine.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="Iterators.html" title="Iterators: Decoupling Algorithms from Containers"
+             accesskey="N">next</a> |</li>
+        <li class="right" >
+          <a href="StateMachine.html" title="StateMachine"
+             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="decorator-dynamic-type-selection">
+<h1>Decorator: Dynamic Type Selection<a class="headerlink" href="#decorator-dynamic-type-selection" title="Permalink to this headline">¶</a></h1>
+<div class="admonition note">
+<p class="first admonition-title">Note</p>
+<p class="last">I think we can rewrite this chapter to use Python decorators as
+implementation (thus the decorators chapter should precede this one).</p>
+</div>
+<p>The use of layered objects to dynamically and transparently add responsibilities
+to individual objects is referred to as the <em>decorator</em> pattern.</p>
+<p>Used when subclassing creates too many (&amp; inflexible) classes</p>
+<p>All decorators that wrap around the original object must have the same basic
+interface</p>
+<p>Dynamic proxy/surrogate?</p>
+<p>This accounts for the odd inheritance structure</p>
+<p>Tradeoff: coding is more complicated when using decorators</p>
+<div class="section" id="basic-decorator-structure">
+<h2>Basic Decorator Structure<a class="headerlink" href="#basic-decorator-structure" title="Permalink to this headline">¶</a></h2>
+<img alt="_images/decorator.gif" src="_images/decorator.gif" />
+</div>
+<div class="section" id="a-coffee-example">
+<h2>A Coffee Example<a class="headerlink" href="#a-coffee-example" title="Permalink to this headline">¶</a></h2>
+<p>Consider going down to the local coffee shop, <em>BeanMeUp</em>, for a coffee.  There
+are typically many different drinks on offer &#8211; espressos, lattes, teas, iced
+coffees, hot chocolate to name a few, as well as a number of extras (which cost
+extra too) such as whipped cream or an extra shot of espresso. You can also make
+certain changes to your drink at no extra cost, such as asking for decaf coffee
+instead of regular coffee.</p>
+<p>Quite clearly if we are going to model all these drinks and combinations, there
+will be sizeable class diagrams. So for clarity we will only consider a subset
+of the coffees: Espresso, Espresso Con Panna, Café Late, Cappuccino and Café
+Mocha. We&#8217;ll include 2 extras - whipped cream (&#8220;whipped&#8221;) and an extra shot of
+espresso; and three changes - decaf, steamed milk (&#8220;wet&#8221;) and foamed milk
+(&#8220;dry&#8221;).</p>
+</div>
+<div class="section" id="class-for-each-combination">
+<h2>Class for Each Combination<a class="headerlink" href="#class-for-each-combination" title="Permalink to this headline">¶</a></h2>
+<p>One solution is to create an individual class for every combination. Each class
+describes the drink and is responsible for the cost etc. The resulting menu is
+huge, and a part of the class diagram would look something like this:</p>
+<img alt="_images/coffeeExplosion.gif" src="_images/coffeeExplosion.gif" />
+<p>The key to using this method is to find the particular combination you want.
+So, once you&#8217;ve found the drink you would like, here is how you would use it, as
+shown in the <strong>CoffeeShop</strong> class in the following code:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="c"># Decorator/nodecorators/CoffeeShop.py</span>
+<span class="c"># Coffee example with no decorators</span>
+
+<span class="k">class</span> <span class="nc">Espresso</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">DoubleEspresso</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">EspressoConPanna</span><span class="p">:</span> <span class="k">pass</span>
+
+<span class="k">class</span> <span class="nc">Cappuccino</span><span class="p">:</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="bp">self</span><span class="o">.</span><span class="n">cost</span> <span class="o">=</span> <span class="mi">1</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="o">=</span> <span class="s">&quot;Cappucino&quot;</span>
+    <span class="k">def</span> <span class="nf">getCost</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">cost</span>
+    <span class="k">def</span> <span class="nf">getDescription</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span>
+
+<span class="k">class</span> <span class="nc">CappuccinoDecaf</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CappuccinoDecafWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CappuccinoDry</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CappuccinoDryWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CappuccinoExtraEspresso</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CappuccinoExtraEspressoWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CappuccinoWhipped</span><span class="p">:</span> <span class="k">pass</span>
+
+<span class="k">class</span> <span class="nc">CafeMocha</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeMochaDecaf</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeMochaDecafWhipped</span><span class="p">:</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="bp">self</span><span class="o">.</span><span class="n">cost</span> <span class="o">=</span> <span class="mf">1.25</span>
+        <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="o">=</span> \
+          <span class="s">&quot;Cafe Mocha decaf whipped cream&quot;</span>
+    <span class="k">def</span> <span class="nf">getCost</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">cost</span>
+    <span class="k">def</span> <span class="nf">getDescription</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
+        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span>
+
+<span class="k">class</span> <span class="nc">CafeMochaExtraEspresso</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeMochaExtraEspressoWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeMochaWet</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeMochaWetWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeMochaWhipped</span><span class="p">:</span> <span class="k">pass</span>
+
+<span class="k">class</span> <span class="nc">CafeLatte</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteDecaf</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteDecafWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteExtraEspresso</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteExtraEspressoWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteWet</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteWetWhipped</span><span class="p">:</span> <span class="k">pass</span>
+<span class="k">class</span> <span class="nc">CafeLatteWhipped</span><span class="p">:</span> <span class="k">pass</span>
+
+<span class="n">cappuccino</span> <span class="o">=</span> <span class="n">Cappuccino</span><span class="p">()</span>
+<span class="k">print</span><span class="p">((</span><span class="n">cappuccino</span><span class="o">.</span><span class="n">getDescription</span><span class="p">()</span> <span class="o">+</span> <span class="s">&quot;: $&quot;</span> <span class="o">+</span>
+  <span class="sb">`cappuccino.getCost()`</span><span class="p">))</span>
+
+<span class="n">cafeMocha</span> <span class="o">=</span> <span class="n">CafeMochaDecafWhipped</span><span class="p">()</span>
+<span class="k">print</span><span class="p">((</span><span class="n">cafeMocha</span><span class="o">.</span><span class="n">getDescription</span><span class="p">()</span>
+  <span class="o">+</span> <span class="s">&quot;: $&quot;</span> <span class="o">+</span> <span class="sb">`cafeMocha.getCost()`</span><span class="p">))</span>
+</pre></div>
+</div>
+<p>And here is the corresponding output:</p>
+<div class="highlight-python"><pre>Cappucino: $1.0
+Cafe Mocha decaf whipped cream: $1.25</pre>
+</div>
+<p>You can see that creating the particular combination you want is easy, since you
+are just creating an instance of a class. However, there are a number of
+problems with this approach. Firstly, the combinations are fixed statically so
+that any combination a customer may wish to order needs to be created up front.
+Secondly, the resulting menu is so huge that finding your particular combination
+is difficult and time consuming.</p>
+</div>
+<div class="section" id="the-decorator-approach">
+<h2>The Decorator Approach<a class="headerlink" href="#the-decorator-approach" title="Permalink to this headline">¶</a></h2>
+<p>Another approach would be to break the drinks down into the various components
+such as espresso and foamed milk, and then let the customer combine the
+components to describe a particular coffee.</p>
+<p>In order to do this programmatically, we use the Decorator pattern.  A Decorator
+adds responsibility to a component by wrapping it, but the Decorator conforms to
+the interface of the component it encloses, so the wrapping is transparent.
+Decorators can also be nested without the loss of this transparency.</p>
+<img alt="_images/decoratedCoffee.gif" src="_images/decoratedCoffee.gif" />
+<p>Methods invoked on the Decorator can in turn invoke methods in the component,
+and can of course perform processing before or after the invocation.</p>
+<p>So if we added <strong>getTotalCost()</strong> and <strong>getDescription()</strong> methods to the
+<strong>DrinkComponent</strong> interface, an Espresso looks like this:</p>
+<div class="highlight-python"><pre># Decorator/alldecorators/EspressoDecorator.py
+
+class Espresso(Decorator):
+    cost = 0.75f
+    description = " espresso"
+    def __init__(DrinkComponent):
+        Decorator.__init__(self, component)
+
+    def getTotalCost(self):
+        return self.component.getTotalCost() + cost
+
+    def getDescription(self):
+        return self.component.getDescription() +
+            description</pre>
+</div>
+<p>You combine the components to create a drink as follows, as shown in the code
+below:</p>
+<div class="highlight-python"><pre># Decorator/alldecorators/CoffeeShop.py
+# Coffee example using decorators
+
+class DrinkComponent:
+    def getDescription(self):
+        return self.__class__.__name__
+    def getTotalCost(self):
+        return self.__class__.cost
+
+class Mug(DrinkComponent):
+    cost = 0.0
+
+class Decorator(DrinkComponent):
+    def __init__(self, drinkComponent):
+        self.component = drinkComponent
+    def getTotalCost(self):
+        return self.component.getTotalCost() + \
+          DrinkComponent.getTotalCost(self)
+    def getDescription(self):
+        return self.component.getDescription() + \
+          ' ' + DrinkComponent.getDescription(self)
+
+class Espresso(Decorator):
+    cost = 0.75
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Decaf(Decorator):
+    cost = 0.0
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class FoamedMilk(Decorator):
+    cost = 0.25
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class SteamedMilk(Decorator):
+    cost = 0.25
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Whipped(Decorator):
+    cost = 0.25
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Chocolate(Decorator):
+    cost = 0.25
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+cappuccino = Espresso(FoamedMilk(Mug()))
+print(cappuccino.getDescription().strip() + \)
+  ": $" + `cappuccino.getTotalCost()`
+
+cafeMocha = Espresso(SteamedMilk(Chocolate(
+  Whipped(Decaf(Mug())))))
+
+print(cafeMocha.getDescription().strip() + \)
+  ": $" + `cafeMocha.getTotalCost()`</pre>
+</div>
+<p>This approach would certainly provide the most flexibility and the smallest
+menu. You have a small number of components to choose from, but assembling the
+description of the coffee then becomes rather arduous.</p>
+<p>If you want to describe a plain cappuccino, you create it with:</p>
+<div class="highlight-python"><div class="highlight"><pre><span class="n">plainCap</span> <span class="o">=</span> <span class="n">Espresso</span><span class="p">(</span><span class="n">FoamedMilk</span><span class="p">(</span><span class="n">Mug</span><span class="p">()))</span>
+</pre></div>
+</div>
+<p>Creating a decaf Cafe Mocha with whipped cream requires an even longer
+description.</p>
+</div>
+<div class="section" id="compromise">
+<h2>Compromise<a class="headerlink" href="#compromise" title="Permalink to this headline">¶</a></h2>
+<p>The previous approach takes too long to describe a coffee. There will also be
+certain combinations that you will describe regularly, and it would be
+convenient to have a quick way of describing them.</p>
+<p>The 3rd approach is a mixture of the first 2 approaches, and combines
+flexibility with ease of use. This compromise is achieved by creating a
+reasonably sized menu of basic selections, which would often work exactly as
+they are, but if you wanted to decorate them (whipped cream, decaf etc.) then
+you would use decorators to make the modifications. This is the type of menu you
+are presented with in most coffee shops.</p>
+<img alt="_images/compromiseDecoration.gif" src="_images/compromiseDecoration.gif" />
+<p>Here is how to create a basic selection, as well as a decorated selection:</p>
+<div class="highlight-python"><pre># Decorator/compromise/CoffeeShop.py
+# Coffee example with a compromise of basic
+# combinations and decorators
+
+class DrinkComponent:
+    def getDescription(self):
+        return self.__class__.__name__
+    def getTotalCost(self):
+        return self.__class__.cost
+
+class Espresso(DrinkComponent):
+    cost = 0.75
+
+class EspressoConPanna(DrinkComponent):
+    cost = 1.0
+
+class Cappuccino(DrinkComponent):
+    cost = 1.0
+
+class CafeLatte(DrinkComponent):
+    cost = 1.0
+
+class CafeMocha(DrinkComponent):
+    cost = 1.25
+
+class Decorator(DrinkComponent):
+    def __init__(self, drinkComponent):
+        self.component = drinkComponent
+    def getTotalCost(self):
+        return self.component.getTotalCost() + \
+          DrinkComponent.getTotalCost(self)
+    def getDescription(self):
+        return self.component.getDescription() + \
+          ' ' + DrinkComponent.getDescription(self)
+
+class ExtraEspresso(Decorator):
+    cost = 0.75
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Whipped(Decorator):
+    cost = 0.50
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Decaf(Decorator):
+    cost = 0.0
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Dry(Decorator):
+    cost = 0.0
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+class Wet(Decorator):
+    cost = 0.0
+    def __init__(self, drinkComponent):
+        Decorator.__init__(self, drinkComponent)
+
+cappuccino = Cappuccino()
+print(cappuccino.getDescription() + ": $" + \)
+  `cappuccino.getTotalCost()`
+
+cafeMocha = Whipped(Decaf(CafeMocha()))
+print(cafeMocha.getDescription() + ": $" + \)
+  `cafeMocha.getTotalCost()`</pre>
+</div>
+<p>You can see that creating a basic selection is quick and easy, which makes sense
+since they will be described regularly.  Describing a decorated drink is more
+work than when using a class per combination, but clearly less work than when
+only using decorators.</p>
+<p>The final result is not too many classes, but not too many decorators either.
+Most of the time it&#8217;s possible to get away without using any decorators at all,
+so we have the benefits of both approaches.</p>
+</div>