80% found this document useful (5 votes)
60 views

(Ebooks PDF) Download Real World Python A Hacker S Guide To Solving Problems With Code 1st Edition Lee Vaughan Full Chapters

Python

Uploaded by

danurmozhui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
80% found this document useful (5 votes)
60 views

(Ebooks PDF) Download Real World Python A Hacker S Guide To Solving Problems With Code 1st Edition Lee Vaughan Full Chapters

Python

Uploaded by

danurmozhui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Download the full version of the textbook now at textbookfull.

com

Real World Python A Hacker s Guide to Solving


Problems with Code 1st Edition Lee Vaughan

https://textbookfull.com/product/real-world-
python-a-hacker-s-guide-to-solving-problems-with-
code-1st-edition-lee-vaughan/

Explore and download more textbook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Statistics With R Solving Problems Using Real World Data


1st Edition Jenine K Harris

https://textbookfull.com/product/statistics-with-r-solving-problems-
using-real-world-data-1st-edition-jenine-k-harris/

textbookfull.com

Optimization in the Real World Toward Solving Real World


Optimization Problems 1st Edition Katsuki Fujisawa

https://textbookfull.com/product/optimization-in-the-real-world-
toward-solving-real-world-optimization-problems-1st-edition-katsuki-
fujisawa/
textbookfull.com

Impractical Python Projects Playful Programming Activities


to Make You Smarter 1st Edition Lee Vaughan

https://textbookfull.com/product/impractical-python-projects-playful-
programming-activities-to-make-you-smarter-1st-edition-lee-vaughan/

textbookfull.com

Industrial Hygiene Control of Airborne Chemical Hazards,


Second Edition William Popendorf (Author)

https://textbookfull.com/product/industrial-hygiene-control-of-
airborne-chemical-hazards-second-edition-william-popendorf-author/

textbookfull.com
Understanding Ethics and Responsibilities in a Globalizing
World 1st Edition Maria Cecilia Coutinho De Arruda

https://textbookfull.com/product/understanding-ethics-and-
responsibilities-in-a-globalizing-world-1st-edition-maria-cecilia-
coutinho-de-arruda/
textbookfull.com

Emerging and Traditional Technologies for Safe Healthy and


Quality Food 1st Edition Viktor Nedovi■

https://textbookfull.com/product/emerging-and-traditional-
technologies-for-safe-healthy-and-quality-food-1st-edition-viktor-
nedovic/
textbookfull.com

Attention, balance and coordination : the A. B. C. of


learning success Second Edition Beuret

https://textbookfull.com/product/attention-balance-and-coordination-
the-a-b-c-of-learning-success-second-edition-beuret/

textbookfull.com

Japanese Robot Culture: Performance, Imagination, and


Modernity 1st Edition Yuji Sone (Auth.)

https://textbookfull.com/product/japanese-robot-culture-performance-
imagination-and-modernity-1st-edition-yuji-sone-auth/

textbookfull.com

Peacebuilding and the Arts Jolyon Mitchell

https://textbookfull.com/product/peacebuilding-and-the-arts-jolyon-
mitchell/

textbookfull.com
Management And Competition In The Nhs, Second Edition
Chris Ham

https://textbookfull.com/product/management-and-competition-in-the-
nhs-second-edition-chris-ham/

textbookfull.com
RE AL WORLD
PY THON
A H A C K E R ’ S G U I D E TO
S O L V I N G P R O B L E M S W ITH CODE

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.

ISBN-13: 978-1-7185-0062-4 (print)


ISBN-13: 978-1-7185-0063-1 (ebook)

Publisher: William Pollock


Executive Editor: Barbara Yien
Production Editor: Kassie Andreadis
Developmental Editor: Frances Saux
Project Editor: Dapinder Dosanjh
Cover Illustrator: Rob Gale
Interior Design: Octopod Studios
Technical Reviewers: Chris Kren and Eric Mortenson
Copyeditor: Kim Wimpsett
Compositor: Shawn Morningstar
Proofreader: Paula L. Fleming
Indexer: Beth Nauman-Montana

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

Library of Congress Cataloging-in-Publication Data


Names: Vaughan, Lee, author.
Title: Real-world python: a hacker’s guide to solving problems with code / Lee Vaughan.
Description: San Francisco, CA : No Starch Press, Inc., [2020] | Includes
index.
Identifiers: LCCN 2020022671 (print) | LCCN 2020022672 (ebook) | ISBN
9781718500624 (paperback) | ISBN 1718500629 (paperback) | ISBN
9781718500631 (ebook)
Subjects: LCSH: Python (Computer program language)
Classification: LCC QA76.73.P98 V383 2020 (print) | LCC QA76.73.P98
(ebook) | DDC 005.1/33--dc23
LC record available at https://lccn.loc.gov/2020022671
LC ebook record available at https://lccn.loc.gov/2020022672

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!

About the Technical Reviewers


Chris Kren graduated from the University of South Alabama with an M.S. in
Information Systems. He currently works in the field of cybersecurity and
often uses Python for reporting, data analysis, and automation.
Eric Mortenson has a PhD in mathematics from the University of Wisconsin
at Madison. He has held research and teaching positions at The Pennsylvania
State University, The University of Queensland, and the Max Planck Institute
for Mathematics. He is an associate professor in mathematics at St. Petersburg
State University.
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
BRIEF CONTENTS

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix

Chapter 1: Saving Shipwrecked Sailors with Bayes’ Rule . . . . . . . . . . . . . . . . . . . . . . . . 1

Chapter 2: Attributing Authorship with Stylometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

Chapter 3: Summarizing Speeches with Natural Language Processing . . . . . . . . . . . . . 51

Chapter 4: Sending Super-Secret Messages with a Book Cipher . . . . . . . . . . . . . . . . . . 77

Chapter 5: Finding Pluto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

Chapter 6: Winning the Moon Race with Apollo 8 . . . . . . . . . . . . . . . . . . . . . . . . . . 123

Chapter 7: Selecting Martian Landing Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

Chapter 8: Detecting Distant Exoplanets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

Chapter 9: Identifying Friend or Foe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

Chapter 10: Restricting Access with Face Recognition . . . . . . . . . . . . . . . . . . . . . . . . 225

Chapter 11: Creating an Interactive Zombie Escape Map . . . . . . . . . . . . . . . . . . . . . 245

Chapter 12: Are We Living in a Computer Simulation? . . . . . . . . . . . . . . . . . . . . . . . 269

Appendix: Practice Project Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

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

Despite operating during a global pandemic, the team at No Starch Press


delivered another excellent effort at book making. They are professionals
without peer, and this book would not exist without them. They have my
deepest gratitude and respect.
Thanks also to Chris Kren and Eric Evenchick for their code reviews,
Joseph B. Paul and Sarah and Lora Vaughan for their cosplay enthusiasm,
and Hannah Vaughan for supplying useful photographs.
Special thanks to Eric T. Mortenson for his meticulous technical reviews
and many helpful suggestions and additions. Eric proposed the chapter on
Bayes’ Rule and supplied numerous practice and challenge projects including
applying Monte Carlo simulation to Bayes, summarizing a novel by chapter,
modeling interactions between the moon and Apollo 8, viewing Mars in 3D,
calculating the light curve for an exoplanet with an orbiting moon, and more.
This book is immensely better for his efforts.
Finally, thanks to all the contributors to stackoverflow.com. One of the
best things about Python is its extensive and inclusive user community. No
matter what question you may have, someone can answer it; no matter what
strange thing you want to do, someone has probably done it before, and you
can find them on Stack Overflow.
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
INTRODUCTION

If you’ve learned the basics of coding in


Python, you’re ready to write complete
programs that take on real-world tasks.
In Real-World Python, you’ll write programs to
win the moon race with Apollo 8, help Clyde Tombaugh
discover Pluto, select landing sites for a Mars rover,
locate exoplanets, send super-secret messages to your
friends, battle monstrous mutants, save shipwrecked
sailors, escape the walking dead, and more, all using the
Python programming language. In the process, you’ll
apply powerful computer vision, natural language pro-
cessing, and scientific modules, such as OpenCV, NLTK,
NumPy, pandas, and matplotlib, as well as a host of other
packages designed to make your computing life easier.
Who Should Read This Book?
You can think of this as a sophomore Python book. It isn’t a tutorial on
programming basics but rather a way for you to continue training using a
project-based approach. This way, you won’t have to waste your money or
shelf space rehashing concepts you’ve already learned. I’ll still explain every
step of the projects, and you’ll receive detailed instructions about using the
libraries and modules, including how to install them.
These projects will appeal to anyone who wants to use programming
to conduct experiments, test theories, simulate nature, or just have fun. As
you work through them, you’ll increase your knowledge of Python libraries
and modules and learn handy shortcuts, useful functions, and helpful tech-
niques. Rather than focus on isolated modular code snippets, these projects
teach you how to build complete, working programs involving real-world
applications, datasets, and issues.

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.

What’s in This Book?


The following is an overview of the chapters in this book. You don’t have
to work through them sequentially, but I’ll explain new modules and tech-
niques more thoroughly when they’re first introduced.
Chapter 1: Saving Shipwrecked Sailors with Bayes’ Rule Use Bayesian
probability to efficiently direct Coast Guard search and rescue efforts
off Cape Python. Gain experience with OpenCV, NumPy, and the itertools
module.
Chapter 2: Attributing Authorship with Stylometry Use natural language
processing to determine whether Sir Arthur Conan Doyle or H. G. Wells
wrote the novel The Lost World. Gain experience with NLTK, matplotlib,
and stylometric techniques such as stop words, parts of speech, lexical
richness, and Jaccard similarity.
Chapter 3: Summarizing Speeches with Natural Language Processing
Scrape famous speeches off the internet and automatically produce
a summary of the salient points. Then turn the text of a novel into a

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.

Python Version, Platform, and IDE


I built all the projects in this book with Python v3.7.2 in a Microsoft
Windows 10 environment. If you’re using a different operating system,
no problem: I suggest compatible modules for other platforms, where
appropriate.
The code examples in this book are from either the Python IDLE text
editor or the interactive shell. IDLE stands for integrated development and
learning environment. It’s an integrated development environment (IDE) with an
L added so that the acronym references Eric Idle of Monty Python fame. The
interactive shell, also called the interpreter, is a window that lets you immedi-
ately execute commands and test code without needing to create a file.
IDLE has numerous drawbacks, such as the lack of a line-number column,
but it’s free and bundled with Python, so everyone has access to it. You’re
welcome to use whichever IDE you want. Popular choices include Visual
Studio Code, Atom, Geany (pronounced “genie”), PyCharm, and Sublime
Text. These work with a wide range of operating systems, including Linux,
macOS, and Windows. Another IDE, PyScripter, works only with Windows.
For an extensive listing of available Python editors and compatible platforms,
visit https://wiki.python.org/moin/PythonEditors/.

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.

Figure 2: Listing of specific releases from the Python.org downloads page

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.

Figure 3: File listing for Python 3.8.2 version on Python.org

Introduction   xxiii
Random documents with unrelated
content Scribd suggests to you:
THE TRUTH ABOUT LUCK.
How often we hear some one say:
“My, but he’s lucky!” or “It’s better to be born lucky than rich.”
Boys and girls are too often in the habit of thinking that one of
their schoolmates are “lucky” because they always stand well in their
classes and frequently have spending money in their pockets.
It is not likely that “luck” had anything to do with it. They probably
stood well and were at the head of the class in school because they
studied and tried harder than the other scholars, and had money to
spend because they spent their time out of school hours in working
to earn it instead of at play.
Some years ago I happened to find myself near the terminal of
the great East River Bridge in New York City. Two little boys were
standing near one of the large iron posts crying their afternoon
papers. I tarried near them because I was waiting for a particular
car. One little fellow said to the other,—
“How many papers have you sold today, Tommie?”
“Nearly one hundred an’ fifty,” was Tommie’s quick reply.
“Honor bright?”
“Yes; honor bright.”
“Whoopee! but ain’t you in big luck, Tommie?”
“Luck!” exclaimed Tommie, wiping the perspiration from his brow.
“There ain’t no luck about it; I’ve just been everlastingly at it since
four o’clock this morning—that’s all!”
And that is the all of real success. Those who achieve success are
“everlastingly at” what they are trying to do. Tommie was right in
declining to have his hard and honest work cheapened by calling the
result of it luck.
“You are the luckiest chap I ever saw,” I once heard a little boy
about sixteen years say to another boy of about the same age.

“How Many Papers Have You Sold Today, Tommy?”

“Why do you say that?” asked the other.


“Because you have had your salary raised twice in the same year.”
“Well,” was the reply, “you may call it luck; but I don’t. I have
always done my work the very best I knew how. I have never once
in the whole year been a single minute late in getting to the office,
nor have I ever left a single minute before it was time for me to
leave. When I have worked over-time, I have not made any fuss
about it. My boss said when he raised my salary last week that he
had taken these things into account. So, I don’t see where the luck
comes in.”
“All the same,” said the first boy, “some bosses wouldn’t have
raised your salary.”
“Then I would have the satisfaction of knowing that I had done
my duty.”
Boys, I tell you that’s right. Nine out of ten employers know that it
is to their advantage to show appreciation of faithful work and they
show it. When this appreciation comes luck has had nothing to do
with it. The thing that passes for luck is in nearly all cases the just
reward of honest endeavor.
Do not, therefore, start out in life with the expectation that some
“lucky turn” will bring you sudden honor or wealth or position
without any effort on your part. Substitute that fine old word “work”
for that deceitful word “luck,” and base your hopes of future success
and usefulness upon the honorable labor that it is a God-given
privilege for every well and strong and right-minded boy to give his
heart and hands to performing.
AN EVENING AT HOME.
Boys and girls between the ages of eleven and seventeen ought to
spend their evenings at home, as much as possible. In these busy,
bustling twentieth century days, there are many families—so much
the worse for them—that scarcely know what it is to spend an
evening at home together. Not only the young people but the older
people are “on the go.” The evenings are crowded with calls and
invitations, which come from far and near. It is nothing to go five or
even ten miles to an evening concert or social gathering, the trolley
is so near, so cheap and so universal. But I tell you, boys and girls,
no matter what the pleasure or amusement afforded—no matter
what the instruction or culture received—there are no social or
similar opportunities good enough to displace the home circle. The
sooner young people realize this the happier they will be.
Boys and girls ought to plan for some evenings at home. Let other
things have a share, but do not give up all the time to other things.
Once a week the young people ought to arrange for an evening at
home. Decline everything else for that evening, the same as you
would for any other engagement. Gather the family together. Make a
special place for grandma and grandpa. Sing merry songs; play
innocent and amusing games; take time to tell the home folks about
some of the things that you do and that you have seen in the world;
get acquainted with the home folks; be delighted in their delight; by
special appointment, spend one or two cheerful hours with the folks
at home each week.
An Evening at Home.

The young folks themselves should take the lead in this matter. A
home is not merely a place with four walls where people meet to eat
and drink and sleep securely beneath a roof. Nay, boys and girls, a
house is reared to be a home—the center where a family may gather
into one; to be a serene retreat where the tenderest affections may
find rest; where love may have a dwelling place, and the amenities
of life gain ample scope; where parents and children may press one
another heart to heart; where sorrows and joys may be freely
shared in sacred confidence; in a word, where the great work of
training human beings for the duties of the present life, and the
perfection of another, may be begun and carried on.
There is one special reason for making much of the evenings at
home that young people are not likely to think of. Inevitably the
family circle will be broken up very soon. Perhaps not by death, but
most certainly by change. When Fred goes to college that is the
beginning of new ties and new associations, and the home privileges
can never be quite so complete to him again. The years of the
complete unity of the home are very few indeed. While these years
are passing, young people especially should make the most of them.
My dear boys and girls, get the benefit of these years; get their joys;
store up memories of home life, for they will be in future years the
most beautiful pictures of the heart. However some may sneer at it,
the memory of home and mother is a great power for righteousness.
It has saved many a person to God and native land and race.

“Be it ever so humble—


There’s no place like home.”
THE MAKING OF A MAN.[1]
Mr. Stamps, seated near the table, was glancing over the
afternoon paper. Mrs. Stamps, in an easy chair, was doing some
fancy work. Little Bobby, six years old, more or less, was playing
with his toys on the floor. All at once the precocious little boy
stopped short in the middle of his sport and, looking up at his
mother, asked,—
“Mama, who made the world?”
“God,” replied Mrs. Stamps, sweetly.
“Who made the sea?” continued Bobby.
Mrs. Stamps answered, “God.”
“Well,” said Bobby, “did God make everything?”
“Yes, my son; the Lord made everything.”
“And did he make everybody?”
“Yes; the Lord made everybody.”
Bobby was silent for a moment. Presently he looked anxiously at
his father, and then, turning to his mother, he asked,—
“Mama, did God make papa, too?”
“Yes; God made papa also.”
After a lengthy pause Bobby asked,—
“Mama, do you think that I could make a man, if I was to try real
hard?”
“You had better run out to play now, Bobby,” said Mrs. Stamps,
somewhat non-plused by her son’s curiosity.
Bobby and His “Man.”

Bobby left the room almost immediately. He went straight to the


beach in front of the house, and labored long and earnestly in piling
up some wet sand. Pretty soon he was joined in his work by two
other little boys. For some time the three little fellows worked
vigorously in piling up the mud. Mrs. Stamps called her husband to
the window, so that he might see what the boys were doing.
“Wife,” said Mr. Stamps, “I believe those little Satans are trying to
make a man.”
Toward sunset Bobby ran into the house and exclaimed with
delight,—
“Mama, we’ve got our man almost finished. We didn’t have but
one marble, and we used that for one of his eyes. I came in to ask
you to give me a marble, so that we might put in his other eye.”
“It’s too late to bother now, Bobby,” said Mrs. Stamps. “Wait until
to-morrow morning; then I will give you a marble and let you finish
your man.”
The next morning, bright and early, Bobby went out to look for his
man. Lo and behold! the sea had washed the man away during the
night. But, Bobby, of course, did not suspect that. He thought that
the man had gone away of his own accord. So the little fellow spent
the entire morning looking for his man. He looked under the house;
he looked in the stable; he went up to the garret; he walked up and
down the beach; he went into the woods—looking for his man. But
his man was nowhere to be found.
Two or three weeks later an African Methodist Episcopal
Conference assembled in Bobby’s town. Among the ministers present
there happened to be a short, chubby, tan-colored brother with only
one eye. When Bobby spied him he examined the man curiously and
cautiously from head to foot. The examination ended, Bobby
concluded that that was his man. At once the little fellow left his
mother and went over and took a seat beside the man. Bobby’s
mother was somewhat embarrassed. The man was evidently
pleased, although, to be sure, he himself was not quite certain why
he should be an object of special interest to the little boy. The man
went to the secretary’s table to have his name enrolled—Bobby went
with him. He went into the vestibule to get a drink of water—and
Bobby followed him there. But all the while the man was still in
doubt as to the cause of the little boy’s apparent affection. By this
time, thoroughly exasperated, Bobby’s mother decided to go home.
She approached the pew in a very ladylike manner and said,—
“Bobby, dear, come; we must be going home now.”
“All right, Mama,” said Bobby in dead earnest, “but you will please
let me take my man home with me—won’t you? I just found him to-
day, and you know I’ve been looking for him for over two weeks!”
Then, for the first time, it suddenly dawned upon Mrs. Stamps
what was the matter with Bobby. In spite of herself she laughed
heartily at the boy’s perversity. Finding that his mother hesitated to
reply, Bobby turned to the man and said,—
“Come on: we’re going home now. Why did you leave before I
finished you?”
[1] Published in the Voice of the Negro.
FALSE PRIDE.
Once upon a time the head clerk in a carpet store requested one
of his junior clerks to go to a patron’s home to measure a room, and
suggested that he take along a five-yard sample. The junior clerk
objected to “carting” such a big bundle, as he said, “all over town,”
and asked that one of the boys be sent with it. The proprietor of the
establishment, who happened to overhear the remark, privately told
the head-clerk to inform the proud young fellow that a boy would be
sent on after him with the roll. Shortly after the young man reached
the house, the proprietor of the establishment covered him with
confusion by appearing at the house in person with the roll of carpet
under his arm. Handing the bundle to the bewildered young man,
the proprietor remarked:
“Here is the carpet, young man. I hope I have not kept you
waiting for it. If you have any other orders, I’ll take them now.”
“Here Is theCarpet, Young Man. I Hope I Have
Not Kept You Waiting.”

* * * * *
A young woman of my acquaintance refused to carry home a
yeast cake, though it was needed at once for the family baking and
she was bound directly homeward. She said that she wasn’t a
delivery wagon, and so the yeast cake had to be sent to her home.
A great many foolish young people are so absorbingly regardful of
their trim appearance on the street that they will never under any
circumstances carry a basket or bundle, however much
inconvenience they may cause others by refusing to do so.
* * * * *
Now, it is not proper pride or self-respect which prompts people to
act as the young folks acted whom I have just referred to. It is
silliness which prompts them to act so. Any honest work is honorable
that is honorably done, and you will notice that young people of
good social position and strength of character are above such
pettiness. Only inferior people act that way. Superior people do not
act so, because they are well aware that they cannot be
compromised by doing straightforwardly, without fuss or apology,
whatever needs to be done. Yet, I admit, that it seems to be human
nature that whatever is distasteful or supposedly menial should be
done by somebody else. When young people, or old people for that
matter, are tempted to be foolish in such things they should
remember the lesson of humility that Christ taught his disciples,
when in that warm Oriental country, where only sandals are worn,
He performed the necessary service of washing the disciples’ feet.
For us to be above our business—for us to think ourselves too good
or too dainty to soil our hands with honest toil—for us to feel that it
is a lowering of our dignity to carry a bundle through the street, is to
prove by our conduct that we are not up to the level of our business,
that we are possessed of a great amount of false pride, and, in a
higher sense, it shows that we have a foolish and wicked distaste of
true service. There is nothing low, nothing degrading, nothing
disgraceful, in honest labor, in honest work of any kind, whether it
be to boil an egg properly, to sweep a floor well, to carry a bundle or
package through the streets, or bring a pail of water. In fact, if
somebody were to say that “chores” done or undone are the making
or the unmaking of boys and girls, it would be a homely way of
putting an important truth. Bringing up coal or bringing in wood,
weeding the garden bed, running errands, washing dishes, sewing
seams, dusting furniture, doing any odd jobs where there is need,
cheerfully, faithfully—these lead to the highway of greater
opportunities and are the usual avenues to the only manhood and
womanhood that is worth having. My young friends, the castle of
your noblest dream is built out of what lies nearest at hand. It is the
uncommonly good use of common things, the everyday
opportunities, that makes honored lives, and helps us, and helps us
to help others, along the sunroad. “He that is faithful in that which is
least is faithful also in much.” “Pride goeth before destruction, and a
haughty spirit before a fall.”
THANKSGIVING AT PINEY GROVE.
The people of the Piney Grove settlement, both white and black,
had been free for nearly a generation. The whites had been freed
from the curse of being slave-holders, and the blacks had been freed
from the curse of being held in bondage. But never in the history of
this little town, in the very heart of the so-called “Black Belt” of
Georgia, had the people known anything about the proper
observance of Thanksgiving Day until 189—. And in that year the
revolution was brought about by a young colored woman named
Grace Wilkins.
Grace Wilkins was the only daughter of Solomon and Amanda
Wilkins. Solomon and his wife were farmers—plain, simple, ordinary
country folk. Amanda was literally her husband’s helpmeet. She went
along with him every morning to the field, and, in season, chopped
as much wood, picked as much cotton, hoed as much corn, pulled as
much fodder, and plowed as much as her husband did. Up to her
fourteenth year Grace had been reared on a farm, and had learned
to do all the things that any farmer’s child has to do—such as
milking cows, feeding hogs and chickens, hoeing cotton and corn,
picking cotton, pulling fodder and the like. In her fourteenth year,
acting upon the advice of an uneducated colored preacher, her
parents sent Grace away from home to attend one of the great
normal and industrial institutes for the training of the black boys and
girls of the South.
Grace Before Going to School.

At first her mother and father were filled with forebodings. It was
the first time that they had ever allowed their daughter to be away
from them, and they missed her so much and longed for her so
constantly that they thought that they had made a mistake in
sending her off to “boardin’ school.” Ignorant and superstitious
neighbors, though they knew as little about such matters as did
Solomon and Amanda, were loud in saying that “Sol” and “Mandy”
would live to regret the step they had taken in sending Grace away
from home. The only rays of sunshine that came in to brighten these
periods of mental unrest and gloom on the part of Mr. and Mrs.
Wilkins were found in the letters which they received regularly from
their daughter. Grace invariably informed her parents, whenever she
wrote, that she was “well an’ doin’ well.” Thus reassured from time
to time, Solomon and Amanda managed somehow to undergo the
terrible strain of having their daughter absent from them for eight
months. But meantime they were firmly of the opinion that, once
they got their hands on her again, they would never allow Grace to
return to school.
With glad and thankful hearts Mr. and Mrs. Wilkins joyously
embraced their daughter when she came home at the close of her
first year in school. With keen and genuine interest, they listened to
her wonderful accounts of the great school and of the great man at
the head of it. Grace dressed differently and talked differently; and
her mother said, speaking one day in confidence to her husband
shortly after Grace’s return, “Dat gal’s sho got a new walk on her!”
Grace Wilkins brought back a toothbrush with her from school.
That was something which she had never had before. She used that
toothbrush every morning and night. That was something that she
had never done before. She was now careful to keep her hair well
combed every day. That was something that she had been
accustomed to do on Sundays only or on special occasions. She
washed her face two or three times a day now, as her mother and
father noticed. Before she went to school she had been in the habit
of giving her face, as the old people say, “a lick and a promise” early
each morning. Besides, Grace kept the house cleaner than she had
kept it before. She brought home with her a brand new Bible which
she read regularly at home and always carried to church and Sunday
school. She also had a song book called “Jubilee Songs and
Plantation Melodies,” and it gladdened the hearts of the good “old
folks at home” to hear their daughter sing from a book some of the
very songs that they had sung all their lifetime and which were so
dear to them.
All these things and others made a deep and abiding impression
upon Solomon and his wife. And finding that withal their daughter
was just as loving and kind as she had been before, and that she
was just as industrious and faithful as formerly, Mr. and Mrs. Wilkins
were not long in deciding that their daughter should go back to that
school another year, and that they would work hard and stint
themselves in order that they might keep her there until she had
finished the normal course.
So back to school Grace Wilkins went—that year, and the next
year, and the next. It was the proudest day in Solomon’s and
Amanda’s lives when they sat in the magnificent chapel of the school
and heard their daughter read her graduation essay on “The Gospel
of Service.” Glad tears welled up in their eyes when they heard the
principal call their daughter’s name, and then saw Grace step up to
receive her certificate of graduation.
Coming back to Piney Grove to live, “Miss Gracie”—everybody
called her that after graduation—established a little school which she
called “The Piney Grove Academy.” It was the first public school for
colored children ever opened within the corporate limits of the little
village. Before that the schools were district schools or county
schools, which were taught about in different places for only three or
four months in the year, mainly during the summer. Miss Gracie
began her school the first day of October. By special arrangement
she used the first three months for the public term allowed by the
state, and supplemented that with a five-months term, for which the
pupils were required to pay fifty cents each per month. The plan
worked well, the parents joining in heartily in the movement, and
the Piney Grove Academy soon became the model school for the
surrounding counties.
Grace’s Graduation.

Among other things Miss Gracie had learned at school what was
the import of our national Thanksgiving Day. At the opening of the
second year of the Piney Grove Academy she decided that she would
inaugurate an annual Thanksgiving service. Accordingly on the
opening day of the second year Miss Gracie informed the pupils of
her plan, and told them that she would begin the very next day to
prepare a suitable program for the exercises. Afterwards Miss Gracie
secured the cooperation of the village pastor—the same man who
had been instrumental in having her parents send her away to
school. Through him she was permitted to talk to the people at the
church two or three times about the proposed celebration. She was
careful to tell them that the Thanksgiving festival was meant
specially to be a home festival in addition to being a time for the
people to come together in their accustomed places of worship to
thank God for the blessings of the year. She urged them, therefore,
as far as they were able without going to unnecessary expense, to
have family dinners and bring together at one time and in one place
as many members of the family as possible. She explained to them
how this might be done successfully and economically, and with
pleasure and profit to all concerned. She also urged them to be
planning beforehand so that nothing might prevent their attending
church Thanksgiving Day morning. She was going to hold the
exercises in the church, because her little school was not large
enough to furnish an assembly hall for the people who would be
likely to be present.
On Thanksgiving Day nearly everybody in town went to the
exercises. Many white people attended, including the county school
commissioner and the school trustees. It was the first Thanksgiving
service that any of them had ever witnessed.
The program was made up, for the most part, of choice selections
from negro authors, composers, orators, and so forth. A selection
from Frederick Douglass on “Patriotism” was declaimed; one from
Booker T. Washington’s Atlanta Exposition speech was also delivered.
Paul Laurence Dunbar’s poem entitled “Signs of the Times” (a
Thanksgiving poem) was read by one of the pupils, and also “The
Party,” another of Dunbar’s pieces, was rendered. “The Negro
National Hymn,” words by James W. Johnson and music by his
brother, Rosamond Johnson, was sung by a chorus of fifty voices. At
the opening of the service the president’s Thanksgiving proclamation
was read and appropriate remarks were made by Miss Wilkins. The
closing remarks were made by the Rev. John Jones, the village
pastor. The remarks of Mr. Jones were in the congratulatory mood.
He was naturally proud of Miss Gracie’s achievements, because he
had had something to do with putting her on the road to an
education. He spoke of the teacher as the leaven that was leavening
the whole lump, and the applause which followed the statement
showed plainly the high esteem in which the teacher was held by all
the people. Everyone enjoyed the service. None of the villagers had
ever seen anything like it before. After singing “America” all of them
went away happy, many of them, in obedience to Miss Gracie’s
previous counsel, going home to eat for the first time, well knowing
what they were doing, a Thanksgiving dinner.
At the home of Miss Wilkins there was an excellent spread of
’possum, potatoes, rice, chicken, pickles, macaroni, bread, a
precious Thanksgiving turkey, and the inevitable mincemeat pie.
Besides Miss Gracie, there sat at the table that day her parents, Mr.
and Mrs. Solomon Wilkins, John and Joseph Wilkins, brothers of
Solomon who had come from a distance, Mary Andrews, a sister of
Mrs. Wilkins, who also came from a distance, Grandma Wilkins,
Grandma and Grandpa Andrews, the Rev. John Jones, his wife, his
daughter, and his only son, Jasper Jones.
Jasper had gone to school at T—— one year after Gracie went,
and, of course, was one year later in finishing the course there. On
this Thanksgiving Day, nevertheless, he had been out of school long
enough to have successfully established himself in the business of
poultry raising and dairying.
Just before the dinner party was dismissed the Rev. Mr. Jones
arose and said:
“There is another little ceremony you’all is invited to witness befo’
you go out to see the baseball game. I am authorized by these
credentials which I hol’ in my hands to unite in the holy bonds of
matrimony Miss Grace Wilkins and Mr. Jasper Jones. If there is no
objection, these two persons will please stan’ up, an’ I’ll tie the
knot.”
Of course there were no objections. The knot was tied. And when
the villagers learned of the occurrence not long afterwards they had
additional reason for believing that they were right when they voted
that Piney Grove had never seen the like of such a Thanksgiving
Day, and that Miss Gracie Wilkins was one of the best women in all
the world.
THE LOUD GIRL.
I do not know of a more sorrowful spectacle than that of a girl
who is loud in her dress, loud in her manners, and loud in her
speech. It is a great mistake for a girl to suppose that this loudness
will be mistaken by her friends and acquaintances for smartness.
The desire to be regarded as bright and witty has led many a girl
into the folly of being loud in her manners. She often cherishes the
illusion that the attention such manners attract is combined with
admiration, when the truth is that those who witness her strange
conduct are simply wondering how it is possible for her to throw to
the winds that charm of all girlhood—modesty.
Blab-Mouthed and Noisy.

One afternoon not long ago I saw a group of girls of the loud
type. They came into the street car in which I was sitting. They all
wore boys’ hats. One wore a vivid red jacket with brass buttons, and
another had on a brass belt. A third one had on a most conspicuous
plaid skirt. This third one had a box of bonbons, and when the three
were seated she opened the box and offered it to her companions,
saying as she did so, in a voice loud enough and shrill enough to be
heard in every part of the car:
“It’s my treat; have some, chums!”
Upon this invitation one of the
girls dived down into the box like a
hungry bear, and held up a piece of
the candy in triumph and then
dashed it into her mouth with a
great guffaw. “O, Mame!” said one
of the girls, “if you ain’t just horrid
to go and take the very piece I
wanted!”
“Mame” laughed and, taking the
candy from her mouth, offered it to
the other girl, saying as she did so:
“Well, here it is, Lulu!”
“Lulu” struck the candy from
“Mame’s” hand, and it flew across
the aisle into the lap of a lady sitting
opposite the girls. This set all three
of the girls to giggling and tittering,
and they seemed in danger of
convulsions when the owner of the
box of candy let it fall and a part of
the candy rolled out on the floor.
The conductor came forward and
picked up the box and candy and
handed them to the owner. She
giggled out her thanks, and “Lulu”
Modest and Quiet. said: “Why didn’t you give him a
gumdrop for his trouble?”
This seemed to impress the other girls as a most brilliant
witticism, and they fell to tittering violently over it.
Presently a gentleman came in and stumbled slightly over the feet
of one of the girls thrust out into the aisle.
“I beg your pardon,” said the gentleman, as he lifted his hat,
whereupon the three girls grinned and giggled and giggled and
grinned immoderately, and one of them said:
“Roxy, you had better ride out on the platform, where there is
more room for your feet!”
“Roxy” then struck “Lulu” for making this speech. “Lulu” pretended
to be much offended and flung herself over to the other side of the
car, where she made a grimace at the other girls.
The conduct of these girls during the half hour that they were on
the car was such as caused every father and mother who saw them
to regard them with pity. The loud girl, my dear readers, is always
an object of pity. She should be a sorry object for her own
contemplation. An old writer has said: “You little know what you
have done when you have first broken the bounds of modesty; you
have set open the door of your fancy to the devil, so that he can
represent the same sinful pleasure to you anew.”
Now, the loud girl may be entirely innocent of any actual wrong-
doing, but she is regarded with dislike, distrust, and even disdain, by
the better class of people. She acquires a reputation for rudeness
and coarseness, and the people of refinement will not associate with
her. Her character suffers, no matter how innocent she may be of
any intention of doing wrong. Delicacy, modesty, is the certain sign
of sweetness, purity and gentleness of character, just as indelicacy is
the certain sign of a lack of these beautiful traits.
THE ROWDY BOY.
You can tell him wherever you see him. There are certain marks or
appearances which he carries about with him and which are never
absent. For one thing you will find him with a cigarette stuck in his
mouth, and a cigarette is one of the deadliest poisons in the world
for boy or man. He wears his hat on the side or cocked back on his
head. Frequently he stuffs both hands in his trousers’ pockets. He
doesn’t attend school regularly; sometimes he starts for school and
ends at the bathing pond or the baseball park. He is late at Sunday
school, if he goes at all, and he stands ’round on the outside at
church while the service is going on inside. He steals rides on trains
and on trolley cars, and on passing vehicles of all descriptions. He is
saucy and impudent to older people, and is always ready and willing
to quarrel or fight with his mates. He is what the boys call a “bully.”
The loud girl and the rowdy boy are two things of which we have
seen enough in this world. They are things; they are hardly worth
the dignity of being called human beings.
I saw one of these rowdy boys in his own home not a great while
ago. His mother said to him:
“Johnnie, you must always take off your hat whenever you come
into the house.”
“Good gracious alive,” he said, “I can’t do anything right. What is
the use of grabbing off your hat every time you come into your own
house?”
He Stuffed Both Hands in His Trousers’ Pocket.

His mother looked sad, but said nothing. Presently she discovered
that her little boy had brought some mud into the house on his
shoes. In her sweetest tones she said:
“Johnnie, you must go to the door and wipe your feet now. See
how you are tracking up the floor there!”
“Well,” said the rowdy boy with a snarl, “can’t the old floor be
scoured? You must think this old house is gold.”
Now, I am a preacher, boys, and, being a preacher, of course I am
what is called a “man of peace,” but I tell you that that was one time
I came pretty near wishing that I wasn’t a preacher so that I might
have given that boy what he deserved. I was sorry, for the time
being, that he wasn’t my son. No manly little boy will ever talk to his
mother in any such way. I suppose that boy thought it made him
appear to be a very important personage, but he was very much
mistaken. Don’t be rowdy, boys; don’t be rough; don’t be rude. You
were made for better things.
HONESTY.
Early in the morning two little boys came to the market place.
They arranged their little stands and spread out their wares, and sat
down to wait for customers. One sold watermelons and fruit, and the
other sold fish and oysters. The hours passed on and both were
doing well. By-and-by Sammie had only one melon left on his stand.
A gentleman came along and said:
“What a fine, large melon! I think I will buy that one. What do you
ask for it, my boy?”
“How Much for the Melon?”

“This is my last melon, sir; and though it looks fair, there is an


unsound spot on the other side,” said the boy, turning the melon
over.
“So there is,” said the man. “I don’t believe I’ll take it. But,” he
added, looking straight at the boy, “is it very good business for you
to point out the defects of your goods to customers?”
“Perhaps not, sir,” said the boy with becoming modesty, “but it is
better than being dishonest.”
“You are right, my boy; always speak the truth and you will find
favor with God and man. I shall not forget your little stand in the
future.”
Then turning to the other boy’s stand the man asked:
“Are those fresh oysters?”
“Yes, sir,” said Freddie, “these are fresh this morning—just
arrived.”
The gentleman bought them and went away.
“Sammie,” said Freddie, “you never will learn any sense. What did
you want to show that man that spot on the melon for? He never
would have looked at it until he got home. I’ve got an eye to
business, myself. You see how I got rid of those stale oysters—sold
them for just the same price as fresh oysters.”
“Freddie,” said the other boy, “I wouldn’t tell a lie, or act one
either, for twice the money we have both earned today. Besides I
have gained a customer and you have lost one.”
And it turned out just as Sammie said. The next day the
gentleman bought a large supply of fruit from Sammie, but he never
spent another penny at Freddie’s stand. It continued that way
through all the summer. At the close of the season he took Sammie
into his store, and, after awhile, gave him a share in the business.

You might also like