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