stdlib2.html 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  6. <meta property="og:title" content="11. Brief Tour of the Standard Library — Part II" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/tutorial/stdlib2.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="This second tour covers more advanced modules that support professional programming needs. These modules rarely occur in small scripts. Output Formatting: The reprlib module provides a version of r..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="This second tour covers more advanced modules that support professional programming needs. These modules rarely occur in small scripts. Output Formatting: The reprlib module provides a version of r..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>11. Brief Tour of the Standard Library — Part II &#8212; Python 3.12.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
  21. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  22. <script src="../_static/jquery.js"></script>
  23. <script src="../_static/underscore.js"></script>
  24. <script src="../_static/doctools.js"></script>
  25. <script src="../_static/sidebar.js"></script>
  26. <link rel="search" type="application/opensearchdescription+xml"
  27. title="Search within Python 3.12.0 documentation"
  28. href="../_static/opensearch.xml"/>
  29. <link rel="author" title="About these documents" href="../about.html" />
  30. <link rel="index" title="Index" href="../genindex.html" />
  31. <link rel="search" title="Search" href="../search.html" />
  32. <link rel="copyright" title="Copyright" href="../copyright.html" />
  33. <link rel="next" title="12. Virtual Environments and Packages" href="venv.html" />
  34. <link rel="prev" title="10. Brief Tour of the Standard Library" href="stdlib.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/tutorial/stdlib2.html" />
  36. <style>
  37. @media only screen {
  38. table.full-width-table {
  39. width: 100%;
  40. }
  41. }
  42. </style>
  43. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  44. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  45. <script type="text/javascript" src="../_static/copybutton.js"></script>
  46. <script type="text/javascript" src="../_static/menu.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">11. Brief Tour of the Standard Library — Part II</a><ul>
  86. <li><a class="reference internal" href="#output-formatting">11.1. Output Formatting</a></li>
  87. <li><a class="reference internal" href="#templating">11.2. Templating</a></li>
  88. <li><a class="reference internal" href="#working-with-binary-data-record-layouts">11.3. Working with Binary Data Record Layouts</a></li>
  89. <li><a class="reference internal" href="#multi-threading">11.4. Multi-threading</a></li>
  90. <li><a class="reference internal" href="#logging">11.5. Logging</a></li>
  91. <li><a class="reference internal" href="#weak-references">11.6. Weak References</a></li>
  92. <li><a class="reference internal" href="#tools-for-working-with-lists">11.7. Tools for Working with Lists</a></li>
  93. <li><a class="reference internal" href="#decimal-floating-point-arithmetic">11.8. Decimal Floating Point Arithmetic</a></li>
  94. </ul>
  95. </li>
  96. </ul>
  97. </div>
  98. <div>
  99. <h4>Previous topic</h4>
  100. <p class="topless"><a href="stdlib.html"
  101. title="previous chapter"><span class="section-number">10. </span>Brief Tour of the Standard Library</a></p>
  102. </div>
  103. <div>
  104. <h4>Next topic</h4>
  105. <p class="topless"><a href="venv.html"
  106. title="next chapter"><span class="section-number">12. </span>Virtual Environments and Packages</a></p>
  107. </div>
  108. <div role="note" aria-label="source link">
  109. <h3>This Page</h3>
  110. <ul class="this-page-menu">
  111. <li><a href="../bugs.html">Report a Bug</a></li>
  112. <li>
  113. <a href="https://github.com/python/cpython/blob/main/Doc/tutorial/stdlib2.rst"
  114. rel="nofollow">Show Source
  115. </a>
  116. </li>
  117. </ul>
  118. </div>
  119. </nav>
  120. </div>
  121. </div>
  122. <div class="related" role="navigation" aria-label="related navigation">
  123. <h3>Navigation</h3>
  124. <ul>
  125. <li class="right" style="margin-right: 10px">
  126. <a href="../genindex.html" title="General Index"
  127. accesskey="I">index</a></li>
  128. <li class="right" >
  129. <a href="../py-modindex.html" title="Python Module Index"
  130. >modules</a> |</li>
  131. <li class="right" >
  132. <a href="venv.html" title="12. Virtual Environments and Packages"
  133. accesskey="N">next</a> |</li>
  134. <li class="right" >
  135. <a href="stdlib.html" title="10. Brief Tour of the Standard Library"
  136. accesskey="P">previous</a> |</li>
  137. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  138. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  139. <li class="switchers">
  140. <div class="language_switcher_placeholder"></div>
  141. <div class="version_switcher_placeholder"></div>
  142. </li>
  143. <li>
  144. </li>
  145. <li id="cpython-language-and-version">
  146. <a href="../index.html">3.12.0 Documentation</a> &#187;
  147. </li>
  148. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Tutorial</a> &#187;</li>
  149. <li class="nav-item nav-item-this"><a href=""><span class="section-number">11. </span>Brief Tour of the Standard Library — Part II</a></li>
  150. <li class="right">
  151. <div class="inline-search" role="search">
  152. <form class="inline-search" action="../search.html" method="get">
  153. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  154. <input type="submit" value="Go" />
  155. </form>
  156. </div>
  157. |
  158. </li>
  159. <li class="right">
  160. <label class="theme-selector-label">
  161. Theme
  162. <select class="theme-selector" oninput="activateTheme(this.value)">
  163. <option value="auto" selected>Auto</option>
  164. <option value="light">Light</option>
  165. <option value="dark">Dark</option>
  166. </select>
  167. </label> |</li>
  168. </ul>
  169. </div>
  170. <div class="document">
  171. <div class="documentwrapper">
  172. <div class="bodywrapper">
  173. <div class="body" role="main">
  174. <section id="brief-tour-of-the-standard-library-part-ii">
  175. <span id="tut-brieftourtwo"></span><h1><span class="section-number">11. </span>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>
  176. <p>This second tour covers more advanced modules that support professional
  177. programming needs. These modules rarely occur in small scripts.</p>
  178. <section id="output-formatting">
  179. <span id="tut-output-formatting"></span><h2><span class="section-number">11.1. </span>Output Formatting<a class="headerlink" href="#output-formatting" title="Permalink to this headline">¶</a></h2>
  180. <p>The <a class="reference internal" href="../library/reprlib.html#module-reprlib" title="reprlib: Alternate repr() implementation with size limits."><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code></a> module provides a version of <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> customized for
  181. abbreviated displays of large or deeply nested containers:</p>
  182. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">reprlib</span>
  183. <span class="gp">&gt;&gt;&gt; </span><span class="n">reprlib</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="s1">&#39;supercalifragilisticexpialidocious&#39;</span><span class="p">))</span>
  184. <span class="go">&quot;{&#39;a&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, ...}&quot;</span>
  185. </pre></div>
  186. </div>
  187. <p>The <a class="reference internal" href="../library/pprint.html#module-pprint" title="pprint: Data pretty printer."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code></a> module offers more sophisticated control over printing both
  188. built-in and user defined objects in a way that is readable by the interpreter.
  189. When the result is longer than one line, the “pretty printer” adds line breaks
  190. and indentation to more clearly reveal data structure:</p>
  191. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pprint</span>
  192. <span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="p">[[[[</span><span class="s1">&#39;black&#39;</span><span class="p">,</span> <span class="s1">&#39;cyan&#39;</span><span class="p">],</span> <span class="s1">&#39;white&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;green&#39;</span><span class="p">,</span> <span class="s1">&#39;red&#39;</span><span class="p">]],</span> <span class="p">[[</span><span class="s1">&#39;magenta&#39;</span><span class="p">,</span>
  193. <span class="gp">... </span> <span class="s1">&#39;yellow&#39;</span><span class="p">],</span> <span class="s1">&#39;blue&#39;</span><span class="p">]]]</span>
  194. <span class="gp">...</span>
  195. <span class="gp">&gt;&gt;&gt; </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>
  196. <span class="go">[[[[&#39;black&#39;, &#39;cyan&#39;],</span>
  197. <span class="go"> &#39;white&#39;,</span>
  198. <span class="go"> [&#39;green&#39;, &#39;red&#39;]],</span>
  199. <span class="go"> [[&#39;magenta&#39;, &#39;yellow&#39;],</span>
  200. <span class="go"> &#39;blue&#39;]]]</span>
  201. </pre></div>
  202. </div>
  203. <p>The <a class="reference internal" href="../library/textwrap.html#module-textwrap" title="textwrap: Text wrapping and filling"><code class="xref py py-mod docutils literal notranslate"><span class="pre">textwrap</span></code></a> module formats paragraphs of text to fit a given screen
  204. width:</p>
  205. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">textwrap</span>
  206. <span class="gp">&gt;&gt;&gt; </span><span class="n">doc</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;The wrap() method is just like fill() except that it returns</span>
  207. <span class="gp">... </span><span class="s2">a list of strings instead of one big string with newlines to separate</span>
  208. <span class="gp">... </span><span class="s2">the wrapped lines.&quot;&quot;&quot;</span>
  209. <span class="gp">...</span>
  210. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</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>
  211. <span class="go">The wrap() method is just like fill()</span>
  212. <span class="go">except that it returns a list of strings</span>
  213. <span class="go">instead of one big string with newlines</span>
  214. <span class="go">to separate the wrapped lines.</span>
  215. </pre></div>
  216. </div>
  217. <p>The <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> module accesses a database of culture specific data formats.
  218. The grouping attribute of locale’s format function provides a direct way of
  219. formatting numbers with group separators:</p>
  220. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">locale</span>
  221. <span class="gp">&gt;&gt;&gt; </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="s1">&#39;English_United States.1252&#39;</span><span class="p">)</span>
  222. <span class="go">&#39;English_United States.1252&#39;</span>
  223. <span class="gp">&gt;&gt;&gt; </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="c1"># get a mapping of conventions</span>
  224. <span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="mf">1234567.8</span>
  225. <span class="gp">&gt;&gt;&gt; </span><span class="n">locale</span><span class="o">.</span><span class="n">format_string</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2">&quot;</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="kc">True</span><span class="p">)</span>
  226. <span class="go">&#39;1,234,567&#39;</span>
  227. <span class="gp">&gt;&gt;&gt; </span><span class="n">locale</span><span class="o">.</span><span class="n">format_string</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s%.*f</span><span class="s2">&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">conv</span><span class="p">[</span><span class="s1">&#39;currency_symbol&#39;</span><span class="p">],</span>
  228. <span class="gp">... </span> <span class="n">conv</span><span class="p">[</span><span class="s1">&#39;frac_digits&#39;</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="kc">True</span><span class="p">)</span>
  229. <span class="go">&#39;$1,234,567.80&#39;</span>
  230. </pre></div>
  231. </div>
  232. </section>
  233. <section id="templating">
  234. <span id="tut-templating"></span><h2><span class="section-number">11.2. </span>Templating<a class="headerlink" href="#templating" title="Permalink to this headline">¶</a></h2>
  235. <p>The <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> module includes a versatile <a class="reference internal" href="../library/string.html#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a> class
  236. with a simplified syntax suitable for editing by end-users. This allows users
  237. to customize their applications without having to alter the application.</p>
  238. <p>The format uses placeholder names formed by <code class="docutils literal notranslate"><span class="pre">$</span></code> with valid Python identifiers
  239. (alphanumeric characters and underscores). Surrounding the placeholder with
  240. braces allows it to be followed by more alphanumeric letters with no intervening
  241. spaces. Writing <code class="docutils literal notranslate"><span class="pre">$$</span></code> creates a single escaped <code class="docutils literal notranslate"><span class="pre">$</span></code>:</p>
  242. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">string</span> <span class="kn">import</span> <span class="n">Template</span>
  243. <span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s1">&#39;$</span><span class="si">{village}</span><span class="s1">folk send $$10 to $cause.&#39;</span><span class="p">)</span>
  244. <span class="gp">&gt;&gt;&gt; </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="s1">&#39;Nottingham&#39;</span><span class="p">,</span> <span class="n">cause</span><span class="o">=</span><span class="s1">&#39;the ditch fund&#39;</span><span class="p">)</span>
  245. <span class="go">&#39;Nottinghamfolk send $10 to the ditch fund.&#39;</span>
  246. </pre></div>
  247. </div>
  248. <p>The <a class="reference internal" href="../library/string.html#string.Template.substitute" title="string.Template.substitute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">substitute()</span></code></a> method raises a <a class="reference internal" href="../library/exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> when a
  249. placeholder is not supplied in a dictionary or a keyword argument. For
  250. mail-merge style applications, user supplied data may be incomplete and the
  251. <a class="reference internal" href="../library/string.html#string.Template.safe_substitute" title="string.Template.safe_substitute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">safe_substitute()</span></code></a> method may be more appropriate —
  252. it will leave placeholders unchanged if data is missing:</p>
  253. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s1">&#39;Return the $item to $owner.&#39;</span><span class="p">)</span>
  254. <span class="gp">&gt;&gt;&gt; </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="s1">&#39;unladen swallow&#39;</span><span class="p">)</span>
  255. <span class="gp">&gt;&gt;&gt; </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>
  256. <span class="gt">Traceback (most recent call last):</span>
  257. <span class="w"> </span><span class="c">...</span>
  258. <span class="gr">KeyError</span>: <span class="n">&#39;owner&#39;</span>
  259. <span class="gp">&gt;&gt;&gt; </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>
  260. <span class="go">&#39;Return the unladen swallow to $owner.&#39;</span>
  261. </pre></div>
  262. </div>
  263. <p>Template subclasses can specify a custom delimiter. For example, a batch
  264. renaming utility for a photo browser may elect to use percent signs for
  265. placeholders such as the current date, image sequence number, or file format:</p>
  266. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">os.path</span>
  267. <span class="gp">&gt;&gt;&gt; </span><span class="n">photofiles</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;img_1074.jpg&#39;</span><span class="p">,</span> <span class="s1">&#39;img_1076.jpg&#39;</span><span class="p">,</span> <span class="s1">&#39;img_1077.jpg&#39;</span><span class="p">]</span>
  268. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">BatchRename</span><span class="p">(</span><span class="n">Template</span><span class="p">):</span>
  269. <span class="gp">... </span> <span class="n">delimiter</span> <span class="o">=</span> <span class="s1">&#39;%&#39;</span>
  270. <span class="gp">...</span>
  271. <span class="gp">&gt;&gt;&gt; </span><span class="n">fmt</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s1">&#39;Enter rename style (</span><span class="si">%d</span><span class="s1">-date %n-seqnum </span><span class="si">%f</span><span class="s1">-format): &#39;</span><span class="p">)</span>
  272. <span class="go">Enter rename style (%d-date %n-seqnum %f-format): Ashley_%n%f</span>
  273. <span class="gp">&gt;&gt;&gt; </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>
  274. <span class="gp">&gt;&gt;&gt; </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="s1">&#39;</span><span class="si">%d</span><span class="s1">%b%y&#39;</span><span class="p">)</span>
  275. <span class="gp">&gt;&gt;&gt; </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>
  276. <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>
  277. <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>
  278. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{0}</span><span class="s1"> --&gt; </span><span class="si">{1}</span><span class="s1">&#39;</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>
  279. <span class="go">img_1074.jpg --&gt; Ashley_0.jpg</span>
  280. <span class="go">img_1076.jpg --&gt; Ashley_1.jpg</span>
  281. <span class="go">img_1077.jpg --&gt; Ashley_2.jpg</span>
  282. </pre></div>
  283. </div>
  284. <p>Another application for templating is separating program logic from the details
  285. of multiple output formats. This makes it possible to substitute custom
  286. templates for XML files, plain text reports, and HTML web reports.</p>
  287. </section>
  288. <section id="working-with-binary-data-record-layouts">
  289. <span id="tut-binary-formats"></span><h2><span class="section-number">11.3. </span>Working with Binary Data Record Layouts<a class="headerlink" href="#working-with-binary-data-record-layouts" title="Permalink to this headline">¶</a></h2>
  290. <p>The <a class="reference internal" href="../library/struct.html#module-struct" title="struct: Interpret bytes as packed binary data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code></a> module provides <a class="reference internal" href="../library/struct.html#struct.pack" title="struct.pack"><code class="xref py py-func docutils literal notranslate"><span class="pre">pack()</span></code></a> and
  291. <a class="reference internal" href="../library/struct.html#struct.unpack" title="struct.unpack"><code class="xref py py-func docutils literal notranslate"><span class="pre">unpack()</span></code></a> functions for working with variable length binary
  292. record formats. The following example shows
  293. how to loop through header information in a ZIP file without using the
  294. <a class="reference internal" href="../library/zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code></a> module. Pack codes <code class="docutils literal notranslate"><span class="pre">&quot;H&quot;</span></code> and <code class="docutils literal notranslate"><span class="pre">&quot;I&quot;</span></code> represent two and four
  295. byte unsigned numbers respectively. The <code class="docutils literal notranslate"><span class="pre">&quot;&lt;&quot;</span></code> indicates that they are
  296. standard size and in little-endian byte order:</p>
  297. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">struct</span>
  298. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;myfile.zip&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  299. <span class="n">data</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  300. <span class="n">start</span> <span class="o">=</span> <span class="mi">0</span>
  301. <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="c1"># show the first 3 file headers</span>
  302. <span class="n">start</span> <span class="o">+=</span> <span class="mi">14</span>
  303. <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="s1">&#39;&lt;IIIHH&#39;</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>
  304. <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>
  305. <span class="n">start</span> <span class="o">+=</span> <span class="mi">16</span>
  306. <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>
  307. <span class="n">start</span> <span class="o">+=</span> <span class="n">filenamesize</span>
  308. <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>
  309. <span class="nb">print</span><span class="p">(</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="p">)</span>
  310. <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="c1"># skip to the next header</span>
  311. </pre></div>
  312. </div>
  313. </section>
  314. <section id="multi-threading">
  315. <span id="tut-multi-threading"></span><h2><span class="section-number">11.4. </span>Multi-threading<a class="headerlink" href="#multi-threading" title="Permalink to this headline">¶</a></h2>
  316. <p>Threading is a technique for decoupling tasks which are not sequentially
  317. dependent. Threads can be used to improve the responsiveness of applications
  318. that accept user input while other tasks run in the background. A related use
  319. case is running I/O in parallel with computations in another thread.</p>
  320. <p>The following code shows how the high level <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module can run
  321. tasks in background while the main program continues to run:</p>
  322. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">threading</span><span class="o">,</span> <span class="nn">zipfile</span>
  323. <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>
  324. <span class="k">def</span> <span class="fm">__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>
  325. <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
  326. <span class="bp">self</span><span class="o">.</span><span class="n">infile</span> <span class="o">=</span> <span class="n">infile</span>
  327. <span class="bp">self</span><span class="o">.</span><span class="n">outfile</span> <span class="o">=</span> <span class="n">outfile</span>
  328. <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  329. <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="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">zipfile</span><span class="o">.</span><span class="n">ZIP_DEFLATED</span><span class="p">)</span>
  330. <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>
  331. <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  332. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Finished background zip of:&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">infile</span><span class="p">)</span>
  333. <span class="n">background</span> <span class="o">=</span> <span class="n">AsyncZip</span><span class="p">(</span><span class="s1">&#39;mydata.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;myarchive.zip&#39;</span><span class="p">)</span>
  334. <span class="n">background</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  335. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;The main program continues to run in foreground.&#39;</span><span class="p">)</span>
  336. <span class="n">background</span><span class="o">.</span><span class="n">join</span><span class="p">()</span> <span class="c1"># Wait for the background task to finish</span>
  337. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Main program waited until background was done.&#39;</span><span class="p">)</span>
  338. </pre></div>
  339. </div>
  340. <p>The principal challenge of multi-threaded applications is coordinating threads
  341. that share data or other resources. To that end, the threading module provides
  342. a number of synchronization primitives including locks, events, condition
  343. variables, and semaphores.</p>
  344. <p>While those tools are powerful, minor design errors can result in problems that
  345. are difficult to reproduce. So, the preferred approach to task coordination is
  346. to concentrate all access to a resource in a single thread and then use the
  347. <a class="reference internal" href="../library/queue.html#module-queue" title="queue: A synchronized queue class."><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code></a> module to feed that thread with requests from other threads.
  348. Applications using <a class="reference internal" href="../library/queue.html#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> objects for inter-thread communication and
  349. coordination are easier to design, more readable, and more reliable.</p>
  350. </section>
  351. <section id="logging">
  352. <span id="tut-logging"></span><h2><span class="section-number">11.5. </span>Logging<a class="headerlink" href="#logging" title="Permalink to this headline">¶</a></h2>
  353. <p>The <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a> module offers a full featured and flexible logging system.
  354. At its simplest, log messages are sent to a file or to <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>:</p>
  355. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
  356. <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Debugging information&#39;</span><span class="p">)</span>
  357. <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Informational message&#39;</span><span class="p">)</span>
  358. <span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">&#39;Warning:config file </span><span class="si">%s</span><span class="s1"> not found&#39;</span><span class="p">,</span> <span class="s1">&#39;server.conf&#39;</span><span class="p">)</span>
  359. <span class="n">logging</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;Error occurred&#39;</span><span class="p">)</span>
  360. <span class="n">logging</span><span class="o">.</span><span class="n">critical</span><span class="p">(</span><span class="s1">&#39;Critical error -- shutting down&#39;</span><span class="p">)</span>
  361. </pre></div>
  362. </div>
  363. <p>This produces the following output:</p>
  364. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>WARNING:root:Warning:config file server.conf not found
  365. ERROR:root:Error occurred
  366. CRITICAL:root:Critical error -- shutting down
  367. </pre></div>
  368. </div>
  369. <p>By default, informational and debugging messages are suppressed and the output
  370. is sent to standard error. Other output options include routing messages
  371. through email, datagrams, sockets, or to an HTTP Server. New filters can select
  372. different routing based on message priority: <a class="reference internal" href="../library/logging.html#logging.DEBUG" title="logging.DEBUG"><code class="xref py py-const docutils literal notranslate"><span class="pre">DEBUG</span></code></a>,
  373. <a class="reference internal" href="../library/logging.html#logging.INFO" title="logging.INFO"><code class="xref py py-const docutils literal notranslate"><span class="pre">INFO</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.WARNING" title="logging.WARNING"><code class="xref py py-const docutils literal notranslate"><span class="pre">WARNING</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.ERROR" title="logging.ERROR"><code class="xref py py-const docutils literal notranslate"><span class="pre">ERROR</span></code></a>,
  374. and <a class="reference internal" href="../library/logging.html#logging.CRITICAL" title="logging.CRITICAL"><code class="xref py py-const docutils literal notranslate"><span class="pre">CRITICAL</span></code></a>.</p>
  375. <p>The logging system can be configured directly from Python or can be loaded from
  376. a user editable configuration file for customized logging without altering the
  377. application.</p>
  378. </section>
  379. <section id="weak-references">
  380. <span id="tut-weak-references"></span><h2><span class="section-number">11.6. </span>Weak References<a class="headerlink" href="#weak-references" title="Permalink to this headline">¶</a></h2>
  381. <p>Python does automatic memory management (reference counting for most objects and
  382. <a class="reference internal" href="../glossary.html#term-garbage-collection"><span class="xref std std-term">garbage collection</span></a> to eliminate cycles). The memory is freed shortly
  383. after the last reference to it has been eliminated.</p>
  384. <p>This approach works fine for most applications but occasionally there is a need
  385. to track objects only as long as they are being used by something else.
  386. Unfortunately, just tracking them creates a reference that makes them permanent.
  387. The <a class="reference internal" href="../library/weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> module provides tools for tracking objects without creating a
  388. reference. When the object is no longer needed, it is automatically removed
  389. from a weakref table and a callback is triggered for weakref objects. Typical
  390. applications include caching objects that are expensive to create:</p>
  391. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">weakref</span><span class="o">,</span> <span class="nn">gc</span>
  392. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
  393. <span class="gp">... </span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
  394. <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>
  395. <span class="gp">... </span> <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  396. <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>
  397. <span class="gp">...</span>
  398. <span class="gp">&gt;&gt;&gt; </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="c1"># create a reference</span>
  399. <span class="gp">&gt;&gt;&gt; </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>
  400. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="s1">&#39;primary&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span> <span class="c1"># does not create a reference</span>
  401. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="s1">&#39;primary&#39;</span><span class="p">]</span> <span class="c1"># fetch the object if it is still alive</span>
  402. <span class="go">10</span>
  403. <span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">a</span> <span class="c1"># remove the one reference</span>
  404. <span class="gp">&gt;&gt;&gt; </span><span class="n">gc</span><span class="o">.</span><span class="n">collect</span><span class="p">()</span> <span class="c1"># run garbage collection right away</span>
  405. <span class="go">0</span>
  406. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="s1">&#39;primary&#39;</span><span class="p">]</span> <span class="c1"># entry was automatically removed</span>
  407. <span class="gt">Traceback (most recent call last):</span>
  408. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  409. <span class="w"> </span><span class="n">d</span><span class="p">[</span><span class="s1">&#39;primary&#39;</span><span class="p">]</span> <span class="c1"># entry was automatically removed</span>
  410. File <span class="nb">&quot;C:/python312/lib/weakref.py&quot;</span>, line <span class="m">46</span>, in <span class="n">__getitem__</span>
  411. <span class="w"> </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>
  412. <span class="gr">KeyError</span>: <span class="n">&#39;primary&#39;</span>
  413. </pre></div>
  414. </div>
  415. </section>
  416. <section id="tools-for-working-with-lists">
  417. <span id="tut-list-tools"></span><h2><span class="section-number">11.7. </span>Tools for Working with Lists<a class="headerlink" href="#tools-for-working-with-lists" title="Permalink to this headline">¶</a></h2>
  418. <p>Many data structure needs can be met with the built-in list type. However,
  419. sometimes there is a need for alternative implementations with different
  420. performance trade-offs.</p>
  421. <p>The <a class="reference internal" href="../library/array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code></a> module provides an <a class="reference internal" href="../library/array.html#array.array" title="array.array"><code class="xref py py-class docutils literal notranslate"><span class="pre">array()</span></code></a> object that is like
  422. a list that stores only homogeneous data and stores it more compactly. The
  423. following example shows an array of numbers stored as two byte unsigned binary
  424. numbers (typecode <code class="docutils literal notranslate"><span class="pre">&quot;H&quot;</span></code>) rather than the usual 16 bytes per entry for regular
  425. lists of Python int objects:</p>
  426. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">array</span> <span class="kn">import</span> <span class="n">array</span>
  427. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">array</span><span class="p">(</span><span class="s1">&#39;H&#39;</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>
  428. <span class="gp">&gt;&gt;&gt; </span><span class="nb">sum</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
  429. <span class="go">26932</span>
  430. <span class="gp">&gt;&gt;&gt; </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>
  431. <span class="go">array(&#39;H&#39;, [10, 700])</span>
  432. </pre></div>
  433. </div>
  434. <p>The <a class="reference internal" href="../library/collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module provides a <a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><code class="xref py py-class docutils literal notranslate"><span class="pre">deque()</span></code></a> object
  435. that is like a list with faster appends and pops from the left side but slower
  436. lookups in the middle. These objects are well suited for implementing queues
  437. and breadth first tree searches:</p>
  438. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">deque</span>
  439. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">deque</span><span class="p">([</span><span class="s2">&quot;task1&quot;</span><span class="p">,</span> <span class="s2">&quot;task2&quot;</span><span class="p">,</span> <span class="s2">&quot;task3&quot;</span><span class="p">])</span>
  440. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;task4&quot;</span><span class="p">)</span>
  441. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Handling&quot;</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">popleft</span><span class="p">())</span>
  442. <span class="go">Handling task1</span>
  443. </pre></div>
  444. </div>
  445. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  446. <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>
  447. <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>
  448. <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>
  449. <span class="k">if</span> <span class="n">is_goal</span><span class="p">(</span><span class="n">m</span><span class="p">):</span>
  450. <span class="k">return</span> <span class="n">m</span>
  451. <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>
  452. </pre></div>
  453. </div>
  454. <p>In addition to alternative list implementations, the library also offers other
  455. tools such as the <a class="reference internal" href="../library/bisect.html#module-bisect" title="bisect: Array bisection algorithms for binary searching."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bisect</span></code></a> module with functions for manipulating sorted
  456. lists:</p>
  457. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">bisect</span>
  458. <span class="gp">&gt;&gt;&gt; </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="s1">&#39;perl&#39;</span><span class="p">),</span> <span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="s1">&#39;tcl&#39;</span><span class="p">),</span> <span class="p">(</span><span class="mi">400</span><span class="p">,</span> <span class="s1">&#39;lua&#39;</span><span class="p">),</span> <span class="p">(</span><span class="mi">500</span><span class="p">,</span> <span class="s1">&#39;python&#39;</span><span class="p">)]</span>
  459. <span class="gp">&gt;&gt;&gt; </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="s1">&#39;ruby&#39;</span><span class="p">))</span>
  460. <span class="gp">&gt;&gt;&gt; </span><span class="n">scores</span>
  461. <span class="go">[(100, &#39;perl&#39;), (200, &#39;tcl&#39;), (300, &#39;ruby&#39;), (400, &#39;lua&#39;), (500, &#39;python&#39;)]</span>
  462. </pre></div>
  463. </div>
  464. <p>The <a class="reference internal" href="../library/heapq.html#module-heapq" title="heapq: Heap queue algorithm (a.k.a. priority queue)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">heapq</span></code></a> module provides functions for implementing heaps based on
  465. regular lists. The lowest valued entry is always kept at position zero. This
  466. is useful for applications which repeatedly access the smallest element but do
  467. not want to run a full list sort:</p>
  468. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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>
  469. <span class="gp">&gt;&gt;&gt; </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>
  470. <span class="gp">&gt;&gt;&gt; </span><span class="n">heapify</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="c1"># rearrange the list into heap order</span>
  471. <span class="gp">&gt;&gt;&gt; </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="c1"># add a new entry</span>
  472. <span class="gp">&gt;&gt;&gt; </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="c1"># fetch the three smallest entries</span>
  473. <span class="go">[-5, 0, 1]</span>
  474. </pre></div>
  475. </div>
  476. </section>
  477. <section id="decimal-floating-point-arithmetic">
  478. <span id="tut-decimal-fp"></span><h2><span class="section-number">11.8. </span>Decimal Floating Point Arithmetic<a class="headerlink" href="#decimal-floating-point-arithmetic" title="Permalink to this headline">¶</a></h2>
  479. <p>The <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code></a> module offers a <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a> datatype for
  480. decimal floating point arithmetic. Compared to the built-in <a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>
  481. implementation of binary floating point, the class is especially helpful for</p>
  482. <ul class="simple">
  483. <li><p>financial applications and other uses which require exact decimal
  484. representation,</p></li>
  485. <li><p>control over precision,</p></li>
  486. <li><p>control over rounding to meet legal or regulatory requirements,</p></li>
  487. <li><p>tracking of significant decimal places, or</p></li>
  488. <li><p>applications where the user expects the results to match calculations done by
  489. hand.</p></li>
  490. </ul>
  491. <p>For example, calculating a 5% tax on a 70 cent phone charge gives different
  492. results in decimal floating point and binary floating point. The difference
  493. becomes significant if the results are rounded to the nearest cent:</p>
  494. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">decimal</span> <span class="kn">import</span> <span class="o">*</span>
  495. <span class="gp">&gt;&gt;&gt; </span><span class="nb">round</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;0.70&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;1.05&#39;</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span>
  496. <span class="go">Decimal(&#39;0.74&#39;)</span>
  497. <span class="gp">&gt;&gt;&gt; </span><span class="nb">round</span><span class="p">(</span><span class="mf">.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>
  498. <span class="go">0.73</span>
  499. </pre></div>
  500. </div>
  501. <p>The <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a> result keeps a trailing zero, automatically
  502. inferring four place significance from multiplicands with two place
  503. significance. Decimal reproduces mathematics as done by hand and avoids
  504. issues that can arise when binary floating point cannot exactly represent
  505. decimal quantities.</p>
  506. <p>Exact representation enables the <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a> class to perform
  507. modulo calculations and equality tests that are unsuitable for binary floating
  508. point:</p>
  509. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;1.00&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;.10&#39;</span><span class="p">)</span>
  510. <span class="go">Decimal(&#39;0.00&#39;)</span>
  511. <span class="gp">&gt;&gt;&gt; </span><span class="mf">1.00</span> <span class="o">%</span> <span class="mf">0.10</span>
  512. <span class="go">0.09999999999999995</span>
  513. <span class="gp">&gt;&gt;&gt; </span><span class="nb">sum</span><span class="p">([</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;0.1&#39;</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="s1">&#39;1.0&#39;</span><span class="p">)</span>
  514. <span class="go">True</span>
  515. <span class="gp">&gt;&gt;&gt; </span><span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">==</span> <span class="mf">1.0</span>
  516. <span class="go">False</span>
  517. </pre></div>
  518. </div>
  519. <p>The <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code></a> module provides arithmetic with as much precision as needed:</p>
  520. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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>
  521. <span class="gp">&gt;&gt;&gt; </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>
  522. <span class="go">Decimal(&#39;0.142857142857142857142857142857142857&#39;)</span>
  523. </pre></div>
  524. </div>
  525. </section>
  526. </section>
  527. <div class="clearer"></div>
  528. </div>
  529. </div>
  530. </div>
  531. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  532. <div class="sphinxsidebarwrapper">
  533. <div>
  534. <h3><a href="../contents.html">Table of Contents</a></h3>
  535. <ul>
  536. <li><a class="reference internal" href="#">11. Brief Tour of the Standard Library — Part II</a><ul>
  537. <li><a class="reference internal" href="#output-formatting">11.1. Output Formatting</a></li>
  538. <li><a class="reference internal" href="#templating">11.2. Templating</a></li>
  539. <li><a class="reference internal" href="#working-with-binary-data-record-layouts">11.3. Working with Binary Data Record Layouts</a></li>
  540. <li><a class="reference internal" href="#multi-threading">11.4. Multi-threading</a></li>
  541. <li><a class="reference internal" href="#logging">11.5. Logging</a></li>
  542. <li><a class="reference internal" href="#weak-references">11.6. Weak References</a></li>
  543. <li><a class="reference internal" href="#tools-for-working-with-lists">11.7. Tools for Working with Lists</a></li>
  544. <li><a class="reference internal" href="#decimal-floating-point-arithmetic">11.8. Decimal Floating Point Arithmetic</a></li>
  545. </ul>
  546. </li>
  547. </ul>
  548. </div>
  549. <div>
  550. <h4>Previous topic</h4>
  551. <p class="topless"><a href="stdlib.html"
  552. title="previous chapter"><span class="section-number">10. </span>Brief Tour of the Standard Library</a></p>
  553. </div>
  554. <div>
  555. <h4>Next topic</h4>
  556. <p class="topless"><a href="venv.html"
  557. title="next chapter"><span class="section-number">12. </span>Virtual Environments and Packages</a></p>
  558. </div>
  559. <div role="note" aria-label="source link">
  560. <h3>This Page</h3>
  561. <ul class="this-page-menu">
  562. <li><a href="../bugs.html">Report a Bug</a></li>
  563. <li>
  564. <a href="https://github.com/python/cpython/blob/main/Doc/tutorial/stdlib2.rst"
  565. rel="nofollow">Show Source
  566. </a>
  567. </li>
  568. </ul>
  569. </div>
  570. </div>
  571. </div>
  572. <div class="clearer"></div>
  573. </div>
  574. <div class="related" role="navigation" aria-label="related navigation">
  575. <h3>Navigation</h3>
  576. <ul>
  577. <li class="right" style="margin-right: 10px">
  578. <a href="../genindex.html" title="General Index"
  579. >index</a></li>
  580. <li class="right" >
  581. <a href="../py-modindex.html" title="Python Module Index"
  582. >modules</a> |</li>
  583. <li class="right" >
  584. <a href="venv.html" title="12. Virtual Environments and Packages"
  585. >next</a> |</li>
  586. <li class="right" >
  587. <a href="stdlib.html" title="10. Brief Tour of the Standard Library"
  588. >previous</a> |</li>
  589. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  590. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  591. <li class="switchers">
  592. <div class="language_switcher_placeholder"></div>
  593. <div class="version_switcher_placeholder"></div>
  594. </li>
  595. <li>
  596. </li>
  597. <li id="cpython-language-and-version">
  598. <a href="../index.html">3.12.0 Documentation</a> &#187;
  599. </li>
  600. <li class="nav-item nav-item-1"><a href="index.html" >The Python Tutorial</a> &#187;</li>
  601. <li class="nav-item nav-item-this"><a href=""><span class="section-number">11. </span>Brief Tour of the Standard Library — Part II</a></li>
  602. <li class="right">
  603. <div class="inline-search" role="search">
  604. <form class="inline-search" action="../search.html" method="get">
  605. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  606. <input type="submit" value="Go" />
  607. </form>
  608. </div>
  609. |
  610. </li>
  611. <li class="right">
  612. <label class="theme-selector-label">
  613. Theme
  614. <select class="theme-selector" oninput="activateTheme(this.value)">
  615. <option value="auto" selected>Auto</option>
  616. <option value="light">Light</option>
  617. <option value="dark">Dark</option>
  618. </select>
  619. </label> |</li>
  620. </ul>
  621. </div>
  622. <div class="footer">
  623. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  624. <br />
  625. This page is licensed under the Python Software Foundation License Version 2.
  626. <br />
  627. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  628. <br />
  629. See <a href="/license.html">History and License</a> for more information.<br />
  630. <br />
  631. The Python Software Foundation is a non-profit corporation.
  632. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  633. <br />
  634. <br />
  635. Last updated on Oct 02, 2023.
  636. <a href="/bugs.html">Found a bug</a>?
  637. <br />
  638. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  639. </div>
  640. </body>
  641. </html>