Advanced Guide to Python 3 Programming, 2nd 2nd Edition John Huntdownload
Advanced Guide to Python 3 Programming, 2nd 2nd Edition John Huntdownload
https://ebookmass.com/product/advanced-guide-to-
python-3-programming-2nd-2nd-edition-john-hunt/
https://ebookmass.com/product/a-beginners-guide-to-
python-3-programming-2nd-edition-john-hunt/
https://ebookmass.com/product/python-programming-3-books-
in-1-ultimate-beginners-intermediate-advanced-guide-to-learn-python-
step-by-step/
https://ebookmass.com/product/learning-scientific-programming-with-
python-2nd-edition-christian-hill-2/
https://ebookmass.com/product/learning-scientific-programming-with-
python-2nd-edition-christian-hill/
Python Mini Reference 2022: A Quick Guide to the Modern
Python Programming Language for Busy Coders (A
Hitchhiker's Guide to the Modern Programming Languages
Book 3) Harry Yoon
https://ebookmass.com/product/python-mini-reference-2022-a-quick-
guide-to-the-modern-python-programming-language-for-busy-coders-a-
hitchhikers-guide-to-the-modern-programming-languages-book-3-harry-
yoon/
https://ebookmass.com/product/python-for-beginners-master-python-
programming-from-basics-to-advanced-level-tim-simon/
https://ebookmass.com/product/introduction-to-computation-and-
programming-using-python-third-edition-john-v-guttag/
https://ebookmass.com/product/a-guide-to-assessments-that-work-2nd-
edition-john-hunsley-editor/
Undergraduate Topics in Computer Science
John Hunt
Advanced Guide
to Python 3
Programming
Second Edition
Undergraduate Topics in Computer Science
Series Editor
Ian Mackie, University of Sussex, Brighton, UK
Advisory Editors
Samson Abramsky , Department of Computer Science, University of Oxford,
Oxford, UK
Chris Hankin , Department of Computing, Imperial College London, London,
UK
Mike Hinchey , Lero – The Irish Software Research Centre, University of
Limerick, Limerick, Ireland
Dexter C. Kozen, Department of Computer Science, Cornell University, Ithaca,
NY, USA
Andrew Pitts , Department of Computer Science and Technology, University of
Cambridge, Cambridge, UK
Hanne Riis Nielson , Department of Applied Mathematics and Computer
Science, Technical University of Denmark, Kongens Lyngby, Denmark
Steven S. Skiena, Department of Computer Science, Stony Brook University,
Stony Brook, NY, USA
Iain Stewart , Department of Computer Science, Durham University, Durham,
UK
Joseph Migga Kizza, College of Engineering and Computer Science, The
University of Tennessee-Chattanooga, Chattanooga, TN, USA
‘Undergraduate Topics in Computer Science’ (UTiCS) delivers high-quality instruc-
tional content for undergraduates studying in all areas of computing and information
science. From core foundational and theoretical material to final-year topics and
applications, UTiCS books take a fresh, concise, and modern approach and are ideal
for self-study or for a one- or two-semester course. The texts are all authored by
established experts in their fields, reviewed by an international advisory board, and
contain numerous examples and problems, many of which include fully worked
solutions.
The UTiCS concept relies on high-quality, concise books in softback format, and
generally a maximum of 275–300 pages. For undergraduate textbooks that are likely
to be longer, more expository, Springer continues to offer the highly regarded Texts
in Computer Science series, to which we refer potential authors.
John Hunt
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of
the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology
now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication
does not imply, even in the absence of a specific statement, that such names are exempt from the relevant
protective laws and regulations and therefore free for general use.
The publisher, the authors, and the editors are safe to assume that the advice and information in this book
are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or
the editors give a warranty, expressed or implied, with respect to the material contained herein or for any
errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
This Springer imprint is published by the registered company Springer Nature Switzerland AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
For Denise, my wife, my soulmate, my best
friend.
Preface to the Second Edition
This second edition represents a significant expansion of the material in the first
edition, as well as an update of that book from Python 3.7 to 3.12.
This book includes whole new sections on advanced language features, Reactive
Programming in Python and data analysts. New chapters on working with Tkinter,
on event handling with Tkinter and a simple drawing application using Tkinter have
been added. A new chapter on performance monitoring and profiling has also been
added. A chapter on pip and conda is included at the end of the book.
In all there are 18 completely new chapters that take you far further on your Python
journey. Enjoy the book and I hope you find it useful.
vii
Preface to the First Edition
You can of course just read this book; however following the examples in this book
will ensure that you get as much as possible out of the content. For this you will need
a computer.
Python is a cross-platform programming language, and as such you can use Python
on a Windows PC, a Linux box, an Apple Mac, etc. So you are not tied to a particular
type of operating system; you can use whatever you have available.
However you will need to install some software on that computer. At a minimum
you will need Python. The focus of this book is Python 3 so that is the version that
is assumed for all examples and exercises. As Python is available for a wide range
ix
x Preface to the First Edition
of platforms from Windows, to Mac OS and Linux, you will need to ensure that you
download the version for your operating system.
Python can be downloaded from the main Python website which can be found at
http://www.python.org/.
You will also need some form of editor to write your programs. There are numerous
generic programming editors available for different operating systems with VIM on
Linux, Notepad++ on Windows and Sublime Text on windows and Macs being
popular choices.
However, using an Integrated Development Environment (IDE) editor such as
PyCharm, Visual Studio Code or Spyder can make writing and running your programs
much easier.
However, this book does not assume any particular editor, IDE or environment
(other than Python 3 itself).
Conventions
Throughout this book you will find a number of conventions used for text styles.
These text styles distinguish between different kinds of information. Code words,
variable and Python values, used within the main body of the text, are shown using
a Courier font. A block of Python code is set out as shown here:
Preface to the First Edition xi
Note that keywords and points of interest are shown in bold font.
Any command line or user input is shown in standard font as shown below, for
example:
Hello, world
Enter your name: John
Hello John
The examples used in this book (along with sample solutions for the exercises at the
end of most chapters) are available in a GitHub repository. GitHub provides a web
interface to Git, as well as a server environment hosting Git.
Git is a version control system typically used to manage source code files (such
as those used to create systems in programming languages such as Python but also
Java, C#, C++ and Scala). Systems such as Git are very useful for collaborative
development as they allow multiple people to work on an implementation and to
merge their work together. They also provide a useful historical view of the code
(which also allows developers to roll back changes if modifications prove to be
unsuitable).
The GitHub repository for this book can be found at:
• https://github.com/johnehunt/advancedpython3_2nd
If you already have Git installed on your computer, then you can clone (obtain a
copy of) the repository locally using:
git clone https://github.com/johnehunt/advancedpyth
on3_2nd.git
If you do not have Git, then you can obtain a zip file of the examples using
https://github.com/johnehunt/advancedpython3_2nd/arc
hive/refs/heads/main.zip
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
xii Preface to the First Edition
You can of course install Git yourself if you wish. To do this, see https://git-scm.
com/downloads. Versions of the Git client for Mac OS, Windows and Linux/Unix
are available here.
However, many IDEs such as PyCharm come with Git support and so offer another
approach to obtaining a Git repository.
For more information on Git see http://git-scm.com/doc. This Git guide
provides a very good primer and is highly recommended.
Acknowledgement I would like to thank Phoebe Hunt for creating the pixel images used for the
Starship Meteors game in Chap. 22.
Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Useful Python Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
xiii
xiv Contents
Part IV Testing
23 Introduction to Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
23.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
23.2 Types of Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
23.3 What Should Be Tested? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
23.4 Types of Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
23.4.1 Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
23.4.2 Integration Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
23.4.3 System Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
23.4.4 Installation/Upgrade Testing . . . . . . . . . . . . . . . . . . . . . 252
23.4.5 Smoke Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
23.5 Automating Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
23.6 Test-Driven Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
23.6.1 The TDD Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
23.6.2 Test Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
23.6.3 Refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
23.7 Design for Testability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
23.7.1 Testability Rules of Thumb . . . . . . . . . . . . . . . . . . . . . . 255
23.8 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
23.9 Book Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
xx Contents
Language: Finnish
METSÄN POVESSA
Tuokiokuva 1:ssä näytöksessä
Kirj.
Raiju Ranta
Rajala, viinankeittäjä.
Rietu, hänen vaimonsa.
Leiniö, |
Pekkala, | salametsästäjiä.
Vallesmanni.
Raksi, metsänvartija.
Miehiä.
Näyttämö:
Leiniö: Hyvä juttu! Ei, mutta istu nyt sinäkin. Minua todellakaan ei
huvita enää hyppääminen. Kun vähän huilataan niin palataan kotiin.
— Pääni on niin hemmetin kipeä.
Pekkala: Ole hiidessä! Mitä sinä akalla tekisit. Hyvä kun itse saat
suuhusi pantavaa, vielä sitte akalle, ha, ha!
Leiniö: Juuri senvuoksi, kun itselläni ei ole mitään, olen päättänyt
ottaa sellaisen, jolla jotain on minullekin antaa.
Pekkala: Otat jos saat. — Ketä sinä muuten meinaat, saisiko tuota
tietää?
Pekkala: Ha, ha, ha, onpa sinusta jo tullut aivan pähkä hullu,
huomaan. Vai lukkarin leskeä, ha, ha, ha! Mutta sen sanon, että se
leski ei katso korvillaankaan sinua. Vallesmannihan sitä kuuluu
hyvänä pitävän. Olisit sinäkin iskenyt Sepän Eevaan, rahoja silläkin
oli — vanhalla piialla.
Ja se rakkauden kipinä
Joka rinnassa riksuttaa,
Ei taida ennen sammua
Kuin kuolla kupsahtaa.
Tukkipoika se lautallansa
On niin iso herra,
Huh, hei mä laulan vaan
Ja on niin iso herra.
Pekkala: Ei; en ole Heikkiä nähnyt pitkiin aikoikin. Oliko sillä asiaa
minulle?
Nyt on herttainen aamu, eikö totta. Oi, oi, oi, jospa ihminen saisi
aina maata ja laulaa ja olla huolehtimatta huomisesta päivästä. Se
olisi paratiisi-elämää. (Veisaamista kuuluu etäältä, läheten.) Tuolla
yläilmoissa on varmaan yhtä tyyntä kuin täällä alhaallakin, koska
veisuu kirkosta noin selvään kuuluu. Mitä sinä tuumailet?
(Piiloutuvat.)
Rietu: Sehän se… ei ollut itse… mutta kyllä minä jyviä sain.
Rajala: Missä sinä sitten juorusit. Sitä pitää liikkua työn aikana,
eikä uneksia.
Rajala: Haukut. Eikö ole syytä sitten. Koko päivän yhdellä asialla
viivyt.
Rietu (pelon sekaisesti): Olisinhan minä kyllä pikemminkin
joutunut, mutta kun näin vallesmannin, miehiä mukanaan, tulevan
tänne korpeen, niin tein pitkän kierroksen, etteivät nähneet minua.
Rajala: Älä siinä turhia höpise! Mistä ne tietävät, missä sinä olet,
kun pidät suusi lukossa.
Rietu: Voi, voi sentään, sinua Rajala. Kyllä sinun pitäisi lopettaa
tämä viheliäinen viinankeitto. — Ei Jumala salli sinun enää kauan
pilkata itseään, näin sunnuntaisin varsinkin… joudumme vielä tästä
linnaan… Voi voi sentään…
Leiniö: Ha, ha, ha! Kaikkia sitä näkee ja kuulee, ennenkuin kuolee.
Nyt Pekkala, meillä on kala koukussa, emmekä sitä laske
käsistämme. Ole sinä täällä hetkinen, minä otan selvää missäpäin se
herrasväen viinakanuuna on… Eikös se ollut Rajala, tuo äijä?
Leiniö: Mitä? Joko sinua rupeaa jänistämään? No, jos tahdot, niin
mene, minä ainakin jään tänne ja otan asiasta selvän.
Pekkala: Enhän minä nyt yksin, mutta jos sinäkin tulet, niin…
Raksi: Voi hyvä mies, se hakee juuri sinua, kuten kuulit, jos
kuuntelit. Miten me nyt pääsemme pälkähästä, jos ne sattuvat
aparaattisi löytämään?
Rajala: Akka kävi hakemassa vakallisen. Hän jäi sinne pannun luo
vahtiin, kun minun pitää mennä kotoa viinalekku hakemaan. Unhotin
sen.
(Menee.)
Pekkala: Ha, ha, haa! Kuulitko mitä Rajala kertoi lukkarin leskestä?
Vieläkö tekee mielesi isännäksi hänen taloonsa?
Leiniö (Koettaa avata astiaa): Joko jo? — Aina sitä saa kun
vuovaa… Hyvä mies, kelpo mies se Rajala. — Äh! Onko sulla
puukkoa mukana?
Pekkala: Aijotko sitten avata sen täällä? Eiköhän sentään olisi
viisainta, että mentäisi kotiin saaliinemme. Ymmärrät kyllä miksi.
Koirajoukko on kintereillä ja minulla, tottavieköön, ei ole halua
joutua niitten pureskeltavaksi.
Leiniö: Elä sure… Kas niin, nyt maistetaan. Saakuri kun ei ole
mitään astiaa. Täytyy imeä tapinreiästä. Maista nyt.
Pekkala: Ei, ota sinä ensin, sinäpä siinä suuremman työnkin olet
tehnyt.
Leiniö: Ha, ha, ha, enkö sitä sanonut! Mutta kun tottuu niin
harjaantuu, sano entinen äijä. (Juo.) Siitä saamme oivallista tavaraa,
kun lisäämme pari saavillista vettä. Nyt on meillä pitkiksi ajoiksi
känninkiä. Ota vielä kulaus ja sitten lähdemme saaliinemme kotiin.
Pekkala (estellen): En, en ota enää. Tuntuu jo pääni olevan täysi.
Jos vielä otan, en pääse paikaltani liikahtamaan.
Pekkala (naurahtaen): Ha, ha, haa! Mutta kuka meidät sitten kotiin
laahaa, kun tulemme aivan äälättömäksi.
(Ojentaa kätensä.)
(Äänettömyys.)
Pekkala (kirkaisee): Ai, ai, ai! Sääreni taittuvat!
(Lyhyt äänettömyys.)
Rajala (its.): Olisipa onni, jos eivät löytäisi. Mutta mitenkä pääsisin
minä tästä ikävästä asemasta?
ebookmasss.com