Home | History | Annotate | Download | only in tutorial
      1 .. _tut-brieftour:
      2 
      3 **********************************
      4 Brief Tour of the Standard Library
      5 **********************************
      6 
      7 
      8 .. _tut-os-interface:
      9 
     10 Operating System Interface
     11 ==========================
     12 
     13 The :mod:`os` module provides dozens of functions for interacting with the
     14 operating system::
     15 
     16    >>> import os
     17    >>> os.getcwd()      # Return the current working directory
     18    'C:\\Python37'
     19    >>> os.chdir('/server/accesslogs')   # Change current working directory
     20    >>> os.system('mkdir today')   # Run the command mkdir in the system shell
     21    0
     22 
     23 Be sure to use the ``import os`` style instead of ``from os import *``.  This
     24 will keep :func:`os.open` from shadowing the built-in :func:`open` function which
     25 operates much differently.
     26 
     27 .. index:: builtin: help
     28 
     29 The built-in :func:`dir` and :func:`help` functions are useful as interactive
     30 aids for working with large modules like :mod:`os`::
     31 
     32    >>> import os
     33    >>> dir(os)
     34    <returns a list of all module functions>
     35    >>> help(os)
     36    <returns an extensive manual page created from the module's docstrings>
     37 
     38 For daily file and directory management tasks, the :mod:`shutil` module provides
     39 a higher level interface that is easier to use::
     40 
     41    >>> import shutil
     42    >>> shutil.copyfile('data.db', 'archive.db')
     43    'archive.db'
     44    >>> shutil.move('/build/executables', 'installdir')
     45    'installdir'
     46 
     47 
     48 .. _tut-file-wildcards:
     49 
     50 File Wildcards
     51 ==============
     52 
     53 The :mod:`glob` module provides a function for making file lists from directory
     54 wildcard searches::
     55 
     56    >>> import glob
     57    >>> glob.glob('*.py')
     58    ['primes.py', 'random.py', 'quote.py']
     59 
     60 
     61 .. _tut-command-line-arguments:
     62 
     63 Command Line Arguments
     64 ======================
     65 
     66 Common utility scripts often need to process command line arguments. These
     67 arguments are stored in the :mod:`sys` module's *argv* attribute as a list.  For
     68 instance the following output results from running ``python demo.py one two
     69 three`` at the command line::
     70 
     71    >>> import sys
     72    >>> print(sys.argv)
     73    ['demo.py', 'one', 'two', 'three']
     74 
     75 The :mod:`getopt` module processes *sys.argv* using the conventions of the Unix
     76 :func:`getopt` function.  More powerful and flexible command line processing is
     77 provided by the :mod:`argparse` module.
     78 
     79 
     80 .. _tut-stderr:
     81 
     82 Error Output Redirection and Program Termination
     83 ================================================
     84 
     85 The :mod:`sys` module also has attributes for *stdin*, *stdout*, and *stderr*.
     86 The latter is useful for emitting warnings and error messages to make them
     87 visible even when *stdout* has been redirected::
     88 
     89    >>> sys.stderr.write('Warning, log file not found starting a new one\n')
     90    Warning, log file not found starting a new one
     91 
     92 The most direct way to terminate a script is to use ``sys.exit()``.
     93 
     94 
     95 .. _tut-string-pattern-matching:
     96 
     97 String Pattern Matching
     98 =======================
     99 
    100 The :mod:`re` module provides regular expression tools for advanced string
    101 processing. For complex matching and manipulation, regular expressions offer
    102 succinct, optimized solutions::
    103 
    104    >>> import re
    105    >>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
    106    ['foot', 'fell', 'fastest']
    107    >>> re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')
    108    'cat in the hat'
    109 
    110 When only simple capabilities are needed, string methods are preferred because
    111 they are easier to read and debug::
    112 
    113    >>> 'tea for too'.replace('too', 'two')
    114    'tea for two'
    115 
    116 
    117 .. _tut-mathematics:
    118 
    119 Mathematics
    120 ===========
    121 
    122 The :mod:`math` module gives access to the underlying C library functions for
    123 floating point math::
    124 
    125    >>> import math
    126    >>> math.cos(math.pi / 4)
    127    0.70710678118654757
    128    >>> math.log(1024, 2)
    129    10.0
    130 
    131 The :mod:`random` module provides tools for making random selections::
    132 
    133    >>> import random
    134    >>> random.choice(['apple', 'pear', 'banana'])
    135    'apple'
    136    >>> random.sample(range(100), 10)   # sampling without replacement
    137    [30, 83, 16, 4, 8, 81, 41, 50, 18, 33]
    138    >>> random.random()    # random float
    139    0.17970987693706186
    140    >>> random.randrange(6)    # random integer chosen from range(6)
    141    4
    142 
    143 The :mod:`statistics` module calculates basic statistical properties
    144 (the mean, median, variance, etc.) of numeric data::
    145 
    146     >>> import statistics
    147     >>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
    148     >>> statistics.mean(data)
    149     1.6071428571428572
    150     >>> statistics.median(data)
    151     1.25
    152     >>> statistics.variance(data)
    153     1.3720238095238095
    154 
    155 The SciPy project <https://scipy.org> has many other modules for numerical
    156 computations.
    157 
    158 .. _tut-internet-access:
    159 
    160 Internet Access
    161 ===============
    162 
    163 There are a number of modules for accessing the internet and processing internet
    164 protocols. Two of the simplest are :mod:`urllib.request` for retrieving data
    165 from URLs and :mod:`smtplib` for sending mail::
    166 
    167    >>> from urllib.request import urlopen
    168    >>> with urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl') as response:
    169    ...     for line in response:
    170    ...         line = line.decode('utf-8')  # Decoding the binary data to text.
    171    ...         if 'EST' in line or 'EDT' in line:  # look for Eastern Time
    172    ...             print(line)
    173 
    174    <BR>Nov. 25, 09:43:32 PM EST
    175 
    176    >>> import smtplib
    177    >>> server = smtplib.SMTP('localhost')
    178    >>> server.sendmail('soothsayer (a] example.org', 'jcaesar (a] example.org',
    179    ... """To: jcaesar (a] example.org
    180    ... From: soothsayer (a] example.org
    181    ...
    182    ... Beware the Ides of March.
    183    ... """)
    184    >>> server.quit()
    185 
    186 (Note that the second example needs a mailserver running on localhost.)
    187 
    188 
    189 .. _tut-dates-and-times:
    190 
    191 Dates and Times
    192 ===============
    193 
    194 The :mod:`datetime` module supplies classes for manipulating dates and times in
    195 both simple and complex ways. While date and time arithmetic is supported, the
    196 focus of the implementation is on efficient member extraction for output
    197 formatting and manipulation.  The module also supports objects that are timezone
    198 aware. ::
    199 
    200    >>> # dates are easily constructed and formatted
    201    >>> from datetime import date
    202    >>> now = date.today()
    203    >>> now
    204    datetime.date(2003, 12, 2)
    205    >>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
    206    '12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'
    207 
    208    >>> # dates support calendar arithmetic
    209    >>> birthday = date(1964, 7, 31)
    210    >>> age = now - birthday
    211    >>> age.days
    212    14368
    213 
    214 
    215 .. _tut-data-compression:
    216 
    217 Data Compression
    218 ================
    219 
    220 Common data archiving and compression formats are directly supported by modules
    221 including: :mod:`zlib`, :mod:`gzip`, :mod:`bz2`, :mod:`lzma`, :mod:`zipfile` and
    222 :mod:`tarfile`. ::
    223 
    224    >>> import zlib
    225    >>> s = b'witch which has which witches wrist watch'
    226    >>> len(s)
    227    41
    228    >>> t = zlib.compress(s)
    229    >>> len(t)
    230    37
    231    >>> zlib.decompress(t)
    232    b'witch which has which witches wrist watch'
    233    >>> zlib.crc32(s)
    234    226805979
    235 
    236 
    237 .. _tut-performance-measurement:
    238 
    239 Performance Measurement
    240 =======================
    241 
    242 Some Python users develop a deep interest in knowing the relative performance of
    243 different approaches to the same problem. Python provides a measurement tool
    244 that answers those questions immediately.
    245 
    246 For example, it may be tempting to use the tuple packing and unpacking feature
    247 instead of the traditional approach to swapping arguments. The :mod:`timeit`
    248 module quickly demonstrates a modest performance advantage::
    249 
    250    >>> from timeit import Timer
    251    >>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
    252    0.57535828626024577
    253    >>> Timer('a,b = b,a', 'a=1; b=2').timeit()
    254    0.54962537085770791
    255 
    256 In contrast to :mod:`timeit`'s fine level of granularity, the :mod:`profile` and
    257 :mod:`pstats` modules provide tools for identifying time critical sections in
    258 larger blocks of code.
    259 
    260 
    261 .. _tut-quality-control:
    262 
    263 Quality Control
    264 ===============
    265 
    266 One approach for developing high quality software is to write tests for each
    267 function as it is developed and to run those tests frequently during the
    268 development process.
    269 
    270 The :mod:`doctest` module provides a tool for scanning a module and validating
    271 tests embedded in a program's docstrings.  Test construction is as simple as
    272 cutting-and-pasting a typical call along with its results into the docstring.
    273 This improves the documentation by providing the user with an example and it
    274 allows the doctest module to make sure the code remains true to the
    275 documentation::
    276 
    277    def average(values):
    278        """Computes the arithmetic mean of a list of numbers.
    279 
    280        >>> print(average([20, 30, 70]))
    281        40.0
    282        """
    283        return sum(values) / len(values)
    284 
    285    import doctest
    286    doctest.testmod()   # automatically validate the embedded tests
    287 
    288 The :mod:`unittest` module is not as effortless as the :mod:`doctest` module,
    289 but it allows a more comprehensive set of tests to be maintained in a separate
    290 file::
    291 
    292    import unittest
    293 
    294    class TestStatisticalFunctions(unittest.TestCase):
    295 
    296        def test_average(self):
    297            self.assertEqual(average([20, 30, 70]), 40.0)
    298            self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
    299            with self.assertRaises(ZeroDivisionError):
    300                average([])
    301            with self.assertRaises(TypeError):
    302                average(20, 30, 70)
    303 
    304    unittest.main()  # Calling from the command line invokes all tests
    305 
    306 
    307 .. _tut-batteries-included:
    308 
    309 Batteries Included
    310 ==================
    311 
    312 Python has a "batteries included" philosophy.  This is best seen through the
    313 sophisticated and robust capabilities of its larger packages. For example:
    314 
    315 * The :mod:`xmlrpc.client` and :mod:`xmlrpc.server` modules make implementing
    316   remote procedure calls into an almost trivial task.  Despite the modules
    317   names, no direct knowledge or handling of XML is needed.
    318 
    319 * The :mod:`email` package is a library for managing email messages, including
    320   MIME and other :rfc:`2822`-based message documents. Unlike :mod:`smtplib` and
    321   :mod:`poplib` which actually send and receive messages, the email package has
    322   a complete toolset for building or decoding complex message structures
    323   (including attachments) and for implementing internet encoding and header
    324   protocols.
    325 
    326 * The :mod:`json` package provides robust support for parsing this
    327   popular data interchange format.  The :mod:`csv` module supports
    328   direct reading and writing of files in Comma-Separated Value format,
    329   commonly supported by databases and spreadsheets.  XML processing is
    330   supported by the :mod:`xml.etree.ElementTree`, :mod:`xml.dom` and
    331   :mod:`xml.sax` packages. Together, these modules and packages
    332   greatly simplify data interchange between Python applications and
    333   other tools.
    334 
    335 * The :mod:`sqlite3` module is a wrapper for the SQLite database
    336   library, providing a persistent database that can be updated and
    337   accessed using slightly nonstandard SQL syntax.
    338 
    339 * Internationalization is supported by a number of modules including
    340   :mod:`gettext`, :mod:`locale`, and the :mod:`codecs` package.
    341