python_3_standard_library_by_example_index

Python 3 Standard Library by Example Index

A

line-oriented command processors. See cmd module

overview of]], 887–888

parsing shell-style syntaxes. See shlex module

parsing/validating command-line arguments. See argparse module

program shutdown callbacks. See atexit module

secure password prompt. See getpass module

supplements to standard library]], 1370–1371

timed event scheduler]], 998–1002

Application threads. See threading module

Applications

configuring logging for]], 980–981

localization]], 1011

Approximation distribution, random]], 263

Arbitrary API names]], xmlrpc.server]], 831–832

Arbitrary classes, pprint]], 138–139

Arbitrary context callbacks, contextlib]], 206–207

Archives

accessing tar. See tarfile module

managing in shutil]], 346–350

manipulating email. See mailbox module

ZIP. See ZIP archive; zipfile module

argparse module

advanced argument processing]], 908–916

argument actions]], 891–894

changes in Python 3]], 1356

defining arguments]], 888

help output]], 897–901

interface-related supplements to standard library]], 1370

option prefixes]], 894–895

overview of]], 887

parser organization]], 901–908

parsing command line]], 889

parsing/validating command-line arguments]], 888

setting up parser]], 888

simple examples]], 889–892

sources of arguments]], 895–897

args command, pdb]], 1107–1108

ArgumentParser. See argparse module

Arguments

chain() taking iterators as, itertools]], 164–166

command]], 939–940

command-line option parsing. See getopt module

encoding, urllib.request]], 763–764

mail server base class, smtpd]], 848

parsing/validating command-line. See argparse module

passing to registered functions, atexit]], 993–994

Arithmetic

aggregating results, counter]], 82–83

calculating timedeltas, datetime]], 225–226

date]], 226–227

decimal]], 242–243

fractions]], 252–253

operators]], 184–186

array module alternative byte ordering]], 101–102

arrays and files]], 100–101

changes in Python 3]], 1357

defined]], 65

initialization]], 98–99

manipulating arrays]], 99–100

sequence of fixed-type data]], 98

supplements to standard library]], 1368

ASCII

encoding binary data with. See base64 module

restricting escape codes to]], 38

string constants]], 6

asinh()function]], math]], 288

astimezone()]], datetime

conversion]], 232

asynchat module, deprecated]], 1355

Asynchronous I/O

with protocol class abstractions]], 644–650

using coroutines and streams]], 650–655

Asynchronous system events. See signal module

asyncio module

asynchronous concurrency concepts]], 618

asynchronous I/O using coroutines/streams]], 650–655

asynchronous I/O using

protocol class abstractions]], 644–650

combining coroutines with threads/processes]], 670–673

composing coroutines with control structures]], 632–637

cooperative multitasking with coroutines]], 618–622

debugging with]], 673–675

executing tasks concurrently]], 628–632

interacting with DNS]], 658–660

Internet-related supplements to standard library]], 1370

loop supplements to standard library]], 1369

new in Python 3]], 1352

overview of]], 617

producing results asynchronously]], 625–628

receiving Unix signals]], 668–670

scheduling calls to regular functions]], 622–625

subprocesses]], 661–668

synchronization primitives. See Synchronization primitives, asyncio using SSL]], 656–658

asyncio_executor_thread.py]], 670–673

asyncore module, deprecated]], 1355

atanh() function, math]], 288

atexit module

canceling callbacks]], 994–995

changes in Python 3]], 1357

decorator syntax]], 994

getting plain text help]], 1024–1026

handling exceptions]], 997–998

program shutdown callbacks with]], 993

registering exit callbacks]], 993–994

when callbacks are not called]], 995–997

atexit property, weakref]], 124–125

atof() function, locale]], 1021

atoi() function, locale]], 1021

Attribute getters, operator]], 188–189

AttributeError, namedtuple]], 91

Attributes

configuring cmd through]], 946–947

IEEE 802

MAC Address, UUID 1]], 799

namedtuple special]], 92–94

objects with non-picklable]], 400

XML element property]], 459–461

XML parsed node]], 449–450

Authentication create argument group for]], 905

email, in smtplib]], 843–846

failure, in imaplib]], 865–866

Authorizer function, sqlite3]], 442–443

Auto-completion, command]], 942–944

Autocommit mode, sqlite3]], 434

Automatically generated help, argparse]], 897–901

Averages, statistics]], 290–291

await keyword, asyncio

asynchronous I/O using]], 651, 653–654

chaining coroutines]], 621

composing coroutines]], 632–637

coroutines pausing execution with]], 618

using Future with]], 626–627

B

backslashreplace, lossless error handling]], 374

Backup file, fileinput]], 992

Barrier, synchronizing threads]], 579–581

Base classes

implementing Web servers. See http.server module

overriding methods in cmd]], 944–946

Base16 encoded data]], 778–779

Base32 encoded data]], 778

base64 module

Base64 decoding]], 778

Base64 encoding]], 777

changes in Python 3]], 1357

encode binary data with ASCII using]], 776

other encodings]], 779–781

URL-safe variations]], 778–779

Base85 encoded data]], 778–780

BaseHTTPRequestHandler class, http.server]], 781–786

basename() function, parsing paths in os.path]], 297–298

BaseServer class, socketserver]], 742–743

basicConfig() function, log files]], 981–982

BCC (blind carbon-copy), smtplib]], 843

BeautifulSoup library]], 1370

Beta distribution, random]], 264

betavariate() function, Beta distribution]], 264

Bidirectional process communication, subprocess]], 543

Binary class, passing in XML-RPC]], 823–825

Binary data encoding with ASCII. See base64 module

passing in XML-RPC]], 823–825

sending, socket]], 721–723

structures. See struct module

Binary digests, hmac]], 529–530

Binary heap]], 103

bind() function choosing address for listening]], 711

socket echo server]], 704–705

bisect module]], 109–111

Blank lines, managing in doctest]], 1034–1035

Blind carbon-copy (BCC), smtplib]], 843

BOM (byte order marker), codecs]], 370–372

Boolean values in configparser]], 966–968

testing files in os.path]], 303–304

trace options]], 1077

braced patterns, advanced string templates]], 5–6

break command, breakpoints]], 1118–1120, 1124–1125

Breakpoints, pdb

changing execution flow]], 1129–1130

conditional breakpoints]], 1124–1125

ignoring]], 1125–1127

jumps forward or backward]], 1130–1134

managing]], 1120–1123

temporary breakpoints]], 1123–1124

triggering actions]], 1127–1128

using]], 1117–1120

watching data change]], 1128–1129

BrokenBarrierError, threads]], 580

BROWSER variable, webbrowser]], 797

Browser, webbrowser]], 796

BSD socket interface. See socket module bsddb module, removed from Python 3]], 1354

BufferedIncrementalDecoder]], 389

BufferedIncrementalEncoder]], 389

Buffers, struct]], 120–121

Building paths

os.path]], 300–301

pathlib]], 305–307

Building trees, xml.etree.ElementTree module]], 461–464

Built-in modules]], 1201–1202

Bulk loading, sqlite3]], 421–422

Byte code, compiling source files to]], 1155–1159

Byte order alternative arrays]], 101–102

codecs]], 370–372

memory management]], 1194

Byte

order marker (BOM), codecs]], 370–372

Bytes raw]], 391

understanding encodings through]], 366–368

Unicode]], 365–366

BytesIO buffer

file-like streams in pickle]], 398

message signatures]], 531–532

streams in gzip]], 490–491

wrapping byte streams for text data]], 392–393

byteswap() method, arrays]], 102

bz2 module

bzip2 compression via]], 491

changes in Python 3]], 1357

compressing network data]], 499–503

incremental compression and decompression]], 493–494

mixed-content streams]], 494–495

one-shot operations in memory]], 492–493

reading and writing Unicode data]], 498–499

reading compressed files]], 497–498

tarfile using compressed archives of]], 510–511

writing compressed files]], 495–497

BZ2Compressor object]], 493–494, 499

BZ2Decompressor object]], 493–495, 501

Bz2RequestHandler]], 500

bzip2 compression. See bz2 module

C

C modules, as built-in]], 1201–1202

Cache

functools]], 155–158

importer]], 1217–1218

weakref]], 127–130

Calculations, commonly used math]], 274–277

Calendar date values]], 222–225

calendar module

calculating dates]], 236–238

defined]], 211

formatting examples]], 233–236

locales]], 236

overview of]], 233

working with dates]], 236

call() function, subprocess]], 535

Callable objects, functools

acquiring function properties]], 145–147

acquiring function properties for decorators]], 149–151

partial objects working with any]], 147–148

wrapping with partial class]], 143–145

Callbacks

arbitrary context, contextlib]], 206–207

Future, concurrent.futures]], 680–681

program shutdown. See atexit module receiving signals]], 554–555

weakref]], 122–123

Callbacks, asyncio

concept of]], 618

Future invoking]], 627–628

scheduling for soon]], 622–623

scheduling for specific time]], 624–625

scheduling with delay]], 623–625

CalledProcessError exception, subprocess]], 537–538

call_later function, scheduling callbacks in asyncio]], 623–625

call_soon function, scheduling callbacks in asyncio]], 622–623, 624

Cancel events, sched]], 1001–1002

cancel() method, concurrent.futures]], 681–683

cancel_task() method, asyncio]], 629–631

capwords() function, string module]], 1–2

Case-insensitive matching, regular expression search]], 36–37

ceil() function, math]], 270

cgitb module

command-line interface]], 1152–1153

detailed tracebacks]], 1090–1092

examining local variables in tracebacks]], 1093–1096

exception properties]], 1096–1098

HTML output format]], 1098

logging tracebacks]], 1098–1101

overview of]], 1023, 1089

standard traceback dump]], 1089–1090

chain() function, itertools]], 164–167

chain.from_iterable(), itertools, 164–165

Chaining coroutines, asyncio, 620–621

ChainMap class, search multiple dictionaries, 75–79

Changes, preserving in sqlite3, 428–430

Channels, managing registered data, 737–738

Character map–based codecs, 383–384

Character ranges

filename pattern matching in glob, 322

regular expression character sets, 21–22

Character sets, regular expression, 20–23, 38–39

charmap_decode(), 389

charmap_encode(), 389

charset, regular expression, 20–23

check_output() function, subprocess, 535, 539–541

Checksums, computing, 481–482

chmod() method, file permissions, 317–318, 1231–1232

choice() function, random, 258

Circular references, pickle, 401–402

Class browser, pyclbr, 1160–1163

class syntax, creating enumerations, 66

Classes

abstract base. See abc module disassembling methods, 1300

helper base, 1289–1290

implementing Web servers with base. See http.server module inspecting, 1314–1316

inspecting method resolution order, 1323–1324

json encoder and decoder, 810–812

mail server base, 847–850

managing file system paths, 305

marking class and static methods as abstract, 1295–1296

operator combined with custom, 190–191

registering concrete, for use with abc, 1287–1288

retrieving source code for, 1318–1319

scanning for, 1161–1162

in socketserver, 742–743

subclassing from base, 1288–1289

working with hierarchies, 1322–1324

clear command, deleting breakpoints, 1123

clear() method, threading, 572

click (Command-Line Interface Construction Kit), 1370

Client

I/O multiplexing abstractions in selectors, 726–728

library, IMAP4. See imaplib module library, XML-RPC. See xmlrpc.client module sending binary data, 722

TCP/IP, 704–711

UDP, 711–713

UDS, 714–716

cliff (Command-Line Interface Formulation Framework), 1370

Clock time. See time module clock()function, 211, 214–216

Closing

open handles, contextlib, 198–199

partial stacks, contextlib, 208–209

TCP/IP server, socket, 705

Cmd (command prompt), 938–939

cmd module

alternative inputs, 948–949

auto-completion, 942–944

command arguments, 940–941

commands from sys.argv, 950

configuring through attributes, 946–947

defined, 887

line-oriented command processors with, 938

live help, 941–942

overriding base class methods, 944–946

processing commands, 938–939

running shell commands, 947–948

cmdloop() method, cmd, 945

Code coverage, 1070–1073, 1076–1077

Code, source. See Source code CodecInfo instance, custom encoding, 387

codecs module

byte order, 370–372

defining custom encoding, 383–389

encoding translation, 376–377

encodings, 366–368

error handling, 372–376

incremental encoding, 378–380

non-Unicode encodings, 377–378

as string encoding and decoding, 365

Unicode data and network communication, 380–383

Unicode primer, 365–366

working with files, 368–370

collections module

ChainMap, 75–79

changes in Python 3, 1357

collections.abc, 97–98

container data types, 65, 75

Counter, 79–82

defaultdict, 82–84

deque, 84–89

namedtuple, 89–94

OrderedDict, 94–97

collections.abc module, 97–98

Columns, in sqlite3

defining new types, 422–425

determining types, 426–427

restricting access, 442–443

Combining dates and times, datetime, 228–230

Command handler, cmd, 938–939

Command-Line Interface Construction Kit (click), 1370

Command-Line Interface Formulation Framework (cliff), 1370

Command-line programs arguments captured by interpreter, 1185–1186

building, 1370–1371

compiling files from, 1158–1159

CPython, 1180–1181

filter framework for text files, 987–992

http.server, 789–790

inspect, 1327–1328

json, 815

option parsing. See getopt module parsing, 889

parsing/validating arguments in. See argparse module running unittest tests, 1051–1052

starting pdb from, 1101–1102

timeit, 1152–1153

trace, 1069–1070

webbrowser, 797

Commands

auto-completion for, 942

breakpoints triggering actions, 1127

processing in cmd, 938–939

running external, 1239–1240

commands module, 1354, 1357

Comments parsing embedded, 954

in verbose regular expressions, 42

commit() method, transactions in sqlite3, 428–429

communicate() method asyncio, 667

subprocess, 546–548

Communication

bi-directional process, 543

capturing error output, 543–544

interprocess, 398–399

network. See socket module non-blocking, 723

one-way process, 542–543

Unicode and network, 380–383

compact flag, pprint(), 140–142

Compact vs. human-consumable output, json, 805–807

compare_digest() method, message signatures, 532–533

Comparison bodies of text, 59

of clocks, 211–213

enum, 67

file. See filecmp module functions, functools, 151–155

functions, math, 267–269

operators, 183, 228

sequences. See difflib module of values, datetime, 228

compileall module

compiling from command line, 1158–1159

compiling individual files, 1157–1158

compiling source files to byte-code, 1155–1157

compiling sys.path, 1157

overview of, 1024

Compiled expressions, 15

compiler module, removed from Python 3, 1354

Compilers, 1004, 1309–1311

complete() method, readline, 925–931

complete_prefix, auto-completion for commands, 942

CompletedProcess, subprocess, 536–538

Completion buffer, readline, 927–931

Compress class, zlib, 479–480

Compression

adding for new archive in zipfile, 515–516

bz2. See bz2 module GNU zlib. See zlib module levels, gzip, 488

levels, zlib, 479

read and write GNU zip files. See gzip module

Concrete paths, pathlib, 309

Concurrency asynchronous system events. See signal module

concurrency/asynchronous I/O management. See asyncio module

managing concurrent operations within process. See threading module

managing pools of concurrent tasks. See concurrent.futures module

managing processes like threads. See multiprocessing module

overview of, 535

spawning additional processes. See subprocess module

supplements to standard library, 1369

concurrent module, new in Python 3, 1352

concurrent.futures module

canceling tasks, 681–683

context manager, 683–684

exceptions in tasks, 683

future callbacks, 680–681

managing pools of concurrent tasks, 677

process pools, 685–686

scheduling individual tasks, 678–679

using map() with basic thread pool, 677–678

waiting for tasks in any order, 679–680

Condition objects synchronizing in multiprocessing, 604–605

synchronizing in threading, 578–581

configparser module

accessing configuration settings, 963–970

application construction supplements to standard library, 1371

changes in Python 3, 1358

combining values with interpolation, 975–979

configuration file format, 961

modifying settings, 970–972

option search path, 972–975

reading configuration files, 961–963

saving configuration files, 972

sources of arguments in argparse, 895–896

working with configuration files, 960–961

Configuration

customizing site, 1175–1176

customizing user, 1176–1177

files. See configparser module properties in shlex, 955–956

readline library, 923–924

saving settings in pdb, 1137–1139

site-wide, 1169

system-specific, 1178

variables, 1270–1272

Conflicting options, argparse, 902–904

Connections

create database, sqlite3, 413

echo client, socket, 705

to IMAP server, 864–865

managing multiple at same time with select(), 732–733

send email message, smtplib, 841

sharing in sqlite3, 441–442

TCP/IP easy client, 707–708

Constants

compiler optimizations, 1309–1311

quoting options in CSV files as, 469

string module, 6–7

Consuming deque, 85–87

Containers

abstract base classes for, 236

comparing in unittest, 1056–1061

data types. See collections module

Context managers

executors working as, 683–684

locks as, 577–578

utilities. See contextlib module ContextDecorator class, 194–195

context_diff() function, difflib, 61

contextlib module

changes in Python 3, 1358

closing open handles, 198–199

context manager API, 191–194

as context manager utilities, 202

context manager utilities, 191

context managers as function decorators, 194–195

defined, 143

dynamic context manager stacks, 202–209

from generator to context manager, 196–198

ignoring exceptions, 199–201

redirecting output streams, 201

Contexts, decimal

current context, 244–245

local context, 247–248

overview of, 244

per-instance context, 248

precision, 245

rounding, 245–247

threads, 248–249

continue command, breakpoint in pdb, 1118–1120

Conversion

functions, configparser, 968–969

inputs, itertools, 167–169

new column types, sqlite3, 424

convert_to_builtin_type(), json, 808, 812

Cookies. See http.cookies module Cooperative multitasking, coroutines in asyncio, 618–622

copy module

customizing copy behavior, 132–133

deep copies, 131–132

duplicate objects, 130

recursion in deep copy, 133–136

shallow copies, 130–131

Copying files

duplicate objects, memory management, 65

memory-map, 363–364

messages, imaplib, 883–884

metadata, 340–341

shallow copies, 130–131

in shutil, 337–342

copysign() function, math, 273

copytree() function, shutil, 342–343

Coroutines, asyncio

asynchronous I/O using streams and, 650–655

calling subprocesses in asyncio with, 664–666

chaining, 620–621

combining with threads/processes in asyncio, 670–673

concept of, 618

cooperative multitasking with, 618

creating tasks from, 631–632

Future acting like, 625–626

gathering results from, 635–636

generators instead of, 621–622

handling background operations as they finish, 636–637

returning values from, 619–620

starting, 619

supplements to standard library, 1369

waiting for multiple, 632–635

Cosine functions, math, 284, 288

count() function, new values in itertools, 169–172

–count option, code coverage report, 1070–1072

Counter module

accessing counts, 81–82

arithmetic, 82–83

as counting hashable objects, 79–82

defined, 79

initializing, 80

count_words() function, MapReduce in multiprocessing, 616

CPU time. See Processor clock time CPython, 1179–1181

crc32() function, computing checksums, 481–482

Create database dbm, 409–410

sqlite3, 412–415

create_connection() method, asynchronous I/O, 648

Cryptography hashing. See hashlib module message signing/verification. See hmac module

supplements to standard library, 1369

UUID 3 and 5, name-based values, 800–801

csv module

bulk loading in sqlite3, 421–422

changes in Python 3, 1358

comma-separated values, 466

dialects, 469–474

event-style processing in XML, 452–453

handling XML parse events, 453–455

quoting, 468–469

reading, 466–467

setting XML element properties, 459–461

using field names, 474–476

writing, 467–468

ctime() function logging or printing time, 213–214

processor clock time, 215–216

Cultural localization API. See locale module curio package, supplements to standard library, 1369

curl, http.server, 783, 785

Currency, updating when changing locale, 1018–1019

Current context, decimal, 244–245

Current date, datetime_date, 222–223

Current locale, probing, 1013–1018

Current thread, determining, 562

Current working directory, 1238–1239

currentframe(), inspecting at top of stack, 1324

Cursor, sqlite3, 415, 417

Customizing

aggregation, sqlite3, 440–441

argument actions, argparse, 913–915

classes, operator, 190–191

copy behavior, copy, 132–133

encoding, codecs, 383–389

help, argparse, 898–901

mail server, stmpd, 847–851

TreeBuilder, 453–455

types, json, 807–810

CustomSMTPServer, 847–850

cycle() function, values in itertools, 170

D

Daemon processes, multiprocessing, 589–593

Daemon threads, 564–567

Data

communication, in Unicode, 380–383

finding non-code, during import, 1348–1350

sending to subprocess in asyncio, 666–668

Data

compression and archiving bzip2 compression. See bz2 module

GNU zlib compression. See zlib module

overview of, 477

read and write GNU zip files. See gzip module

tar archive access. See tarfile module

ZIP archive access. See zipfile module

data definition language (DDL) statements, database in sqlite3, 413

Data files, in packages, 1341–1344

Data persistence and exchange comma-separated values. See csv module

embedded relational database. See sqlite3 module

object serialization. See pickle module

overview of, 395–396

persistent storage of objects. See shelve module

supplements to standard library, 1368–1369

Unix key-value databases. See dbm module

XML manipulation API. See xml.etree.ElementTree module Data sets, reducing in functools, 158–160

Data structures

binary data structures. See struct module

bisect module, 109–111

container data types. See collections module

duplicate objects. See copy module

enumeration types. See enum module

heap-sort algorithm. See heapq module

impermanent references to objects. See weakref module

overview of, 65–66

pretty-print. See pprint module

sequence of fixed-type data, see array module

thread-safe FIFO implementation. See queue module

Data types customizing, in json, 807–810

encoding/decoding in json, 804–805

XML-RPC server, 819–822

Databases

embedded relational. See sqlite3 module

Unix key-value dbm, 408–411

Datagram sockets, 694

Dates

date/time value manipulation. See datetime module formatting in locale, 1022

overview, 211

supplements to standard library, 1368

as supported data type in XML-RPC, 822

working with. See calendar module

datetime module changes in Python 3, 1358

combining dates and times, 228–230

comparing values, 228

date arithmetic, 226–227

as date-time value manipulation, 221

dates as supported data type in XML-RPC, 822

defined, 211

formatting and parsing, 230–231

new column types in sqlite3, 423

supplements to standard library, 1368

time, 221–225

time zones, 231–232

timedeltas, 225–226

dateutil package, date/time supplements, 1368

DbfilenameShelf class, 405–406, 408

dbm module creating new database, 409–410

creating new shelf, 406

database types, 408–409

error cases, 411

opening existing database, 410

Unix key-value databases via, 408

dbm.dumb module, 409–410

dbm.gnu module, 409–410

dbm.ndbm module, 409–410

DDL (data definition language) statements, database in sqlite3, 413

Deadlocks, debugging threads, 1198–1200

Debugging

built-in, asnycio, 673–675

data structures, pprint, 65

detailed tracebacks. See cgitb module

interactive. See pdb module

memory leaks, 1265–1270

multiprocessing, 594–596

server, stmpd, 850

threads, 1198–1200

tracebacks for. See traceback module

using disassembly for, 1302–1303

DebuggingServer, stmpd, 850

decimal module

arithmetic, 242–243

changes in Python 3, 1358

context, 244–249

creating fraction instances, 252

Decimals, 239–241

formatting, 241–242

overview of, 239

special values, 243–244

Decoding

binary data with base64, 778

creating map for, codecs, 384–386

data in strings, pickle, 396–397

error handling, codecs, 372–376

incremental classes, 389

simple data types, json, 804–805

string. See codecs module

understanding, codecs, 367–368

working with files, codecs, 368–370

Decompression, zlib

incremental, 479–480

mixed content streams, 480–481

of network data, 483–486

working with data in memory, 477–479

Decorator syntax, atexit, 994

Decorators

algorithms supplementing standard library, 1367–1368

context managers, as function, 194–195

Decorators, functools acquiring function properties, 145–147

acquiring function properties for decorators, 149–151

methods and functions, 148–149

other callables, 147–148

overview of, 143

partial objects, 144–145

Dedented text, 8–10

Deep copies duplicate objects, 131–132

making recursive copies, 65, 133–136

deep module, printing messages, 1340

default() method, cmd, 939, 945

DEFAULT_COMPRESSION level, zlib, 479

defaultdict, 65, 82–84

Defaults, interpolation using, 976

DefaultSelector, 724

DEFERRED isolation level, sqlite3, 432–433

DefragResult value, urllib.parse, 756

defusedxml package, 1369

degrees() function, math angles, 283

Delay

running events with, 999

sched module, 998–999

Deleting from file system, pathlib, 318–319

Deleting messages imaplib, 884–885

from Maildir mailbox, 858–860

from mbox mailbox, 854

delimiter class attribute, advanced string templates, 4–5

delitem() function, sequence operators, 187

delocalize() method, locale, 1020

Deltas, human-readable, 58

depth argument, pprint, 139–140

deque module

constraining queue size, 88–89

consuming, 85–87

defined, 65

as double-ended queue, 84–85

populating, 85

rotating, 87–88

description attribute, cursor in sqlite3, 417

dest argument, 888

detect_types flag, sqlite3, 422, 427

Developer tools automated tests. See unittest module

class browser, 1160–1163

collecting and analyzing statistics. See profile module

compiling source files to byte-code, 1155–1159

creating virtual environments, 1163–1167

detailed tracebacks. See cgitb module

following program flow. See trace module

interactive debugging. See pdb module

online help, 1023–1026

stack traces and exceptions. See traceback module

supplements to standard library, 1371–1372

testing through documentation. See doctest module

timing execution of code. See timeit module

validating indentation, 1153–1155

working with Python package installer, 1167–1169

Development, managing package versions in, 1336–1338

DEVNULL, suppressing output in subprocesses, 541–542

Dialects, csv module

automatically detecting, 472–474

creating, 469–470

overview of, 469

parameters, 470–472

dict, OrderedDict vs., 93–94

Dictionaries

encoding, json, 807

initialization, Counter, 80

non-integer member values using, enum, 75

remember order that keys are added to, 94–97

retrieving values, Counter, 81

search multiple, ChainMap, 75–79

using field names, cvs, 474–476

values stored in, timeit, 1149–1152

DictReader class, cvs, 474–476

DictWriter class, cvs, 474–476

Differ class, difflib, 58–60

Difference-based reporting, managing blank lines, 1036–1037

Differences in programs, using, 356

difflib module

compare bodies of text, 58–61

comparing arbitrary types, 62–64

comparing sequences with, 58

defined, 1

junk data, 61–62

overview of, 58

Digests

applications of message signatures, 530–533

hashlib MD5

hash example, 524–525

hashlib SHA1

example, 525

hmac alternative types, 528–529

hmac binary, 529–530

dircache module, removed from Python 3, 1354

dircmp class, comparing large directory trees, 355–356

Directories

accessing contents, pathlib, 309–312

comparing, filecmp, 354, 355–356

creating/deleting, 1233

creating temporary, filecmp, 335

examining file system contents, 1228–1230

installing message catalogs into, 1005–1006

managing process working directory, 1238–1239

managing user, 1171–1172

manipulating, pathlib, 312–313

moving, shutil, 345

removing empty, pathlib, 318

removing, shutil, 344–345

Directory trees

recursive comparison of large, 355–356

working in shutil with, 342–345

dirname() function, parsing paths in os.path, 297–298

dis(), basic dissassembly, 1297

dis module

analyzing compiler optimizations, 1309–1311

analyzing loop performance, 1303–1309

applying to class methods, 1300

applying to functions, 1297–1299

applying to source code, 1301–1302

basic dissassembly, 1297

overview of, 1279, 1296–1297

using for debugging, 1302–1303

Disabling interpolation, configparser, 979

Disassembler/disassembling. See dis module

disk_usage() function, shutil, 350–351

display command, breakpoints for watching data change, 1128–1129

Display hook, interpreter, 1183–1184

distutils module, supplement to standard library, 1371

Django package, 1370

DNS (domain name services) asyncio, 658–660

name lookups using UDP, 694

network hostname lookup, socket, 695

DocFileSuite class, 1047–1048

docopt package, 1371

docstring, retrieving for objects, 1316–1317

doctest module

execution context, 1048–1051

for external documentation, 1042–1043

integration with unittests, 1047–1048

overview of, 1023, 1026–1028

running by file, 1046–1047

running by module

(testmod()), 1044–1046

test locations, 1039–1042

for tracebacks, 1032–1033

for unpredictable output, 1028–1032

for whitespace management, 1034–1039

DocTestSuite class, 1047–1048

Documentation

external, using doctest, 1042–1043

retrieving docstring for an object, 1316–1317

Domain, creating message catalogs, 1005–1006

Domain name services. See DNS (domain name services) DOTALL flag, search for multiline text, 37–38

Dotted API names, xmlrpc.server, 830–831

Dotted notation, accessing fields of namedtuple, 90

Double-ended queue. See deque module

download_enclosures() function, threaded podcast client, 115

dropwhile() function, filtering iterable contents, 172–173

dumps() function

encoding/decoding data in strings, pickle, 396–397

human-readable output, json, 805–807

working with streams/files, json, 813

dup list, shallow copies, 131–132

Duplicate

lists in sorted order, 110–111

objects. See copy module

dynamic context manager stacks

arbitrary context callbacks, 206–207

overview of, 202

partial stacks, 207–209

stacking context managers, 202–206

E

EasyDialogs module, removed from Python 3, 1354

Echo client

asynchronous I/O,asyncio, 646–649, 652–654

enabling SSL on sockets, asyncio, 652–654

example, socketserver, 744–749

I/O multiplexing abstractions, selectors, 726–728

TCP/IP, socket, 705–711

UDP, socket, 712–713

Echo server asynchronous I/O,asyncio, 644–646, 650–652

enabling SSL on sockets, asyncio, 650–652

example, socketserver, 743–749

I/O multiplexing abstractions, selectors, 724–728

TCP/IP, socket, 704–711

UDP, socket, 712–713

using select()with, 729–730

EchoRequestHandler, socketserver, 743–748

ehlo(), authentication/encryption in smtplib, 843–845

Element nodes, building XML documents from, 457–458

ElementTree library, see xml.etree.ElementTree module

Email

IMAP4 client library. See imaplib module

manipulate email archives. See mailbox module

overview of, 841

sample mail servers. See smtpd module

SMTP. See smtplib module

supplements to standard library, 1370

Embedded comments, parsing in shlex, 954

Embedded relational database. See sqlite3 module

emptyline() method, overriding base class methods in cmd, 945

Encoded value, http.cookies, 793–794

Encoder/decoder classes, json, 810

Encoding

arguments, urllib.request, 763–764

binary data with ASCII. See base64 module

connecting to XML-RPC server, 818

data in strings, pickle, 396–397

defining custom, codecs, 383–389

dictionaries, json, 807

error handling, codecs, 372–373

errors, in codec, 373–374

files, codecs, 368–370

files for upload, urllib.request, 767–768

incremental, bz2, 378–380

maps, 384–386

non-Unicode, 377–378

query arguments, urllib.parse, 759–760

simple data types, json, 804–805

strings. See codecs module

translation, codecs, 376–377

understanding, 366–368

values in cookie header, 793–794

Western languages, 366

Encryption email, smtplib, 843–846

enabling SSL on sockets in asyncio, 656

Endianness, 118–120, 370–372

Ending argument processing, getopt, 922

ensurepip module

new in Python 3, 1352

overview of, 1024

working with Python package installer, 1167–1169

enter_context(), stacking context managers, 202–206

enter() function

canceling events in sched, 1001–1002

running events with delay, 999

enterabs() function, canceling events in sched, 1001–1002

enum module

creating enumerations, 66

creating enumerations programmatically, 71–72

defined, 65

enumeration type, 66

iteration, 67–69

new in Python 3, 1352

non-integer member values, 72–75

unique enumeration values, 69–71

Enumeration

of all threads, 567–568

enum. See enum module

Environment variables, process environment, 1237–1238

Equality-checking

almost equal in unittests, 1055–1056

comparing in unittests, 1056–1061

unittest for, 1054–1055

Equality, OrderedDict, 94

erf(-x) error function, math, 289

erfc() error function, math, 289–290

error, conflict_handler, argparse, 903

Error handling

capturing output, subprocess, 543–545

in codecs, 372–376

in http.server, 787–788

read text in linecache, 328–329

running external command in subprocess, 537–538

in shlex, 957–958

Errors. See also Exceptions in dbm, 411

operating system codes for, 1245–1246

recoverable. See warnings module

Escape

codes, 23–26, 39

meta-characters in glob, 322–323

escape() function, meta-characters, 322–323

Event loop, asyncio

chaining coroutines, 620–621

concept of, 618

scheduling calls to regular functions, 622–625

starting coroutine, 619

Events

asynchronous system. See signal module

timed event scheduler. See sched module

XML parse, 451–455

Exception handling

program shutdown callbacks, atexit, 997–998

in XML-RPC, 825–826

Exceptional sockets, 732

Exceptional values, testing in math, 265–267

Exceptions applied to low-level exception APIs, 1082–1086

applied to low-level stack APIs, 1086–1089

applying dis to, 1302–1303

argument types, argparse, 911

authentication failure, imaplib, 865–866

causing, argparse, 902

current, sys, 1195–1196

detailed tracebacks, cgitb, 1090–1092

exception properties, cgitb, 1096–1098

handling, sys, 1194

handling module

import errors, sys, 1214–1215

HTML output format, cgitb, 1098

ignoring, 199–201

local variables in tracebacks, cgitb, 1093–1096

logging tracebacks, cgitb, 1098–1101

monitoring, sys, 1225–1227

previous interactive, sys, 1196–1197

in tasks, concurrent.futures, 683

testing for, unittest, 1061–1062

TracebackException class, 1081–1082

unhandled, 1194–1195

exceptions module, removed from Python 3, 1354

Exclusive mode isolation level, sqlite3, 433–434

exec()method, creating processes, 1240

execute() method

autocommit mode, 434

positional parameters in sqlite3, 419

sqlite3, 415

using variables with queries in sqlite3, 419

executemany() method, bulk loading in sqlite3, 421–422

executescript() method, create database in sqlite3, 414

Executors

managing pools of workers, 677

working as context managers, 683–684

Exit callbacks, registering, 994–997

exit(), sys, 1187

exitcode, multiprocessing, 594–596

ExitStack arbitrary context callbacks, 206–207

dynamic context manager stacks, 202

partial stacks, 208–209

stacking context managers, 202–206

exp() function, math, 282

expanduser() function, os.path, 300–301

expandvars() function, os.path, 301

expectedFailure(), unittest, 1068–1069

expm1() function, math, 282

Exponent, math functions, 278–282

Exponential distribution, random, 263–264

Exponentiation operator (**), 278

Exporting database contents, sqlite3, 435

expovariate() function, exponential distribution, 263

EXPUNGE command, imaplib, 884–885

ExtendedInterpolation, configparser, 978–979

External command, subprocess capturing output, 538–541

error handling, 537–538

overview of, 536–537

suppressing output, 541–542

Extremes, heap data, 107–108

F fabs() function, math, 272–274

factorial() function, math, 274–275

Fault objects, XML-RPC, 825–827

FauxNFSHandler class, urrlib.request, 771–772

feedparser module, threaded podcast client, 116

fetch() method, messages in imaplib, 874–881

fetchall() method, sqlite3, 415

fetchmany() method, sqlite3, 416

fetchone() method, sqlite3, 416

fetch_podcasts, threaded podcast client, 114–115

Fibonacci sequence, profile applied to, 1140–1142

Field names, 91–92, 474–476

FIFO (first-in, first-out) queue. See queue module

File arguments, 912–913

File extensions, 1330–1331

File system

comparing files. See filecmp module

filename pattern matching. See glob module

high level file operations. See shutil module

memory-map files. See mmap module

overview of, 295–296

parsing paths. See os.path module

as paths. See pathlib module

read text files efficiently. See linecache module

string encoding/decoding. See codecs module

temporary file system objects. See tempfile module

text, binary, and raw stream i/o tools. See io module

Unix-style glob pattern matching. See fnmatch module

File system space, shutil, 350–351

File systems

creating/deleting directories, 1233

examination of file system contents, 1228–1230

managing file system permissions, 1230–1233

rename/replace files, 1234–1235

symbolic links, 1234

File times, os.path, 302–303

File types, pathlib, 313–315

filecmp module

comparing directories, 355–356

comparing files, 353–355

example data, 351–353

using differences in program, 357–360

fileinput module

command-line filter framework with, 986

converting M3U files to RSS, 987–989

in-place filtering, 990–992

progress metadata, 989–990

Filenames

changing archive, tarfile, 508–509

changing archive, zipfile, 516

pattern matching, 319–323

platform-independent manipulation of. See os.path module

fileno() method, 334, 729–734

FileNotFoundError exception, pathlib, 318

Files

arrays and, 100–101

configuring logs to, 981

encoding/decoding strings, 368–370

working with json, 813

FileType, file arguments, 912–913

fill() function

combining dedent()with, 9–10

paragraphs, 8

Filters

command-line framework for text files, 987–992

fnmatch glob pattern matching, 325

in itertools, 172–174

in warnings, 1280–1283

finalize() method, custom aggregation in sqlite3, 440–441

finalize objects, weakref, 123–126

Finding

files, shutil, 345–346

message catalogs at runtime, 1007–1008

multiple matches in text, 16

nodes in XML document, 447–448

paragraphs, 55

find_module(), inside ZIP archive, 1345

First-in, first-out (FIFO) queue. See queue module

Fixtures

package, 1371

unittest applied to, 1062–1065

in unittest structure, 1051

Flags

mailbox message, 862–864

options as, configparser, 969–970

registered data channels, 737–741

regular expressions. See re module, search options

Float representation, wall clock time, 213

Floating-point values, math, 966–968

Floating-point values, math alternative representations, 271–272

calculate absolute value, 272–274

commonly used calculations, 274–277

comparisons involving, 267–269

converting to integers, 270–271

division, 185–186

fixed- and floating-point math. See decimal module

fraction module, 251–254

random() generating, 257

testing for exceptional, 265–267

Floating-point values, memory management, 1192–1193

floor() function, converting floating-point values, 270

floordiv() operator, 185–186

flush() method

incremental compression, zlib, 480

messages, mbox mailbox, 854

fmod() function, math, 276–277

fnmatch module

filtering, 325

simple matching, 323–324

translating patterns, 325–326

as Unix-style glob pattern matching, 325

Folders, Maildir mailbox, 860–862

Forking

adding in http.server, 786–787

adding to server in socketserver, 749–751

creating processes in os.fork, 1240–1242

ForkingMixIn http.server, 787

socket.server, 750–751

formatter module, deprecated, 1355

Formatting calendar, 233–236

configuration files, 975–979

datetime, 230–231

decimal, 241–242

numbers, locale, 1019–1021

pprint, 137–138

stack, traceback, 1087–1088

string, 6

time, clock time, 219–220

traceback exception, 1084–1085

warnings, 1285

fractions module

approximating values, 253

arithmetic, 252–253

changes in Python 3, 1358

creating fraction instances, 250–252

as rational numbers, 250

Frames, inspecting stacks and, 1324–1327

frexp() function, math, 271–272

frombytes() method, arrays and files, 101

fromfile() method, arrays and files, 101

fromfile_prefix_chars, arparse, 896–897

from_float() method, decimal, 239

fromordinal() function, datetime, 224, 229

fromtimestamp() function, datetime, 224, 229

fsum() function, math, 274

fullmatch() function, search constraints in re, 29

Fully qualified domain name, socket, 697

Function

decorators, 194–195, 197–198

Functions asyncio event loop scheduling calls to, 622–625

disassembling, 1297–1299

examining stack variables in pdb, 1107–1111

mathematical. See math module

printing, 1146–1148

Python, sqlite3, 436–438

report on relationships between, trace, 1073–1074

scanning for, 1162–1163

signatures for, 1319–1322

string module, 1–2

Struct class vs., 117

tools for manipulating. See functools module

tracing, 1221–1224

functools module

caching, 155–158

comparison, 151–155

creating decorators, 1367–1368

decorators. See decorators, functools defined, 143

generic functions, 161–163

reducing data set, 158–160

as tools for manipulating functions, 143

Future, asyncio

callbacks, 627–628

concept of, 618

producing results asynchronously, 625

running client in asynchronous I/O, 647–648

waiting for a, 625–627

Future, concurrent.futures callbacks, 680–681

canceling tasks, 681–682

exceptions in tasks, 683

managing results by workers, 677

G

Gamma distribution, random, 264

gamma() function, math, 275

Garbage collection. See also gc module

caching objects, 127–128

memory management, sys, 1187–1188

Gauss error function, statistics, 289

gc module

caching objects, weakref, 127–130

changes in Python 3, 1358

collection thresholds and generations, 1261–1265

debugging memory leaks, 1265–1270

finalizing objects, weakref, 125–126

finding references to uncollectable objects, 1259–1261

forcing garbage collection, 1258–1259

overview of, 1169, 1254

tracing references, 1255–1257

gcd() function, math, 277

Generator functions

converting into context manager, 196–198

instead of coroutines in asyncio, 621

Generator sequences, 188

Generic functions, functools, 161–163

get() method

basic FIFO queue, 112

basic LIFO queue, 112

calling interpolation by default, configparser, 975–976

options as flags, configparser, 969–970

testing if section exists, configparser, 965

using specific browser in webbrowser, 796

value types, configparser, 967

getaddrinfo()

DNS, asyncio, 658–659

server addresses, socket, 700–702

TCP/IP client connections, socket, 708

getmember() method, tarfile, 505

getmembers() method inspecting classes, 1314–1316

inspecting instances, 1316

inspecting modules, 1312–1313

load metadata from archive, tarfile, 505

getopt module

abbreviating long-form options, 920

command-line option parsing with, 916

complete example, 918–920

ending argument processing, 922

function arguments, 916–917

GNU-style option parsing, 920–922

interface-related supplements to standard library, 1370

long-form options, 917–918

replace by argparse for newer applications, 922

short-form options, 917

getpass module

defined, 887

example, 935–937

secure password prompt with, 935

use without terminal, 937–938

Getters, operator

module/attribute/item, 188–190

gettext module

application vs. module

localization, 1012

changes in Python 3, 1359

creating message catalogs from source code, 1004–1007

finding message catalogs at runtime, 1007–1008

message catalogs with, 1003

plural values, 1008–1010

translation workflow overview, 1003–1004

geturl() method, 756–758, 770–773

GIL (global interpreter lock), threads, 1197

glob() method, 310–311

glob module

changes in Python 3, 1359

character ranges, 322

escaping meta-characters, 322–323

example data, 320

filename pattern matching, 319–320

fnmatch Unix-style, 323–326

single-character wildcard, 321–322

wildcards, 320–321

global interpreter lock (GIL), controlling/debugging threads, 1197

gmtime() function, 217

GNU

option parsing, 920–922

read and write zip files. See gzip module

readline library. See readline module

zlib compression. See zlib module

Graph class, 134–136

Greediness, regular expressions, 18–19, 20–23

group() method, 32, 316

groupby() function, itertools, 175–176

groupdict() method, regular expression groups, 33

grouping character, formatting numbers in locale, 1018–1019

Groups

argument, argparse, 904–905

data, itertools, 175–176

dissecting matches in regular expressions, 30–36

mutually exclusive options, argparse, 906

splitting with patterns, 56–57

traversing parsed tree, 447

groups() method, regular

expressions, 31, 33–35

gzip module

read and write GNU zip files, 486

reading compressed data, 489–490

tarfile using compressed archives of, 510–511

working with streams, 490–491

writing compressed files, 486–488

H

handle() method, socketserver, 743

Handler objects, 980, 984–985

handle_request()method, socketserver, 743

Handles, contextlib closing open, 198–199

Hanging indents, 12

Hardware, obtaining operating information, 1248–1250

hasattr(), interface checking alternatives, 1287

Hashable keys, functools caching, 157–158

Hashable objects, counting, 79–83

Hashed values, doctests for unpredictable, 1030–1032

Hashing, cryptographic. See hashlib module

hashlib module

creating hash by name, 525–526

SHA1 algorithm in hmac, 529

cryptographic hashing with, 523

hash algorithms, 523–524

incremental updates, 526–527

MD5

hash example, 524–525

sample data, 524

SHA1 digest example, 525

has_option() method, configparser, 965–966, 969–970

has_section() method, configparser, 965

Headers

adding outgoing, urllib.request, 765–766

encoding values, cookie, 793–794

receiving and parsing, cookie, 794–795

send email message, smtplib, 841–843

setting, http.server, 788–789

Heap. See heapq module

Heap sort algorithm. See heapq module

heapify() method, 105, 106

heappop() method, 105–106

heappush() method, 104–105

heapq module

accessing contents of heap, 104–106

creating heap, 103–104

data extremes from heaps, 106–108

defined, 65

example data, 103

as heap sort algorithm, 103

heapreplace() method, 106–107

Help in argparse, 897–901

HTML, 1025–1026

live, 941–942

online, 1024–1026

output, argparse, 897–901

plain text, in atexit, 1024–1026

help command, Cmd class, 938–939

Helper base classes, 1289–1290

hexdigest() method MD5 hash, 524–525

using Base64 version of binary digest vs., 529–530

High-level file operations. See shutil module

HistoryCompleter, 933

HMAC authentication, 530–533

hmac module

alternative digest types, 528–529

applications of message signatures, 530–533

binary digests, 529–530

cryptographic message signing/verification, 528

signing messages, 528

Hooks, readline, 934–935

Hostname, looking up network, 694–697

Hosts, in sqlite3, 419

hosts() method, ipaddress, 689–691

HTML

formatting HTMLCalendar, 234

getting help, 1025–1026

html module, 1370

htmlib module, removed from Python, 1354

output format, 1098

HTTP cookies. See http.cookies module

HTTP GET operation http.server, 781–783

urllib.request, 761–763

HTTP POST operation http.server, 784–786

posting form data, urllib.request, 766–767

urllib.request, 764–765

http.cookies module

alternative output formats, 795–796

changes in Python 3, 1359

creating and setting cookies, 790–791

encoded values, 793–794

HTTP cookies, 790

morsels, 791–793

receiving and parsing cookie headers, 794–795

http.server module

base classes implementing Web servers with, 781

command-line use, 789–790

handling errors, 787–788

HTTP GET, 781–783

HTTP POST, 784–786

setting headers, 788–789

threading and forking, 786–787

http.server package, 1370

Human-consumable vs. compact output, json, 805–807

Hyperbolic functions, math, 288

hypot() function, math

trigonometry, 285–287

I

I/O operations

asynchronous, in asyncio, 644–650

encoding and decoding strings in codecs, 368–370

multiplexing abstractions. See selectors module

text, binary and raw stream tools for, 390–393

waiting efficiently for with select. See select module

ID numbers, managing breakpoints, 1120–1121

Identation validator, tabnanny, 1153–1154

idpattern class attribute, advanced string templates, 4–5

IDs, detect/change process owner, 1235–1237

IEEE 802

MAC Address, UUID 1, 798–800

ignore command

decoding errors, codecs, 375

encoding errors, codecs, 374

ignoring breakpoints, pdb, 1125–1127

Ignore signals, 557–558

IGNORECASE flag, regular expressions, 36–37, 49–51

ignore_patterns() function, directory trees in shutil, 342–343

IMAP4 client class, using clear text sockets, 864

IMAP4

client library. See imaplib module

IMAP4_SSL client class, 864

IMAP4_stream client class, 864

imapclient package, 1370

imaplib module

changes in Python 3, 1359

connecting to server, 864–866

deleting messages, 884–885

email supplements to standard library, 1370

example configuration, 866

fetching messages, 874–880

IMAP4

client library with, 864

listing mailboxes, 866–869

mailbox status, 869–871

moving and copying messages, 883–884

search criteria, 872–874

searching for messages, 872

selecting mailbox, 871

uploading messages, 881–883

variations, 864

whole messages, 880–881

Immediate mode isolation level, sqlite3, 433

Immutable, tuple and namedtuple as, 90–91

imp module, deprecated, 1356

Import hooks, custom importers, 1204–1205

Import path

backward compatibility and, 1177–1178

modules, 1202–1203

packages, 1334–1336

scanning for path configuration files, 1172–1175

site, 1169–1170

import statement, 1329

Importable target functions, multiprocessing, 587–588

Importers. See also zipimport module, 1344

Importing modules. See also importlib module

custom importers, 1203–1205

custom package, 1211–1213

handling import errors, 1214–1215

importer cache, 1217–1218

mapping to module

objects, 1200–1201

meta-path, 1218–1221

retrieving import package data, 1215–1217

from shelf, 1205–1211

importlib module

accessing loaders, 1332–1334

example, 1329–1330

importing modules, 1331–1332

overview of, 1329

Python supported module styles, 1330–1331

import_module(), 1331–1332

In-memory approach

bz2 compression, 492–493

io streams, 390–391

sqlite3 databases, 434–435

zlib compression, 477–479

in operator, ipaddress, 691

In-place filtering, fileinput, 990–992

In-place operators, functional interface, 187–188

INADDR_ANY address, choosing for listening, 710–711

Incremental

compression/decompression bz2, 493

zlib, 479–480

Incremental encoding, codecs, 378–380

Incremental updates, hash calculators, 526–527

IncrementalDecoder, 378–380, 389

IncrementalEncoder, 378–380, 389

Indents, text paragraph

block, 10–11

combining dedent() and fill(), 9–10

hanging, 12

removing existing, 8–9

inf special value, math, 265–267, 269

infolist() method, zipfile, 512–514

Initialization array, 98–99

Counter, 80

initializer argument, functools, 159–160

input() function, converting M3U files to RSS, 987–989

Input history, tracked in readline, 931–934

Input/output streams (stdin/stdout), sys, 1186–1187

input_loop() function, readline, 925

Inputs alternative, in cmd, 948–949

combining in itertools, 176–182

insert statements, sqlite3, 415, 421

insert_text(), readline, 934

insort() method, 109–111

inspect module

changes in Python 3, 1359

command-line interface for, 1327–1328

example, 1311–1312

inspecting classes, 1314–1316

inspecting instances, 1316

inspecting method resolution order, 1323–1324

inspecting modules, 1312–1313

inspecting stack and frames, 1324–1327

overview of, 1279, 1311

retrieving docstring for an object, 1316–1317

retrieving source code for class or method, 1318–1319

returning specifcation of method or function arguments, 1319–1322

working with class hierarchies, 1322–1324

install() function, application localization, 1011

Installation paths, managing, 1272–1276

Instances, creating fraction, 250–252

Instances, inspecting, 1316

Integer division, floordiv() operator, 185–186

Integers

converting floating-point values to, math, 270–271

creating fraction, 253

parsing command line arguments with, 889–891

value types, configparser, 966–967

values, memory management, 1193

IntEnum class, 68–69

Interactive prompts, Interpreter, 1182–1183

Interfaces

network ipaddress, 692–693

pdb, 1104–1105

trace programming, 1074–1075

Internationalization and localization

cultural localization API. See locale module

message catalogs. See gettext module

overview of, 1003

Internet

addresses. See ipaddress module

base classes implementing Web servers. See http.server module

display Web pages via webbrowser, 796–797

encode binary data with ASCII. See base64 module

HTTP cookies. See http.cookies module

JavaScript Object Notation. See json module

network resource access. See urllib.request module

overview of, 753

spider access control. See urllib.robotparser split URLs into components. See urllib.parse module

supplements to standard library, 1369–1370

Universally Unique Identifiers. See uuid module

XML-RPC client library. See xmlrpc.client module

XML-RPC server. See xmlrpc.server module

Interpolation

configparser, 975–979

string.Template, 2–4

Interpreter

build-time version information, 1178–1179

command-line arguments captured by, 1185–1186

display hook, 1183–1184

implementation, 1179–1180

install location, 1184–1185

interactive prompts, 1182–1183

obtaining operating information, 1246–1247

Unicode defaults, 1181–1182

Introspection API, xmlrpc.server, 837–839

Inverse math functions, 287, 288

Invertcaps

character map-based codecs, 383–384

define custom encoding, 384–389

io module

in-memory streams, 390–392

text, binary and raw stream I/O tools, 390

wrapping byte streams for text data, 392–393

IOError

copying files in shutil, 337

testing tar files, 504

ipaddress module

addresses, 687–688

interfaces, 692–693

networks, 688–692

new in Python 3, 1352

working with IPv4

and IPv6

addresses, 687

IPv4

and IPv6

addresses. See ipaddress module; socket module

isclose() function, comparisons in math, 267–269

isfinite() function, math, 267

islice() function, itertools, 166

isnan, math, 266–267

ISO-8601

format, datetime objects, 230–231

Isolation levels, sqlite3

autocommit mode, 434

DEFERRED, 432–433

exclusive mode, 433–434

immediate mode, 433

overview of, 431–432

is_() function, 183

is_not() function, 183

is_tarfile() function, 503–504

is_zipfile() function, 512

Item getters, operator, 188–190

items() method, 859, 964

iter() function, traversing parsed tree, 446–447

iterall() function, regular expression search, 29

Iteration enum, 67

over networks, 689–691

Iterators functions for. See itertools module

using getters with, 188

itertools module

changes in Python 3, 1359

combining inputs, 176–182

converting inputs, 167–169

defined, 143

filtering, 172–175

grouping data, 175–176

merging and splitting iterators, 164–167

overview of, 163–164

producing new values, 169–172

J

JavaScript Object Notation. See json module

join() method

building paths in os.path, 300

daemon threads, 565–567

join process after terminating it, 594

normalizing paths in os.path, 301

threaded podcast client, 116

waiting for processes, multiprocessing, 591–593

joinpath()method, pathlib, 306

json module

changes in Python 3, 1359

encoder and decoder classes, 810–812

encoding/decoding simple data types, 804–805

encoding dictionaries, 807

human-consumable vs. compact output, 805–807

JavaScript Object Notation with, 803–804

JSON at command-line, 815

mixed data streams, 813–814

working with custom types, 807–810

working with streams and files, 813

JSON, YAML and, 1371

JSONDecoder class, 812–814

JSONEncoder class, 810–811

json.tool module, 815

js_output() method, http.cookie, 795–796

jump command, breakpoints, 1130–1134

Junk data, difflib, 61–64

K

Key-value databases, Unix, 408

KeyError

Counter, 81

tarfile, 505

Keys, Morsel, 793

Kill command, sending signals, 554

L

Language, creating message catalogs, 1005–1006

Language tools

abstract base classes. See abc module

disassembler. See dis module

inspecting live objects. See inspect module

non-fatal alerts/recoverable errors. See warnings module

overview of, 1279

ldexp() function, math, 272

Length, constraining queue, 88–89

Levels

logging tree structure, 984–985

verbosity, logging API, 982–984

lgamma() function, math, 276

Libraries

configuring logging for, 980

runtime. See Runtime LIFO (last-in, first-out) queue, 112

LifoQueue, 112

Limiting concurrent access to resources, threading, 581–583

Line-oriented command

processors. See cmd module

linecache module

error handling, 328–329

handling blank lines, 328

overview of, 326

reading Python source files, 329–330

reading specific lines, 327–328

test data, 326–327

Lines, in linecache, 327–328

lineterm argument, unified_diff(), 60

Linux, operating information functions, 1249

list_contents(), xmlrpc.server, 828–830

list_dialects() method, csv, 469

listdir(), examine file system contents, 1228–1229

listen(), socket echo server, 705

–listfuncs, calling relationships in trace, 1073–1074

Lists

hashlib algorithms, 524

of mailboxes, in imaplib, 866–869

maintain in sorted order with bisect, 109–111

of nodes, building XML trees, 461–464

passing to select(), 730

variable argument, 908–910

Literal expressions, compiler optimization, 1309–1310

Literal strings, 1003–1007

load() method

decoding cookie headers in http.cookie, 794–795

file-like streams in pickle, 398

streams/files in json, 813

Loaders

accessing, 1332–1334

importing modules, 1205

load_module(), 1332–1334

local() class, thread specific data, 583–585

local_data list, pprint recursion, 139

Locale

calendar formatting for, 236

directory, 1005–1008

locale module

changes in Python 3, 1359

currency, 1018–1019

dates and times, 1022

formatting numbers, 1019–1021

overview of, 1012–1013

parsing numbers, 1021

probing current locale, 1013–1018

Localization and

internationalization cultural localization API. See locale module

message catalogs. See gettext module

localtime() function, 217

Location, temporary file, 336–337

lock() method, remove messages from mbox mailbox, 854

Lock object, resource access control in multiprocessing, 603–604

Lock object, threading

as context managers, 577–578

controlling access to resources, 572–576

re-entrant locks, 576–577

RLock, 576–577

synchronizing threads, 578–581

log() function, math, 279–281

log1p() function, math, 281

Logarithms, math, 278–282

Logger, 980, 984–985

Logging

multiprocessing, 596–597

tracebacks, 1098–1101

xmlrpc.server, 829

logging module

asynchronous I/O using, 644–647, 650–653

changes in Python 3, 1360

combining coroutines with threads in asyncio, 671–672

debugging with asyncio, 673–674

embedding thread name in, 563–564

integration with warning module, 985–986

logging components, 980

logging in applications vs. libraries, 980

logging to file, 981

logging tree, 984–985

naming logger instances, 984

report status, error, informational messages with, 980

rotating log files, 981–982

verbosity levels, 982–984

Logical operations, functional interface, 183–184

LogRecord, as logging component, 980

log_to_stderr(), multiprocessing, 596–597

Long-form options, getopt, 917–918

Long-lived spiders, urllib.robotparser, 775–776

longlist, navigating execution stack, 1106

Look ahead assertion syntax, regular expressions, 44–46

Look behind assertion syntax, regular expressions, 46–48

Loops, analyzing performance, 1303–1309

lru_cache() decorator, functools, 155–158

lstat() method, 315, 1230

lxml package, 1368

M

M3U files, converting to RSS, 987–989

m3utorss, converting M3U files to RSS, 987–989

MAC address, UUID 1, 798–800

Mail server base class, smtpd, 847–850

mailbox module

changes in Python 3, 1360

Maildir format. See Maildir format manipulating email archives, 852

mbox format, 852–855

message flags, 862–864

other formats, 862

mailbox status, imaplib, 869–871

Mailboxes

IMAP4 client library. See imaplib module

mailbox module. See mailbox module

Maildir format

create Maildir mailbox, 854–858

Maildir folders, 860–862

overview of, 854

read from Maildir mailbox, 858

remove messages from Maildir

mailbox, 858–860

MaildirMessage, 857

make_archive()function, shutil, 347–348

make_encoding_map(), custom encoding, 384–385

makefile(), Unicode data/network communication, 381, 383

Manager, shared namespaces, 608–610

Manipulating arrays, 99–100

map() method

converting inputs in itertools, 167–168

new values in itertools, 171–172

process pools in multiprocessing, 611–613

using with basic thread pool, 677–678

Mapping

accessing configuration settings, 964–965

using poll() in select, 738

MapReduce, multiprocessing, 613–617

Maps attribute, ChainMap, 75–76

Masks, network, 688, 691–692

match() function, regular expression search, 28, 39

Match object

find multiple matches in text, 16

regular expression groups, 30–36

search for patterns in text, 14–15

math module

angles, 282–284

commonly used calculations, 274–277

comparing, 267–269

converting floating-point values to integers, 270–271

exponents and logarithms, 278–282

hyperbolic functions, 288

as mathematical functions, 264

positive and negative signs, 272–274

representations of floating-point values, 271–272

special constants, 265

special functions, 289–290

supplements to standard library, 1368

testing for exceptional values, 265–267

trigonometry, 284–288

Mathematics

fixed- and floating-point math. See decimal module

mathematical functions. See math module

overview of, 239

pseudorandom number generators. See random module

rational numbers. See fractions module

statistical calculations, 290–294

supplements to standard library, 1368

max attribute

datetime_date, 224

datetime_time, 222

random numbers, 254

Max-heap, 103

maxsize argument, caching in functools, 156–158

maxtasksperchild parameter, process pools, 612–613

mbox, mailbox, 852–855

MD5 algorithm hashlib module, 524–525

hmac module, 528–529

UUID 3 and 5, name-based values, 800–801

md5 module, removed from Python 3, 1354

mean() function, statistics, 290–291

median() function, statistics, 291–292

median_grouped() function, statistics, 292

median_high() function, statistics, 292

median_low() function, statistics, 292

Memory

debugging leaks, 1265–1270

holding data in, zlib, 477–479

modules for managing, 65

Memory management

byte ordering in, 1194

controlling recursion, 1190–1191

defining maximum values, 1191

determining object size, 1188–1190

floating-point values, 1192–1193

integer values, 1193

reference counts for, 1187–1188

sys features, 1187

Memory-map files. See mmap module

merge() method, heapq, 108–109

Merging iterators, itertools, 164–167

Message catalogs. See gettext module

Message flags, mailbox, 862–864

Messages

cryptographic signatures for. See hmac module

multicast, 718–720

passing to processes, multiprocessing, 598–599

remove from Maildir mailbox, 858–860

remove from mbox mailbox, 854–855

sending, smtplib, 841–843

Meta-characters

escaping in glob, 322–323

regular expressions. See re module, pattern syntax Meta-path, module

import and, 1218–1221

Metaclasses, helper base classes specifying, 1289–1290

Metadata

copying file, in shutil, 340–342

progress, in fileinput, 989–990

querying, in sqlite3, 417

reading from archive, in tarfile, 504–505

reading from archive, in zipfile, 512–514

Method resolution order (MRO), inspecting, 1323–1324

Methods

accessing configuration settings, 963–970

concrete methods in abc, 1291–1292

disassembling class methods, 1300

exposing object, xmlrpc.server, 832–834

marking class/static methods as abstract, 1295–1296

overriding base class, in cmd, 944–946

retrieving source code for class/method, 1318–1319

returning specification of method/function arguments, 1319–1322

Microsoft Windows, 305

mimetools module, removed from Python 3, 1354

MimeWriter module, removed from Python 3, 1354

mimify module, removed from Python 3, 1354

min attribute

datetime_date, 224

datetime_time, 222

generating random numbers, 254

Min-heap, 103

mkdir() method, pathlib, 312–313

mktime() function, 217

mmap module

changes in Python 3, 1360

copy mode, 363–364

as memory-map files, 361

reading, 361–362

regular expressions, 364–365

writing, 362–363

mode() function, statistics, 291

mod() function, math, 271

Module localization, 1011–1012

Modules. See also by individual types

built-in modules, 1201–1202

custom importers, 1203–1205

determining if package or regular module, 1348

handling of import errors, 1214–1215

import path, 1202–1203

imported modules, 1200–1201

importing from shelf, 1205–1211

inspecting, 1312–1313

new in Python 3, 1352

Python supported module

styles, 1330–1331

reloading, 1213–1214

removed from Python 3, 1354–1355

renamed in Python 3, 1352–1353

third party, 1367

Modules, porting Python 2 to Python 3

deprecated modules, 1355–1356

new modules, 1352

removed modules, 1354–1355

renamed modules, 1352–1353

summarizing changes to modules, 1356–1365

Monotonic clocks

comparing, 212–213

defined, 211

overview of, 214

scheduling callback for specific time, 624–625

monotonic() function, sched, 999

monthCalendar() method, formatting calendar, 234, 237

Morsel object, RFC attributes for cookies, 791–793

most_common() method, Counter, 81–82

move() function

file or directory in shutil, 345

messages in imaplib, 883–884

move_to_end() method, OrderedDict, 96–97

MRO (method resolution order), inspecting, 1323–1324

msgformat, building message catalogs, 1006

Multicall, XML-RPC, 826–827

Multicasting

groups, 718

socket, 717–718

UDP used for, 694

multifile module, removed from Python 3, 1354

Multiline strings, configparser, 970

Multiline text, regular expression search for, 37–38

MultiPartForm class, encoding files for upload, 767–770

Multiple matches, finding in text, 16

Multiple simultaneous

generators, random, 261–262

multiplexing abstractions, I/O. See selectors module

multiprocessing module

controlling access to resources, 603–604

controlling concurrent access to resources, 605–607

daemon processes, 589–591

determining current process, 588–589

implementing MapReduce, 613–617

importable target functions, 587–588

logging, 596–597

managing processes like threads, 586

managing shared state, 608

multiprocessing basics, 586–587

passing messages to processes, 598–602

process exit status, 594–596

process pools, 611–613

shared namespaces, 608–610

signaling between processes, 602–603

subclassing Process, 597–598

synchronizing operations, 604–605

terminating processes, 593–594

waiting for processes, 591–593

Mutually exclusive options, argparse, 906

MyThreadWithArgs, 569

N

Name

current process in multiprocessing, 588–589

DNS address lookup in asyncio by, 658–660

DNS address lookups in UDP by, 694

hash in hlib, 525–526

Logger instances, 984

predicting tempfile, 335–336

renaming namedtuple invalid fields, 70

temporary files, 333

threads, in server processes, 562–564

UUID 3 and 5

values based on, 800–801

name property

enumerations, 66

parsing paths in pathlib, 308

Named groups

modify strings with patterns, 53

regular expressions, 32–33

test matching, 51–52

Named parameters, sqlite3, 420–421

NamedTemporaryFile() function, 333

namedtuple() factory function, 90

namedtuple module

invalid field names, 91–92

overview of, 89–90

special attributes, 92–94

as tuple subclass with named fields, 90–91

names argument, enumerations, 71

Namespaces

Manager creating shared, 608

parsing command line arguments with, 889–891

UUID 3 and 5, name-based values, 800–801

nan special value, math, 266–267, 269

nargs, variable argument lists in argparse, 908–910

ndiff() function, difflib, 60, 62

Negative look ahead assertion (?!pattern), re, 45–46

Negative look behind assertion (?<!pattern), re, 46–47

Nesting

data types in XML-RPC server, 820–822

for loops, 177–178

output in pprint, 139–140

packages, 1340–1341

parsers, 906–908

Networking

communication, Unicode, 380–383

compressing data, bz2, 499–503

compressing data, zlib, 482–486

creating network servers. See socketserver module

I/O multiplexing abstractions. See selectors module

Internet addresses. See ipaddress module

network communication. See socket module

overview of, 687

resource access. See urllib.request module

wait for I/O efficiently. See select module

new_child(), ChainMap, 77

new()function creating hash by name, hashlib, 525–526

signing cryptographic messages, hmac, 528

Newton-Mercator series, 281–282

next, stepping through program in pdb, 1114–1115

NFSFile class, urrlib.request, 770–773

ngettext() function, 1008–1011

nlargest() method, heap, 107–108

NLTK package, 1367

nmap() function, reading memory-map files, 361–362

no-expression, regular expressions, 51–52

Nodes

building XML documents from Element, 457–458

building XML trees from lists of, 461–464

finding in XML document, 447–448

traversing parsed tree, 446–447

XML attributes of, 449–450

Non-blocking communication and timeouts, socket, 723

Non-blocking I/O with timeouts, select, 734–737

Non-capturing groups, regular expressions, 35–36

Non-daemon processes, multiprocessing, 591–593

Non-daemon threads, threading, 564–567

Non-integer member values, enum, 72–75

Non-Unicode encodings, codecs, 377–378

Non-uniform distributions, random, 263–264

None value

alternative regular expression patterns, 35

disabling interpolation, configparser, 979

retrieving registered handlers, 555–556

search for patterns in text, 14

NoOptionError, option search in configparser, 973–974

Normal distribution, random, 263

Normalizing paths, os.path, 301–302

normpath() function, os.path, 301

NoSectionError, configparser, 972, 974

not_() function, logical operations, 183

NotADirectoryError, pathlib, 309

not_called(), callbacks, 995–996

nsmallest() method, heap, 107–108

NULL byte, 653, 824

Number

generators, multiple random, 261–262

Numbers arithmetic operator functions for, 184–185

formatting in locale, 1019–1021

numeric types in configparser, 966–967

parsing in locale, 1021

rational. See fractions module

NumPly, math supplement to standard library, 1368

O

Object relational mapper (ORM), 1368–1369

object_hook argument, customizing types in json, 809–810

Objects

correcting problems reconstructing, in pickle, 399–400

determining size of, 1188–1190

exposing methods in xmlrpc.server, 832–834

file system paths as. See pathlib module

impermanent references to. See weakref module

inspecting live. See inspect module

passing in XML-RPC, 822–823

persistent storage of. See shelve module

retrieving docstring for, 1316–1317

serializing. See pickle module

server, in socketserver, 743

sqlite3 row, 417–419

temporary file system. See tempfile module

types of logging component, 980

unpicklable, 400–402

working with UUID, 802–803

One-way process

communication, subprocess, 542–543

onecmd() method, sys.argv, 950–951

Online help, pydoc, 1024–1026

open() function

creating new database, 409–410

customizing protocol handlers in urrlib.request, 770–773

reading/writing files in pathlib, 312

webbrowser example using, 796

working with files in codecs, 368–370

writing compressed files in gzip, 486–488

open_connection()

asynchronous I/O using coroutines/streams, 653

connecting to IMAP server, 864–865

enabling SSL on sockets, 652–654

open_new() function, webbrowser, 796

openssl command, enabling SSL on sockets, 656

Operating systems. See also os module

error codes, 1245–1246

obtaining operating information, 1248–1250

operator module

arithmetic operators, 184–186

attribute and item getters, 188–190

changes in Python 3, 1360

combining operators/custom classes, 190–191

comparison operators, 184

as functional interface to built-in operators, 183

logical operations, 183–184

in-place operators, 187–188

sequence operators, 186–187

Optimization

analyzing compiler optimizations, 1309–1311

analyzing loop performance, 1303–1309

Options

argument groups in argparse, 904

configuration file format in configparser, 961

as flags in configparser, 969–970

modifying settings in configparser, 970–971

mutually exclusive, in argparse, 906

parsing in command-line. See getopt module

prefixes in argparse, 894–895

search path in configparser, 972–975

searching for in configparser, 972–975

testing if present in configparser, 965–966

value types in configparser, 966–969

optparse module, deprecated, 1356

OrderedDict subclass

changing namedtuple instances to, 93

defined, 65

equality, 95

remembers order that keys are added to dictionary, 94–95

reordering, 96

ORM (object relational mapper), 1368–1369

os module

changes in Python 3, 1360–1361

creating/deleting directories, 1233

creating processes, 1240–1242

detect/change process owner, 1235–1237

error codes, 1245–1246

examination of file system contents, 1228–1230

management of file system permissions, 1230–1233

management of process environment, 1237–1238

management of process working directory, 1238–1239

overview of, 1227

rename/replace files, 1234–1235

running external commands, 1239–1240

signaling between processes, subprocess, 548–553

spawning new processes, 1244–1245

symbolic links, 1234

waiting for child processes, 1242–1244

OS X, operating information functions, 1249

os.curdir variable, 296

os._exit(), avoid atexit callbacks, 996–997

os.extsep variable, 296

os.fork(), interprocess communication, 398–399

os.kill() method, sending signals, 554, 556

os.pardir variable, os.path, 296

os.path module

building paths, 300–301

changes in Python 3, 1361

file times, 302–303

normalizing paths, 301–302

parsing paths, 296–300

as platform-independent manipulation of filenames, 296

testing files, 303–304

os.pipe(), interprocess communication in pickles, 398–399

os.sep variable, parsing paths in os.path, 296

os.stat(), comparing files in filecmp, 354

os.urandom() function, SystemRandom class, 261–262

Outcomes, unittest, 1052–1054

Outline nodes, finding in XML document, 447–448

Output

alternative cookie formats, 794–795

asynchronous I/O, 649–650

asynchronous I/O using coroutines/streams, 654–655

capturing error, subprocess, 543–545

capturing, subprocess, 538–541

human-consumable vs. compact, in json, 805–807

multicast messages, 720

redirecting streams, contextlib, 201

suppressing, subprocess, 541–542

Overlapping events, sched, 1000

owner() method, pathlib file properties, 316

P

Package installer (pip), 1024, 1167–1168

Packages. See also pkgutil module

custom package importing, 1211–1213

data files in, 1341–1344

determining if package or regular module, 1348

development versions of, 1336–1338

import paths, 1334–1336

importing modules from shelf, 1205–1211

managing installation paths, 1272–1276

managing path with PKG files, 1338–1339

nested packages, 1340–1341

retrieving import package data, 1215–1217

submodules within, 1333

Packing data into strings, in struct, 117

pack_into() method, struct buffers, 120–121

Paragraph formatting. See textwrap module

Parameters

CSV dialect, 470–472

named, sqlite3, 420–421

positional, sqlite3, 419–420

parent property, parsing paths in pathlib, 307

parents property, parsing paths in pathlib, 307–308

parse() function, configuring GNU readline library, 923–924

parse_args(), command line in argparse, 889

PARSE_COLNAMES, column types in sqlite3, 427

Parsed node attributes, xml.etree.ElementTree, 449–450

PARSE_DECLTYPES, new column types in sqlite3, 422

parse_qs() method, urllib.parse, 759–760

parse_qsl() method, urllib.parse, 759–760

Parser, argparse

argument groups, 904–905

conflicting options, 902–904

mutually exclusive options, 906

nesting parsers, 906–908

setting up, 888

sharing parser rules, 901–902

Parsing

command line in argparse, 889

command line in getopt. See getopt module

cookie headers in http.cookie, 794–795

creating UUID objects, 802–803

datetime, 230–231

manipulating URLs in urllib.parse, 754–756

numbers in locale, 1021

paths in os.path, 296–300

paths in pathlib, 307–308

POSIX vs. non-POSIX, 959–960

reading data from CSV files, 467

shell-style syntaxes. See shlex module

strings in XML text, 455–457

time, clock time, 219–220

unparsing URLs in urllib.parse, 756–758

and validating command-line arguments. See argparse module

watching events in XML while, 451–453

XML document, 445–446

XML parsed tree, 446–447

partial() method

algorithms in functools, 148–149

Future callbacks in asyncio, 627–628

receiving Unix signals in asyncio, 668

scheduling callbacks soon in asyncio, 622–623

partial objects acquiring function properties, 145–147

functools primarily using, 143

overview of, 144–145

working with any callable object, 147–148

parts property, parsing paths in pathlib, 307

PassThrough, Unicode data/network communication, 382–383

Password prompt security. See getpass module

pathlib module

building paths, 305–307

creating concrete paths, 309

deleting from file system, 318–319

directory contents, 309–312

file properties, 315–317

file types, 313–315

manipulating directories and symbolic links, 312–313

new in Python 3, 1352

parsing paths, 307–308

path representations, 305

permissions, 317–318

reading and writing files, 312

Paths

file system paths as objects. See pathlib module

platform-independent manipulation of filenames. See os.path module

representations in pathlib, 305

scanning for configuration files, 1172–1175

Pattern matching

fnmatch Unix-style glob, 323–326

glob filename, 319–323

Patterns, regular expressions. See re module

pdb module

breakpoint jumps, 1130–1134

breakpoint management, 1120–1123

breakpoint use, 1117–1120

breakpoints for changing execution flow, 1129–1130

breakpoints for watching data change, 1128–1129

breakpoints triggering actions, 1127–1128

changes in Python 3, 1361

conditional breakpoints, 1124–1125

control interface, 1104–1105

customization using aliases, 1136–1137

debugging after failure, 1104

examining of stack variables, 1107–1111

ignoring breakpoints, 1125–1127

for interactive debugging, 1101

navigating execution stack, 1105–1107

overview of, 1023

program restart, 1134–1136

saving configuration settings, 1137–1139

startup from command line, 1101–1102

startup within interactive interpreter, 1102–1103

startup within program, 1103

stepping through a program, 1111–1117

temporary breakpoints, 1123–1124

peer argument, process_message()in smtpd, 848

PEP 230, warnings, 1279

Per-instance context, decimal, 248

perf_counter()

clock time, 211

comparing clocks, 212–213

determining best clock data source, 216–217

Performance analysis

compiler optimizations, 1309–1311

loop performance, 1303–1309

Permissions, file

changing in pathlib, 317–318

copying from one file to another, 340–342

managing, 1230–1233

removing file system object in pathlib, 319

testing, urllib.robotparser, 774–775

UDS, 716–717

permutations() function, itertools, 180–181

Permutations, random, 258–260

permutations_with_ replacement() function, itertools, 182

Persistent storage of objects. See shelve module

pformat() function, pprint, 138–139

Pi character, handling encoding errors, 374

Picking random items, random, 258

pickle module

applications of message signatures, 530–533

changes in Python 3, 1361–1362

circular references, 402–404

encoding/decoding data in strings, 396–397

json module

vs., 803–804

new column types in sqlite3, 424–425

object serialization via, 396

passing arguments to multiprocessing process, 587

problems reconstructing objects, 399–400

sending binary objects in XML-RPC, 824–825

unpicklable objects, 400–402

working with streams, 397–399

pip (package installer), 1024, 1167–1168

PIPE, subprocess

capturing output, 538–540

connecting segments, 545–546

select() function monitoring, 729–734

working directly with, 542–545

pipe_data_received(), protocol abstraction with subprocesses, 662

pipes module, changes in Python 3, 1362

PKG files, managing paths, 1338–1339

pkgutil module

data files in packages, 1341–1344

development versions of packages, 1336–1338

import paths for packages, 1334–1336

nested packages, 1340–1341

overview of, 1329, 1332–1334

Plain text help, 1024–1025

platform module

basic FIFO queue, 112

changes in Python 3, 1362

executable architecture, 1250

interpreter, 1246–1247

operating system/hardware information, 1248–1250

overview of, 1169, 1246

platform() function, 1247–1248

platform(), as identifier of platform, 1247–1248

Platforms

filename manipulation independent of. See os.path module

getting version/platform information, 1276–1277

module styles supported by Python, 1330–1331

obtaining information about architecture, 1250

specific options in select, 742

symbolic links and, 1234

system version information. See platform module

Plural values, in gettext, 1008–1010

PLY package, text-related supplements to standard library, 1367

Podcast-feed URLs, traversing parsed tree, 447

Podcasting client, building threaded, 114–117

POLLERR error, using poll() in select, 740

poll()function, select, 737–741

Pool class

managing concurrent tasks. See concurrent.futures module

multiprocessing, 611–617

pop_all() method, ExitStack, 208–209

Popen class, subprocess, 535–536

popen2 module, removed from Python 3, 1354

Populating deque, 85

Port numbers, socket addresses, 697–700

Porting Python 2 to Python 3

deprecated modules, 1355–1356

new modules, 1352

references, 1351

removed modules, 1354–1355

renamed modules, 1352–1353

summarizing changes to modules, 1356–1365

Positional arguments, in argparse, 904–905

Positional parameters, in sqlite3, 419–420

Positive and negative signs, math, 272–274

Positive look ahead assertion (?=pattern), regular expressions, 44–45

Positive look behind assertion (?⇐pattern), regular expressions, 47–48

POSIX

managing file system paths in pathlib, 305

timestamps in datetime_date, 223–224

vs. non-POSIX parsing in shlex, 959–960

Posixfile module, 1355

postcmd() method, overriding base class methods, 945–946

Posting form data, urllib.request, 766–767

postloop() method, overriding base class methods in cmd, 945–946

Postmortem debugging, pdb, 1104

pow() function, math exponents, 278

pprint() function, 136–137

pprint module

arbitrary classes, 138–139

controlling output width, 140–142

debugging with, 65

formatting, 137–138

limiting nested output, 139–140

pretty-print data structures, 136

printing, 136–137

recursion, 139

Pre-input hook, readline, 934

prec attribute of context, 244–245

Precision context, decimal, 245

precmd() method, overriding base class methods in cmd, 945

predicate, textwrap_ indent_predicate.py, 10–11

Predicting names, tempfile, 335–336

preloop() method, overriding base class methods in cmd, 945

Pretty-print data structures. See pprint module

Pretty-printing XML, 458–459

print()call, printing time, 214

print_event(), running events with delay, 999–1000

print_exc(), traceback exception reporting, 1082–1084

print_stack(), traceback applied to low-level stack APIs, 1086–1087

Priorities for events, in sched, 1001

PriorityQueue, 112–113

prmonth(), formatting calendar, 233

Process pools concurrent.futures, 685–686

multiprocessing, 611–613

Processes

combining coroutines with threads and, 670–673

concurrency using system. See multiprocessing module

creating, 1240–1242

detect/change process owner, 1235–1237

determining current, multiprocessing, 588–589

exit status, multiprocessing, 594–596

managing concurrent operations within. See threading module

managing environment of, 1237–1238

managing like threads. See multiprocessing module

managing working directory for, 1238–1239

multiprocessing basics, 586–587

signaling between, subprocess, 550–553

spawning additional. See subprocess module

spawning new, 1244–1245

subclassing, multiprocessing, 597–598

supplements to standard library, 1369

UDS socket communication, 717

waiting for child, 1242–1244

process_exited() method, protocol abstraction, 662

process_message() method, smtpd, 847–848

Processor clock time, 211, 214–216

ProcessPoolExecutor, 672–673, 684–685

process_time(), CPU time via, 211

product()function, itertools, 177–180

profile module

overview of, 1023, 1140

run method, 1140–1142

statistics (pstats), 1144–1148

Programmatically, creating enumerations, 71–72

Programming interface, trace, 1074–1075

Programs

restarting using pdb, 1134–1136

shutdown callbacks. See atexit module

stepping through using pdb, 1111–1117

Progress metadata, fileinput, 989–990

Prompts, interactive, 1182–1183

prompt_toolkit, 1371

Properties

abstract, 1292–1295

file, in os.path, 302–303

function, 145–147, 149–151

parsing paths in pathlib, 307–308

pathlib file, 315

setting element, 459–461

shlex, 955–956

socket, 693

Protocol abstraction, with subprocesses in asyncio, 661–664

Protocol class, asynchronous I/O, 644–650

Protocol handlers, customizing in urrlib.request, 770–773

Proxies, weakref, 126–127

Proxy server, stmpd, 851

Pseudorandom numbers. See random module

pstats

limiting report contents, 1145–1146

printing callers and callees of functions, 1146–1148

profiling and, 1144–1145

pstdev()function, statistics variance, 293–294

PurePosixPath class, managing file paths in pathlib, 305

PureProxy class, proxy server in stmpd, 851

PureWindowsPath class, managing file paths in pathlib, 305

put() method, basic FIFO queue, 112

pvariance()function, statistics variance, 293–294

pyclbr module

class browser, 1160–1161

overview of, 1024

scanning for classes, 1161–1162

scanning for functions, 1162–1163

pydoc module, 1023–1026

PyParsing tool, 1367

PyQuery library, 1370

Pyramid package, 1370

Python

Code Quality Authority, 1371–1372

ZIP archives, 519–521

pytz package, 1368

pytz, time zone support via, 232

pyvenu, virtual environments, 1163–1164

PyYAML, 1371

PyZipFile class, Python ZIP archives, 519–521

Q

Queries, sqlite3

calling functions during, 436–438

with regular expressions, 439–440

retrieving data, 415

using variables with, 419

Queue, double-ended. See deque module

queue module

building threaded podcast client, 114–117

defined, 65

LIFO queue, 112

priority queue, 113–114

as thread-safe FIFO implementation, 111

quote() function, 760, 953

Quoted strings, parsing in shlex, 951–953

quote_plus() function, 760

Quoting options, CSV files, 468–469

R

Race conditions, searching readable files, 346

radians() function, math angles, 282

randint() function, random integers, 257

random() function. See random module

Random integers, 257

random module

changes in Python 3, 1362

as generating random numbers, 254

multiple simultaneous

generators, 261–262

non-uniform distributions, 263–264

permutations, 258–260

picking random items, 258

as pseudorandom number generators, 254

random integers, 257

sampling, 260–261

saving state, 255–256

seeding, 255

SystemRandom, 262–263

Random values, UUID 4, 802

randrange() function, random integers, 257

Rational numbers. See fractions module

raw argument, 975–976

Raw bytes, 391–393

raw_decode method, JSONDecoder, 813–814

Re-entrant locks, threading, 576–578

re module

changes in Python 3, 1362

compiling expressions, 15

constraining search, 28–29

dissecting matches with groups, 30–36

finding patterns in text, 14–15

listing mailboxes, imaplib, 867–868

looking ahead or behind, 44–48

matching files using, 1156–1157

memory-map files used with, 364–365

modifying string with patterns, 53–54

multiple matches, 16

queryies in sqlite3 with, 439–440

as regular expressions, 13–14

searching and parsing text, 1367

self-referencing, 48–53

splitting with patterns, 55–57

translating patterns in fnmatch, 325–326

re module, pattern syntax

anchoring, 26–27

character sets, 20–23

escape codes, 23–26

overview, 17

repetition, 18–20

re module, search options case-insensitive matching, 36–37

embedding flags in patterns, 43

input with multiple lines, 37–38

overview of, 14

Unicode, 38–39

verbose expression syntax, 39–43

read() method

access data from archive, 514

asynchronous I/O, 651, 654

calling subprocesses, 664–666

customizing protocol handlers, 770–773

I/O multiplexing abstractions, 724–725

reading configuration files, 961–963

readable sockets, 730–731, 738–739

Reading

compressed data in gzip, 489–490

compressed files in bz2, 497–498

configuration files, 961

files in pathlib, 312

GNU zip files, gzip, 486

from Maildir mailbox, 858

mbox mailbox, 853–854

memory-map files, 361–362

metadata from archive, tarfile, 504–505

metadata from archive, zipfile, 512–514

read text efficiently. See linecache module

Unicode data, 498–499

readline module

accessing completion buffer, 927–931

auto-completion for commands, 942–944

cmd module

using, 938

completing text, 924–927

configuring readline, 923–924

GNU readline library with, 922–923

hooks, 934–935

input history, 931–934

overview of, 887

readmodule(), 1161–1163

Receiving signals, 554–555

Recursion

controlling, 1190–1191

deep copy, 133–136

examining file system contents, 1228–1229

pprint, 139

in substitution errors during interpolation, 977

Redirecting output streams, contextlib, 201

Reducing data sets, functools, 158–160

ref class, reference callbacks, 122–123

Reference counts, memory management, 1187–1188

ReferenceError exception, weakref proxies, 127

References

circular, 402–404

finding to uncollectable objects, 1259–1261

impermanent object. See weakref module

tracing, 1255–1257

register() attribute, of generic functions, 161–163

Registering

alternate API names, xmlrpc.server, 831–832

arbitrary API names, xmlrpc.server, 831–832

concrete classes, abc, 1287–1288

custom encoding, codecs, 386–389

data channels using poll(), 737–738

decorator syntax, atexit, 994

dispatching calls, xmlrpc.server, 835–836

dotted API names, xmlrpc.server, 830–831

exit callbacks, atexit, 993–994

introspection API, xmlrpc.server, 837–839

new column types, sqlite3, 424

Registry, shutil-managed archive, 348–349

Regular expressions. See re module

Relational database, embedded. See sqlite3 module

Reload modules, 1213–1214

Removing

directory contents, 344–345

empty directory, 318

messages from Maildir mailbox, 858–860

messages from mbox mailbox, 854

sections/options from ConfigParser, 971

Reordering

keys in OrderedDict, 96–97

mappings with ChainMap, 76–77

repeat() function, values in itertools, 170–172

Repetition

regular expression character sets, 23

regular expression escape codes, 24–26

regular expression patterns, 18–20

of warnings, 1283–1284

replace() method, dates, 224–225

replace mode, decoding errors, 375

Request handlers

creating network servers, socketserver, 742–744, 748

implementing web servers, http.server, 781–783

requests package, Internet, 1369

Resolution, datetime, 221–222, 224

resolve() method, normalizing path, 306, 313

resource module, 1169, 1251–1254

Resources

controlling access to, multiprocessing, 603–604

controlling access to, threading, 572–576

controlling concurrent access to, multiprocessing, 605–607

limiting access to concurrent, threading, 581–583

thread specific data, 583–585

ResourceWarning warnings, debugging with asyncio, 673

Restricting access to data, sqlite3, 442–444

Results

gathering from coroutines, 635–636

of tasks, concurrent.futures, 679–680, 683

Retrieving data, sqlite3, 415

return command, stepping through program in pdb, 1116–1117

Reverse lookups, socket, 697, 698–699

rfc822 module, removed from Python 3, 1354

rglob() method, directory contents in pathlib, 311–312

Rich comparison methods, functools, 151–153

Rich comparison operators, 184

RLock, re-entrant locks, 576–577

RobotFileParser.can_fetch(), testing access permissions, 774–775

robots.txt, urllib. robotparser, 773–776

rollback() method, preserve changes in transactions, 429–430

rollover() method, temporary spooled files, 334

Rotation

double-ended queue, 87–88

of log files, 981–982

Rounding, decimal, 245–247

Row objects, sqlite3, 417–419

RSS feed, converting M3U files to, 987–989

Rules, sharing parser (in argparse), 901–902

Runtime

accessing operating system features. See os module

accessing system version information. See platform module

determining interpreter settings. See sysconfig module

finding message catalogs at, 1007–1008

garbage collection. See gc module

load and activate message catalog in gettext, 1004

managing system resources. See Resources overview of, 1169

system-specific configuration. See sys module

rx argument, matching files with regular expression, 1156–1157

S

safe_substitute() method, string.Template, 3

Sampling, random, 260–261

Saving

configuration files, 972

state, random, 255–256

scandir, examining file system contents, 1229–1230

sched module

canceling events, 1001–1002

event priorities, 1001

implementing timed event scheduler, 998–999

overlapping events, 1000

running events with delay, 999–1000

timed event scheduler using, 998–999

Scheduling

callbacks in asyncio, 622–623

implementing timed event scheduler. See sched module

individual tasks in concurrent.futures, 678–679

Schema, database in sqlite3, 413

Search. See also re module, search options

constraints in regular expressions, 28–29

criteria in imaplib, 872–874

files in shutil, 345–346

messages in imaplib, 872

options in configparser, 972–975

Sections, configparser

configuration file format, 961

modifying settings, 970–971

option search path, 972–975

testing if present, 965

Secure password prompt. See getpass module

Seeding, in random, 255

seek() method

in-memory streams, io, 391

reading compressed data, gzip, 489–490

reading compressed files, bz2, 497–498

reading/writing Unicode data, bz2, 499

temporary files, tempfile, 332

select module

non-blocking I/O with timeouts, 734–737

platform-specific options, 742

using poll(), 737–741

using select(), 729–734

wait for I/O efficiently with, 728–729

select statements, query parameters in sqlite3, 421

select()function, calling programs in selectors, 724

Selecting mailbox, imaplib, 871

selectors module

Echo client, 726–727

Echo server, 724–725

I/O multiplexing abstractions with, 724

new in Python 3, 1352

operating model, 724

server and client together, 727–728

Self-referencing regular expressions, 48–53

Semaphore

multiprocessing, 605–607

threading, 581–583

Sequence operators, functional interface for, 186–187

SequenceMatcher class, difflib, 61–64

Sequences

comparing. See difflib module

Counter initialization via data, 80

deques as type of container for, 84

efficiently merging sorted, 108–109

of fixed type data. See array module

reducing data set in functools, 160

Serialization

object. See also pickle module, 587

XML to stream, 464–466

ServerProxy object, connecting to XML-RPC server, 817–819

Server(s)

connecting to IMAP, 864–866

connecting to XML-RPC, 817–819

creating network. See socketserver module

I/O multiplexing abstractions, selectors, 724–728

implementing web. See http.server module

looking up addresses in network, 700–702

sample mail. See smtpd module

sending binary data to, 722

TCP/IP, 704–711

types, socketserver, 742–743

UDP, 711–713

UDS, 714–716

Service information, socket, 697

Sessions, signaling between processes, 550–553

set() method

modifying settings in ConfigParser, 970–971

signaling between threads, 571–572

sets module, removed from Python 3, 1355

Settings

accessing configuration, 963–970

modifying configparser, 970–971

setuptools-git, 1371

setuptools module, 1371

sha module, removed from Python 3, 1355

SHA1 algorithm

in hashlib, 525

in hmac, 529

UUID 3 and 5, name-based values, 800–801

shallow argument, comparing files in filecmp, 354

Shallow copies, duplicate objects, 130–131

shallow module, 1340

Shared namespaces, multiprocessing, 608–610

Shared state, multiprocessing, 608

Sharing parser rules, argparse, 901–902

shell argument, subprocess, 537

Shell commands, running, 947–948

Shell-style syntaxes, parsing. See shlex module

shelve module, 405–408, 1362

ShelveLoader

custom package importing, 1211–1213

importing modules from shelf, 1205–1211

reloading modules, 1213–1214

shlex module

controlling parser, 956–957

embedded comments, 954

error handling, 957–958

including other sources of tokens, 955–956

making safe strings for shells, 953

parse-style syntaxes with, 951

parsing quoted strings, 951–953

POSIX vs. non-POSIX parsing, 959–960

splitting strings into tokens, 954

Short-form options, getopt, 917

shorten() function, truncating long text, 12–13

show_code, printing summary of function, 1299

showwarning(), warning message delivery, 1284

shuffle() function, random, 258–260

shutdown() method, concurrent.futures, 683–684

shutil module

archives, 346–350

copying file metadata, 340–342

copying files, 337–340

file system space, 350–351

finding files, 345–346

as high level file operations, 337

working with directory trees, 342–345

SIGINT. See signal module

Sign flag, creating Decimals from, 240

Signal handlers

mixing signals and threads, 558–560

receiving signals via, 554–555

retrieving registered, 555–556

signal module

alarms, 556–557

asynchronous system events via, 553

changes in Python 3, 1362

ignoring signals, 557–558

receiving signals, 554–555

retrieving registered handlers, 555–556

signaling between processes, subprocess, 548–553

signals and threads, 558–560

Signals

mixing threads and, 558–560

between processes, 602–603

between threads, 571–572

Signals, receiving Unix, 668–670

signature(), method or function arguments, 1319–1322

Signing cryptographic messages, hmac, 528

Simple Mail Transport Protocol (SMTP)

email. See smtplib module

sample mail servers. See smtpd module

SimpleCompleter class, autocompletion of text in readline, 925–927

SimpleObject, reconstructing objects in pickle, 400

SimpleXMLRPCServer, 828–829, 835–836

Sine function, math, 284, 288

Single-character wildcards, 321–322

singledispatch() decorator, generic functions, 161–163

site module

customizing site configuration, 1175–1176

customizing user configuration, 1176–1177

import path, 1169–1170

managing user directories, 1171–1172

overview of, 1169, 1177–1178

scanning for path configuration files, 1172–1175

site-wide configuration, 1169

Size

caching in functools, 156–158

constraining queue, 88–89, 111

struct buffer. See weakref module

skipkeys argument, encoding dictionaries, 807

skipping unittests for unmet conditions, 1067

sleep() function

alarms in signal, 557

mixing signals and threads, 559–560

processor clock time, 216

sched using, 999

using map() with basic thread pool, 677–678

SMTP (Simple Mail Transport Protocol)

email. See smtplib module

sample mail servers. See smtpd module

smtpd module

create custom mail server, 847

debugging server, 850

mail server base class, 847–850

proxy server, 851

smtplib module

authentication and encryption, 843–846

create client to send data to test server, 848–849

sending email message, 841–843

verifying email address, 846–847

SMTP_SSL, email encryption in smtplib, 843

Sniffer class, detecting CSV dialects, 472–474

SOCK_DGRAM sockets, 694

socket module

changes in Python 3, 1362

finding service information, 697–700

IP address representations, 702–703

looking up hosts on network, 694–697

looking up server addresses, 700–702

multicast, 717–721

network communication with, 693

non-blocking communication and timeouts, 723

sending binary data, 721–723

TCP/IP client and server, 704–711

user datagram client and server, 711–717

socket.error exception, 695, 723

socketpair() function, UDS sockets, 717

Sockets

enabling SSL in asyncio on, 656–658

monitoring using select()function, 729–734

using poll() in select, 737–741

socketserver module

changes in Python 3, 1363

compressing network data in bz2, 499–500

creating network servers with, 742

Echo example, 744–749

implementing server, 743

request handlers, 743

server objects, 743

server types, 742–743

threading and forking, 749–751

socketserver.TCPServer, 786

SOCK_STREAM sockets, 694

Sort algorithm. See heapq module

Sorted order

queue. See queue module

use bisect to maintain lists in, 109–111

Source code

creating message catalogs from, 1004–1007

disassembling, 1301–1302

retrieving for class or method, 1318–1319

retrieving from ZIP archive, 1346–1347

Source files

compiling to byte-code, 1155–1159

reading Python, 329–330

Sources of arguments, argparse, 895–897

spawning new processes, 1244–1245

Special constants, math, 265

Special values, decimal, 243–244

Spider access control, Internet, 773–776

Splitting

iterators, itertools, 164–167

parsing paths, os.path, 296–298

with patterns, re, 55–57

strings into tokens, shlex, 954

Spooled files, tempfile, 333–334

SQL-injection attacks, 419

sqlalchemy ORM, 1368

sqlite3 module

bulk loading, 421–422

creating database, 412–415

custom aggregation, 440–441

defining new column types, 422–425

determining types for columns, 426–427

embedded relational database via, 412

exporting contents of database, 435–436

isolation levels, 431–434

in-memory databases, 434–435

query metadata, 417

querying with regular expressions, 439–440

restricting access to data, 442–444

retrieving data, 415–417

row objects, 417–419

threading and connection sharing, 441–442

transactions, 428–430

using Python functions in SQL, 436–438

using variables with queries, 419–421

Square root, computing in math, 278–279

sre module, removed from Python 3, 1355

SSL communication, enabling on sockets, 656–658

SSLContext, encryption, 656

Stack

controlling recursion, 1190–1191

examination of variables in, 1107–1111

examining, 1079–1081

inspecting frames and, 1324–1327

stack level in warnings, 1286

traces. See traceback module

watching with trace hooks, 1224–1225

Stacking behavior, ChainMap, 77

Stacks, dynamic context manager, 202–209

Standard deviation, statistics variance, 292–294

starmap() function, converting inputs in itertools, 169

start argument, 170, 257

start() method, search for patterns in text, 14

Start-up hook, readline, 934

start_server() method, 652, 656

STARTTLS extension, email authentication/encryption, 844–845

stat() method

managing file system permissions, 1230–1231

pathlib file properties, 315–316

Static analysis tool, Python Code Quality Authority, 1372

Static methods, marking as abstract, 1295–1296

statistics module

averages, 290–292

new in Python 3, 1352

variance, 292–294

Statistics, profiling and, 1140, 1144–1148

Status conditions, IMAP4

mailbox, 869–871

status(), mailbox in imaplib, 869–871

statvfs module, removed from Python 3, 1355

stddev() function, statistics variance, 293–294

stderr attribute, subprocess capturing error output, 543–544

capturing output, 538–541

interaction with another command, 546–548

stdin attribute, subprocess, 542–543, 546–548

stdin attribute, sys, 1186–1187

stdout attribute, subprocess capturing error output, 543–544

capturing output, 538–541

connecting segments of pipe, 545–546

interaction with another command, 546–548

working with pipes, 542–543

stdout attribute, sys, 1186–1187

step argument

new values, itertools, 170

random integers, math, 257

stepping through program, pdb, 1111–1114

step() method, custom aggregation in sqlite3, 440–441

stop argument, random integers in math, 257

Storage of objects, persistent. See shelve module

store action, arguments, 891

str class

modify strings with patterns, 53–54

as text processing tool, 1

Stream-oriented transport, SOCK_STREAM sockets, 694

StreamReader instance, asynchronous I/O, 651, 653

Streams

asynchronous IO, in asyncio, 650–655

calling subprocesses with, in asyncio, 664–666

mixed-content, in bz2, 495

mixed content, in zlib, 480–481

serializing XML to, 464–466

working with file-like, in pickle, 397–399

working with, in gzip, 490–491

working with, in json, 813–814

wrapping byte, in io, 392–393

StreamWriter instance, asynchronous I/O, 651, 653

strftime() function, 219–220, 230–231

strict mode

handling decoding errors, 375

handling encoding errors, 373

string module

advanced templates, 4–6

changes in Python 3, 1363

constants, 1, 6–7

formatter, 6

functions, 1–2

template tool, 1, 2–4, 1367

StringIO buffers, in-memory streams, 390–391

Strings

configuration settings, 964

converting in argparse, 910–912

creating fractions, 251

dis accepting string arguments, 1301–1302

encoding and decoding. See codecs module

encoding/decoding data in, 396–397

making safe for shells, 953

multiliner, 970

packing/unpacking data into, 117–118

parsing command line arguments with, 889–890

parsing in XML, 455–457

parsing quoted, 951–953

retrieving docstring for object, 1316–1317

splitting into tokens, 954

Unicode primer, 365–366

value types, 966–969

string.Template, 1–6

strptime() function

datetime, 230–231

time, 218–219

Struct class, 117–121

struct module

as binary data structures, 117

buffers, 120–121

changes in Python 3, 1363

defined, 65

endianness, 118–120

functions vs. Struct class, 117

packing and unpacking, 117–118

sending binary data, 721–723

struct_time, 217–220

sub(), modify strings with patterns, 53–54

Subclasses

from abstract base class, 1289–1291

from base class, 1288–1289

creating Process, multiprocessing, 597–598

creating Thread, threading, 568–569

Subdirectory, listing files in, 321

Submodules, within packages, 1333

subn(), modify strings with patterns, 54

subprocess module

changes in Python 3, 1363

connecting segments of pipe, 545–546

interacting with another command, 546–548

running external command, 536–542

signaling between processes, 548–553

spawning additional processes, 535–536

working with pipes directly, 542–545

subprocesses, asyncio

calling with coroutines/streams, 664–666

sending data to, 666–668

using protocol abstraction with, 661–664

SubprocessProtocol, asyncio, 661–664

Substitution errors, interpolation in configparser, 977–978

subTest(), repeating unitests with different inputs, 1065–1066

suffix property, parsing paths in pathlib, 308

Switch interval, controlling/debugging threads, 1197–1198

Switching translations, gettext, 1012

Symbolic links

creating in pathlib, 313

functions for, 1234

removing in pathlib, 318–319

Synchronization primitives, asyncio

Condition, 640–641

Event, 639–640

Lock, 637–639

overview of, 637

Queue, 642–644

Synchronizing operations, multiprocessing, 604–605

Synchronizing threads, threading, 578–581

sys module

applied to build-time version information, 1178–1179

built-in modules, 1201–1202

byte ordering, 1194

changes in Python 3, 1363–1364

command-line arguments captured by interpreter, 1185–1186

CPython command-line flags, 1180–1181

custom importers, 1203–1205

custom package importing, 1211–1213

debugging threads, 1198–1200

defining maximum values in memory, 1191

determining object size, 1188–1190

exception handling, 1194–1197

exception monitoring, 1225–1227

floating-point values, 1192–1193

handling of import errors, 1214–1215

imported modules, 1200–1201

importer cache, 1217–1218

importing from shelf, 1205–1211

input/output streams (stdin/stdout), 1186–1187

integer values in memory management, 1193

interactive prompts, 1182–1183

interpreter install location, 1184–1185

interpreter display hook, 1183–1184

memory management features, 1187

meta-path, 1218–1221

module import path, 1202–1203

module reload, 1213–1214

overview of, 1169

recursion, 1190–1191

reference counts and garbage collection, 1187–1188

retrieval of import package data, 1215–1217

returning exit code (exit()), 1187

switch interval for threads, 1197–1198

for system-specific configuration, 1178

thread support, 1197

tracing function calls, 1221–1222

tracing inside functions, 1222–1224

Unicode defaults, 1181–1182

watching the stack, 1224–1225

sys.argv, commands from, 950–951

sysconfig module

configuration variables, 1270–1272

getting version and platform information, 1276–1277

management of installation paths, 1272–1276

overview of, 1169, 1270

sys.exit(), invoking registered callbacks, 997

sys.path, compiling, 1157

sys.stderr, getpass(), 936–937

sys.stdout, getpass(), 936

sys.stdout.buffer, 464–466

System clock, 214

system(), running external commands, 1239–1240

SystemRandom class, random, 262–263

T

Tab completion

GNU readline library, 923

text in readline, 924–927

tabnanny module

indentation validator, 1153–1154

overview of, 1023–1024

supplements to standard library, 1371

Tabs vs. windows, webbrowser, 796

Tangent

hyperbolic functions, 288

trigonemetric functions, 284

tar archive access. See tarfile module

tarfile module

appending to archives, 510

creating new archives, 508

extracting files from archive, 506–507

reading metadata from archive, 504–505

tar archive access via, 503

testing tar files, 503–504

using alternative archive member names, 508–509

working with compressed archives, 510–512

writing data from sources other than files, 509

Tasks

duration modeling, 264

executing concurrently, asyncio, 628–632

managing pools of concurrent. See concurrent.futures module

as subclass of Future, asyncio, 618

timed event scheduler, sched, 998–1002

TCP/IP (Transmission Control

Protocol/Internet Protocol) socket choosing address for listening, 708–711

as client, 705–706

client and server together, 706–707

easy client connections, 707–708

as server, 704–705

UDS vs., 714

TCP (Transmission Control Protocol), 694

TCPServer class, socketserver, 742–743

tempfile module

named files, 333

predicting names, 335–336

spooled files, 333–334

temporary directories, 335

temporary file location, 336–337

as temporary file system objects, 330

temporary files, 331–332

Templates

advanced string, 4–6

string, 1–2

using ChainMap instances as, 77

Temporary directories, tempfile, 335

Temporary files, tempfile, 331–332, 336–337

Terminal, using getpass() without, 937–938

Terminating processes, multiprocessing, 564–567

Testing

access permissions, urllib.robotparser, 774–775

with doctest. See doctest module

exceptional values, math, 265–267

file types, pathlib, 313–314

files, os.path, 303–304

in-memory databases, sqlite3, 434–435

tar files, 503–504

with unittest. See unittest module

ZIP files, 512

test_patterns() function, regular expressions, 17, 33–34

Text

command-line filter framework for, 987–992

comparing. See difflib module

completing in readline, 924–927

constants and templates. See string module

content of nodes, 449–450

formatting paragraphs. See textwrap module

getting help with plain, 1024–1025

overview of, 1

reading efficiently. See linecache module

regular expressions. See re module

supplements to standard library, 1367

wrapping byte streams for, 392–393

TextIOWrapper

reading compressed data in gzip, 489–490

reading compressed files in bz2, 495

reading/writing Unicode data in bz2, 498–499

wrapping byte streams for text data, 392–393

textwrap module

combining dedent and fill(), 9–10

defined, 1

example data, 8

filling paragraphs, 8

as formatting text paragraphs, 7

hanging indents, 12

indenting blocks, 10–11

removing existing indentation, 8–9

truncating long text, 12–13

Thread context, decimal, 248–249

thread module, removed from Python 3, 1355

Thread objects, 560–561

Thread-safe FIFO implementation. See queue module

Thread-specific data, threading module, 583–585

Threading

adding in http.server, 786–787

adding in socketserver, 749–751

combining coroutines with, asyncio, 670–673

and connection sharing, sqlite3, 441–442

debugging, 1198–1200

managing processes like. See multiprocessing module

mixing signals and, 558–560

supplements to standard library, 1369

switch interval for, 1197–1198

sys support for, 1197

threading module

changes in Python 3, 1364

controlling access to resources, 572–576

daemon versus non-daemon threads, 564–567

determining current thread, 562–564

enumerating all threads, 567–568

isolation levels, sqlite3, 432

limiting concurrent access to resources, 581–583

managing concurrent operations within process via, 560

re-entrant locks, 576–578

signaling between threads, 571–572

subclassing Thread, 568–569

synchronizing threads, 578–581

Thread objects, 560–562

thread-specific data, 583–585

Timer threads, 570

ThreadingMixIn, 749–750, 787

ThreadPoolExecutor, 670–673, 677–678

Thresholds, garbage collection, 1261–1265

Time

clock time. See time module

components, 217–218

datetime. See datetime module

event scheduler to run tasks at specific, 998–1002

execution, for small bits of Python code, 1148–1153

formatting in locale, 1022

overview, 211

scheduling callback for specific, 624–625

supplements to standard library, 1368

zones, 218–219, 231–232

time class, datetime_time, 221–222

time module

changes in Python 3, 1364

as clock time, 211

comparing clocks, 211–213

defined, 211

monotonic clocks, 214

parsing and formatting times, 219–220

performance counter, 216–217

processor clock time, 214–216

supplements to standard library, 1368

time components, 217–218

wall clock time, 213–214

working with time zones, 218–219

time-to-live value (TTL), sending multicast messages, 720

timedelta objects, datetime, 225–227

time()function, clock time, 211, 213–214

timeit module

contents of, 1148

example, 1148–1149

overview of, 1023

values stored in dictionary, 1149–1152

Timeouts

non-blocking I/O with, 734–735

socket operations, 723

using poll(), 737

Timer class, timeit, 1148–1151

TLS (transport layer security) encryption, email in smtplib, 843–846

today() class method, datetime_date, 222–223

tofile() method, arrays and files, 101

Tokens

including other sources of, shlex, 955–956

splitting strings into, shlex, 954

Trace hooks

overview of, 1221

tracing inside functions, 1222–1224

watching stack, 1224–1225

trace module

applied to execution, 1069–1070

code coverage report, 1070–1073

example program, 1069

function relationship report, 1073–1074

options, 1077–1078

overview of, 1023

programming interface, 1074–1075

saving data, 1076–1077

Trace object, 1074–1075

traceback module

applied to low-level exception APIs, 1082–1086

applied to low-level stack APIs, 1086–1089

examining stack (FrameSummary), 1080–1081

examining stack (StackSummary), 1079–1080

example, 1079

exceptions, 1081–1082

overview of, 1023, 1078

stack summary, 1079–1081

TracebackException class, 1081–1082

Tracebacks. See also traceback module

applying dis to, 1302–1303

detailed tracebacks, cgitb, 1090–1092

doctest for, 1032–1033

examining local variables, cgitb, 1093–1096

logging tracebacks, cgitb, 1098–1101

traceback dump, cgitb, 1089–1090

Tracing

exception monitoring, 1225–1227

function calls, 1221–1222

inside functions, 1222–1224

references, 1255–1257

–trackcalls, function calls report, 1073–1074

Transactions

database, sqlite3, 428

discarding changes, sqlite3, 429–430

preserving changes, sqlite3, 428–429

Translations

codec used for many data, 377–378

encoding, 376–377

message catalog. See gettext module

pattern, 325–326

Transmission Control

Protocol/Internet Protocol. See TCP/IP (Transmission Control Protocol/Internet Protocol) socket

Transmission Control Protocol (TCP), 694

Transport argument, asyncio, 662

Transport classes, asynchronous I/O, 644–645

Transport layer security (TLS) encryption, email in smtplib, 843–846

Transport protocol, retrieve port number to, 699–700

Tree structure, logging, 984–985

TreeBuilder, customizing, 453–455

Triangular distribution, random, 263

Trigonometry, math, 284–288

truediv()function, operator, 185–186

trunc() function, math, 270–271

Truncating long text, 12–13

Truth-checking, unittest for, 1054

truth() function, logical operations, 183

try:except statement, ignoring exceptions in contextlib, 199–200

try:finally block, context manager API, 192

TTL (time-to-live value), sending multicast messages, 720

tty, using getpass() without terminal, 937–938

Tuples

as convenient containers for simple uses, 89

encoding/decoding simple data types in json, 804–805

Enum non-integer member values, 72–75

row objects in sqlite3, 417–419

subclass with named fields. See namedtuple module

Twisted library, 1369

TypeError exceptions

caching in functools, 158

comparing enums, 68

dbm module, 411

reducing data set in functools, 160

Types, specific shelve, 408

TZ environment variable, time zones, 218–219

tzinfo abstract base class, datetime_timezone, 231–232

tzset() function, time zones, 218–219

U

UDP (user datagram protocol)

datagram sockets associated with, 694

sending multicast messages via, 718–720

UDPServer class, socketserver, 743

UDS (Unix Domain Sockets), 694, 714–717

Unicode

data and network communication, codecs, 380–383

decoding errors, codecs, 374–376

defaults, 1181–1182

encoding errors, codecs, 373–374

primer, codecs, 365–366

reading and writing, bz2, 498–499

reading configuration files, config, 962–963

regular expression search, re, 38–39

Unique values, Enum, 69–71

unittest module

addCleanup(), 1371

basic test structure, 1051

changes in Python 3, 1364

containers, 1056–1061

doctests compared with, 1026

equality-checking, 1054–1056

exceptions, 1061–1062

fixtures tests, 1062–1065

ignoring test failure, 1068–1069

integrating doctests with, 1047–1048

outcomes, 1052–1054

overview of, 1023

repeating tests with different inputs, 1065–1066

running, 1051–1052

skipping tests for unmet conditions, 1067

truth-checking, 1054

Universally Unique Identifiers. See uuid module

Unix

key-value databases. See dbm module

receiving signals with asyncio, 668–670

Unix Domain Sockets (UDS), 694, 714–717

UnixDatagramServer class, socketserver, 743

UnixStreamServer class, socketserver, 743

unlink() method, removing file system object in pathlib, 318–319

unlock() method, remove messages from mbox mailbox, 854

Unpacking

archive files, shutil, 349–350

data into strings, struct, 117–118

pre-allocated buffers, struct, 120–121

Unparsing URLs, urllib.parse, 756–758

Unpicklable objects, 400–402

Unregister, canceling exit callbacks in atexit, 994–995

until command, stepping through program in pdb, 1115–1116

unused_data attribute, mixed-content streams, 481, 495

Updates

incremental, hashlib, 526–527

initialization, Counter, 80

query parameters, sqlite3, 421

wrapper, functools, 145–147

Upload

files for encoding, urllib.request, 767–770

messages, imaplib, 881–883

urllib.parse module

encoding query arguments, 759–761

joining, 758

parsing, 754–756

split URLs into components with, 753

unparsing, 756–758

urllib.request module

adding outgoing headers, 765–766

creating custom protocol handlers, 770–773

encoding arguments, 763–764

HTTP GET, 761–763

HTTP POST, 764–765

Internet-related supplements to standard library, 1369

network resource access with, 761

posting form data from request, 766–767

uploading files, 767–770

urllib.robotparser

Internet spider access control with, 773

long-lived spiders, 775–776

robots.txt, 773–774

testing access permissions, 774–775

urlopen() function, urllib.request, 761–766

urlparse() function, urllib.parse, 754–758

URLs

building threaded podcast client, 114–117

finding nodes in XML document, 447–448

network resource access. See urllib.request module

safe variations for Base64

encoding, 778–780

splitting into components. See urllib.parse module

traversing parsed tree, 447

User datagram protocol. See UDP (user datagram protocol) user module, removed from Python 3, 1355

use_rawinput, cmd, 949

USER_BASE directory, 1171–1172

UserDict module, changes in Python 3, 1365

UserList module, changes in Python 3, 1365

Users

customizing configuration of, 1176–1177

managing directories, 1171–1172

UserString module, changes in Python 3, 1365

UTC, time zones, datetime, 231–232

UTF-16 encoding

byte order in codecs, 370–372

understanding encodings, 366

Western languages, 366

UTF-32 encoding, codecs, 370–372

UTF-8 encoding

Base64 encoding, 777

loading standard codecs from, 387

reading Unicode configuration files, 962–963

understanding encodings, 367

Western languages, 366

uuid module

changes in Python 3, 1365

implementing UUIDs, 797–798

UUID 1, IEEE 802

MAC Address, 798–800

UUID 3

and 5, name-based values, 800–801

UUID 4, random values, 802

working with UUID objects, 802–803

uvloop library, 1369

V value property, enumerations, 66, 71

Value types, configparser, 966–969

ValueError exception

Enum, 70

handling in shlex, 957–958

namedtuple invalid field names, 91–92

square root of negative value, 279

Values

accessing existing, ChainMap, 75–76

approximating, fraction, 253

combining with interpolation, configparser, 975–979

duplicate, bisect, 110–111

encoding, http.cookies, 793–794

floating-point, math, 271–272

floating-point, memory management, 1192–1193

integer, memory management, 1193

manipulating date and time, datetime, 221–222

maximum, memory management, 1191

missing keys returning default, Counter, 82–84

new iterator, itertools, 169–172

retrieving, Counter, 81

returning from coroutines, asyncio, 619–620

special, decimal, 243–244

testing for exceptional, math, 265–267

unique enumeration, enum, 69–75

updating, ChainMap, 77–78

Variable argument lists, argparse, 908–910

Variables

compiler optimizations, 1309–1311

configuration, 1270–1272

environment, managing process environment, 1237–1238

examining local, in tracebacks, 1093–1096

examining stack, pdb, 1107–1111

parsing paths in os.path, 296

using with queries, sqlite3, 419

variable_stack() method, contextlib, 204, 209

Variance, statistics, 292–294

variations, IMAP4

client library, 864

venv module

contents of virtual environment, 1164–1165

creating virtual environments, 1163–1164

managing package versions during development, 1336–1338

new in Python 3, 1352

supplements to standard library, 1371

using virtual environments, 1165–1167

Verbosity

levels, logging API, 982–984

regular expression syntax, 39–43

verbose option, connecting to XML-RPC server, 818

verbose_copy(), directory trees in shutil, 342–343

Verify email address, smtplib, 846–847

version action, arguments, 891–892

Versions

build-time information, 1178–1179

getting platform information and, 1276–1277

site disabled for backward compatibility, 1177–1178

Virtual environments creating, 1163–1167

venv module. See venv module

virtualenv module, 1336–1338, 1371

virtualenv module, 1336–1338, 1371

von Mises, or circular normal, distribution, random, 264

vonmisesvariate() function, angular distribution, 264

W

wait for I/O efficiently. See select module

wait() method

child processes, 1242–1244

multiple coroutines, 632–635

signaling between threads, 571–572

synchronizing threads, 580–581

walk(), examine file system contents, 1228–1229

Wall clock time. See also time module, 213–214

warn(), generating warnings, 1280–1281

warnings module

alternatives for message delivery, 1284–1285

categories and filtering, 1280

filtering with patterns, 1281–1283

formatting, 1285

generating, 1280–1281

logging integration with, 985–986

overview of, 1279

repeated, 1283–1284

stack level in, 1286

wc() function, statistics variance, 293

WeakKeyDictionary class, caching objects, 127

weakref module

caching objects, 127–130

finalizing objects, 123–126

as impermanent references to objects, 121

memory management with, 65

proxies, 126–127

reference callbacks, 122–123

references, 122

WeakValueDictionary class, caching objects, 127–130

Web Robots Page, robots.txt, 774

webbrowser module, displaying Web pages, 796–797

weekheader() method, calendar, 234

Weibull distribution, random, 264

weibullvariate() function, Weibull distribution, 264 What’s New, Python documentation, 1351

which() function, finding files in shutil, 345–346

whichdb module, 408–411, 1365

Whitespace

difference-based reporting (REPORT_NDIFF), 1036–1037

managing blank lines in doctest, 1034–1036

NORMALIZE_WHITESPACE, 1037–1039

width argument, pprint, 140–142

Wildcards, filename pattern matching in glob, 320–322

Windows OS

obtaining operating information, 1249–1250

vs. tabs, webbrowser, 796

with statement

context manager enabled by, 191–194

converting generator function into context manager, 196–198

decimal_context_manager, 247–248

stacking context managers, 202–206

WRAPPER_ASSIGNMENTS, functools, 146

WRAPPER_UPDATES, functools, 146–147

wrapt package, 1367–1368

writable sockets, 730–732, 738–739

write() method

alternative archive member names, zipfile, 516

creating new archive, zipfile, 514–516

saving configuration files in config, 972

serializing XML to stream, 464–466

Writeback, shelves, 406–408

writepy() method, Python ZIP archives, 519–521

writer() function, sqlite3, 432

Writing

compressed files in bz2, 495–497

compressed files in gzip, 486–488

data from sources other than files, tarfile, 509

data from sources other than files, zipfile, 517–518

files in pathlib, 312

GNU zip files, gzip, 486

Unicode data, 498–499

X

xgettext, message catalogs, 1004–1005

XML manipulation API. See xml.etree.ElementTree module

XML-RPC

client library for. See xmlrpc.client module

server. See xmlrpc.server module

xmlcharrefreplace, lossless error handling, 374

xml.dom.minidom, pretty-printing XML, 458–459

xml.etree.ElementTree module

building documents with element nodes, 457–458

building trees from lists of nodes, 461–464

changes in Python 3, 1365

creating custom TreeBuilder, 453–455

data supplements to standard library, 1368

ElementTree library, 445

finding nodes in document, 447–448

parsed node attributes, 449–450

parsing strings, 455–457

parsing XML document, 445–446

pretty-printing XML, 458–459

serializing XML to stream, 464–466

setting element properties, 459–461

traversing parsed tree, 446–447

watching events while parsing, 451–453

XML manipulation API via, 445

xmlrpc.client module

binary data, 823–825

combining calls into one message, 826–827

connecting to server, 817–819

data types, 819–822

exception handling, 825–826

passing objects, 822–823

XML-RPC client library, 816–817

xmlrpc.server module

alternate API names, 829–830

arbitrary API names, 831–832

dispatching calls, 835–836

dotted API names, 830–831

exposing methods of objects, 832–834

introspection API, 837–839

simple server, 828–829

XML-RPC server, 827

Y

YAML, application configuration, 1371

Z

ZIP archive. See also zipimport module

accessing code from, 1345–1346

example, 1344–1345

finding modules in, 1345

retrieving source code from, 1346–1347

Zip files, read and write GNU. See gzip module

zip() function, itertools, 165, 171

zipfile module

appending to files, 518–519

creating new archives, 514–516

extracting archived files from archive, 514

limitations, 521

Python ZIP archives, 519–521

reading metadata from archive, 512–514

testing ZIP files, 512

using alternative archive member names, 516

writing data from sources other than files, 517

writing with ZipInfo instance, 517–518

ZIP archive access with, 511

zipimport module

accessing code from ZIP archive, 1345–1346

changes in Python 3, 1365

determining if package or regular module, 1348

example, 1344–1345

finding modules inside ZIP archive, 1345

finding non-code data, 1348–1350

overview of, 1329, 1344

retrieving source code from ZIP archive, 1346–1347

ZipInfo instance, zipfile, 517–518

zip_longest() function, itertools, 165

zlib module

checksums, 481–482

compressing network data, 482–486

GNU zlib compression via, 477

GZipFile vs., 490

incremental compression/decompression, 479–480

mixed content streams, 480–481

working with data in memory, 477–479

Fair Use Source: B072QZZDV7

python_3_standard_library_by_example_index.txt · Last modified: 2020/11/20 01:12 by 127.0.0.1