Real World Python A Hacker s Guide to Solving Problems with Code 1st Edition Lee Vaughan download
Real World Python A Hacker s Guide to Solving Problems with Code 1st Edition Lee Vaughan download
https://ebookmeta.com/product/real-world-python-a-hacker-s-guide-
to-solving-problems-with-code-1st-edition-lee-vaughan/
https://ebookmeta.com/product/learn-to-code-by-solving-problems-
a-python-programming-primer-1st-edition-daniel-zingaro-2/
https://ebookmeta.com/product/learn-to-code-by-solving-problems-
a-python-programming-primer-1st-edition-daniel-zingaro/
https://ebookmeta.com/product/learn-to-code-by-solving-problems-
a-python-programming-primer-1st-edition-daniel-zingaro-3/
https://ebookmeta.com/product/the-duke-manual-of-corneal-and-
cataract-surgery-duke-manuals-of-ophthalmic-surgery-first-
edition-gupta/
Intelligent Motorized Spindle Technology Springer
Tracts in Mechanical Engineering Yuhou Wu Lixiu Zhang
https://ebookmeta.com/product/intelligent-motorized-spindle-
technology-springer-tracts-in-mechanical-engineering-yuhou-wu-
lixiu-zhang/
https://ebookmeta.com/product/reference-range-values-for-
pediatric-care-lamia-soghier/
https://ebookmeta.com/product/critical-thinking-skills-effective-
analysis-argument-and-reflection-4th-edition-stella-cottrell/
https://ebookmeta.com/product/interviewing-a-guide-for-
journalists-and-professional-writers-3rd-edition-gail-sedorkin-2/
https://ebookmeta.com/product/political-ecology-system-change-
not-climate-change-1st-edition-dimitri-roussopoulos/
Corruption Lord 4 A GameLit Harem Fantasy 1st Edition
Neil Bimbeau
https://ebookmeta.com/product/corruption-lord-4-a-gamelit-harem-
fantasy-1st-edition-neil-bimbeau/
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
No Starch Press and the No Starch Press logo are registered trademarks of No Starch
Press, Inc. Other product 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.
BRIEF CONTENTS
Acknowledgments
Introduction
Index
CONTENTS IN DETAIL
ACKNOWLEDGMENTS
INTRODUCTION
Who Should Read This Book?
Why Python?
What’s in This Book?
Python Version, Platform, and IDE
Installing Python
Running Python
Using a Virtual Environment
Onward!
1
SAVING SHIPWRECKED SAILORS WITH
BAYES’ RULE
Bayes’ Rule
Project #1: Search and Rescue
The Strategy
Installing the Python Libraries
The Bayes Code
Playing the Game
Summary
Further Reading
Challenge Project: Smarter Searches
Challenge Project: Finding the Best Strategy with MCS
Challenge Project: Calculating the Probability of Detection
2
ATTRIBUTING AUTHORSHIP WITH
STYLOMETRY
Project #2: The Hound, The War, and The Lost World
The Strategy
Installing NLTK
The Corpora
The Stylometry Code
Summary
Further Reading
Practice Project: Hunting the Hound with Dispersion
Practice Project: Punctuation Heatmap
Challenge Project: Fixing Frequency
3
SUMMARIZING SPEECHES WITH NATURAL
LANGUAGE PROCESSING
Project #3: I Have a Dream . . . to Summarize Speeches!
The Strategy
Web Scraping
The “I Have a Dream” Code
Project #4: Summarizing Speeches with gensim
Installing gensim
The Make Your Bed Code
Project #5: Summarizing Text with Word Clouds
The Word Cloud and PIL Modules
The Word Cloud Code
Fine-Tuning the Word Cloud
Summary
Further Reading
Challenge Project: Game Night
Challenge Project: Summarizing Summaries
Challenge Project: Summarizing a Novel
Challenge Project: It’s Not Just What You Say, It’s How You Say It!
4
SENDING SUPER-SECRET MESSAGES WITH
A BOOK CIPHER
The One-Time Pad
The Rebecca Cipher
Project #6: The Digital Key to Rebecca
The Strategy
The Encryption Code
Sending Messages
Summary
Further Reading
Practice Project: Charting the Characters
Practice Project: Sending Secrets the WWII Way
5
FINDING PLUTO
Project #7: Replicating a Blink Comparator
The Strategy
The Data
The Blink Comparator Code
Using the Blink Comparator
Project #8: Detecting Astronomical Transients with Image Differencing
The Strategy
The Transient Detector Code
Using the Transient Detector
Summary
Further Reading
Practice Project: Plotting the Orbital Path
Practice Project: What’s the Difference?
Challenge Project: Counting Stars
6
WINNING THE MOON RACE WITH APOLLO
8
Understanding the Apollo 8 Mission
The Free Return Trajectory
The Three-Body Problem
Project #9: To the Moon with Apollo 8!
Using the turtle Module
The Strategy
The Apollo 8 Free Return Code
Running the Simulation
Summary
Further Reading
Practice Project: Simulating a Search Pattern
Practice Project: Start Me Up!
Practice Project: Shut Me Down!
Challenge Project: True-Scale Simulation
Challenge Project: The Real Apollo 8
7
SELECTING MARTIAN LANDING SITES
How to Land on Mars
The MOLA Map
Project #10: Selecting Martian Landing Sites
The Strategy
The Site Selector Code
Results
Summary
Further Reading
Practice Project: Confirming That Drawings Become Part of an Image
Practice Project: Extracting an Elevation Profile
Practice Project: Plotting in 3D
Practice Project: Mixing Maps
Challenge Project: Making It Three in a Row
Challenge Project: Wrapping Rectangles
8
DETECTING DISTANT EXOPLANETS
Transit Photometry
Project #11: Simulating an Exoplanet Transit
The Strategy
The Transit Code
Experimenting with Transit Photometry
Project #12: Imaging Exoplanets
The Strategy
The Pixelator Code
Summary
Further Reading
Practice Project: Detecting Alien Megastructures
Practice Project: Detecting Asteroid Transits
Practice Project: Incorporating Limb Darkening
Practice Project: Detecting Starspots
Practice Project: Detecting an Alien Armada
Practice Project: Detecting a Planet with a Moon
Practice Project: Measuring the Length of an Exoplanet’s Day
Challenge Project: Generating a Dynamic Light Curve
9
IDENTIFYING FRIEND OR FOE
Detecting Faces in Photographs
Project #13: Programming a Robot Sentry Gun
The Strategy
The Code
Results
Detecting Faces from a Video Stream
Summary
Further Reading
Practice Project: Blurring Faces
Challenge Project: Detecting Cat Faces
10
RESTRICTING ACCESS WITH FACE
RECOGNITION
Recognizing Faces with Local Binary Pattern Histograms
The Face Recognition Flowchart
Extracting Local Binary Pattern Histograms
Project #14: Restricting Access to the Alien Artifact
The Strategy
Supporting Modules and Files
The Video Capture Code
The Face Trainer Code
The Face Predictor Code
Results
Summary
Further Reading
Challenge Project: Adding a Password and Video Capture
Challenge Project: Look-Alikes and Twins
Challenge Project: Time Machine
11
CREATING AN INTERACTIVE ZOMBIE
ESCAPE MAP
Project #15: Visualizing Population Density with a Choropleth Map
The Strategy
The Python Data Analysis Library
The bokeh and holoviews Libraries
Installing pandas, bokeh, and holoviews
Accessing the County, State, Unemployment, and Population
Data
Hacking holoviews
The Choropleth Code
Planning the Escape
Summary
Further Reading
Challenge Project: Mapping US Population Change
12
ARE WE LIVING IN A COMPUTER
SIMULATION?
Project #16: Life, the Universe, and Yertle’s Pond
The Pond Simulation Code
Implications of the Pond Simulation
Measuring the Cost of Crossing the Lattice
Results
The Strategy
Summary
Further Reading
Moving On
Challenge Project: Finding a Safe Space
Challenge Project: Here Comes the Sun
Discovering Diverse Content Through
Random Scribd Documents
quoting the fable of Daphne to illustrate the denial of the Apostle.
“The nymph of the wood,” said he, “being pursued by the shepherd
Apollo, fled over hill and dale, till she reached the foot of a rock up
which she could not climb, and, seeing herself at the mercy of her
pursuer, she began to weep,—in like manner, St. Peter seeing
himself arrested by the rock of his denial, ‘wept bitterly.’” And
Camus, Bishop of Belley, who flourished in the beginning of the
seventeenth century, could use such words as these on Christmas
Day:—“We now, skimming over the sea in our boat, come to behold
the Infant born into the world to conquer it. He is our Bellerophon,
who, mounted on the Pegasus of His humanity, winged by union
with the Deity, has overcome the world, ‘confidite, ego vici
mundum;’ the world, a true and strange Chimera! lion as to its front
by its pride, dragon behind in its avarice, goat in the midst by its
pollution! He is our youthful Horatius overcoming the three Curiatii
of ambition, avarice, and sensuality! He is our Hercules, who has
beaten down the triple-throated Cerberus, and who has in His cradle
strangled serpents. The one crushed only two, but ours has
destroyed three, the vanity of the world by His subjection, the
avarice of the world by His poverty, the delights of the world by His
mortification.”
Sometimes preachers, carried away by their feelings, gave vent to
the most violent and indecorous expressions. As, for instance, the
Père Guerin preaching on the danger of reading improper literature,
could not refrain from using the following language with reference to
Theophilus Viaud, who had written a very immoral poem, “La
Parnasse des Poètes,” 1625, for which he and his book were
condemned to be burned. “Cursed be the spirit which dictated such
thoughts,” howled the preacher. “Cursed be the hand which wrote
them! Woe to the publisher who had them printed! Woe to those
who have read them! Woe to those who have ever made the
author’s acquaintance! But blessed be Monsieur le premier Président,
blessed be M. le Procureur Général, who have purged our Paris of
this plague! You are the originator of the plague in this city; I would
say, after the Rev. Father Garasse, that you are a scoundrel, a great
calf! but no! shall I call you a calf? Veal is good when boiled, veal is
good when roast, calfskin is good for binding books; but yours,
miscreant! is only fit to be well grilled, and that it will be, to-morrow.
You have raised the laugh at monks, and now the monks will laugh
at you.”
Preachers have been quite unable at times to resist the chance of
saying a bon mot. Father André, being required to give out before
his sermon that a collection would be made for the dower of a
young lady who wished to take the veil, said—“Gentlemen, your
alms are solicited in behalf of a young lady who is not rich enough to
take the vow of poverty.” I believe it is of the same man that the
story is told, that he halted suddenly in the midst of a sermon to
rebuke the congregation for indulging in conversation whilst he was
speaking. One good woman took this in dudgeon, and standing up,
assured the preacher that the buzz of voices came from the men’s
side of the church, and not from that reserved for the females. “I am
delighted to hear it,” replied the preacher, “the talking will then be
sooner over.” This reminds me of Gabriel Barlette’s dictum, “Pone
quatuor mulieres ab unâ parte, decem viros ab aliâ, plus garrulabunt
mulieres.”
Kings even have been publicly rebuked for something of the same
kind. Every one knows that Mademoiselle d’Entragues, Marchioness
of Verneuil, was mistress of Henry IV. One day that the Jesuit father,
Gonthier, was preaching at St. Gervais, the king attended with
Mademoiselle d’Entragues, and a suite of court ladies. During the
sermon the marchioness whispered and made signs to the king,
trying to make him laugh. The preacher, indignant at this conduct,
turned to Henry and said, “Sire, never again permit yourself to come
to hear the word of God surrounded by a seraglio, and thus to offer
so great a scandal in a holy place.” The marchioness was furious,
and endeavoured to obtain the punishment of the preacher, but
Henry, instead of consenting, had the good sense to show that he
was not offended, by returning to hear Father Gonthier preach on
the following day. He took him aside however, and said, “My father,
fear nothing. I thank you for your reproof; only, for Heaven’s sake,
don’t give it in public again.”
I have said that the preachers of the fifteenth century often
degenerated into the burlesque, in order to attract the attention they
failed to rivet by the excellence of their matter. Unfortunately this
fault was not confined to the fifteenth century, but we find it again
and again appearing among inferior preachers of the next two
centuries. It must be remembered that the monasteries had then
fallen from their high estate through the intolerable oppression of
the “in commendam,” and that learning was far less cultivated than
in an earlier age. The popular friar-preachers, the hedge-priests,
who took with the vulgar, were much of the stamp of modern
dissenting ministers, men of little education but considerable
assurance; they spoke in the dialect of the people, they understood
their troubles, they knew their tastes; and, at the same time, like all
people who have got a smattering of knowledge, they loved to
display it, and in displaying it consisted much of their grotesqueness.
The following sketch of one of these discourses is given by Father
Labat, in his “Voyages en Espagne et en Italie, Amst., 1731, 8 vols.
in 12mo.” He says that he was present on the 15th September, 1709,
at a sermon preached in the open air under a clump of olives near
Tivoli.
The day was the Feast of the Name of Mary. “Those who did the
honours of the feast placed me, politely, right in front of the
preacher. He appeared, after having kept us waiting sufficiently long,
mounted the pulpit, sat down without ceremony, examined his
audience in a grave and perhaps slightly contemptuous manner; and
then, after a few moments’ silence, he rose, took off his cap, made
the sign of the cross on his brow, then on his mouth, and then on
his heart, which after the old system he supposed to be on his left
side; lastly, he made a fourth sign, which covered up all the others,
since it extended from his head to the pit of his stomach. This
operation complete, he sat down, put on his cap, and began his
discourse with these words, ‘I beheld a great book written within
and without,’ which he explained thus: Ecco il verissimo ritrato di
Maria sempre Virgine; that is to say, Behold the veritable portrait of
the ever Virgin Mary. This application was followed by a long
digression upon all books ever known in MS. or in print. Those which
compose the Holy Scriptures passed first in review; he named their
authors, he fixed their date, and gave the reasons for their
composition. He passed next to those of the ancient philosophers, of
the Egyptians and of the Greeks; those of the Sibyls appeared next
on the scene, and the praise of the Tiburtine Sibyl was neatly
interwoven into the discourse. Homer’s Iliad was not forgotten, any
more than the Æneid; not a book escaped him; and then he
declared that none were equal to the great book written within and
without; a book, said he, imprinted with the characters of divine
virtues, bound in Heaven, dedicated to wisdom uncreate[1],
approved by the doctors of the nine angelic hierarchies, published by
the twelve Apostles in the four quarters of the globe; a book
occupying the first place in the celestial library, in which angels and
saints study ever, which is the terror of demons, the joy of heaven,
the delights of saints, the recompense of the triumphant Church, the
hope of the suffering, the support, the strength, the buckler of the
militant. He never left this great book, the leaves of which he kept
turning, so to speak, for three good quarters of an hour, and then
finding that it was time to rest, he quitted us suddenly without a
‘good-bye.’ I mean without the blessing, and without having spoken
of the Blessed Virgin in any other light than that which served him in
the explanation of his text.
“I confess I never heard a sermon which pleased me better, for I
was not a bit wearied during it; and, in his style, I suspect he was
unequalled. The Passion of Father Imbert, Superior of our mission at
Guadaloupe, his sermon on St. Jean de Dieu, that of Father Ange de
Rouen, a Capuchin, on a certain indulgence, had hitherto appeared
to me inimitable masterpieces; but I must award the palm to that
which I have just reported, and to do the preacher justice, he
surpassed the others mentioned as the empyrean sky surpasses the
lunar sky in grandeur and elevation.”
I must speak here of a famous preacher of the fifteenth century,
to whom I cannot afford a separate notice, and who is more
offensively ridiculous than the man spoken of by Labat; I mean
Gabriel Barlette. I do not give him other notice than this for two
reasons; the first, because there is reason to believe that the
sermons which pass under his name are spurious compositions, as
indeed is asserted by a cotemporary, Leander Alberti, who says that
they were the composition of a pretender who took the name of the
great preacher.
It is therefore not fair to judge of a really famous man from works
which may not be his. Another reason why I have limited to a few
lines my notice of sermons which were undoubtedly popular, if we
may judge of the number of impressions they went through, is that
there is positively no good to be got from them; they are full of the
grossest absurdities and the most profane buffoonery. I have given
an account of some three or four of this class of sermon, and I can
afford no more room to similar profanities.
Gabriel Barlette was a Dominican, and was born at Barletta in the
kingdom of Naples. He lived beyond 1481, for he speaks of the siege
and capture of Otranto by Mahomet II. as a thing of the past. In one
of the sermons attributed to him is the following passage on the
close of the temptations:—“After His victory over Satan, the Blessed
Virgin sends Him the dinner she had prepared for herself, cabbage,
soup, spinach, and perhaps even sardines.”
In a sermon for Whitsun-Tuesday, he rebukes distractions in
prayer, and he illustrates them in this unseemly way. He represents a
priest engaged at his morning devotions, saying, “Pater noster qui es
in cœlis—I say, lad, saddle the horse, I’m going to town to-day!—
sanctificatur nomen tuum,—Cath’rine, put the pot on the fire!—fiat
voluntas tua—Take care! the cat’s at the cheese!—panem nostrum
quotidianum—Mind the white horse has his feed of oats.… Is this
praying?” No, Gabriel, nor is this preaching!
Another preacher of the same stamp was Menot. Michael Menot
was born in Paris; he was a Franciscan, and died at an advanced age
in 1518.
Take this specimen of his reasoning—