stdlib.html 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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="10. Brief Tour of the Standard Library" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/tutorial/stdlib.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Operating System Interface: The os module provides dozens of functions for interacting with the operating system: Be sure to use the import os style instead of from os import *. This will keep os.o..." />
  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="Operating System Interface: The os module provides dozens of functions for interacting with the operating system: Be sure to use the import os style instead of from os import *. This will keep os.o..." />
  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>10. Brief Tour of the Standard Library &#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="11. Brief Tour of the Standard Library — Part II" href="stdlib2.html" />
  34. <link rel="prev" title="9. Classes" href="classes.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/tutorial/stdlib.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="#">10. Brief Tour of the Standard Library</a><ul>
  86. <li><a class="reference internal" href="#operating-system-interface">10.1. Operating System Interface</a></li>
  87. <li><a class="reference internal" href="#file-wildcards">10.2. File Wildcards</a></li>
  88. <li><a class="reference internal" href="#command-line-arguments">10.3. Command Line Arguments</a></li>
  89. <li><a class="reference internal" href="#error-output-redirection-and-program-termination">10.4. Error Output Redirection and Program Termination</a></li>
  90. <li><a class="reference internal" href="#string-pattern-matching">10.5. String Pattern Matching</a></li>
  91. <li><a class="reference internal" href="#mathematics">10.6. Mathematics</a></li>
  92. <li><a class="reference internal" href="#internet-access">10.7. Internet Access</a></li>
  93. <li><a class="reference internal" href="#dates-and-times">10.8. Dates and Times</a></li>
  94. <li><a class="reference internal" href="#data-compression">10.9. Data Compression</a></li>
  95. <li><a class="reference internal" href="#performance-measurement">10.10. Performance Measurement</a></li>
  96. <li><a class="reference internal" href="#quality-control">10.11. Quality Control</a></li>
  97. <li><a class="reference internal" href="#batteries-included">10.12. Batteries Included</a></li>
  98. </ul>
  99. </li>
  100. </ul>
  101. </div>
  102. <div>
  103. <h4>Previous topic</h4>
  104. <p class="topless"><a href="classes.html"
  105. title="previous chapter"><span class="section-number">9. </span>Classes</a></p>
  106. </div>
  107. <div>
  108. <h4>Next topic</h4>
  109. <p class="topless"><a href="stdlib2.html"
  110. title="next chapter"><span class="section-number">11. </span>Brief Tour of the Standard Library — Part II</a></p>
  111. </div>
  112. <div role="note" aria-label="source link">
  113. <h3>This Page</h3>
  114. <ul class="this-page-menu">
  115. <li><a href="../bugs.html">Report a Bug</a></li>
  116. <li>
  117. <a href="https://github.com/python/cpython/blob/main/Doc/tutorial/stdlib.rst"
  118. rel="nofollow">Show Source
  119. </a>
  120. </li>
  121. </ul>
  122. </div>
  123. </nav>
  124. </div>
  125. </div>
  126. <div class="related" role="navigation" aria-label="related navigation">
  127. <h3>Navigation</h3>
  128. <ul>
  129. <li class="right" style="margin-right: 10px">
  130. <a href="../genindex.html" title="General Index"
  131. accesskey="I">index</a></li>
  132. <li class="right" >
  133. <a href="../py-modindex.html" title="Python Module Index"
  134. >modules</a> |</li>
  135. <li class="right" >
  136. <a href="stdlib2.html" title="11. Brief Tour of the Standard Library — Part II"
  137. accesskey="N">next</a> |</li>
  138. <li class="right" >
  139. <a href="classes.html" title="9. Classes"
  140. accesskey="P">previous</a> |</li>
  141. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  142. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  143. <li class="switchers">
  144. <div class="language_switcher_placeholder"></div>
  145. <div class="version_switcher_placeholder"></div>
  146. </li>
  147. <li>
  148. </li>
  149. <li id="cpython-language-and-version">
  150. <a href="../index.html">3.12.0 Documentation</a> &#187;
  151. </li>
  152. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Tutorial</a> &#187;</li>
  153. <li class="nav-item nav-item-this"><a href=""><span class="section-number">10. </span>Brief Tour of the Standard Library</a></li>
  154. <li class="right">
  155. <div class="inline-search" role="search">
  156. <form class="inline-search" action="../search.html" method="get">
  157. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  158. <input type="submit" value="Go" />
  159. </form>
  160. </div>
  161. |
  162. </li>
  163. <li class="right">
  164. <label class="theme-selector-label">
  165. Theme
  166. <select class="theme-selector" oninput="activateTheme(this.value)">
  167. <option value="auto" selected>Auto</option>
  168. <option value="light">Light</option>
  169. <option value="dark">Dark</option>
  170. </select>
  171. </label> |</li>
  172. </ul>
  173. </div>
  174. <div class="document">
  175. <div class="documentwrapper">
  176. <div class="bodywrapper">
  177. <div class="body" role="main">
  178. <section id="brief-tour-of-the-standard-library">
  179. <span id="tut-brieftour"></span><h1><span class="section-number">10. </span>Brief Tour of the Standard Library<a class="headerlink" href="#brief-tour-of-the-standard-library" title="Permalink to this headline">¶</a></h1>
  180. <section id="operating-system-interface">
  181. <span id="tut-os-interface"></span><h2><span class="section-number">10.1. </span>Operating System Interface<a class="headerlink" href="#operating-system-interface" title="Permalink to this headline">¶</a></h2>
  182. <p>The <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module provides dozens of functions for interacting with the
  183. operating system:</p>
  184. <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">os</span>
  185. <span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span> <span class="c1"># Return the current working directory</span>
  186. <span class="go">&#39;C:\\Python312&#39;</span>
  187. <span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s1">&#39;/server/accesslogs&#39;</span><span class="p">)</span> <span class="c1"># Change current working directory</span>
  188. <span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s1">&#39;mkdir today&#39;</span><span class="p">)</span> <span class="c1"># Run the command mkdir in the system shell</span>
  189. <span class="go">0</span>
  190. </pre></div>
  191. </div>
  192. <p>Be sure to use the <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">os</span></code> style instead of <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">os</span> <span class="pre">import</span> <span class="pre">*</span></code>. This
  193. will keep <a class="reference internal" href="../library/os.html#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a> from shadowing the built-in <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function which
  194. operates much differently.</p>
  195. <p id="index-0">The built-in <a class="reference internal" href="../library/functions.html#dir" title="dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">dir()</span></code></a> and <a class="reference internal" href="../library/functions.html#help" title="help"><code class="xref py py-func docutils literal notranslate"><span class="pre">help()</span></code></a> functions are useful as interactive
  196. aids for working with large modules like <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a>:</p>
  197. <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">os</span>
  198. <span class="gp">&gt;&gt;&gt; </span><span class="nb">dir</span><span class="p">(</span><span class="n">os</span><span class="p">)</span>
  199. <span class="go">&lt;returns a list of all module functions&gt;</span>
  200. <span class="gp">&gt;&gt;&gt; </span><span class="n">help</span><span class="p">(</span><span class="n">os</span><span class="p">)</span>
  201. <span class="go">&lt;returns an extensive manual page created from the module&#39;s docstrings&gt;</span>
  202. </pre></div>
  203. </div>
  204. <p>For daily file and directory management tasks, the <a class="reference internal" href="../library/shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a> module provides
  205. a higher level interface that is easier to use:</p>
  206. <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">shutil</span>
  207. <span class="gp">&gt;&gt;&gt; </span><span class="n">shutil</span><span class="o">.</span><span class="n">copyfile</span><span class="p">(</span><span class="s1">&#39;data.db&#39;</span><span class="p">,</span> <span class="s1">&#39;archive.db&#39;</span><span class="p">)</span>
  208. <span class="go">&#39;archive.db&#39;</span>
  209. <span class="gp">&gt;&gt;&gt; </span><span class="n">shutil</span><span class="o">.</span><span class="n">move</span><span class="p">(</span><span class="s1">&#39;/build/executables&#39;</span><span class="p">,</span> <span class="s1">&#39;installdir&#39;</span><span class="p">)</span>
  210. <span class="go">&#39;installdir&#39;</span>
  211. </pre></div>
  212. </div>
  213. </section>
  214. <section id="file-wildcards">
  215. <span id="tut-file-wildcards"></span><h2><span class="section-number">10.2. </span>File Wildcards<a class="headerlink" href="#file-wildcards" title="Permalink to this headline">¶</a></h2>
  216. <p>The <a class="reference internal" href="../library/glob.html#module-glob" title="glob: Unix shell style pathname pattern expansion."><code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code></a> module provides a function for making file lists from directory
  217. wildcard searches:</p>
  218. <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">glob</span>
  219. <span class="gp">&gt;&gt;&gt; </span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s1">&#39;*.py&#39;</span><span class="p">)</span>
  220. <span class="go">[&#39;primes.py&#39;, &#39;random.py&#39;, &#39;quote.py&#39;]</span>
  221. </pre></div>
  222. </div>
  223. </section>
  224. <section id="command-line-arguments">
  225. <span id="tut-command-line-arguments"></span><h2><span class="section-number">10.3. </span>Command Line Arguments<a class="headerlink" href="#command-line-arguments" title="Permalink to this headline">¶</a></h2>
  226. <p>Common utility scripts often need to process command line arguments. These
  227. arguments are stored in the <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> module’s <em>argv</em> attribute as a list. For
  228. instance, let’s take the following <code class="file docutils literal notranslate"><span class="pre">demo.py</span></code> file:</p>
  229. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># File demo.py</span>
  230. <span class="kn">import</span> <span class="nn">sys</span>
  231. <span class="nb">print</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span>
  232. </pre></div>
  233. </div>
  234. <p>Here is the output from running <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">demo.py</span> <span class="pre">one</span> <span class="pre">two</span> <span class="pre">three</span></code> at the command
  235. line:</p>
  236. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="s1">&#39;demo.py&#39;</span><span class="p">,</span> <span class="s1">&#39;one&#39;</span><span class="p">,</span> <span class="s1">&#39;two&#39;</span><span class="p">,</span> <span class="s1">&#39;three&#39;</span><span class="p">]</span>
  237. </pre></div>
  238. </div>
  239. <p>The <a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">argparse</span></code></a> module provides a more sophisticated mechanism to process
  240. command line arguments. The following script extracts one or more filenames
  241. and an optional number of lines to be displayed:</p>
  242. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">argparse</span>
  243. <span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span>
  244. <span class="n">prog</span><span class="o">=</span><span class="s1">&#39;top&#39;</span><span class="p">,</span>
  245. <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Show top lines from each file&#39;</span><span class="p">)</span>
  246. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;filenames&#39;</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s1">&#39;+&#39;</span><span class="p">)</span>
  247. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;-l&#39;</span><span class="p">,</span> <span class="s1">&#39;--lines&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
  248. <span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
  249. <span class="nb">print</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
  250. </pre></div>
  251. </div>
  252. <p>When run at the command line with <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">top.py</span> <span class="pre">--lines=5</span> <span class="pre">alpha.txt</span>
  253. <span class="pre">beta.txt</span></code>, the script sets <code class="docutils literal notranslate"><span class="pre">args.lines</span></code> to <code class="docutils literal notranslate"><span class="pre">5</span></code> and <code class="docutils literal notranslate"><span class="pre">args.filenames</span></code>
  254. to <code class="docutils literal notranslate"><span class="pre">['alpha.txt',</span> <span class="pre">'beta.txt']</span></code>.</p>
  255. </section>
  256. <section id="error-output-redirection-and-program-termination">
  257. <span id="tut-stderr"></span><h2><span class="section-number">10.4. </span>Error Output Redirection and Program Termination<a class="headerlink" href="#error-output-redirection-and-program-termination" title="Permalink to this headline">¶</a></h2>
  258. <p>The <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> module also has attributes for <em>stdin</em>, <em>stdout</em>, and <em>stderr</em>.
  259. The latter is useful for emitting warnings and error messages to make them
  260. visible even when <em>stdout</em> has been redirected:</p>
  261. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;Warning, log file not found starting a new one</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
  262. <span class="go">Warning, log file not found starting a new one</span>
  263. </pre></div>
  264. </div>
  265. <p>The most direct way to terminate a script is to use <code class="docutils literal notranslate"><span class="pre">sys.exit()</span></code>.</p>
  266. </section>
  267. <section id="string-pattern-matching">
  268. <span id="tut-string-pattern-matching"></span><h2><span class="section-number">10.5. </span>String Pattern Matching<a class="headerlink" href="#string-pattern-matching" title="Permalink to this headline">¶</a></h2>
  269. <p>The <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a> module provides regular expression tools for advanced string
  270. processing. For complex matching and manipulation, regular expressions offer
  271. succinct, optimized solutions:</p>
  272. <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">re</span>
  273. <span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;\bf[a-z]*&#39;</span><span class="p">,</span> <span class="s1">&#39;which foot or hand fell fastest&#39;</span><span class="p">)</span>
  274. <span class="go">[&#39;foot&#39;, &#39;fell&#39;, &#39;fastest&#39;]</span>
  275. <span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;(\b[a-z]+) \1&#39;</span><span class="p">,</span> <span class="sa">r</span><span class="s1">&#39;\1&#39;</span><span class="p">,</span> <span class="s1">&#39;cat in the the hat&#39;</span><span class="p">)</span>
  276. <span class="go">&#39;cat in the hat&#39;</span>
  277. </pre></div>
  278. </div>
  279. <p>When only simple capabilities are needed, string methods are preferred because
  280. they are easier to read and debug:</p>
  281. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;tea for too&#39;</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;too&#39;</span><span class="p">,</span> <span class="s1">&#39;two&#39;</span><span class="p">)</span>
  282. <span class="go">&#39;tea for two&#39;</span>
  283. </pre></div>
  284. </div>
  285. </section>
  286. <section id="mathematics">
  287. <span id="tut-mathematics"></span><h2><span class="section-number">10.6. </span>Mathematics<a class="headerlink" href="#mathematics" title="Permalink to this headline">¶</a></h2>
  288. <p>The <a class="reference internal" href="../library/math.html#module-math" title="math: Mathematical functions (sin() etc.)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code></a> module gives access to the underlying C library functions for
  289. floating point math:</p>
  290. <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">math</span>
  291. <span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mi">4</span><span class="p">)</span>
  292. <span class="go">0.70710678118654757</span>
  293. <span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="mi">1024</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
  294. <span class="go">10.0</span>
  295. </pre></div>
  296. </div>
  297. <p>The <a class="reference internal" href="../library/random.html#module-random" title="random: Generate pseudo-random numbers with various common distributions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">random</span></code></a> module provides tools for making random selections:</p>
  298. <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">random</span>
  299. <span class="gp">&gt;&gt;&gt; </span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">([</span><span class="s1">&#39;apple&#39;</span><span class="p">,</span> <span class="s1">&#39;pear&#39;</span><span class="p">,</span> <span class="s1">&#39;banana&#39;</span><span class="p">])</span>
  300. <span class="go">&#39;apple&#39;</span>
  301. <span class="gp">&gt;&gt;&gt; </span><span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">),</span> <span class="mi">10</span><span class="p">)</span> <span class="c1"># sampling without replacement</span>
  302. <span class="go">[30, 83, 16, 4, 8, 81, 41, 50, 18, 33]</span>
  303. <span class="gp">&gt;&gt;&gt; </span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span> <span class="c1"># random float</span>
  304. <span class="go">0.17970987693706186</span>
  305. <span class="gp">&gt;&gt;&gt; </span><span class="n">random</span><span class="o">.</span><span class="n">randrange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span> <span class="c1"># random integer chosen from range(6)</span>
  306. <span class="go">4</span>
  307. </pre></div>
  308. </div>
  309. <p>The <a class="reference internal" href="../library/statistics.html#module-statistics" title="statistics: Mathematical statistics functions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">statistics</span></code></a> module calculates basic statistical properties
  310. (the mean, median, variance, etc.) of numeric data:</p>
  311. <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">statistics</span>
  312. <span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="mf">2.75</span><span class="p">,</span> <span class="mf">1.75</span><span class="p">,</span> <span class="mf">1.25</span><span class="p">,</span> <span class="mf">0.25</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">1.25</span><span class="p">,</span> <span class="mf">3.5</span><span class="p">]</span>
  313. <span class="gp">&gt;&gt;&gt; </span><span class="n">statistics</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  314. <span class="go">1.6071428571428572</span>
  315. <span class="gp">&gt;&gt;&gt; </span><span class="n">statistics</span><span class="o">.</span><span class="n">median</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  316. <span class="go">1.25</span>
  317. <span class="gp">&gt;&gt;&gt; </span><span class="n">statistics</span><span class="o">.</span><span class="n">variance</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  318. <span class="go">1.3720238095238095</span>
  319. </pre></div>
  320. </div>
  321. <p>The SciPy project &lt;<a class="reference external" href="https://scipy.org">https://scipy.org</a>&gt; has many other modules for numerical
  322. computations.</p>
  323. </section>
  324. <section id="internet-access">
  325. <span id="tut-internet-access"></span><h2><span class="section-number">10.7. </span>Internet Access<a class="headerlink" href="#internet-access" title="Permalink to this headline">¶</a></h2>
  326. <p>There are a number of modules for accessing the internet and processing internet
  327. protocols. Two of the simplest are <a class="reference internal" href="../library/urllib.request.html#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code></a> for retrieving data
  328. from URLs and <a class="reference internal" href="../library/smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code></a> for sending mail:</p>
  329. <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">urllib.request</span> <span class="kn">import</span> <span class="n">urlopen</span>
  330. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">urlopen</span><span class="p">(</span><span class="s1">&#39;http://worldtimeapi.org/api/timezone/etc/UTC.txt&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">response</span><span class="p">:</span>
  331. <span class="gp">... </span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">response</span><span class="p">:</span>
  332. <span class="gp">... </span> <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">decode</span><span class="p">()</span> <span class="c1"># Convert bytes to a str</span>
  333. <span class="gp">... </span> <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;datetime&#39;</span><span class="p">):</span>
  334. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">())</span> <span class="c1"># Remove trailing newline</span>
  335. <span class="gp">...</span>
  336. <span class="go">datetime: 2022-01-01T01:36:47.689215+00:00</span>
  337. <span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">smtplib</span>
  338. <span class="gp">&gt;&gt;&gt; </span><span class="n">server</span> <span class="o">=</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="s1">&#39;localhost&#39;</span><span class="p">)</span>
  339. <span class="gp">&gt;&gt;&gt; </span><span class="n">server</span><span class="o">.</span><span class="n">sendmail</span><span class="p">(</span><span class="s1">&#39;soothsayer@example.org&#39;</span><span class="p">,</span> <span class="s1">&#39;jcaesar@example.org&#39;</span><span class="p">,</span>
  340. <span class="gp">... </span><span class="sd">&quot;&quot;&quot;To: jcaesar@example.org</span>
  341. <span class="gp">... </span><span class="sd">From: soothsayer@example.org</span>
  342. <span class="gp">...</span>
  343. <span class="gp">... </span><span class="sd">Beware the Ides of March.</span>
  344. <span class="gp">... </span><span class="sd">&quot;&quot;&quot;</span><span class="p">)</span>
  345. <span class="gp">&gt;&gt;&gt; </span><span class="n">server</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
  346. </pre></div>
  347. </div>
  348. <p>(Note that the second example needs a mailserver running on localhost.)</p>
  349. </section>
  350. <section id="dates-and-times">
  351. <span id="tut-dates-and-times"></span><h2><span class="section-number">10.8. </span>Dates and Times<a class="headerlink" href="#dates-and-times" title="Permalink to this headline">¶</a></h2>
  352. <p>The <a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> module supplies classes for manipulating dates and times in
  353. both simple and complex ways. While date and time arithmetic is supported, the
  354. focus of the implementation is on efficient member extraction for output
  355. formatting and manipulation. The module also supports objects that are timezone
  356. aware.</p>
  357. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># dates are easily constructed and formatted</span>
  358. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">date</span>
  359. <span class="gp">&gt;&gt;&gt; </span><span class="n">now</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
  360. <span class="gp">&gt;&gt;&gt; </span><span class="n">now</span>
  361. <span class="go">datetime.date(2003, 12, 2)</span>
  362. <span class="gp">&gt;&gt;&gt; </span><span class="n">now</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%m-</span><span class="si">%d</span><span class="s2">-%y. </span><span class="si">%d</span><span class="s2"> %b %Y is a %A on the </span><span class="si">%d</span><span class="s2"> day of %B.&quot;</span><span class="p">)</span>
  363. <span class="go">&#39;12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.&#39;</span>
  364. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># dates support calendar arithmetic</span>
  365. <span class="gp">&gt;&gt;&gt; </span><span class="n">birthday</span> <span class="o">=</span> <span class="n">date</span><span class="p">(</span><span class="mi">1964</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">31</span><span class="p">)</span>
  366. <span class="gp">&gt;&gt;&gt; </span><span class="n">age</span> <span class="o">=</span> <span class="n">now</span> <span class="o">-</span> <span class="n">birthday</span>
  367. <span class="gp">&gt;&gt;&gt; </span><span class="n">age</span><span class="o">.</span><span class="n">days</span>
  368. <span class="go">14368</span>
  369. </pre></div>
  370. </div>
  371. </section>
  372. <section id="data-compression">
  373. <span id="tut-data-compression"></span><h2><span class="section-number">10.9. </span>Data Compression<a class="headerlink" href="#data-compression" title="Permalink to this headline">¶</a></h2>
  374. <p>Common data archiving and compression formats are directly supported by modules
  375. including: <a class="reference internal" href="../library/zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a>, <a class="reference internal" href="../library/gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code></a>, <a class="reference internal" href="../library/bz2.html#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code></a>, <a class="reference internal" href="../library/lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a>, <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> and
  376. <a class="reference internal" href="../library/tarfile.html#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a>.</p>
  377. <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">zlib</span>
  378. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;witch which has which witches wrist watch&#39;</span>
  379. <span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
  380. <span class="go">41</span>
  381. <span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">zlib</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
  382. <span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
  383. <span class="go">37</span>
  384. <span class="gp">&gt;&gt;&gt; </span><span class="n">zlib</span><span class="o">.</span><span class="n">decompress</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
  385. <span class="go">b&#39;witch which has which witches wrist watch&#39;</span>
  386. <span class="gp">&gt;&gt;&gt; </span><span class="n">zlib</span><span class="o">.</span><span class="n">crc32</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
  387. <span class="go">226805979</span>
  388. </pre></div>
  389. </div>
  390. </section>
  391. <section id="performance-measurement">
  392. <span id="tut-performance-measurement"></span><h2><span class="section-number">10.10. </span>Performance Measurement<a class="headerlink" href="#performance-measurement" title="Permalink to this headline">¶</a></h2>
  393. <p>Some Python users develop a deep interest in knowing the relative performance of
  394. different approaches to the same problem. Python provides a measurement tool
  395. that answers those questions immediately.</p>
  396. <p>For example, it may be tempting to use the tuple packing and unpacking feature
  397. instead of the traditional approach to swapping arguments. The <a class="reference internal" href="../library/timeit.html#module-timeit" title="timeit: Measure the execution time of small code snippets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">timeit</span></code></a>
  398. module quickly demonstrates a modest performance advantage:</p>
  399. <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">timeit</span> <span class="kn">import</span> <span class="n">Timer</span>
  400. <span class="gp">&gt;&gt;&gt; </span><span class="n">Timer</span><span class="p">(</span><span class="s1">&#39;t=a; a=b; b=t&#39;</span><span class="p">,</span> <span class="s1">&#39;a=1; b=2&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">timeit</span><span class="p">()</span>
  401. <span class="go">0.57535828626024577</span>
  402. <span class="gp">&gt;&gt;&gt; </span><span class="n">Timer</span><span class="p">(</span><span class="s1">&#39;a,b = b,a&#39;</span><span class="p">,</span> <span class="s1">&#39;a=1; b=2&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">timeit</span><span class="p">()</span>
  403. <span class="go">0.54962537085770791</span>
  404. </pre></div>
  405. </div>
  406. <p>In contrast to <a class="reference internal" href="../library/timeit.html#module-timeit" title="timeit: Measure the execution time of small code snippets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">timeit</span></code></a>’s fine level of granularity, the <a class="reference internal" href="../library/profile.html#module-profile" title="profile: Python source profiler."><code class="xref py py-mod docutils literal notranslate"><span class="pre">profile</span></code></a> and
  407. <a class="reference internal" href="../library/profile.html#module-pstats" title="pstats: Statistics object for use with the profiler."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pstats</span></code></a> modules provide tools for identifying time critical sections in
  408. larger blocks of code.</p>
  409. </section>
  410. <section id="quality-control">
  411. <span id="tut-quality-control"></span><h2><span class="section-number">10.11. </span>Quality Control<a class="headerlink" href="#quality-control" title="Permalink to this headline">¶</a></h2>
  412. <p>One approach for developing high quality software is to write tests for each
  413. function as it is developed and to run those tests frequently during the
  414. development process.</p>
  415. <p>The <a class="reference internal" href="../library/doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> module provides a tool for scanning a module and validating
  416. tests embedded in a program’s docstrings. Test construction is as simple as
  417. cutting-and-pasting a typical call along with its results into the docstring.
  418. This improves the documentation by providing the user with an example and it
  419. allows the doctest module to make sure the code remains true to the
  420. documentation:</p>
  421. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">average</span><span class="p">(</span><span class="n">values</span><span class="p">):</span>
  422. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Computes the arithmetic mean of a list of numbers.</span>
  423. <span class="sd"> &gt;&gt;&gt; print(average([20, 30, 70]))</span>
  424. <span class="sd"> 40.0</span>
  425. <span class="sd"> &quot;&quot;&quot;</span>
  426. <span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="n">values</span><span class="p">)</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
  427. <span class="kn">import</span> <span class="nn">doctest</span>
  428. <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span> <span class="c1"># automatically validate the embedded tests</span>
  429. </pre></div>
  430. </div>
  431. <p>The <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> module is not as effortless as the <a class="reference internal" href="../library/doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> module,
  432. but it allows a more comprehensive set of tests to be maintained in a separate
  433. file:</p>
  434. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">unittest</span>
  435. <span class="k">class</span> <span class="nc">TestStatisticalFunctions</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
  436. <span class="k">def</span> <span class="nf">test_average</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  437. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">average</span><span class="p">([</span><span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">70</span><span class="p">]),</span> <span class="mf">40.0</span><span class="p">)</span>
  438. <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">average</span><span class="p">([</span><span class="mi">1</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">1</span><span class="p">),</span> <span class="mf">4.3</span><span class="p">)</span>
  439. <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">):</span>
  440. <span class="n">average</span><span class="p">([])</span>
  441. <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="ne">TypeError</span><span class="p">):</span>
  442. <span class="n">average</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">70</span><span class="p">)</span>
  443. <span class="n">unittest</span><span class="o">.</span><span class="n">main</span><span class="p">()</span> <span class="c1"># Calling from the command line invokes all tests</span>
  444. </pre></div>
  445. </div>
  446. </section>
  447. <section id="batteries-included">
  448. <span id="tut-batteries-included"></span><h2><span class="section-number">10.12. </span>Batteries Included<a class="headerlink" href="#batteries-included" title="Permalink to this headline">¶</a></h2>
  449. <p>Python has a “batteries included” philosophy. This is best seen through the
  450. sophisticated and robust capabilities of its larger packages. For example:</p>
  451. <ul class="simple">
  452. <li><p>The <a class="reference internal" href="../library/xmlrpc.client.html#module-xmlrpc.client" title="xmlrpc.client: XML-RPC client access."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpc.client</span></code></a> and <a class="reference internal" href="../library/xmlrpc.server.html#module-xmlrpc.server" title="xmlrpc.server: Basic XML-RPC server implementations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpc.server</span></code></a> modules make implementing
  453. remote procedure calls into an almost trivial task. Despite the modules’
  454. names, no direct knowledge or handling of XML is needed.</p></li>
  455. <li><p>The <a class="reference internal" href="../library/email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> package is a library for managing email messages, including
  456. MIME and other <span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2822.html"><strong>RFC 2822</strong></a>-based message documents. Unlike <a class="reference internal" href="../library/smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code></a> and
  457. <a class="reference internal" href="../library/poplib.html#module-poplib" title="poplib: POP3 protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">poplib</span></code></a> which actually send and receive messages, the email package has
  458. a complete toolset for building or decoding complex message structures
  459. (including attachments) and for implementing internet encoding and header
  460. protocols.</p></li>
  461. <li><p>The <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a> package provides robust support for parsing this
  462. popular data interchange format. The <a class="reference internal" href="../library/csv.html#module-csv" title="csv: Write and read tabular data to and from delimited files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">csv</span></code></a> module supports
  463. direct reading and writing of files in Comma-Separated Value format,
  464. commonly supported by databases and spreadsheets. XML processing is
  465. supported by the <a class="reference internal" href="../library/xml.etree.elementtree.html#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a>, <a class="reference internal" href="../library/xml.dom.html#module-xml.dom" title="xml.dom: Document Object Model API for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom</span></code></a> and
  466. <a class="reference internal" href="../library/xml.sax.html#module-xml.sax" title="xml.sax: Package containing SAX2 base classes and convenience functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.sax</span></code></a> packages. Together, these modules and packages
  467. greatly simplify data interchange between Python applications and
  468. other tools.</p></li>
  469. <li><p>The <a class="reference internal" href="../library/sqlite3.html#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code></a> module is a wrapper for the SQLite database
  470. library, providing a persistent database that can be updated and
  471. accessed using slightly nonstandard SQL syntax.</p></li>
  472. <li><p>Internationalization is supported by a number of modules including
  473. <a class="reference internal" href="../library/gettext.html#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a>, <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>, and the <a class="reference internal" href="../library/codecs.html#module-codecs" title="codecs: Encode and decode data and streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code></a> package.</p></li>
  474. </ul>
  475. </section>
  476. </section>
  477. <div class="clearer"></div>
  478. </div>
  479. </div>
  480. </div>
  481. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  482. <div class="sphinxsidebarwrapper">
  483. <div>
  484. <h3><a href="../contents.html">Table of Contents</a></h3>
  485. <ul>
  486. <li><a class="reference internal" href="#">10. Brief Tour of the Standard Library</a><ul>
  487. <li><a class="reference internal" href="#operating-system-interface">10.1. Operating System Interface</a></li>
  488. <li><a class="reference internal" href="#file-wildcards">10.2. File Wildcards</a></li>
  489. <li><a class="reference internal" href="#command-line-arguments">10.3. Command Line Arguments</a></li>
  490. <li><a class="reference internal" href="#error-output-redirection-and-program-termination">10.4. Error Output Redirection and Program Termination</a></li>
  491. <li><a class="reference internal" href="#string-pattern-matching">10.5. String Pattern Matching</a></li>
  492. <li><a class="reference internal" href="#mathematics">10.6. Mathematics</a></li>
  493. <li><a class="reference internal" href="#internet-access">10.7. Internet Access</a></li>
  494. <li><a class="reference internal" href="#dates-and-times">10.8. Dates and Times</a></li>
  495. <li><a class="reference internal" href="#data-compression">10.9. Data Compression</a></li>
  496. <li><a class="reference internal" href="#performance-measurement">10.10. Performance Measurement</a></li>
  497. <li><a class="reference internal" href="#quality-control">10.11. Quality Control</a></li>
  498. <li><a class="reference internal" href="#batteries-included">10.12. Batteries Included</a></li>
  499. </ul>
  500. </li>
  501. </ul>
  502. </div>
  503. <div>
  504. <h4>Previous topic</h4>
  505. <p class="topless"><a href="classes.html"
  506. title="previous chapter"><span class="section-number">9. </span>Classes</a></p>
  507. </div>
  508. <div>
  509. <h4>Next topic</h4>
  510. <p class="topless"><a href="stdlib2.html"
  511. title="next chapter"><span class="section-number">11. </span>Brief Tour of the Standard Library — Part II</a></p>
  512. </div>
  513. <div role="note" aria-label="source link">
  514. <h3>This Page</h3>
  515. <ul class="this-page-menu">
  516. <li><a href="../bugs.html">Report a Bug</a></li>
  517. <li>
  518. <a href="https://github.com/python/cpython/blob/main/Doc/tutorial/stdlib.rst"
  519. rel="nofollow">Show Source
  520. </a>
  521. </li>
  522. </ul>
  523. </div>
  524. </div>
  525. </div>
  526. <div class="clearer"></div>
  527. </div>
  528. <div class="related" role="navigation" aria-label="related navigation">
  529. <h3>Navigation</h3>
  530. <ul>
  531. <li class="right" style="margin-right: 10px">
  532. <a href="../genindex.html" title="General Index"
  533. >index</a></li>
  534. <li class="right" >
  535. <a href="../py-modindex.html" title="Python Module Index"
  536. >modules</a> |</li>
  537. <li class="right" >
  538. <a href="stdlib2.html" title="11. Brief Tour of the Standard Library — Part II"
  539. >next</a> |</li>
  540. <li class="right" >
  541. <a href="classes.html" title="9. Classes"
  542. >previous</a> |</li>
  543. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  544. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  545. <li class="switchers">
  546. <div class="language_switcher_placeholder"></div>
  547. <div class="version_switcher_placeholder"></div>
  548. </li>
  549. <li>
  550. </li>
  551. <li id="cpython-language-and-version">
  552. <a href="../index.html">3.12.0 Documentation</a> &#187;
  553. </li>
  554. <li class="nav-item nav-item-1"><a href="index.html" >The Python Tutorial</a> &#187;</li>
  555. <li class="nav-item nav-item-this"><a href=""><span class="section-number">10. </span>Brief Tour of the Standard Library</a></li>
  556. <li class="right">
  557. <div class="inline-search" role="search">
  558. <form class="inline-search" action="../search.html" method="get">
  559. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  560. <input type="submit" value="Go" />
  561. </form>
  562. </div>
  563. |
  564. </li>
  565. <li class="right">
  566. <label class="theme-selector-label">
  567. Theme
  568. <select class="theme-selector" oninput="activateTheme(this.value)">
  569. <option value="auto" selected>Auto</option>
  570. <option value="light">Light</option>
  571. <option value="dark">Dark</option>
  572. </select>
  573. </label> |</li>
  574. </ul>
  575. </div>
  576. <div class="footer">
  577. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  578. <br />
  579. This page is licensed under the Python Software Foundation License Version 2.
  580. <br />
  581. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  582. <br />
  583. See <a href="/license.html">History and License</a> for more information.<br />
  584. <br />
  585. The Python Software Foundation is a non-profit corporation.
  586. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  587. <br />
  588. <br />
  589. Last updated on Oct 02, 2023.
  590. <a href="/bugs.html">Found a bug</a>?
  591. <br />
  592. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  593. </div>
  594. </body>
  595. </html>