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