Python Module of the Week
Release 1.132
Doug Hellmann
December 02, 2016
CONTENTS
1 Data Persistence and Exchange 3
1.1 Serializing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Storing Serialized Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Data Exchange Through Standard Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 In-Memory Data Structures 5
2.1 array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4 collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.5 Decoding Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.6 Custom Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 File Access 7
3.1 Filenames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Meta-data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 Reading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.4 Temporary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.5 Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4 Text Processing Tools 9
4.1 string module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.2 Text Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.3 Text Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.4 Comparing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5 Built-in Objects 11
5.1 exceptions – Built-in error classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6 String Services 23
6.1 codecs – String encoding and decoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6.2 difflib – Compare sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6.3 string – Working with text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.4 StringIO and cStringIO – Work with text buffers using file-like API . . . . . . . . . . . . . . . . . . 54
6.5 re – Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
6.6 struct – Working with Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
6.7 textwrap – Formatting text paragraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
7 Data Types 97
7.1 array – Sequence of fixed-type data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
i
7.2 datetime – Date/time value manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
7.3 calendar – Work with dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
7.4 collections – Container data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
7.5 heapq – In-place heap sort algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
7.6 bisect – Maintain lists in sorted order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
7.7 sched – Generic event scheduler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
7.8 Queue – A thread-safe FIFO implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
7.9 weakref – Garbage-collectable references to objects . . . . . . . . . . . . . . . . . . . . . . . . . . 135
7.10 copy – Duplicate objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
7.11 pprint – Pretty-print data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
8 Numeric and Mathematical Modules 153
8.1 decimal – Fixed and floating point math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
8.2 fractions – Rational Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
8.3 functools – Tools for Manipulating Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
8.4 itertools – Iterator functions for efficient looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
8.5 math – Mathematical functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
8.6 operator – Functional interface to built-in operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
8.7 random – Pseudorandom number generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
9 Internet Data Handling 213
9.1 base64 – Encode binary data into ASCII characters . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
9.2 json – JavaScript Object Notation Serializer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
9.3 mailbox – Access and manipulate email archives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
9.4 mhlib – Work with MH mailboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
10 File Formats 233
10.1 csv – Comma-separated value files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
10.2 ConfigParser – Work with configuration files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
10.3 robotparser – Internet spider access control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
11 Cryptographic Services 255
11.1 hashlib – Cryptographic hashes and message digests . . . . . . . . . . . . . . . . . . . . . . . . . . 255
11.2 hmac – Cryptographic signature and verification of messages. . . . . . . . . . . . . . . . . . . . . . 258
12 File and Directory Access 263
12.1 os.path – Platform-independent manipulation of file names. . . . . . . . . . . . . . . . . . . . . . . 263
12.2 fileinput – Process lines from input streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
12.3 filecmp – Compare files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
12.4 tempfile – Create temporary filesystem resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
12.5 glob – Filename pattern matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
12.6 fnmatch – Compare filenames against Unix-style glob patterns. . . . . . . . . . . . . . . . . . . . . 285
12.7 linecache – Read text files efficiently . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
12.8 shutil – High-level file operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
12.9 dircache – Cache directory listings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
13 Data Compression and Archiving 299
13.1 bz2 – bzip2 compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
13.2 gzip – Read and write GNU zip files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
13.3 tarfile – Tar archive access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
13.4 zipfile – Read and write ZIP archive files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
13.5 zlib – Low-level access to GNU zlib compression library . . . . . . . . . . . . . . . . . . . . . . . . 326
14 Data Persistence 333
14.1 anydbm – Access to DBM-style databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
ii
14.2 dbhash – DBM-style API for the BSD database library . . . . . . . . . . . . . . . . . . . . . . . . . 335
14.3 dbm – Simple database interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
14.4 dumbdbm – Portable DBM Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
14.5 gdbm – GNU’s version of the dbm library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
14.6 pickle and cPickle – Python object serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
14.7 shelve – Persistent storage of arbitrary Python objects . . . . . . . . . . . . . . . . . . . . . . . . . 344
14.8 whichdb – Identify DBM-style database formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
14.9 sqlite3 – Embedded Relational Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
15 Generic Operating System Services 377
15.1 os – Portable access to operating system specific features. . . . . . . . . . . . . . . . . . . . . . . . 377
15.2 time – Functions for manipulating clock time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
15.3 getopt – Command line option parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
15.4 optparse – Command line option parser to replace getopt. . . . . . . . . . . . . . . . . . . . . . . . 402
15.5 argparse – Command line option and argument parsing. . . . . . . . . . . . . . . . . . . . . . . . . 414
15.6 logging – Report status, error, and informational messages. . . . . . . . . . . . . . . . . . . . . . . . 433
15.7 getpass – Prompt the user for a password without echoing. . . . . . . . . . . . . . . . . . . . . . . . 436
15.8 platform – Access system version information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
16 Optional Operating System Services 443
16.1 threading – Manage concurrent threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
16.2 mmap – Memory-map files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
16.3 multiprocessing – Manage processes like threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
16.4 readline – Interface to the GNU readline library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
16.5 rlcompleter – Adds tab-completion to the interactive interpreter . . . . . . . . . . . . . . . . . . . . 494
17 Unix-specific Services 495
17.1 commands – Run external shell commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
17.2 grp – Unix Group Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
17.3 pipes – Unix shell command pipeline templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
17.4 pwd – Unix Password Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
17.5 resource – System resource management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
18 Interprocess Communication and Networking 513
18.1 asynchat – Asynchronous protocol handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
18.2 asyncore – Asynchronous I/O handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
18.3 signal – Receive notification of asynchronous system events . . . . . . . . . . . . . . . . . . . . . . 528
18.4 subprocess – Work with additional processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
19 Internet Protocols and Support 545
19.1 BaseHTTPServer – base classes for implementing web servers . . . . . . . . . . . . . . . . . . . . . 545
19.2 cgitb – Detailed traceback reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
19.3 Cookie – HTTP Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
19.4 imaplib - IMAP4 client library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
19.5 SimpleXMLRPCServer – Implements an XML-RPC server. . . . . . . . . . . . . . . . . . . . . . . 582
19.6 smtpd – Sample SMTP Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
19.7 smtplib – Simple Mail Transfer Protocol client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
19.8 socket – Network Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
19.9 select – Wait for I/O Efficiently . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
19.10 SocketServer – Creating network servers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
19.11 urllib – simple interface for network resource access . . . . . . . . . . . . . . . . . . . . . . . . . . 637
19.12 urllib2 – Library for opening URLs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
19.13 urlparse – Split URL into component pieces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
19.14 uuid – Universally unique identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
19.15 webbrowser – Displays web pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
iii