No description
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
# Serious Python
## 【One-Line Pitch】
A practical, battle-tested guide for intermediate Python developers who want to write cleaner, more maintainable, and more scalable code—drawing on lessons from large-scale open-source projects like OpenStack. If you've outgrown tutorials and want to level up to professional-grade Python, this is your book.
## 【Book Arc】
- **Opening (~0%–10%)**: The author sets the stage by sharing his journey from "garage project" coding to contributing to OpenStack, a 9-million-line Python codebase. He covers Python version selection (target 3.7, forget Python 2 unless doing archaeology) and project layout fundamentals, including versioning conventions like the `.postN` suffix.
- **Early (~10%–23%)**: Dives into Python's internals—the `sys` module, the `__import__` function for dynamic imports, and custom module loaders (using the Hy language as an example). Includes an interview-style discussion on Python's strengths (simplicity, flexibility) and weaknesses (GIL, lambda limitations, pip's dependency resolver).
- **Early (~23%–32%)**: Shifts to documentation and API design. Covers Sphinx automation, the `autodoc` extension, writing custom Sphinx extensions (like `sphinxcontrib-pecanwsme`), and deprecation patterns using the `warnings` module.
- **Middle (~39%–48%)**: Focuses on practical libraries and packaging. Covers time-zone-aware timestamps with `dateutil` (including handling ambiguous DST times with the `fold` attribute from PEP 495), and packaging with Wheel—including universal wheels for Python 2/3 compatibility and uploading to PyPI.
- **Middle (~48%–end)**: Continues with advanced packaging topics, including running Python from zip archives (like Java's `.jar` files) and the nuances of Wheel naming conventions for different architectures and Python implementations.
## 【Key Takeaways】
- **Python version strategy matters** (Early): Target Python 3.7 (or 3.6 if your OS ships it), and don't support Python 2.7 in new programs—it's end-of-life. The book was written with Python 3 in mind, and version selection is the first architectural decision you'll make.
- **Dynamic imports are a hidden power** (Early): The `__import__` function lets you import modules whose names aren't known until runtime—useful for plugin systems and dynamic configuration. Modules are just objects, and `sys.modules` gives you a dictionary of everything currently loaded.
- **Custom module loaders unlock new syntax** (Early): The Hy language example shows how you can write a loader that compiles `.hy` files to Python—a pattern for embedding other languages or DSLs in Python projects.
- **Documentation should be automated, not hand-maintained** (Early): Sphinx's `autodoc` extension extracts docstrings directly into documentation, and you can write custom extensions (like `sphinxcontrib-pecanwsme`) to generate docs from your code's structure. The rule: if you can extract it from code, automate it.
- **Deprecation is a documented process** (Early): Use the `warnings` module with `DeprecationWarning` and document changes in docstrings with the `.. deprecated::` directive. Note that Python 2.7+ hides warnings by default—use `-W all` to see them.
- **Time zones are a solved problem—use `dateutil`** (Middle): Don't reinvent time-zone handling. `dateutil.tz.gettz()` gives you `tzinfo` objects from the IANA database, and `is_ambiguous()` helps handle DST transitions. Python 3.6's `fold` attribute (PEP 495) lets you specify which side of a DST fold a timestamp is on.
- **Wheel packaging is the modern standard** (Middle): Wheel files have naming conventions that encode Python version, architecture, and implementation compatibility. Use `--universal` for Python 2/3-compatible packages, and understand how zip archives can run Python programs directly.
## 【Reading Tips】
- **Skim the interview sections** (~10%–23%): The author interviews other Python developers about their workflows. These are interesting but less actionable than the technical chapters—read them for perspective, not for code.
- **Deep-read the Sphinx chapter** (~23%–32%): If you maintain any Python library, the automation patterns here (autodoc, custom extensions, deprecation workflows) will save you hours. This is where the book's "serious" title earns its keep.
- **Pay attention to the packaging chapters** (Middle): Wheel naming conventions and zip-archive execution are easy to skim past but critical for anyone distributing Python software. The examples are concrete and worth following along with.
- **Don't skip the `dateutil` section** (Middle): Time-zone bugs are notoriously subtle. The DST ambiguity examples are the most practically valuable code in the book for everyday development.
- **Use the book as a reference, not a cover-to-cover read**: Each chapter is fairly self-contained. Jump to the topic you need (packaging, docs, time zones) rather than reading sequentially.
## 【Coverage Limits】
The excerpts cover roughly the first half of the book (through ~48%). Later chapters on testing, performance, the GIL, and concurrency (mentioned in the introduction) are not covered in this guide.
##
Page 10
ams—and build them more Figure 11: Python release timeline On top of that, we should take into consideration the Python 2 versus Python 3 problem. People wo...
View in text
Excerpt 2
le ensures compatibility between Python versions 2 and 3 by removing the u prefix from strings before compilation. USEFUL STANDARD LIBRARIES Python comes wit...
View in text
Excerpt 3
STControllerDirective) Listing 33: Code from sphinxcontrib.pecanwsme.rest.setup that adds the rest-controller directive The add_directive method in Listing ...
View in text
Excerpt 4
datetime(2017, 10, 29, 2, 30, tzinfo=localtz) >>> confusing.replace(fold=0).astime zone(utc) datetime.datetime(2017, 10, 29, 0, 30, tzinfo=tzutc()) >>> confu...
View in text
Excerpt 5
sed to all test functions requesting a database connection. Finally, you can run some common code before and after your tests by marking fixtures as automati...
View in text
Excerpt 6
e following set of functions that need to check whether the username they receive as an argument is the admin or not and, if the user is not an admin, raise ...
View in text
Excerpt 7
to function like coroutines seen in Lua and other languages. PEP 289 introduced generator expressions, making it possible to build oneline generators using ...
View in text
Excerpt 8
made and stored in hello_world. At last, we create an _ast.Module object ➍ to contain all this code as a list of one expression. We can compile _ast.Module ...
View in text
Tags
AI categories
PythonProgramming LanguageBackend
Text Preview (First 20 pages)
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Generating text preview…
Loading comments...
Reply to Comment
Edit Comment