Real World Python A Hacker s Guide to Solving Problems with Code 1st Edition Lee Vaughan - Download the ebook today and own the complete content
Real World Python A Hacker s Guide to Solving Problems with Code 1st Edition Lee Vaughan - Download the ebook today and own the complete content
com
https://textbookfull.com/product/real-world-python-a-hacker-
s-guide-to-solving-problems-with-code-1st-edition-lee-
vaughan/
OR CLICK HERE
DOWLOAD EBOOK
https://textbookfull.com/product/statistics-with-r-solving-problems-
using-real-world-data-1st-edition-jenine-k-harris/
textbookfull.com
https://textbookfull.com/product/optimization-in-the-real-world-
toward-solving-real-world-optimization-problems-1st-edition-katsuki-
fujisawa/
textbookfull.com
https://textbookfull.com/product/impractical-python-projects-playful-
programming-activities-to-make-you-smarter-1st-edition-lee-vaughan/
textbookfull.com
https://textbookfull.com/product/impractical-python-projects-playful-
programming-activities-to-make-you-smarter-1st-edition-lee-vaughan-2/
textbookfull.com
Quantum Computing Solutions: Solving Real-World Problems
Using Quantum Computing and Algorithms 1st Edition Bhagvan
Kommadi
https://textbookfull.com/product/quantum-computing-solutions-solving-
real-world-problems-using-quantum-computing-and-algorithms-1st-
edition-bhagvan-kommadi/
textbookfull.com
https://textbookfull.com/product/begin-to-code-with-python-rob-miles/
textbookfull.com
https://textbookfull.com/product/math-adventures-with-python-an-
illustrated-guide-to-exploring-math-with-code-1st-edition-peter-
farrell/
textbookfull.com
LEE VAUGHAN
REAL-WORLD PYTHON
REAL-WORLD
PYTHON
A Hacker’s Guide to
Solving Problems with Code
by Lee Vaughan
San Francisco
REAL-WORLD PYTHON. Copyright © 2021 by Lee Vaughan.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
The following images are reproduced with permission: Figure 3-3 from istockphoto.com; Figure 5-1 courtesy
of Lowell Observatory Archives; Figures 5-2, 6-2, 7-6, 7-7, 8-18, and 11-2 courtesy of Wikimedia Commons;
Figures 7-2, 7-9, 7-17, 8-20, and 11-1 courtesy of NASA; Figure 8-1 photo by Evan Clark; Figure 8-4 photo by
author; Figure 9-5 from pixelsquid.com; Figure 11-9 photo by Hannah Vaughan
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1-415-863-9900; info@nostarch.com
www.nostarch.com
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other prod-
uct and company names mentioned herein may be the trademarks of their respective owners. Rather than use
a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial
fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution
has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any
liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or
indirectly by the information contained in it.
For my uncle, Kenneth P. Vaughan.
He brightened every room he entered.
About the Author
Lee Vaughan is a programmer, pop culture enthusiast, educator, and author
of Impractical Python Projects (No Starch Press, 2018). As an executive-level
scientist at ExxonMobil, he constructed and reviewed computer models,
developed and tested software, and trained geoscientists and engineers.
He wrote both Impractical Python Projects and Real-World Python to help
self-learners hone their Python skills and have fun doing it!
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
CONTE NT S IN DE TA IL
ACKNOWLEDGMENTS xvii
INTRODUCTION xix
Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
Why Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
Python Version, Platform, and IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii
Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii
Running Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Using a Virtual Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Onward! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
1
SAVING SHIPWRECKED SAILORS WITH BAYES’ RULE 1
Bayes’ Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Project #1: Search and Rescue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Installing the Python Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The Bayes Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Playing the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Challenge Project: Smarter Searches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Challenge Project: Finding the Best Strategy with MCS . . . . . . . . . . . . . . . . . . . . . . . . 25
Challenge Project: Calculating the Probability of Detection . . . . . . . . . . . . . . . . . . . . . . 25
2
ATTRIBUTING AUTHORSHIP WITH STYLOMETRY 27
Project #2: The Hound, The War, and The Lost World . . . . . . . . . . . . . . . . . . . . . . . . . 28
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Installing NLTK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
The Corpora . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
The Stylometry Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Practice Project: Hunting the Hound with Dispersion . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Practice Project: Punctuation Heatmap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Challenge Project: Fixing Frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3
SUMMARIZING SPEECHES WITH NATURAL
LANGUAGE PROCESSING 51
Project #3: I Have a Dream . . . to Summarize Speeches! . . . . . . . . . . . . . . . . . . . . . . 52
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Web Scraping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
The “I Have a Dream” Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Project #4: Summarizing Speeches with gensim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Installing gensim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
The Make Your Bed Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Project #5: Summarizing Text with Word Clouds . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
The Word Cloud and PIL Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
The Word Cloud Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Fine-Tuning the Word Cloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Challenge Project: Game Night . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Challenge Project: Summarizing Summaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Challenge Project: Summarizing a Novel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Challenge Project: It’s Not Just What You Say,
It’s How You Say It! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4
SENDING SUPER-SECRET MESSAGES WITH A BOOK CIPHER 77
The One-Time Pad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
The Rebecca Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Project #6: The Digital Key to Rebecca . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
The Encryption Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Sending Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Practice Project: Charting the Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Practice Project: Sending Secrets the WWII Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
5
FINDING PLUTO 95
Project #7: Replicating a Blink Comparator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
The Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
The Blink Comparator Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Using the Blink Comparator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Project #8: Detecting Astronomical Transients with Image Differencing . . . . . . . . . . . . 112
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
The Transient Detector Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Using the Transient Detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Practice Project: Plotting the Orbital Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
xii Contents in Detail
Practice Project: What’s the Difference? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Challenge Project: Counting Stars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
6
WINNING THE MOON RACE WITH APOLLO 8 123
Understanding the Apollo 8 Mission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
The Free Return Trajectory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
The Three-Body Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Project #9: To the Moon with Apollo 8! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Using the turtle Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
The Apollo 8 Free Return Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Running the Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Practice Project: Simulating a Search Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Practice Project: Start Me Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Practice Project: Shut Me Down! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Challenge Project: True-Scale Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Challenge Project: The Real Apollo 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
7
SELECTING MARTIAN LANDING SITES 151
How to Land on Mars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
The MOLA Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Project #10: Selecting Martian Landing Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
The Site Selector Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Practice Project: Confirming That Drawings Become Part of an Image . . . . . . . . . . . . . 172
Practice Project: Extracting an Elevation Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Practice Project: Plotting in 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Practice Project: Mixing Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Challenge Project: Making It Three in a Row . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Challenge Project: Wrapping Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
8
DETECTING DISTANT EXOPLANETS 177
Transit Photometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Project #11: Simulating an Exoplanet Transit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
The Transit Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Experimenting with Transit Photometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Project #12: Imaging Exoplanets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
The Pixelator Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Contents in Detail xiii
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Practice Project: Detecting Alien Megastructures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Practice Project: Detecting Asteroid Transits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Practice Project: Incorporating Limb Darkening . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Practice Project: Detecting Starspots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Practice Project: Detecting an Alien Armada . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Practice Project: Detecting a Planet with a Moon . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Practice Project: Measuring the Length of an Exoplanet’s Day . . . . . . . . . . . . . . . . . . . 201
Challenge Project: Generating a Dynamic Light Curve . . . . . . . . . . . . . . . . . . . . . . . . 202
9
IDENTIFYING FRIEND OR FOE 203
Detecting Faces in Photographs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Project #13: Programming a Robot Sentry Gun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Detecting Faces from a Video Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Practice Project: Blurring Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Challenge Project: Detecting Cat Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
10
RESTRICTING ACCESS WITH FACE RECOGNITION 225
Recognizing Faces with Local Binary Pattern Histograms . . . . . . . . . . . . . . . . . . . . . . 226
The Face Recognition Flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Extracting Local Binary Pattern Histograms . . . . . . . . . . . . . . . . . . . . . . . . . 228
Project #14: Restricting Access to the Alien Artifact . . . . . . . . . . . . . . . . . . . . . . . . . . 231
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Supporting Modules and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
The Video Capture Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
The Face Trainer Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
The Face Predictor Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Challenge Project: Adding a Password and Video Capture . . . . . . . . . . . . . . . . . . . . 242
Challenge Project: Look-Alikes and Twins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Challenge Project: Time Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
11
CREATING AN INTERACTIVE ZOMBIE ESCAPE MAP 245
Project #15: Visualizing Population Density with a Choropleth Map . . . . . . . . . . . . . . 246
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
The Python Data Analysis Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
The bokeh and holoviews Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Installing pandas, bokeh, and holoviews . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
xiv Contents in Detail
Accessing the County, State, Unemployment, and Population Data . . . . . . . . . 250
Hacking holoviews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
The Choropleth Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Planning the Escape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Challenge Project: Mapping US Population Change . . . . . . . . . . . . . . . . . . . . . . . . . 266
12
ARE WE LIVING IN A COMPUTER SIMULATION? 269
Project #16: Life, the Universe, and Yertle’s Pond . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
The Pond Simulation Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Implications of the Pond Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Measuring the Cost of Crossing the Lattice . . . . . . . . . . . . . . . . . . . . . . . . . 275
Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
The Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Moving On . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Challenge Project: Finding a Safe Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Challenge Project: Here Comes the Sun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Challenge Project: Seeing Through a Dog’s Eyes . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Challenge Project: Customized Word Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Challenge Project: Simplifying a Celebration Slideshow . . . . . . . . . . . . . . . . . . . . . . . 281
Challenge Project: What a Tangled Web We Weave . . . . . . . . . . . . . . . . . . . . . . . . 281
Challenge Project: Go Tell It on the Mountain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
APPENDIX
PRACTICE PROJECT SOLUTIONS 283
Chapter 2: Attributing Authorship with Stylometry . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Hunting the Hound with Dispersion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Punctuation Heatmap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Chapter 4: Sending Super-Secret Messages with a Book Cipher . . . . . . . . . . . . . . . . . 285
Charting the Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Sending Secrets the WWII Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Chapter 5: Finding Pluto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Plotting the Orbital Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
What’s the Difference? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Chapter 6: Winning the Moon Race with Apollo 8 . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Simulating a Search Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Start Me Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Shut Me Down! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Chapter 7: Selecting Martian Landing Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Confirming That Drawings Become Part of an Image . . . . . . . . . . . . . . . . . . 298
Extracting an Elevation Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Plotting in 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Mixing Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Chapter 8: Detecting Distant Exoplanets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Detecting Alien Megastructures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Detecting Asteroid Transits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Contents in Detail xv
Incorporating Limb Darkening . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Detecting an Alien Armada . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Detecting a Planet with a Moon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Measuring the Length of an Exoplanet’s Day . . . . . . . . . . . . . . . . . . . . . . . . 311
Chapter 9: Identifying Friend or Foe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Blurring Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Chapter 10: Restricting Access with Face Recognition . . . . . . . . . . . . . . . . . . . . . . . . 312
Challenge Project: Adding a Password and Video Capture . . . . . . . . . . . . . . 312
INDEX 315
xvi Contents in Detail
ACKNOWLEDGMENTS
Why Python?
Python is a high-level, interpretive, general-purpose programming language.
It’s free, highly interactive, and portable across all major platforms and micro-
controllers such as the Raspberry Pi. Python supports both functional and
object-oriented programming and can interact with code written in many
other programming languages, such as C++.
Because Python is accessible to beginners and useful to experts, it has
penetrated schools, universities, large corporations, financial institutions,
and most, if not all, fields of science. As a result, it’s now the most popular
language for machine learning, data science, and artificial intelligence
applications.
xx Introduction
cool display for advertising or promotional material. Gain experience
with BeautifulSoup, Requests, regex, NLTK, Collections, wordcloud, and
matplotlib.
Chapter 4: Sending Super-Secret Messages with a Book Cipher Share
unbreakable ciphers with your friends by digitally reproducing the one-
time pad approach used in Ken Follet’s best-selling spy novel, The Key to
Rebecca. Gain experience with the Collections module.
Chapter 5: Finding Pluto Reproduce the blink comparator device
used by Clyde Tombaugh to discover Pluto in 1930. Then use modern
computer vision techniques to automatically find and track subtle tran-
sients, such as comets and asteroids, moving against a starfield. Gain
experience with OpenCV and NumPy.
Chapter 6: Winning the Moon Race with Apollo 8 Take the gamble
and help America win the moon race with Apollo 8. Plot and execute
the clever free return flight path that convinced NASA to go to the moon
a year early and effectively killed the Soviet space program. Gain expe-
rience using the turtle module.
Chapter 7: Selecting Martian Landing Sites Scope out potential land-
ing sites for a Mars lander based on realistic mission objectives. Display
the candidate sites on a Mars map, along with a summary of site statis-
tics. Gain experience with OpenCV, the Python Imaging Library, NumPy,
and tkinter.
Chapter 8: Detecting Distant Exoplanets Simulate an exoplanet’s
passing before its sun, plot the resulting changes in relative brightness,
and estimate the diameter of the planet. Finish by simulating the direct
observation of an exoplanet by the new James Webb Space Telescope,
including estimating the length of the planet’s day. Use OpenCV, NumPy,
and matplotlib.
Chapter 9: Identifying Friend or Foe Program a robot sentry gun
to visually distinguish between Space Force Marines and evil mutants.
Gain experience with OpenCV, NumPy, playsound, pyttsxw, and datetime.
Chapter 10: Restricting Access with Face Recognition Restrict access
to a secure lab using face recognition. Use OpenCV, NumPy, playsound,
pyttsxw, and datetime.
Chapter 11: Creating an Interactive Zombie Escape Map Build a pop-
ulation density map to help the survivors in the TV show The Walking Dead
escape Atlanta for the safety of the American West. Gain experience
with pandas, bokeh, holoviews, and webbrowser.
Chapter 12: Are We Living in a Computer Simulation? Identify a way
for simulated beings—perhaps us—to find evidence that they’re living
in a computer simulation. Use turtle, statistics, and perf_counter.
Each chapter ends with at least one practice or challenge project. You
can find solutions to the practice projects in the appendix or online. These
aren’t the only solutions, or necessarily the best ones; you may come up with
better ones on your own.
Introduction xxi
When it comes to the challenge projects, however, you’re on your own.
It’s sink or swim, which is a great way to learn! My hope is that this book
motivates you to create new projects, so think of the challenge projects as
seeds for the fertile ground of your own imagination.
You can download all of the book’s code, including solutions to the
practice projects, from the book’s website at https://nostarch.com/real-world
-python/. You’ll also find the errata sheet there, along with any other updates.
It’s almost impossible to write a book like this without some initial errors.
If you see a problem, please pass it on to the publisher at errata@nostarch.com.
We’ll add any necessary corrections to the errata and include the fix in future
printings of the book, and you will gain eternal glory.
Installing Python
You can choose to install Python directly on your machine or through a
distribution. To install directly, find the installation instructions for your
operating system at https://www.python.org/downloads/. Linux and macOS
machines usually come with Python preinstalled, but you may want to upgrade
this installation. With each new Python release, some features are added and
some are deprecated, so I recommend upgrading if your version predates
Python v3.6.
The download button on the Python site (Figure 1) may install 32-bit
Python by default.
xxii Introduction
Figure 1: Downloads page for Python.org, with the “easy button” for the Windows platform
If you want the 64-bit version, scroll down to the listing of specific
releases (Figure 2) and click the link with the same version number.
Clicking the specific release will take you to the screen shown in
Figure 3. From here, click the 64-bit executable installer, which will launch
an installation wizard. Follow the wizard directions and take the default
suggestions.
Introduction xxiii
Some of the projects in this book call for nonstandard packages that
you’ll need to install individually. This isn’t difficult, but you can make
things easier by installing a Python distribution that efficiently loads and
manages hundreds of Python packages. Think of this as one-stop shopping.
The package managers in these distributions will automatically find and
download the latest version of a package, including all of its dependencies.
Anaconda is a popular free distribution of Python provided by
Continuum Analytics. You can download it from https://www.anaconda.com/.
Another is Enthought Canopy, though only the basic version is free. You
can find it at https://www.enthought.com/product/canopy/. Whether you install
Python and its packages individually or through a distribution, you should
encounter no problems working through the projects in the book.
Running Python
After installation, Python should show up in your operating system’s list of
applications. When you launch it, the shell window should appear (shown
in the background of Figure 4). You can use this interactive environment
to run and test code snippets. But to write larger programs, you’ll use a text
editor, which lets you save your code, as shown in Figure 4 (foreground).
Figure 4: The native Python shell window (background) and text editor (foreground)
To create a new file in the IDLE text editor, click File4New File.
To open an existing file, click File4 Open or File4Recent Files. From
here, you can run your code by clicking Run4Run Module or by pressing
F5 after clicking in the editor window. Note that your environment may
look different from Figure 4 if you chose to use a package manager like
Anaconda or an IDE like PyCharm.
xxiv Introduction
You can also start a Python program by typing the program name in
PowerShell or Terminal. You’ll need to be in the directory where your Python
program is located. For example, if you didn’t launch the Windows PowerShell
from the proper directory, you’ll need to change the directory path using
the cd command (Figure 5).
Figure 5: Changing directories and running a Python program in the Windows PowerShell
Onward!
Many of the projects in this book rely on statistical and scientific concepts
that are hundreds of years old but impractical to apply by hand. But with
the introduction of the personal computer in 1975, our ability to store, pro-
cess, and share information has increased by many orders of magnitude.
In the 200,000-year history of modern humans, only those of us living
in the last 45 years have had the privilege of using this magical device and
realizing dreams long out of reach. To quote Shakespeare, “We few. We
happy few.”
Let’s make the most of the opportunity. In the pages that follow, you’ll
easily accomplish tasks that frustrated past geniuses. You’ll scratch the surface
of some of the amazing feats we’ve recently achieved. And you might even
start to imagine discoveries yet to come.
Introduction xxv
1
SAVING SHIPWRECKED SAILORS
W I T H B AY E S’ R U L E
"And Tomlinson?" continued the latter. "Suppose he's still there, but
I haven't heard from him recently. A short, very dark-featured old
bean, with a very dry sense of humour. Plays 'pack and brag' every
available five minutes, and uses most atrocious language when he's
put out and when he isn't."
CHAPTER IX
HOW THE LIGHTERS FARED
It was one of the jobs that fall to all branches of the Navy. With a
strange crew, and not having navigated a lighter before, McIntosh
was taking on "some stunt." He had charts and navigating
instruments, but he would have felt easier in his mind had he
possessed "local knowledge" of this part of the coast. On an M.L.,
where he was under a competent officer, navigation was fairly simple
as far as the Sub was concerned; but now the whole responsibility of
getting his charges safely into port rested on his shoulders.
It was the morning of von Preussen's visit to Auldhaig. The fog had
dispersed. In its wake had sprung up a fresh southerly breeze, which
in turn gave indications of decreasing in velocity before noon.
Making his way aft to the rough wooden hut that served as a
wheel-house, the Sub gave the signal to the engine-room staff to
"stand by."
"Take her," he ordered, handing over the wheel. "Keep her as she
is: south a half west."
"South a half west it is, sir," replied the man in the time-honoured
formula of the sea.
No. 6 was coming along well. The "bone in her teeth" glistened
white as she pushed her snub nose through the waves. Both craft
were "taking it green" as the water flowed over the tarpaulined
hatches and surged along the broad waterways.
"We'll carry our tide for another hour," he said to himself. "Then it'll
be a slow job. One thing, we can't have every blessed thing in life,
but I hope to goodness nothing goes wrong."
"Yes, sir," was the reply. "Will you be altering course a point or so,
sir? There's a nasty set of the tide inshore about these parts."
"Yes," decided the Sub, and gave the necessary instructions to the
helmsman.
"Get a nun-buoy ready to veer astern," he continued, "and signal to
No. 6 to keep the thing dose under her bows. If she doesn't, we'll be
losing each other."
"Says, sir, she's overheated her bearings. She'll have to stop or her
engines'll seize up."
It was exactly what the Sub was anticipating, and now trouble had
come he met it promptly and resolutely.
While the men were engaged in bringing one end of the hawser to
the after "towing-bitts," McIntosh took the helm and began to run to
starboard in order to close with the disabled lighter. He was working
against time, for already the mist was upon them—the outflung
tentacles of a bank of fog. With a range of visibility of three or four
hundred yards, matters were somewhat complicated, but the
manoeuvre of establishing communication with the helpless craft
would be rendered fourfold difficult, should the baffling fog envelop
the two boats.
"All ready with the heaving-line?" shouted the Sub.
Slowly, even for the low-speed lighter, McIntosh, made for the
disabled vessel, which was now lying broadside on to the fairly
confused sea. The Sub was cautious. Strange to the boat, he knew
that there was a vast difference between the manoeuvring
capabilities of an M.L. and a lighter, and with that fact in mind he
displayed an excess of caution.
There seemed no doubt about it: No. 5 was sinking. Had she been
struck anywhere but right aft, her heavy rubbing-strake would have
saved her. As it was she had been hit in a vital spot—her engine-
room.
As luck would have it, both lighters drifted together, their metal-
bound sides grinding and bumping in the agitated waves. Since No.
5 was evidently sinking, the only refuge for her crew was the deck of
disabled No. 6.
Waiting till the last, the Sub snatched up his confidential papers,
thrust them into the pocket of his oilskins, and, as the two lighters
rolled heavily together, he made a flying leap for the deck of No. 6.
He was not a moment too soon. At the next roll there was a gap of
five or six yards between the two vessels. Separated by a freak eddy
of the tidal stream, they increased their distance more and more,
until the holed lighter, with her stern level with the water, was lost to
sight in the fog.
CHAPTER X
THE SALVAGE SYNDICATE
Lunch was over at Auldhaig Air Station. Most of the officers had
drifted in twos and threes into the ante-room to seize the
opportunity of enjoying a smoke before falling in on parade. The
second-in-command and Captain Cumberleigh found themselves
alone.
"I may be mistaken, sir," replied Cumberleigh, "but I'm not at all
sure about that fellow Fennelburt."
"There are some very queer cusses in the Service, I'll admit,"
remarked Major Sparrowhawk thoughtfully. "Getting a commission in
war time isn't the same as in normal times. The chap may be pulling
your leg, Cumberleigh. But why did you pal up to him and promise
to take him to the theatre and all that?"
"But what aroused your suspicions in the first instance?" asked the
second-in-command.
The Pip-squeak was no chicken, nor did she lay claim to beauty.
Bluff-bowed, and with an almost entire lack of sheer, she had one
compensating quality: she was as stiff as a house.
"Fresh enough," he observed, "but, old son, pity you didn't devote
your energies to the worms instead of wasting your time pulling bits
of glass out of your boots. These won't last any time."
"Get a move on, you lazy hogs!" exclaimed Jefferson. "We're close
on the right spot. Down with the canvas! Blenkinson, stand by to let
go the anchor."
Quickly the anchor was broken out and the sail hoisted.
Cumberleigh, who had been silently keeping the derelict under
observation, suddenly turned and thumped von Preussen on the
shoulder.
It took ten minutes for the Pip-squeak to close with X-lighter No. 5.
Running up into the wind on the lee side, Jefferson got way off the
boat.
"How about it, you fellows?" he inquired. "Think it's safe to run
alongside?"
"No, but we can send for a tug," said Cumberleigh. "Jefferson can
sail back to Auldhaig in about an hour even if he doesn't fall in with
a tug or even an M.L. on the way."
"I agree," added Blenkinson. "After all's said and done, we don't
stand a chance of getting anything out of the deal. And what
matters if the old tub does sink? Her value is but a mere fleabite out
of six millions a day."
CHAPTER XI
VON PREUGFELD'S RESOLVE
Furst shuddered.
"There was some in that Dutch vessel we burnt a week ago," said
Krauss.
"We'll be back again on Friday if all goes well," said the other.
"Then we can enjoy ourselves."
"Yes, I know," snarled Krauss. "Just before the war I came back
from America on the George Washington with eight hundred and
fifty marks to my name. I was going to buy a small business in
Bremen and settle down to a life ashore. I should have done well.
Then came the war. The rascally swindlers told us that if we lent our
money to the State it would be repaid with twenty-five per cent.
when peace was proclaimed. Just imagine! I handed over my eight
hundred marks in silver, fool that I was! Even supposing the
government does pay me back a thousand marks, it will be in rotten
paper money, and I know that five thousand now will not buy the
place I had offered to me for eight hundred and fifty four years
ago."
"There will be trouble," agreed Furst. "Do you know that there is a
movement amongst the men of the U-boats' crews to hoist the Red
Flag?"
"Have I not heard of it!" exclaimed Kaspar grimly. "And when the
time comes here is one who will jump at the opportunity. Now, at
——"
"Empty two and four tanks," came a guttural order through a voice
tube. "And be quick about it, you numskulls!"
U 247 was preparing to rise to the surface in order to verify her
position. For several hours she had rested on the bottom, scared by
the presence of a swarm of destroyers and M.L.'s which had hurried
to avenge the bombardment of Aberspey.
"Of course it is," snapped von Preugfeld. "Any fool could see that.
What I want to know is: what sort of craft is it? Stand aside if you
cannot do better than that."
The U-boat shuddered under the beats of the twin screws, then
forging slowly ahead approached the puzzling object.
"Stop!"
"They have not observed us," muttered von Preugfeld with fervent
gratitude to the providence that looks after Hun submarines.
"There's 'X 5' painted on her bows. Know what that means, Eitel?"
"It would be well to clear out and leave him alone, Herr Kapitan,"
suggested von Loringhoven. "It could only be that von Preussen is
engaged in highly important confidential work that brings him afloat
again. Himmel! He is a clever fellow."
Bells clanged, valves hissed and pumps grated, men hurried to and
fro in execution of loud-voiced orders.
Von Preugfeld turned to his unter-leutnant.
"Jefferson!" he shouted. "Run for it, man. Don't wait for us."
The owner of the Pip-squeak took in the situation at a glance. True,
the U-boat was between him and the shore, but there was a stiff
leading wind. While the Hun was concentrating his attention upon
the X-lighter the sailing-boat had a fair chance of getting away, but
Jefferson was a "white man."
"No fear, old bird!" he shouted. "We're all in this stunt. I am coming
on board."
With that he ran the sailing-boat alongside the barge, and, without
waiting to lower the sail, leapt on deck and secured the painter.
Meanwhile the hatches of the U-boat had been thrown open and
her two guns manned and trained point-blank upon the helpless
lighter.
Of the five, "Captain Fennelburt" was the least perturbed. The spy
was distinctly annoyed at the unexpected turn of events. It looked
as if his carefully prepared campaign was to be nipped in the bud.
Consequently he was liable to heavy financial loss in addition to a
waste of valuable time, for his employers in Berlin paid only for
definite results. "No work, no pay," was the motto of the German
Secret Service, and before von Preussen could be landed in Great
Britain again weeks might elapse. As a secondary consideration,
there was the doubt of how he would be received by his
compatriots. For very good reasons he wished to conceal his identity
from his companions on the lighter. In spite of strenuous
precautions, British prisoners of war sometimes contrived to effect
their escape, and it would be a very serious matter for von Preussen
if it became known through the medium of a former captive in
Germany that the soi-disant Captain Fennelburt was a Secret Service
agent of the German Intelligence Department.
"Gentlemen!" observed Pyecroft facetiously. "The R.A.F. Salvage
Syndicate is dissolved."
With her guns still trained upon the lighter, U 247 approached
slowly and with evident hesitation. At the back of von Preugfeld's
mind lurked the haunting suspicion that X 5 was a snare. The very
temptingness of the bait increased his suspicions. Perhaps a British
submarine was lying in wait to blow him and his U-boat to atoms; or
somewhere in the clouds a coastal airship was floating motionless,
awaiting an opportunity to swoop down and let loose an aerial
torpedo before the Germans had time to close hatches and
submerge.
On the other hand, there was von Preussen, clad in a British R.A.F.
uniform and standing seemingly unconcerned upon the lighter's
deck. Surely, if there were a trap, the Hun would contrive to make a
mute signal to his compatriots.
Von Preussen gave none. He was content to let events take their
course.
"Look you pointed about it!" he exclaimed angrily. "I you give half a
minute to quit der boat."
"Please yourself, old man," replied the captain. "And the very best
of luck."
The four stepped into the Pip-squeak. Her sail was hurriedly
stowed, and under oars the boat approached the submarine.
"These three in No. 3 store-room; this one will go aft. You, there,"
he added, addressing another seaman. "Take an axe and knock out
the garboards of that boat."
Giving another glance skywards and all around the horizon, von
Preugfeld walked aft to the hatchway through which von Preussen
had disappeared. "I'll see you in the ward-room in less than five
minutes, von Preussen," he said. "Apparently this affair requires an
explanation. But what has become of the fourth Englishman?"
"I hardly expected to see you so soon, Karl," he began. "I hope I
haven't disturbed your elaborate plans."
"Himmel! How is that? Were you taken into the confidence of these
English officers, and were your investigations a secret project that
was being experimented upon to the disadvantage of the
Fatherland?"
"No; but a man cannot get about in comfort without his travelling
belongings," remarked the spy. "You will have to land me again, but
my venture in the Auldhaig district is a failure. It means that I must
make my way south and try my luck in Dover and Portsmouth. And I
was getting on so nicely with those fellows at the air station," he
added, little knowing to what purpose the hospitality had been
extended.
"And almost immediately you have undone all the work required of
you in the matter," added the spy.
"And more than likely stumble across some of the officers and men
from Auldhaig Air Station," objected the spy. "No, my friend, I prefer
to lay my own plans; then, if anything does go wrong, I have only
myself to blame. And since Captain George Fennelburt is either a
prisoner of war or 'missing—presumed drowned,' I must needs beg,
borrow or steal another name. Henceforth, until further notice, I am
Captain Broadstone, also of the Royal Air Force. Will you oblige me
by lending me a pen? There are certain forms which I must now fill
in to bear out my new character."
CHAPTER XIII
A STRUGGLE FOR LIFE
The shock of the icy-cold water almost took his breath away.
"By gosh!" he muttered. "It is a bit of a stinger. But cheer up, old
son, you may get it pretty hot in a very short time."
With that he dived under the lighter's hull. Literally groping his way
down the weed and barnacle-covered bottom, he scraped under the
keel and up again on the other side until darkness gave place to a
glint of pale green water that in turn gave place to the salt-laden air.
He had now placed the hull of No. 5 between him and the U-boat.
So far so good, but the late member of the R.A.F. Salvage Syndicate
had to consider another pressing problem.
Even supposing, as he fondly hoped, that the Huns had not noticed
him, it was logical to assume that they would not sheer off before
sending the lighter to Davy Jones's locker. How? By ramming?
Hardly. A U-boat would not hesitate to crash into a ship's boat
deeply laden with the survivors of a torpedoed merchantman, but
she would think twice before trying conclusions with the lighter's
massive rubbing-strake. By placing bombs on board? That meant
making use of a boat and consequently delay. Gunfire? Yes; that
looked like the answer to the question.
Now for the subsidiary problem. Assuming that the Huns would
turn a quick-firer upon the lighter, where would they aim? At the
engine-room? Hardly, as the stern was already awash. Amidships,
into the heavily-laden hold, the work of destruction would be most
easily accomplished.
"So here's for her bows," decided Pyecroft, having reviewed the
situation. "If my theories are all wrong, then it's a case of 'going
west.'" He swam with slow, easy strokes towards the bows. There
was no immediate hurry, since the boat with his companions had not
yet reached the pirate submarine. He knew that he had to conserve
his strength and his energies for the ordeal that promised to be
forthcoming.
To his great delight, he found a rope trailing overboard. A tug
reassured him that it was made fast to the towing bollards. By
hanging on to it Pyecroft could support himself with ease, while the
bluff, overhanging bows would effectually screen him should any of
the Huns board the abandoned craft.
Presently the outlines of the U-boat that had been responsible for
Pyecroft's predicament became visible. She was slowly forging
ahead. Her deck was deserted. She was preparing to submerge.
His boots were likewise ruthlessly sacrificed. Then, quitting his hold
of the basket, he struck out towards the derelict boat. A few strokes
convinced him that the overhand method of swimming has its
disadvantages when hampered with sodden clothing. The breast
stroke, he found, required comparatively little effort, yet by the time
he covered that hundred yards he felt that he had reached the limit
of his prowess in the swimming line.
It was hard work. At the best the water-logged craft made a bare
mile an hour, but the effect of the heavy toil was to bring warmth to
the man's chilled body and limbs. Setting his jaw tightly, he held on,
glancing from time to time over his shoulder in the direction of the
cliffs, now growing dim in the dusk of approaching night.
"How much further?" he asked himself at the end of two hours.
"Hanged if they seem any nearer. Wind and tide are with me, too."
Compared with flying through the air at a hundred and fifty miles
an hour, his present rate of progression was indeed painfully slow,
yet with the dogged determination of an Englishman, "never to say
die till you're dead," he tugged at the heavy oars until his blistered
hands grew raw and his muscles ached as if his back would break.
With night the wind dropped and the sea assumed a placid, oily
aspect. The land was now invisible, for not a light could be seen
from seaward. Fortunate it was that the young airman had been
compelled to undergo a course of astronomy. He hated it at the
time; now he was glad, for by keeping the North Star broad on his
starboard beam, he knew that he was heading towards the shores of
Scotland.
His task was stupendous. The drag of the boat, which contained
more than a ton of the North Sea, was terrific. He was wearing
badly. Cold, hunger and fatigue were telling. Almost mechanically he
swotted at the heavy oars.
He had lost all count of time, when he heard a faint rumble. It was
the surf lashing the beach. Encouraged, yet realising that other
dangers lurked on that surf-beaten shore, he rallied his remaining
energies, counting each stroke as he bent to the oars.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com