Chapter 16 of 24 | Python
The standard library and ecosystem
The standard library provides batteries-included tools for common tasks. Before installing a dependency, check whether a standard module already solves the problem clearly and safely.
Useful modules
from collections import Counter
from pathlib import Path
import argparse
import json
words = Counter("red blue red".split())
config_path = Path("settings.json")
data = json.loads(config_path.read_text(encoding="utf-8"))
Examples include pathlib for paths, json and csv for data formats, argparse for command-line interfaces, logging for diagnostics, collections for specialized containers, and datetime for dates and times.
Iterators and itertools
from itertools import islice, pairwise
values = range(100)
first_five = list(islice(values, 5))
adjacent_pairs = list(pairwise([2, 4, 8, 16]))
Many standard-library tools consume iterables lazily. This can avoid building intermediate lists, but a generator is usually exhausted after one pass. Materialize it when you need repeated traversal or random access. Check whether a tool can be infinite and ensure consumers impose a bound when needed.
Dates, randomness, and logging
Use timezone-aware datetimes for real-world instants and define whether a timestamp is UTC or local time. Daylight-saving changes make local wall-clock arithmetic tricky. For elapsed-time measurement, use time.perf_counter(), not wall-clock time.
import logging
import time
logging.basicConfig(level=logging.INFO)
started = time.perf_counter()
logging.info("Task started")
elapsed = time.perf_counter() - started
Use the random module for simulation and non-security-critical randomness. For secrets such as tokens, use secrets. Logging should provide useful context without exposing passwords, personal information, or other sensitive values.
Third-party libraries
The Python Package Index (PyPI) hosts packages, but package availability does not guarantee quality or safety. Evaluate maintenance, documentation, license, supported versions, dependency tree, and security practices. Prefer established libraries for complex formats and cryptography rather than writing fragile substitutes.
Beginner-to-master progression
- Beginner: use common standard modules rather than reinventing basic tools.
- Practitioner: choose iterators, paths, logging, and date/time types appropriately.
- Advanced: understand lazy evaluation, locale and timezone pitfalls, module compatibility, dependency governance, and performance trade-offs.
Exercises
- Use
argparseto accept a path and a verbosity flag. - Count word frequencies with
Counterand process a large file line by line. - Measure elapsed time with
perf_counterand report it with logging. - Evaluate a third-party package using a written maintenance and security checklist.
Iterator protocols and composable tools
An iterable provides an iterator; an iterator returns one value at a time and eventually raises StopIteration. Many standard-library functions accept iterables and preserve laziness. map, filter, and functions in itertools can process large data without materializing intermediate lists. If a pipeline needs to be traversed twice, either recreate the iterator or store the results intentionally.
from itertools import chain, islice
first = (number for number in range(5))
second = (number for number in range(5, 10))
combined = chain(first, second)
print(list(islice(combined, 3))) # [0, 1, 2]
Logging configuration and library etiquette
Application entry points configure logging handlers and levels. A reusable library should generally create a logger with logging.getLogger(__name__) and avoid configuring the root logger, which belongs to the application. This separation lets host programs decide formatting, destination, and verbosity.
Use structured context such as request IDs or task identifiers when diagnosing concurrent systems. Avoid formatting sensitive data into log messages. Logging calls should be at appropriate levels and should not become a substitute for returning errors to a caller that must act on them.
Time zones and randomness in reliable systems
Use timezone-aware datetimes to represent instants, commonly normalized to UTC. A local wall-clock time may be ambiguous or nonexistent around daylight-saving transitions. A date without a time zone is a different concept from an instant. For benchmarking elapsed durations, use a monotonic performance counter; for security tokens, use secrets rather than a deterministic simulation PRNG.
Standard library versus external ecosystem
The standard library provides portability and reduces dependency management, but specialist tasks may have established third-party tools. For data science, numerical libraries can provide vectorized operations; for web services, frameworks can implement routing and protocol details. Evaluate correctness, maintenance, security, and compatibility, not just how short the sample code looks.
Mastery checkpoint
- Build a lazy pipeline that reads, filters, and aggregates a large file without holding all records in memory.
- Configure logging in an application while keeping a library module's logger configurable by its caller.
- Represent an instant and an elapsed duration with appropriate time APIs.
- Choose between a standard module and a third-party library and justify the maintenance trade-off.