GIF89a;
Direktori : /usr/share/doc/python-docs-2.7.5/html/tutorial/ |
Current File : //usr/share/doc/python-docs-2.7.5/html/tutorial/stdlib2.html |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>11. Brief Tour of the Standard Library – Part II — Python 2.7.5 documentation</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: '2.7.5', 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> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="The Python Tutorial" href="index.html" /> <link rel="next" title="12. What Now?" href="whatnow.html" /> <link rel="prev" title="10. Brief Tour of the Standard Library" href="stdlib.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </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="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="whatnow.html" title="12. What Now?" accesskey="N">next</a> |</li> <li class="right" > <a href="stdlib.html" title="10. Brief Tour of the Standard Library" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" accesskey="U">The Python Tutorial</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="brief-tour-of-the-standard-library-part-ii"> <span id="tut-brieftourtwo"></span><h1>11. Brief Tour of the Standard Library – Part II<a class="headerlink" href="#brief-tour-of-the-standard-library-part-ii" title="Permalink to this headline">¶</a></h1> <p>This second tour covers more advanced modules that support professional programming needs. These modules rarely occur in small scripts.</p> <div class="section" id="output-formatting"> <span id="tut-output-formatting"></span><h2>11.1. Output Formatting<a class="headerlink" href="#output-formatting" title="Permalink to this headline">¶</a></h2> <p>The <a class="reference internal" href="../library/repr.html#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-mod docutils literal"><span class="pre">repr</span></tt></a> module provides a version of <a class="reference internal" href="../library/repr.html#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> customized for abbreviated displays of large or deeply nested containers:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">repr</span> <span class="gp">>>> </span><span class="nb">repr</span><span class="o">.</span><span class="n">repr</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="s">'supercalifragilisticexpialidocious'</span><span class="p">))</span> <span class="go">"set(['a', 'c', 'd', 'e', 'f', 'g', ...])"</span> </pre></div> </div> <p>The <a class="reference internal" href="../library/pprint.html#module-pprint" title="pprint: Data pretty printer."><tt class="xref py py-mod docutils literal"><span class="pre">pprint</span></tt></a> module offers more sophisticated control over printing both built-in and user defined objects in a way that is readable by the interpreter. When the result is longer than one line, the “pretty printer” adds line breaks and indentation to more clearly reveal data structure:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">pprint</span> <span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="p">[[[[</span><span class="s">'black'</span><span class="p">,</span> <span class="s">'cyan'</span><span class="p">],</span> <span class="s">'white'</span><span class="p">,</span> <span class="p">[</span><span class="s">'green'</span><span class="p">,</span> <span class="s">'red'</span><span class="p">]],</span> <span class="p">[[</span><span class="s">'magenta'</span><span class="p">,</span> <span class="gp">... </span> <span class="s">'yellow'</span><span class="p">],</span> <span class="s">'blue'</span><span class="p">]]]</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span> <span class="go">[[[['black', 'cyan'],</span> <span class="go"> 'white',</span> <span class="go"> ['green', 'red']],</span> <span class="go"> [['magenta', 'yellow'],</span> <span class="go"> 'blue']]]</span> </pre></div> </div> <p>The <a class="reference internal" href="../library/textwrap.html#module-textwrap" title="textwrap: Text wrapping and filling"><tt class="xref py py-mod docutils literal"><span class="pre">textwrap</span></tt></a> module formats paragraphs of text to fit a given screen width:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">textwrap</span> <span class="gp">>>> </span><span class="n">doc</span> <span class="o">=</span> <span class="s">"""The wrap() method is just like fill() except that it returns</span> <span class="gp">... </span><span class="s">a list of strings instead of one big string with newlines to separate</span> <span class="gp">... </span><span class="s">the wrapped lines."""</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="k">print</span> <span class="n">textwrap</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">doc</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">40</span><span class="p">)</span> <span class="go">The wrap() method is just like fill()</span> <span class="go">except that it returns a list of strings</span> <span class="go">instead of one big string with newlines</span> <span class="go">to separate the wrapped lines.</span> </pre></div> </div> <p>The <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><tt class="xref py py-mod docutils literal"><span class="pre">locale</span></tt></a> module accesses a database of culture specific data formats. The grouping attribute of locale’s format function provides a direct way of formatting numbers with group separators:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">locale</span> <span class="gp">>>> </span><span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="s">'English_United States.1252'</span><span class="p">)</span> <span class="go">'English_United States.1252'</span> <span class="gp">>>> </span><span class="n">conv</span> <span class="o">=</span> <span class="n">locale</span><span class="o">.</span><span class="n">localeconv</span><span class="p">()</span> <span class="c"># get a mapping of conventions</span> <span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="mf">1234567.8</span> <span class="gp">>>> </span><span class="n">locale</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">"</span><span class="si">%d</span><span class="s">"</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">grouping</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="go">'1,234,567'</span> <span class="gp">>>> </span><span class="n">locale</span><span class="o">.</span><span class="n">format_string</span><span class="p">(</span><span class="s">"</span><span class="si">%s%.*f</span><span class="s">"</span><span class="p">,</span> <span class="p">(</span><span class="n">conv</span><span class="p">[</span><span class="s">'currency_symbol'</span><span class="p">],</span> <span class="gp">... </span> <span class="n">conv</span><span class="p">[</span><span class="s">'frac_digits'</span><span class="p">],</span> <span class="n">x</span><span class="p">),</span> <span class="n">grouping</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="go">'$1,234,567.80'</span> </pre></div> </div> </div> <div class="section" id="templating"> <span id="tut-templating"></span><h2>11.2. Templating<a class="headerlink" href="#templating" title="Permalink to this headline">¶</a></h2> <p>The <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><tt class="xref py py-mod docutils literal"><span class="pre">string</span></tt></a> module includes a versatile <tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt> class with a simplified syntax suitable for editing by end-users. This allows users to customize their applications without having to alter the application.</p> <p>The format uses placeholder names formed by <tt class="docutils literal"><span class="pre">$</span></tt> with valid Python identifiers (alphanumeric characters and underscores). Surrounding the placeholder with braces allows it to be followed by more alphanumeric letters with no intervening spaces. Writing <tt class="docutils literal"><span class="pre">$$</span></tt> creates a single escaped <tt class="docutils literal"><span class="pre">$</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">string</span> <span class="kn">import</span> <span class="n">Template</span> <span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s">'${village}folk send $$10 to $cause.'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">substitute</span><span class="p">(</span><span class="n">village</span><span class="o">=</span><span class="s">'Nottingham'</span><span class="p">,</span> <span class="n">cause</span><span class="o">=</span><span class="s">'the ditch fund'</span><span class="p">)</span> <span class="go">'Nottinghamfolk send $10 to the ditch fund.'</span> </pre></div> </div> <p>The <tt class="xref py py-meth docutils literal"><span class="pre">substitute()</span></tt> method raises a <a class="reference internal" href="../library/exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> when a placeholder is not supplied in a dictionary or a keyword argument. For mail-merge style applications, user supplied data may be incomplete and the <tt class="xref py py-meth docutils literal"><span class="pre">safe_substitute()</span></tt> method may be more appropriate — it will leave placeholders unchanged if data is missing:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s">'Return the $item to $owner.'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">item</span><span class="o">=</span><span class="s">'unladen swallow'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">substitute</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="gt">Traceback (most recent call last):</span> <span class="c">...</span> <span class="gr">KeyError</span>: <span class="n">'owner'</span> <span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">safe_substitute</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="go">'Return the unladen swallow to $owner.'</span> </pre></div> </div> <p>Template subclasses can specify a custom delimiter. For example, a batch renaming utility for a photo browser may elect to use percent signs for placeholders such as the current date, image sequence number, or file format:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">os.path</span> <span class="gp">>>> </span><span class="n">photofiles</span> <span class="o">=</span> <span class="p">[</span><span class="s">'img_1074.jpg'</span><span class="p">,</span> <span class="s">'img_1076.jpg'</span><span class="p">,</span> <span class="s">'img_1077.jpg'</span><span class="p">]</span> <span class="gp">>>> </span><span class="k">class</span> <span class="nc">BatchRename</span><span class="p">(</span><span class="n">Template</span><span class="p">):</span> <span class="gp">... </span> <span class="n">delimiter</span> <span class="o">=</span> <span class="s">'%'</span> <span class="gp">>>> </span><span class="n">fmt</span> <span class="o">=</span> <span class="nb">raw_input</span><span class="p">(</span><span class="s">'Enter rename style (</span><span class="si">%d</span><span class="s">-date %n-seqnum </span><span class="si">%f</span><span class="s">-format): '</span><span class="p">)</span> <span class="go">Enter rename style (%d-date %n-seqnum %f-format): Ashley_%n%f</span> <span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">BatchRename</span><span class="p">(</span><span class="n">fmt</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">date</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s">'</span><span class="si">%d</span><span class="s">%b%y'</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">filename</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">photofiles</span><span class="p">):</span> <span class="gp">... </span> <span class="n">base</span><span class="p">,</span> <span class="n">ext</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">splitext</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="gp">... </span> <span class="n">newname</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">substitute</span><span class="p">(</span><span class="n">d</span><span class="o">=</span><span class="n">date</span><span class="p">,</span> <span class="n">n</span><span class="o">=</span><span class="n">i</span><span class="p">,</span> <span class="n">f</span><span class="o">=</span><span class="n">ext</span><span class="p">)</span> <span class="gp">... </span> <span class="k">print</span> <span class="s">'{0} --> {1}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">newname</span><span class="p">)</span> <span class="go">img_1074.jpg --> Ashley_0.jpg</span> <span class="go">img_1076.jpg --> Ashley_1.jpg</span> <span class="go">img_1077.jpg --> Ashley_2.jpg</span> </pre></div> </div> <p>Another application for templating is separating program logic from the details of multiple output formats. This makes it possible to substitute custom templates for XML files, plain text reports, and HTML web reports.</p> </div> <div class="section" id="working-with-binary-data-record-layouts"> <span id="tut-binary-formats"></span><h2>11.3. Working with Binary Data Record Layouts<a class="headerlink" href="#working-with-binary-data-record-layouts" title="Permalink to this headline">¶</a></h2> <p>The <a class="reference internal" href="../library/struct.html#module-struct" title="struct: Interpret strings as packed binary data."><tt class="xref py py-mod docutils literal"><span class="pre">struct</span></tt></a> module provides <tt class="xref py py-func docutils literal"><span class="pre">pack()</span></tt> and <tt class="xref py py-func docutils literal"><span class="pre">unpack()</span></tt> functions for working with variable length binary record formats. The following example shows how to loop through header information in a ZIP file without using the <a class="reference internal" href="../library/zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><tt class="xref py py-mod docutils literal"><span class="pre">zipfile</span></tt></a> module. Pack codes <tt class="docutils literal"><span class="pre">"H"</span></tt> and <tt class="docutils literal"><span class="pre">"I"</span></tt> represent two and four byte unsigned numbers respectively. The <tt class="docutils literal"><span class="pre">"<"</span></tt> indicates that they are standard size and in little-endian byte order:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">struct</span> <span class="n">data</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">'myfile.zip'</span><span class="p">,</span> <span class="s">'rb'</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="n">start</span> <span class="o">=</span> <span class="mi">0</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">3</span><span class="p">):</span> <span class="c"># show the first 3 file headers</span> <span class="n">start</span> <span class="o">+=</span> <span class="mi">14</span> <span class="n">fields</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s">'<IIIHH'</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">start</span><span class="o">+</span><span class="mi">16</span><span class="p">])</span> <span class="n">crc32</span><span class="p">,</span> <span class="n">comp_size</span><span class="p">,</span> <span class="n">uncomp_size</span><span class="p">,</span> <span class="n">filenamesize</span><span class="p">,</span> <span class="n">extra_size</span> <span class="o">=</span> <span class="n">fields</span> <span class="n">start</span> <span class="o">+=</span> <span class="mi">16</span> <span class="n">filename</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">start</span><span class="o">+</span><span class="n">filenamesize</span><span class="p">]</span> <span class="n">start</span> <span class="o">+=</span> <span class="n">filenamesize</span> <span class="n">extra</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">start</span><span class="o">+</span><span class="n">extra_size</span><span class="p">]</span> <span class="k">print</span> <span class="n">filename</span><span class="p">,</span> <span class="nb">hex</span><span class="p">(</span><span class="n">crc32</span><span class="p">),</span> <span class="n">comp_size</span><span class="p">,</span> <span class="n">uncomp_size</span> <span class="n">start</span> <span class="o">+=</span> <span class="n">extra_size</span> <span class="o">+</span> <span class="n">comp_size</span> <span class="c"># skip to the next header</span> </pre></div> </div> </div> <div class="section" id="multi-threading"> <span id="tut-multi-threading"></span><h2>11.4. Multi-threading<a class="headerlink" href="#multi-threading" title="Permalink to this headline">¶</a></h2> <p>Threading is a technique for decoupling tasks which are not sequentially dependent. Threads can be used to improve the responsiveness of applications that accept user input while other tasks run in the background. A related use case is running I/O in parallel with computations in another thread.</p> <p>The following code shows how the high level <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Higher-level threading interface."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> module can run tasks in background while the main program continues to run:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">threading</span><span class="o">,</span> <span class="nn">zipfile</span> <span class="k">class</span> <span class="nc">AsyncZip</span><span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</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">infile</span><span class="p">,</span> <span class="n">outfile</span><span class="p">):</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="o">.</span><span class="n">__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">infile</span> <span class="o">=</span> <span class="n">infile</span> <span class="bp">self</span><span class="o">.</span><span class="n">outfile</span> <span class="o">=</span> <span class="n">outfile</span> <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="n">f</span> <span class="o">=</span> <span class="n">zipfile</span><span class="o">.</span><span class="n">ZipFile</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">outfile</span><span class="p">,</span> <span class="s">'w'</span><span class="p">,</span> <span class="n">zipfile</span><span class="o">.</span><span class="n">ZIP_DEFLATED</span><span class="p">)</span> <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">infile</span><span class="p">)</span> <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> <span class="k">print</span> <span class="s">'Finished background zip of: '</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">infile</span> <span class="n">background</span> <span class="o">=</span> <span class="n">AsyncZip</span><span class="p">(</span><span class="s">'mydata.txt'</span><span class="p">,</span> <span class="s">'myarchive.zip'</span><span class="p">)</span> <span class="n">background</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> <span class="k">print</span> <span class="s">'The main program continues to run in foreground.'</span> <span class="n">background</span><span class="o">.</span><span class="n">join</span><span class="p">()</span> <span class="c"># Wait for the background task to finish</span> <span class="k">print</span> <span class="s">'Main program waited until background was done.'</span> </pre></div> </div> <p>The principal challenge of multi-threaded applications is coordinating threads that share data or other resources. To that end, the threading module provides a number of synchronization primitives including locks, events, condition variables, and semaphores.</p> <p>While those tools are powerful, minor design errors can result in problems that are difficult to reproduce. So, the preferred approach to task coordination is to concentrate all access to a resource in a single thread and then use the <a class="reference internal" href="../library/queue.html#module-Queue" title="Queue: A synchronized queue class."><tt class="xref py py-mod docutils literal"><span class="pre">Queue</span></tt></a> module to feed that thread with requests from other threads. Applications using <a class="reference internal" href="../library/queue.html#Queue.Queue" title="Queue.Queue"><tt class="xref py py-class docutils literal"><span class="pre">Queue.Queue</span></tt></a> objects for inter-thread communication and coordination are easier to design, more readable, and more reliable.</p> </div> <div class="section" id="logging"> <span id="tut-logging"></span><h2>11.5. Logging<a class="headerlink" href="#logging" title="Permalink to this headline">¶</a></h2> <p>The <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><tt class="xref py py-mod docutils literal"><span class="pre">logging</span></tt></a> module offers a full featured and flexible logging system. At its simplest, log messages are sent to a file or to <tt class="docutils literal"><span class="pre">sys.stderr</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">logging</span> <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">'Debugging information'</span><span class="p">)</span> <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">'Informational message'</span><span class="p">)</span> <span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">'Warning:config file </span><span class="si">%s</span><span class="s"> not found'</span><span class="p">,</span> <span class="s">'server.conf'</span><span class="p">)</span> <span class="n">logging</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">'Error occurred'</span><span class="p">)</span> <span class="n">logging</span><span class="o">.</span><span class="n">critical</span><span class="p">(</span><span class="s">'Critical error -- shutting down'</span><span class="p">)</span> </pre></div> </div> <p>This produces the following output:</p> <div class="highlight-none"><div class="highlight"><pre>WARNING:root:Warning:config file server.conf not found ERROR:root:Error occurred CRITICAL:root:Critical error -- shutting down </pre></div> </div> <p>By default, informational and debugging messages are suppressed and the output is sent to standard error. Other output options include routing messages through email, datagrams, sockets, or to an HTTP Server. New filters can select different routing based on message priority: <tt class="xref py py-const docutils literal"><span class="pre">DEBUG</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">INFO</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">WARNING</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ERROR</span></tt>, and <tt class="xref py py-const docutils literal"><span class="pre">CRITICAL</span></tt>.</p> <p>The logging system can be configured directly from Python or can be loaded from a user editable configuration file for customized logging without altering the application.</p> </div> <div class="section" id="weak-references"> <span id="tut-weak-references"></span><h2>11.6. Weak References<a class="headerlink" href="#weak-references" title="Permalink to this headline">¶</a></h2> <p>Python does automatic memory management (reference counting for most objects and <a class="reference internal" href="../glossary.html#term-garbage-collection"><em class="xref std std-term">garbage collection</em></a> to eliminate cycles). The memory is freed shortly after the last reference to it has been eliminated.</p> <p>This approach works fine for most applications but occasionally there is a need to track objects only as long as they are being used by something else. Unfortunately, just tracking them creates a reference that makes them permanent. The <a class="reference internal" href="../library/weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><tt class="xref py py-mod docutils literal"><span class="pre">weakref</span></tt></a> module provides tools for tracking objects without creating a reference. When the object is no longer needed, it is automatically removed from a weakref table and a callback is triggered for weakref objects. Typical applications include caching objects that are expensive to create:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">weakref</span><span class="o">,</span> <span class="nn">gc</span> <span class="gp">>>> </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span> <span class="gp">... </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">value</span><span class="p">):</span> <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">)</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="c"># create a reference</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">WeakValueDictionary</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">d</span><span class="p">[</span><span class="s">'primary'</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span> <span class="c"># does not create a reference</span> <span class="gp">>>> </span><span class="n">d</span><span class="p">[</span><span class="s">'primary'</span><span class="p">]</span> <span class="c"># fetch the object if it is still alive</span> <span class="go">10</span> <span class="gp">>>> </span><span class="k">del</span> <span class="n">a</span> <span class="c"># remove the one reference</span> <span class="gp">>>> </span><span class="n">gc</span><span class="o">.</span><span class="n">collect</span><span class="p">()</span> <span class="c"># run garbage collection right away</span> <span class="go">0</span> <span class="gp">>>> </span><span class="n">d</span><span class="p">[</span><span class="s">'primary'</span><span class="p">]</span> <span class="c"># entry was automatically removed</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span> <span class="n">d</span><span class="p">[</span><span class="s">'primary'</span><span class="p">]</span> <span class="c"># entry was automatically removed</span> File <span class="nb">"C:/python26/lib/weakref.py"</span>, line <span class="m">46</span>, in <span class="n">__getitem__</span> <span class="n">o</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">key</span><span class="p">]()</span> <span class="gr">KeyError</span>: <span class="n">'primary'</span> </pre></div> </div> </div> <div class="section" id="tools-for-working-with-lists"> <span id="tut-list-tools"></span><h2>11.7. Tools for Working with Lists<a class="headerlink" href="#tools-for-working-with-lists" title="Permalink to this headline">¶</a></h2> <p>Many data structure needs can be met with the built-in list type. However, sometimes there is a need for alternative implementations with different performance trade-offs.</p> <p>The <a class="reference internal" href="../library/array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><tt class="xref py py-mod docutils literal"><span class="pre">array</span></tt></a> module provides an <a class="reference internal" href="../library/array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><tt class="xref py py-class docutils literal"><span class="pre">array()</span></tt></a> object that is like a list that stores only homogeneous data and stores it more compactly. The following example shows an array of numbers stored as two byte unsigned binary numbers (typecode <tt class="docutils literal"><span class="pre">"H"</span></tt>) rather than the usual 16 bytes per entry for regular lists of Python int objects:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">array</span> <span class="kn">import</span> <span class="n">array</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">array</span><span class="p">(</span><span class="s">'H'</span><span class="p">,</span> <span class="p">[</span><span class="mi">4000</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">700</span><span class="p">,</span> <span class="mi">22222</span><span class="p">])</span> <span class="gp">>>> </span><span class="nb">sum</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="go">26932</span> <span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span> <span class="go">array('H', [10, 700])</span> </pre></div> </div> <p>The <a class="reference internal" href="../library/collections.html#module-collections" title="collections: High-performance datatypes"><tt class="xref py py-mod docutils literal"><span class="pre">collections</span></tt></a> module provides a <tt class="xref py py-class docutils literal"><span class="pre">deque()</span></tt> object that is like a list with faster appends and pops from the left side but slower lookups in the middle. These objects are well suited for implementing queues and breadth first tree searches:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">deque</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">deque</span><span class="p">([</span><span class="s">"task1"</span><span class="p">,</span> <span class="s">"task2"</span><span class="p">,</span> <span class="s">"task3"</span><span class="p">])</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">"task4"</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">print</span> <span class="s">"Handling"</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> <span class="go">Handling task1</span> </pre></div> </div> <div class="highlight-python"><div class="highlight"><pre><span class="n">unsearched</span> <span class="o">=</span> <span class="n">deque</span><span class="p">([</span><span class="n">starting_node</span><span class="p">])</span> <span class="k">def</span> <span class="nf">breadth_first_search</span><span class="p">(</span><span class="n">unsearched</span><span class="p">):</span> <span class="n">node</span> <span class="o">=</span> <span class="n">unsearched</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">gen_moves</span><span class="p">(</span><span class="n">node</span><span class="p">):</span> <span class="k">if</span> <span class="n">is_goal</span><span class="p">(</span><span class="n">m</span><span class="p">):</span> <span class="k">return</span> <span class="n">m</span> <span class="n">unsearched</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span> </pre></div> </div> <p>In addition to alternative list implementations, the library also offers other tools such as the <a class="reference internal" href="../library/bisect.html#module-bisect" title="bisect: Array bisection algorithms for binary searching."><tt class="xref py py-mod docutils literal"><span class="pre">bisect</span></tt></a> module with functions for manipulating sorted lists:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">bisect</span> <span class="gp">>>> </span><span class="n">scores</span> <span class="o">=</span> <span class="p">[(</span><span class="mi">100</span><span class="p">,</span> <span class="s">'perl'</span><span class="p">),</span> <span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="s">'tcl'</span><span class="p">),</span> <span class="p">(</span><span class="mi">400</span><span class="p">,</span> <span class="s">'lua'</span><span class="p">),</span> <span class="p">(</span><span class="mi">500</span><span class="p">,</span> <span class="s">'python'</span><span class="p">)]</span> <span class="gp">>>> </span><span class="n">bisect</span><span class="o">.</span><span class="n">insort</span><span class="p">(</span><span class="n">scores</span><span class="p">,</span> <span class="p">(</span><span class="mi">300</span><span class="p">,</span> <span class="s">'ruby'</span><span class="p">))</span> <span class="gp">>>> </span><span class="n">scores</span> <span class="go">[(100, 'perl'), (200, 'tcl'), (300, 'ruby'), (400, 'lua'), (500, 'python')]</span> </pre></div> </div> <p>The <a class="reference internal" href="../library/heapq.html#module-heapq" title="heapq: Heap queue algorithm (a.k.a. priority queue)."><tt class="xref py py-mod docutils literal"><span class="pre">heapq</span></tt></a> module provides functions for implementing heaps based on regular lists. The lowest valued entry is always kept at position zero. This is useful for applications which repeatedly access the smallest element but do not want to run a full list sort:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">heapq</span> <span class="kn">import</span> <span class="n">heapify</span><span class="p">,</span> <span class="n">heappop</span><span class="p">,</span> <span class="n">heappush</span> <span class="gp">>>> </span><span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="gp">>>> </span><span class="n">heapify</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="c"># rearrange the list into heap order</span> <span class="gp">>>> </span><span class="n">heappush</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">-</span><span class="mi">5</span><span class="p">)</span> <span class="c"># add a new entry</span> <span class="gp">>>> </span><span class="p">[</span><span class="n">heappop</span><span class="p">(</span><span class="n">data</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">3</span><span class="p">)]</span> <span class="c"># fetch the three smallest entries</span> <span class="go">[-5, 0, 1]</span> </pre></div> </div> </div> <div class="section" id="decimal-floating-point-arithmetic"> <span id="tut-decimal-fp"></span><h2>11.8. Decimal Floating Point Arithmetic<a class="headerlink" href="#decimal-floating-point-arithmetic" title="Permalink to this headline">¶</a></h2> <p>The <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> module offers a <tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt> datatype for decimal floating point arithmetic. Compared to the built-in <a class="reference internal" href="../library/functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> implementation of binary floating point, the class is especially helpful for</p> <ul class="simple"> <li>financial applications and other uses which require exact decimal representation,</li> <li>control over precision,</li> <li>control over rounding to meet legal or regulatory requirements,</li> <li>tracking of significant decimal places, or</li> <li>applications where the user expects the results to match calculations done by hand.</li> </ul> <p>For example, calculating a 5% tax on a 70 cent phone charge gives different results in decimal floating point and binary floating point. The difference becomes significant if the results are rounded to the nearest cent:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">decimal</span> <span class="kn">import</span> <span class="o">*</span> <span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'0.70'</span><span class="p">)</span> <span class="o">*</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'1.05'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">x</span> <span class="go">Decimal('0.7350')</span> <span class="gp">>>> </span><span class="n">x</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s">'0.01'</span><span class="p">))</span> <span class="c"># round to nearest cent</span> <span class="go">Decimal('0.74')</span> <span class="gp">>>> </span><span class="nb">round</span><span class="p">(</span><span class="o">.</span><span class="mi">70</span> <span class="o">*</span> <span class="mf">1.05</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c"># same calculation with floats</span> <span class="go">0.73</span> </pre></div> </div> <p>The <tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt> result keeps a trailing zero, automatically inferring four place significance from multiplicands with two place significance. Decimal reproduces mathematics as done by hand and avoids issues that can arise when binary floating point cannot exactly represent decimal quantities.</p> <p>Exact representation enables the <tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt> class to perform modulo calculations and equality tests that are unsuitable for binary floating point:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'1.00'</span><span class="p">)</span> <span class="o">%</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'.10'</span><span class="p">)</span> <span class="go">Decimal('0.00')</span> <span class="gp">>>> </span><span class="mf">1.00</span> <span class="o">%</span> <span class="mf">0.10</span> <span class="go">0.09999999999999995</span> <span class="gp">>>> </span><span class="nb">sum</span><span class="p">([</span><span class="n">Decimal</span><span class="p">(</span><span class="s">'0.1'</span><span class="p">)]</span><span class="o">*</span><span class="mi">10</span><span class="p">)</span> <span class="o">==</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'1.0'</span><span class="p">)</span> <span class="go">True</span> <span class="gp">>>> </span><span class="nb">sum</span><span class="p">([</span><span class="mf">0.1</span><span class="p">]</span><span class="o">*</span><span class="mi">10</span><span class="p">)</span> <span class="o">==</span> <span class="mf">1.0</span> <span class="go">False</span> </pre></div> </div> <p>The <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> module provides arithmetic with as much precision as needed:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">36</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="go">Decimal('0.142857142857142857142857142857142857')</span> </pre></div> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">11. Brief Tour of the Standard Library – Part II</a><ul> <li><a class="reference internal" href="#output-formatting">11.1. Output Formatting</a></li> <li><a class="reference internal" href="#templating">11.2. Templating</a></li> <li><a class="reference internal" href="#working-with-binary-data-record-layouts">11.3. Working with Binary Data Record Layouts</a></li> <li><a class="reference internal" href="#multi-threading">11.4. Multi-threading</a></li> <li><a class="reference internal" href="#logging">11.5. Logging</a></li> <li><a class="reference internal" href="#weak-references">11.6. Weak References</a></li> <li><a class="reference internal" href="#tools-for-working-with-lists">11.7. Tools for Working with Lists</a></li> <li><a class="reference internal" href="#decimal-floating-point-arithmetic">11.8. Decimal Floating Point Arithmetic</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="stdlib.html" title="previous chapter">10. Brief Tour of the Standard Library</a></p> <h4>Next topic</h4> <p class="topless"><a href="whatnow.html" title="next chapter">12. What Now?</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/tutorial/stdlib2.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> </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="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="whatnow.html" title="12. What Now?" >next</a> |</li> <li class="right" > <a href="stdlib.html" title="10. Brief Tour of the Standard Library" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Tutorial</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>