Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
46 views

Full download Advanced Guide to Python 3 Programming Hunt pdf docx

Advanced

Uploaded by

quinomunksik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
46 views

Full download Advanced Guide to Python 3 Programming Hunt pdf docx

Advanced

Uploaded by

quinomunksik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Download the Full Version of textbook for Fast Typing at textbookfull.

com

Advanced Guide to Python 3 Programming Hunt

https://textbookfull.com/product/advanced-guide-to-
python-3-programming-hunt/

OR CLICK BUTTON

DOWNLOAD NOW

Download More textbook Instantly Today - Get Yours Now at textbookfull.com


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

Python Advanced Programming: The guide to learn pyhton


programming Marcus Richards

https://textbookfull.com/product/python-advanced-programming-the-
guide-to-learn-pyhton-programming-marcus-richards/

textboxfull.com

Learn to Program with Python 3: A Step-by-Step Guide to


Programming Irv Kalb

https://textbookfull.com/product/learn-to-program-with-
python-3-a-step-by-step-guide-to-programming-irv-kalb/

textboxfull.com

Learn to Program with Python 3: A Step-by-Step Guide to


Programming, 2nd Edition Irv Kalb

https://textbookfull.com/product/learn-to-program-with-
python-3-a-step-by-step-guide-to-programming-2nd-edition-irv-kalb/

textboxfull.com

A Beginner’s Guide to Scala, Object Orientation and


Functional Programming John Hunt

https://textbookfull.com/product/a-beginners-guide-to-scala-object-
orientation-and-functional-programming-john-hunt/

textboxfull.com
A Beginner's Guide to Scala, Object Orientation and
Functional Programming (Second Edition) John Hunt

https://textbookfull.com/product/a-beginners-guide-to-scala-object-
orientation-and-functional-programming-second-edition-john-hunt/

textboxfull.com

Practical Programming An Introduction to Computer Science


Using Python 3 6 3rd Edition Paul Gries

https://textbookfull.com/product/practical-programming-an-
introduction-to-computer-science-using-python-3-6-3rd-edition-paul-
gries/
textboxfull.com

Python 3 Object Oriented Programming 3rd Edition Dusty


Phillips [Dusty Phillips]

https://textbookfull.com/product/python-3-object-oriented-
programming-3rd-edition-dusty-phillips-dusty-phillips/

textboxfull.com

Python Basics A Practical Introduction to Python 3


Fletcher Heisler

https://textbookfull.com/product/python-basics-a-practical-
introduction-to-python-3-fletcher-heisler/

textboxfull.com

Learn Python Programming A beginner s guide to learning


the fundamentals of Python language to write efficient
high quality code Romano
https://textbookfull.com/product/learn-python-programming-a-beginner-
s-guide-to-learning-the-fundamentals-of-python-language-to-write-
efficient-high-quality-code-romano/
textboxfull.com
Undergraduate Topics in Computer Science

John Hunt

Advanced Guide
to Python 3
Programming
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
Dexter C. Kozen, Department of Computer Science, Cornell University, Ithaca, NY,
USA
Andrew Pitts, 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, Science Labs, University of
Durham, Durham, UK
Mike Hinchey, University of Limerick, Limerick, Ireland
‘Undergraduate Topics in Computer Science’ (UTiCS) delivers high-quality
instructional 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.

More information about this series at http://www.springer.com/series/7592


John Hunt

Advanced Guide to Python 3


Programming

123
John Hunt
Marshfield
Midmarsh Technology Ltd.
Chippenham, Wiltshire, UK

ISSN 1863-7310 ISSN 2197-1781 (electronic)


Undergraduate Topics in Computer Science
ISBN 978-3-030-25942-6 ISBN 978-3-030-25943-3 (eBook)
https://doi.org/10.1007/978-3-030-25943-3
© Springer Nature Switzerland AG 2019
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.
Preface

Some of the key aspects of this book are:


1. It assumes knowledge of Python 3 and of concepts such as functions, classes,
protocols, Abstract Base Classes, decorators, iterables, collection types (such as
List and Tuple) etc.
2. However, the book assumes very little knowledge or experience of the topics
presented.
3. The book is divided into eight topic areas; Computer graphics, Games, Testing,
File Input/Output, Database Access, Logging, Concurrency and Parallelism and
Network Programming.
4. Each topic in the book has an introductory chapter followed by chapters that
delve into that topic.
5. The book includes exercises at the end of most chapters.
6. All code examples (and exercise solutions) are provided on line in a GitHub
repository.

Chapter Organisation

Each chapter has a brief introduction, the main body of the chapter, followed by a
list of online references that can be used for further reading.
Following this there is typically an Exercises section that lists one or more
exercises that build on the skills you will have learnt in that chapter.
Sample solutions to the exercises are available in a GitHub repository that
supports this book.

vii
viii Preface

What You Need

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 or an Apple Mac etc. This means that 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 your computer. At a mini-
mum 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 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 web site which can be found at
http://www.python.org.

You will also need some form of editor in which 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.
Preface ix

However, using a IDE (Integrated Development Environment) editor such as


PyCharm can make writing and running your programs much easier.
However, this book doesn’t assume any particular editor, IDE or environment
(other than Python 3 itself).

Python Versions

Currently there are two main versions of Python called Python 2 and Python 3.
• Python 2 was launched in October 2000 and has been, and still is, very widely used.
• Python 3 was launched in December 2008 and is a major revision to the lan-
guage that is not backward compatible.
The issues between the two versions can be highlighted by the simple print
facility:
• In Python 2 this is written as print ‘Hello World’
• In Python 3 this is written as print (‘Hello World’)
It may not look like much of a difference but the inclusion of the ‘()’ marks a
major change and means that any code written for one version of Python will
probably not run on the other version. There are tools available, such as the 2to3
utility, that will (partially) automate translation from Python 2 to Python 3 but in
general you are still left with significant work to do.
This then raises the question which version to use?
Although interest in Python 3 is steadily increasing there are many organisations
that are still using Python 2. Choosing which version to use is a constant concern
for many companies.
However, the Python 2 end of life plan was initially announced back in 2015 and
although it has been postponed to 2020 out of concern that a large body of existing
code could not easily be forward-ported to Python 3, it is still living on borrowed
time. Python 3 is the future of the Python language and it is this version that has
introduced many of the new and improved language and library features (that have
admittedly been back ported to Python 2 in many cases). This book is solely
focussed on Python 3.

Useful Python Resources

There are a wide range of resources on the web for Python; we will highlight a few
here that you should bookmark. We will not keep referring to these to avoid
repetition but you can refer back to this section whenever you need to:
• https://en.wikipedia.org/wiki/Python_Software_Foundation Python Software
Foundation.
x Preface

• https://docs.python.org/3/ The main Python 3 documentation site. It contains


tutorials, library references, set up and installation guides as well as Python
how-tos.
• https://docs.python.org/3/library/index.html A list of all the builtin features for
the Python language—this is where you can find online documentation for the
various class and functions that we will be using throughout this book.
• https://pymotw.com/3/ the Python 3 Module of the week site. This site contains
many, many Python modules with short examples and explanations of what the
modules do. A Python module is a library of features that build on and expand
the core Python language. For example, if you are interested in building games
using Python then pygame is a module specifically designed to make this easier.
• https://www.fullstackpython.com/email.html is a monthly newsletter that
focusses on a single Python topic each month, such as a new library or module.
• http://www.pythonweekly.com/ is a free weekly summary of the latest Python
articles, projects, videos and upcoming events.
Each section of the book will provide additional online references relevant to the
topic being discussed.

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. For example:

This program creates a top level window (the wx.Frame) and gives it a title. It also creates
a label (a wx.StaticText object) to be displayed within the frame.

In the above paragraph wx.Frame and wx.StaticText are classes available in a


Python graphical user interface library.
A block of Python code is set out as shown here:
Preface xi

Note that keywords are shown in bold font.


In some cases something of particular interest may be highlighted with colour:

Any command line or user input is shown in italics and coloured purple; for
example:

Or

Example Code and Sample Solutions

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++, Scala etc.). 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).
If you already have Git installed on your computer then you can clone (obtain a
copy of) the repository locally using:
xii Preface

If you do not have Git then you can obtain a zip file of the examples using

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.

Acknowledgements I would like to thank Phoebe Hunt for creating the pixel images used for the
StarshipMeteors game in Chap. 8.
Contents

1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Part I Computer Graphics


2 Introduction to Computer Graphics . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 The Graphical Computer Era . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4 Interactive and Non Interactive Graphics . . . . . . . . . . . . . . . . 7
2.5 Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.6 Bit Map Versus Vector Graphics . . . . . . . . . . . . . . . . . . . . . . 10
2.7 Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.8 Python and Computer Graphics . . . . . . . . . . . . . . . . . . . . . . . 10
2.9 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.10 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3 Python Turtle Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 The Turtle Graphics Library . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2.1 The Turtle Module . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2.2 Basic Turtle Graphics . . . . . . . . . . . . . . . . . . . . . . . 14
3.2.3 Drawing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.2.4 Filling Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.3 Other Graphics Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.4 3D Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.4.1 PyOpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.5 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

xiii
xiv Contents

4 Computer Generated Art . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23


4.1 Creating Computer Art . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.2 A Computer Art Generator . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.3 Fractals in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.3.1 The Koch Snowflake . . . . . . . . . . . . . . . . . . . . . . . . 28
4.3.2 Mandelbrot Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.4 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5 Introduction to Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.2 Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
5.3 Plot Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.4 Matplotlib Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.4.1 Backend Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.4.2 The Artist Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
5.4.3 The Scripting Layer . . . . . . . . . . . . . . . . . . . . . . . . 41
5.5 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6 Graphing with Matplotlib pyplot . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6.2 The pyplot API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6.3 Line Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
6.3.1 Coded Format Strings . . . . . . . . . . . . . . . . . . . . . . . 46
6.4 Scatter Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.4.1 When to Use Scatter Graphs . . . . . . . . . . . . . . . . . . 49
6.5 Pie Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
6.5.1 Expanding Segments . . . . . . . . . . . . . . . . . . . . . . . . 52
6.5.2 When to Use Pie Charts . . . . . . . . . . . . . . . . . . . . . 53
6.6 Bar Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
6.6.1 Horizontal Bar Charts . . . . . . . . . . . . . . . . . . . . . . . 55
6.6.2 Coloured Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
6.6.3 Stacked Bar Charts . . . . . . . . . . . . . . . . . . . . . . . . . 57
6.6.4 Grouped Bar Charts . . . . . . . . . . . . . . . . . . . . . . . . 58
6.7 Figures and Subplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
6.8 3D Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
6.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
7 Graphical User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
7.2 GUIs and WIMPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Contents xv

7.3 Windowing Frameworks for Python . . . . . . . . . . . . . . . . . . . . 69


7.3.1 Platform-Independent GUI Libraries . . . . . . . . . . . . . 70
7.3.2 Platform-Specific GUI Libraries . . . . . . . . . . . . . . . . 70
7.4 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
8 The wxPython GUI Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
8.1 The wxPython Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
8.1.1 wxPython Modules . . . . . . . . . . . . . . . . . . . . . . . . . 74
8.1.2 Windows as Objects . . . . . . . . . . . . . . . . . . . . . . . . 75
8.1.3 A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . 75
8.2 The wx.App Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
8.3 Window Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
8.4 Widget/Control Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
8.5 Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
8.6 Arranging Widgets Within a Container . . . . . . . . . . . . . . . . . . 82
8.7 Drawing Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
8.8 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
8.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
8.9.1 Simple GUI Application . . . . . . . . . . . . . . . . . . . . . 86
9 Events in wxPython User Interfaces . . . . . . . . . .. . . . . . . . . . . . . . 87
9.1 Event Handling . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 87
9.2 Event Definitions . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 87
9.3 Types of Events . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 88
9.4 Binding an Event to an Event Handler . . . .. . . . . . . . . . . . . . 89
9.5 Implementing Event Handling . . . . . . . . . .. . . . . . . . . . . . . . 89
9.6 An Interactive wxPython GUI . . . . . . . . . .. . . . . . . . . . . . . . 92
9.7 Online Resources . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 96
9.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 96
9.8.1 Simple GUI Application . . . . . . .. . . . . . . . . . . . . . 96
9.8.2 GUI Interface to a Tic Tac Toe Game . . . . . . . . . . . 98
10 PyDraw wxPython Example Application . . . . . . . . . . . . . . . . . . . . . 99
10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
10.2 The PyDraw Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
10.3 The Structure of the Application . . . . . . . . . . . . . . . . . . . . . . 100
10.3.1 Model, View and Controller Architecture . . . . . . . . . 101
10.3.2 PyDraw MVC Architecture . . . . . . . . . . . . . . . . . . . 102
10.3.3 Additional Classes . . . . . . . . . . . . . . . . . . . . . . . . . 103
10.3.4 Object Relationships . . . . . . . . . . . . . . . . . . . . . . . . 104
10.4 The Interactions Between Objects . . . . . . . . . . . . . . . . . . . . . 105
10.4.1 The PyDrawApp . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
10.4.2 The PyDrawFrame Constructor . . . . . . . . . . . . . . . . 106
xvi Contents

10.4.3 Changing the Application Mode . . . . . . . . . . . . . . . 106


10.4.4 Adding a Graphic Object . . . . . . . . . . . . . . . . . . . . 107
10.5 The Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
10.5.1 The PyDrawConstants Class . . . . . . . . . . . . . . . . . . 108
10.5.2 The PyDrawFrame Class . . . . . . . . . . . . . . . . . . . . . 109
10.5.3 The PyDrawMenuBar Class . . . . . . . . . . . . . . . . . . 110
10.5.4 The PyDrawToolBar Class . . . . . . . . . . . . . . . . . . . 111
10.5.5 The PyDrawController Class . . . . . . . . . . . . . . . . . . 111
10.5.6 The DrawingModel Class . . . . . . . . . . . . . . . . . . . . 113
10.5.7 The DrawingPanel Class . . . . . . . . . . . . . . . . . . . . . 113
10.5.8 The DrawingController Class . . . . . . . . . . . . . . . . . . 114
10.5.9 The Figure Class . . . . . . . . . . . . . . . . . . . . . . . . . . 115
10.5.10 The Square Class . . . . . . . . . . . . . . . . . . . . . . . . . . 115
10.5.11 The Circle Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
10.5.12 The Line Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
10.5.13 The Text Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
10.6 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
10.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

Part II Computer Games


11 Introduction to Games Programming . . . . . . . . . . . . . . . . . . . . . . . 121
11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
11.2 Games Frameworks and Libraries . . . . . . . . . . . . . . . . . . . . . 121
11.3 Python Games Development . . . . . . . . . . . . . . . . . . . . . . . . . 122
11.4 Using Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
11.5 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
12 Building Games with pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
12.2 The Display Surface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
12.3 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
12.3.1 Event Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
12.3.2 Event Information . . . . . . . . . . . . . . . . . . . . . . . . . . 128
12.3.3 The Event Queue . . . . . . . . . . . . . . . . . . . . . . . . . . 129
12.4 A First pygame Application . . . . . . . . . . . . . . . . . . . . . . . . . . 130
12.5 Further Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
12.6 A More Interactive pygame Application . . . . . . . . . . . . . . . . . 136
12.7 Alternative Approach to Processing Input Devices . . . . . . . . . 138
12.8 pygame Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
12.9 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Contents xvii

13 StarshipMeteors pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141


13.1 Creating a Spaceship Game . . . . . . . . . . . . . . . . . . . . . . . . . . 141
13.2 The Main Game Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
13.3 The GameObject Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
13.4 Displaying the Starship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
13.5 Moving the Spaceship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
13.6 Adding a Meteor Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
13.7 Moving the Meteors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
13.8 Identifying a Collision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
13.9 Identifying a Win . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
13.10 Increasing the Number of Meteors . . . . . . . . . . . . . . . . . . . . . 154
13.11 Pausing the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
13.12 Displaying the Game Over Message . . . . . . . . . . . . . . . . . . . . 156
13.13 The StarshipMeteors Game . . . . . . . . . . . . . . . . . . . . . . . . . . 157
13.14 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
13.15 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

Part III Testing


14 Introduction to Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
14.2 Types of Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
14.3 What Should Be Tested? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
14.4 Testing Software Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
14.4.1 Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
14.4.2 Integration Testing . . . . . . . . . . . . . . . . . . . . . . . . . 169
14.4.3 System Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
14.4.4 Installation/Upgrade Testing . . . . . . . . . . . . . . . . . . 170
14.4.5 Smoke Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
14.5 Automating Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
14.6 Test Driven Development . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
14.6.1 The TDD Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
14.6.2 Test Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
14.6.3 Refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
14.7 Design for Testability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
14.7.1 Testability Rules of Thumb . . . . . . . . . . . . . . . . . . . 173
14.8 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
14.9 Book Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
15 PyTest Testing Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
15.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
15.2 What Is PyTest? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
15.3 Setting Up PyTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
15.4 A Simple PyTest Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
xviii Contents

15.5 Working with PyTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179


15.6 Parameterised Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
15.7 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
15.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
16 Mocking for Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
16.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
16.2 Why Mock? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
16.3 What Is Mocking? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
16.4 Common Mocking Framework Concepts . . . . . . . . . . . . . . . . 191
16.5 Mocking Frameworks for Python . . . . . . . . . . . . . . . . . . . . . . 192
16.6 The unittest.mock Library . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
16.6.1 Mock and Magic Mock Classes . . . . . . . . . . . . . . . . 193
16.6.2 The Patchers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
16.6.3 Mocking Returned Objects . . . . . . . . . . . . . . . . . . . 195
16.6.4 Validating Mocks Have Been Called . . . . . . . . . . . . 196
16.7 Mock and MagicMock Usage . . . . . . . . . . . . . . . . . . . . . . . . 197
16.7.1 Naming Your Mocks . . . . . . . . . . . . . . . . . . . . . . . 197
16.7.2 Mock Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
16.7.3 Attributes on Mock Classes . . . . . . . . . . . . . . . . . . . 198
16.7.4 Mocking Constants . . . . . . . . . . . . . . . . . . . . . . . . . 199
16.7.5 Mocking Properties . . . . . . . . . . . . . . . . . . . . . . . . . 199
16.7.6 Raising Exceptions with Mocks . . . . . . . . . . . . . . . . 199
16.7.7 Applying Patch to Every Test Method . . . . . . . . . . . 200
16.7.8 Using Patch as a Context Manager . . . . . . . . . . . . . 200
16.8 Mock Where You Use It . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
16.9 Patch Order Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
16.10 How Many Mocks? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
16.11 Mocking Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
16.12 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
16.13 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

Part IV File Input/Output


17 Introduction to Files, Paths and IO . . . . . . . . . . . . . . . . . . . . . . . . . 207
17.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
17.2 File Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
17.3 Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
17.4 File Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
17.5 Sequential Access Versus Random Access . . . . . . . . . . . . . . . 213
17.6 Files and I/O in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
17.7 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Contents xix

18 Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215


18.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
18.2 Obtaining References to Files . . . . . . . . . . . . . . . . . . . . . . . . 215
18.3 Reading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
18.4 File Contents Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
18.5 Writing Data to Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
18.6 Using Files and with Statements . . . . . . . . . . . . . . . . . . . . . . 219
18.7 The Fileinput Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
18.8 Renaming Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
18.9 Deleting Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
18.10 Random Access Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
18.11 Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
18.12 Temporary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
18.13 Working with Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
18.14 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
18.15 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
19 Stream IO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
19.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
19.2 What is a Stream? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
19.3 Python Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
19.4 IOBase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
19.5 Raw IO/UnBuffered IO Classes . . . . . . . . . . . . . . . . . . . . . . . 234
19.6 Binary IO/Buffered IO Classes . . . . . . . . . . . . . . . . . . . . . . . . 234
19.7 Text Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
19.8 Stream Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
19.9 Closing Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
19.10 Returning to the open() Function . . . . . . . . . . . . . . . . . . . . . . 238
19.11 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
19.12 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
20 Working with CSV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
20.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
20.2 CSV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
20.2.1 The CSV Writer Class . . . . . . . . . . . . . . . . . . . . . . 242
20.2.2 The CSV Reader Class . . . . . . . . . . . . . . . . . . . . . . 243
20.2.3 The CSV DictWriter Class . . . . . . . . . . . . . . . . . . . 244
20.2.4 The CSV DictReader Class . . . . . . . . . . . . . . . . . . . 245
20.3 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
20.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
21 Working with Excel Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
21.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
21.2 Excel Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
xx Contents

21.3 The Openpyxl. Workbook Class . . . . . . . . . . . . . . . . . . . . . . 250


21.4 The Openpyxl. WorkSheet Objects . . . . . . . . . . . . . . . . . . . . . 250
21.5 Working with Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
21.6 Sample Excel File Creation Application . . . . . . . . . . . . . . . . . 251
21.7 Loading a Workbook from an Excel File . . . . . . . . . . . . . . . . 253
21.8 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
21.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
22 Regular Expressions in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
22.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
22.2 What Are Regular Expressions? . . . . . . . . . . . . . . . . . . . . . . . 257
22.3 Regular Expression Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . 258
22.3.1 Pattern Metacharacters . . . . . . . . . . . . . . . . . . . . . . 259
22.3.2 Special Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . 259
22.3.3 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
22.4 The Python re Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
22.5 Working with Python Regular Expressions . . . . . . . . . . . . . . . 261
22.5.1 Using Raw Strings . . . . . . . . . . . . . . . . . . . . . . . . . 261
22.5.2 Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
22.5.3 The Match Object . . . . . . . . . . . . . . . . . . . . . . . . . . 262
22.5.4 The search() Function . . . . . . . . . . . . . . . . . . . . . . . 263
22.5.5 The match() Function . . . . . . . . . . . . . . . . . . . . . . . 264
22.5.6 The Difference Between Matching and Searching . . . 265
22.5.7 The findall() Function . . . . . . . . . . . . . . . . . . . . . . . 265
22.5.8 The finditer() Function . . . . . . . . . . . . . . . . . . . . . . 266
22.5.9 The split() Function . . . . . . . . . . . . . . . . . . . . . . . . 266
22.5.10 The sub() Function . . . . . . . . . . . . . . . . . . . . . . . . . 267
22.5.11 The compile() Function . . . . . . . . . . . . . . . . . . . . . . 268
22.6 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
22.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

Part V Database Access


23 Introduction to Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
23.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
23.2 What Is a Database? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
23.2.1 Data Relationships . . . . . . . . . . . . . . . . . . . . . . . . . 276
23.2.2 The Database Schema . . . . . . . . . . . . . . . . . . . . . . . 277
23.3 SQL and Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
23.4 Data Manipulation Language . . . . . . . . . . . . . . . . . . . . . . . . . 280
23.5 Transactions in Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
23.6 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Contents xxi

24 Python DB-API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283


24.1 Accessing a Database from Python . . . . . . . . . . . . . . . . . . . . 283
24.2 The DB-API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
24.2.1 The Connect Function . . . . . . . . . . . . . . . . . . . . . . . 284
24.2.2 The Connection Object . . . . . . . . . . . . . . . . . . . . . . 284
24.2.3 The Cursor Object . . . . . . . . . . . . . . . . . . . . . . . . . 285
24.2.4 Mappings from Database Types to Python Types . . . 286
24.2.5 Generating Errors . . . . . . . . . . . . . . . . . . . . . . . . . . 286
24.2.6 Row Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . 287
24.3 Transactions in PyMySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
24.4 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
25 PyMySQL Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
25.1 The PyMySQL Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
25.2 Working with the PyMySQL Module . . . . . . . . . . . . . . . . . . . 291
25.2.1 Importing the Module . . . . . . . . . . . . . . . . . . . . . . . 292
25.2.2 Connect to the Database . . . . . . . . . . . . . . . . . . . . . 292
25.2.3 Obtaining the Cursor Object . . . . . . . . . . . . . . . . . . 293
25.2.4 Using the Cursor Object . . . . . . . . . . . . . . . . . . . . . 293
25.2.5 Obtaining Information About the Results . . . . . . . . . 294
25.2.6 Fetching Results . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
25.2.7 Close the Connection . . . . . . . . . . . . . . . . . . . . . . . 295
25.3 Complete PyMySQL Query Example . . . . . . . . . . . . . . . . . . . 295
25.4 Inserting Data to the Database . . . . . . . . . . . . . . . . . . . . . . . . 296
25.5 Updating Data in the Database . . . . . . . . . . . . . . . . . . . . . . . . 298
25.6 Deleting Data in the Database . . . . . . . . . . . . . . . . . . . . . . . . 299
25.7 Creating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
25.8 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
25.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301

Part VI Logging
26 Introduction to Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
26.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
26.2 Why Log? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
26.3 What Is the Purpose of Logging? . . . . . . . . . . . . . . . . . . . . . . 306
26.4 What Should You Log? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
26.5 What Not to Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
26.6 Why Not Just Use Print? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
26.7 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
27 Logging in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
27.1 The Logging Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
27.2 The Logger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
xxii Contents

27.3 Controlling the Amount of Information Logged . . . . . . . . . . . 313


27.4 Logger Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
27.5 Default Logger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
27.6 Module Level Loggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
27.7 Logger Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
27.8 Formatters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
27.8.1 Formatting Log Messages . . . . . . . . . . . . . . . . . . . . 319
27.8.2 Formatting Log Output . . . . . . . . . . . . . . . . . . . . . . 319
27.9 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
27.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
28 Advanced Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
28.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
28.2 Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
28.2.1 Setting the Root Output Handler . . . . . . . . . . . . . . . 325
28.2.2 Programmatically Setting the Handler . . . . . . . . . . . 326
28.2.3 Multiple Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . 328
28.3 Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
28.4 Logger Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
28.5 Performance Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . 333
28.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334

Part VII Concurrency and Parallelism


29 Introduction to Concurrency and Parallelism . . . . . . . . . . . . . . . . . 337
29.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
29.2 Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
29.3 Parallelism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
29.4 Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
29.5 Grid Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
29.6 Concurrency and Synchronisation . . . . . . . . . . . . . . . . . . . . . 342
29.7 Object Orientation and Concurrency . . . . . . . . . . . . . . . . . . . . 342
29.8 Threads V Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
29.9 Some Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
29.10 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
30 Threading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
30.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
30.2 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
30.3 Thread States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
30.4 Creating a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
30.5 Instantiating the Thread Class . . . . . . . . . . . . . . . . . . . . . . . . 349
30.6 The Thread Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Contents xxiii

30.7 The Threading Module Functions . . . . . . . . . . . . . . . . . . . . . . 352


30.8 Passing Arguments to a Thread . . . . . . . . . . . . . . . . . . . . . . . 352
30.9 Extending the Thread Class . . . . . . . . . . . . . . . . . . . . . . . . . . 354
30.10 Daemon Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
30.11 Naming Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
30.12 Thread Local Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
30.13 Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
30.14 The Global Interpreter Lock . . . . . . . . . . . . . . . . . . . . . . . . . . 359
30.15 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
30.16 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
31 Multiprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
31.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
31.2 The Process Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
31.3 Working with the Process Class . . . . . . . . . . . . . . . . . . . . . . . 365
31.4 Alternative Ways to Start a Process . . . . . . . . . . . . . . . . . . . . 366
31.5 Using a Pool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
31.6 Exchanging Data Between Processes . . . . . . . . . . . . . . . . . . . 372
31.7 Sharing State Between Processes . . . . . . . . . . . . . . . . . . . . . . 374
31.7.1 Process Shared Memory . . . . . . . . . . . . . . . . . . . . . 374
31.8 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
31.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
32 Inter Thread/Process Synchronisation . . . . . . . . . . . . . . . . . . . . . . . 377
32.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
32.2 Using a Barrier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
32.3 Event Signalling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
32.4 Synchronising Concurrent Code . . . . . . . . . . . . . . . . . . . . . . . 382
32.5 Python Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
32.6 Python Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
32.7 Python Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
32.8 The Concurrent Queue Class . . . . . . . . . . . . . . . . . . . . . . . . . 389
32.9 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
32.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
33 Futures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
33.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
33.2 The Need for a Future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
33.3 Futures in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
33.3.1 Future Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
33.3.2 Simple Example Future . . . . . . . . . . . . . . . . . . . . . . 397
33.4 Running Multiple Futures . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
33.4.1 Waiting for All Futures to Complete . . . . . . . . . . . . 400
33.4.2 Processing Results as Completed . . . . . . . . . . . . . . . 402
xxiv Contents

33.5 Processing Future Results Using a Callback . . . . . . . . . . . . . . 403


33.6 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
33.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
34 Concurrency with AsyncIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
34.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
34.2 Asynchronous IO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
34.3 Async IO Event Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
34.4 The Async and Await Keywords . . . . . . . . . . . . . . . . . . . . . . 409
34.4.1 Using Async and Await . . . . . . . . . . . . . . . . . . . . . 409
34.5 Async IO Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
34.6 Running Multiple Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
34.6.1 Collating Results from Multiple Tasks . . . . . . . . . . . 414
34.6.2 Handling Task Results as They Are Made
Available . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
34.7 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
34.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417

Part VIII Reactive Programming


35 Reactive Programming Introduction . . . . . . . . . . . . . . . . . . . . . . . . 421
35.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
35.2 What Is a Reactive Application? . . . . . . . . . . . . . . . . . . . . . . 421
35.3 The ReactiveX Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
35.4 The Observer Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
35.5 Hot and Cold Observables . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
35.5.1 Cold Observables . . . . . . . . . . . . . . . . . . . . . . . . . . 424
35.5.2 Hot Observables . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
35.5.3 Implications of Hot and Cold Observables . . . . . . . . 424
35.6 Differences Between Event Driven Programming and
Reactive Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
35.7 Advantages of Reactive Programming . . . . . . . . . . . . . . . . . . 425
35.8 Disadvantages of Reactive Programming . . . . . . . . . . . . . . . . 426
35.9 The RxPy Reactive Programming Framework . . . . . . . . . . . . . 426
35.10 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
35.11 Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
36 RxPy Observables, Observers and Subjects . . . . . . . . . . . . . . . . . . 429
36.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
36.2 Observables in RxPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
36.3 Observers in RxPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
36.4 Multiple Subscribers/Observers . . . . . . . . . . . . . . . . . . . . . . . 432
36.5 Subjects in RxPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Contents xxv

36.6 Observer Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435


36.6.1 Available Schedulers . . . . . . . . . . . . . . . . . . . . . . . . 437
36.7 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
36.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
37 RxPy Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
37.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
37.2 Reactive Programming Operators . . . . . . . . . . . . . . . . . . . . . . 439
37.3 Piping Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
37.4 Creational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
37.5 Transformational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 441
37.6 Combinatorial Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
37.7 Filtering Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
37.8 Mathematical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
37.9 Chaining Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
37.10 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
37.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448

Part IX Network Programming


38 Introduction to Sockets and Web Services . . . . . . . . . . . . . . . . . . . 451
38.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
38.2 Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
38.3 Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
38.4 Addressing Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
38.5 Localhost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
38.6 Port Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
38.7 IPv4 Versus IPv6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
38.8 Sockets and Web Services in Python . . . . . . . . . . . . . . . . . . . 455
38.9 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
39 Sockets in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
39.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
39.2 Socket to Socket Communication . . . . . . . . . . . . . . . . . . . . . . 457
39.3 Setting Up a Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
39.4 An Example Client Server Application . . . . . . . . . . . . . . . . . . 458
39.4.1 The System Structure . . . . . . . . . . . . . . . . . . . . . . . 458
39.4.2 Implementing the Server Application . . . . . . . . . . . . 459
39.5 Socket Types and Domains . . . . . . . . . . . . . . . . . . . . . . . . . . 461
39.6 Implementing the Client Application . . . . . . . . . . . . . . . . . . . 461
39.7 The Socketserver Module . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
39.8 HTTP Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
39.9 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
39.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
xxvi Contents

40 Web Services in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471


40.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
40.2 RESTful Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
40.3 A RESTful API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
40.4 Python Web Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
40.5 Flask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
40.6 Hello World in Flask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
40.6.1 Using JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
40.6.2 Implementing a Flask Web Service . . . . . . . . . . . . . 475
40.6.3 A Simple Service . . . . . . . . . . . . . . . . . . . . . . . . . . 475
40.6.4 Providing Routing Information . . . . . . . . . . . . . . . . 476
40.6.5 Running the Service . . . . . . . . . . . . . . . . . . . . . . . . 477
40.6.6 Invoking the Service . . . . . . . . . . . . . . . . . . . . . . . . 478
40.6.7 The Final Solution . . . . . . . . . . . . . . . . . . . . . . . . . 479
40.7 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
41 Bookshop Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
41.1 Building a Flask Bookshop Service . . . . . . . . . . . . . . . . . . . . 481
41.2 The Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
41.3 The Domain Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
41.4 Encoding Books Into JSON . . . . . . . . . . . . . . . . . . . . . . . . . . 484
41.5 Setting Up the GET Services . . . . . . . . . . . . . . . . . . . . . . . . . 486
41.6 Deleting a Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
41.7 Adding a New Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
41.8 Updating a Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
41.9 What Happens if We Get It Wrong? . . . . . . . . . . . . . . . . . . . 492
41.10 Bookshop Services Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
41.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Chapter 1
Introduction

1.1 Introduction

I have heard many people over the years say that Python is an easy language to lean
and that Python is also a simple language.
To some extent both of these statements are true; but only to some extent.
While the core of the Python language is easy to lean and relatively simple (in
part thanks to its consistency); the sheer richness of the language constructs and
flexibility available can be overwhelming. In addition the Python environment, its
eco system, the range of libraries available, the often competing options available
etc., can make moving to the next level daunting.
Once you have learned the core elements of the language such as how classes
and inheritance work, how functions work, what are protocols and Abstract Base
Classes etc. Where do you go next?
The aim of this book is to delve into those next steps. The book is organised into
eight different topics:
1. Computer Graphics. The book covers Computer Graphics and Computer
Generated Art in Python as well as Graphical User Interfaces and Graphing/
Charting via MatPlotLib.
2. Games Programming. This topic is covered using the pygame library.
3. Testing and Mocking. Testing is an important aspect of any software devel-
opment; this book introduces testing in general and the PyTest module in detail.
It also considers mocking within testing including what and when to mock.
4. File Input/Output. The book covers text file reading and writing as well as
reading and writing CSV and Excel files. Although not strictly related to file
input, regulator expressions are included in this section as they can be used to
process textual data held in files.
5. Database Access. The book introduces databases and relational database in
particular. It then presents the Python DB-API database access standard and

© Springer Nature Switzerland AG 2019 1


J. Hunt, Advanced Guide to Python 3 Programming,
Undergraduate Topics in Computer Science,
https://doi.org/10.1007/978-3-030-25943-3_1
2 1 Introduction

one implementation of this standard, the PyMySQL module used to access a


MySQL database.
6. Logging. An often missed topic is that of logging. The book therefore intro-
duces logging the need for logging, what to log and what not to log as well as
the Python logging module.
7. Concurrency and Parallelism. The book provides extensive coverage of
concurrency topics including Threads, Processes and inter thread or process
synchronisation. It also presents Futures and AsyncIO.
8. Reactive Programming. This section of the book introduces Reactive
Programming using the PyRx reactive programming library.
9. Network Programming. The book concludes by introducing socket and web
service communications in Python.
Each section is introduced by a chapter providing the background and key
concepts of that topic. Subsequent chapters then cover various aspects of the topic.
For example, the first topic covered is on Computer Graphics. This section has
an introductory chapter on Computer Graphics in general. It then introduces the
Turtle Graphics Python library which can be used to generate a graphical display.
The following chapter considers the subject of Computer Generated Art and uses
the Turtle Graphics library to illustrate these ideas. Thus several examples are
presented that might be considered art. The chapter concludes by presenting the
well known Koch Snowflake and the Mandelbrot Fractal set.
This is followed by a chapter presenting the MatPlotLib library used for gen-
erating 2D and 3D charts and graphs (such as a line chart, bar chart or scatter
graph).
The section concludes with a chapter on Graphical User Interfaces (or GUIs)
using the wxpython library. This chapter explores what we mean by a GUI and
some of the alternatives available in Python for creating a GUI.
Subsequent topics follow a similar pattern.
Each programming or library oriented chapter also includes numerous sample
programs that can be downloaded from the GutHub repository and executed. These
chapters also include one or more end of chapter exercises (with sample solutions
also in the GutHub repository).
The topics within the book can be read mostly independently of each other. This
allows the reader to dip into subject areas as and when required. For example, the
File Input/Output section and the Database Access section can be read indepen-
dently of each other (although in this case assessing both technologies may be
useful in selecting an appropriate approach to adopt for the long term persistent
storage of data in a particular system).
Within each section there are usually dependencies, for example it is necessary
to understand the pygame library from the ‘Building Games with pygame’
introductory chapter, before exploring the worked case study presented by the
chapter on the StarshipMeteors game. Similarly it is necessary to have read the
Threading and Multiprocessing chapters before reading the Inter Thread/Process
Synchronisation chapter.
Part I
Computer Graphics
Another Random Scribd Document
with Unrelated Content
miserable Negroes, and allowed, with impunity, thus to destroy, may
I not say, murder their fellow-creatures, and that by means so cruel
as cannot be even related but with shame and horror?
When the vessels arrive at their destined port in the Colonies, the
poor Negroes are to be disposed of to the planters; and here they
are again exposed naked, without any distinction of sexes, to the
brutal examination of their purchasers; and this, it may well be
judged is to many of them another occasion of deep distress,
especially to the females. Add to this, that near connections must
now again be separated, to go with their several purchasers: In this
melancholy scene Mothers are seen hanging over their Daughters,
be-dewing their naked breasts with tears, and Daughters clinging to
their Parents; not knowing what new stage of distress must follow
their separation, or if ever they shall meet again: And here what
sympathy, what commiseration are they to expect? why indeed, if
they will not separate as readily as their owners think proper, the
whipper is called for, and the lash exercised upon their naked
bodies, till obliged to part.
Can any human heart, that retains a fellow-feeling for the
Sufferings of mankind, be unconcerned at relations of such grievous
affliction, to which this oppressed part of our Species are subjected:
God gave to man dominion over the fish of the sea, and over the
fowls of the air, and over the cattle, &c. but imposed no involuntary
subjection of one man to another.
The Truth of this Position has of late been clearly set forth by
persons of reputation and ability, particularly George Wallis, in his
System of the Laws of Scotland, whose sentiments are so worthy the
notice of all considerate persons, that I shall here repeat a part of
what he has not long since published, concerning the African Trade,
viz. ‘If this Trade admits of a moral or a rational justification, every
crime, even the most atrocious, may be justified: Government was
instituted for the good of mankind. Kings, Princes, Governors, are
not proprietors of those who are subjected to their authority, they
have not a right to make them miserable. On the contrary, their
authority is vested in them, that they may by the just exercise of it,
promote the Happiness of their people: Of course, they have not a
right to dispose of their Liberty, and to sell them for slaves: Besides,
no man has a right to acquire or to purchase them; men and their
Liberty, are not either saleable or purchaseable: One therefore has
no body but himself to blame, in case he shall find himself deprived
of a man, whom he thought he had, by buying for a price, made his
own; for he dealt in a Trade which was illicit, and was prohibited by
the most obvious dictates of humanity. For these reasons, every one
of those unfortunate men, who are pretended to be slaves, has a
right to be declared free, for he never lost his Liberty, he could not
lose it; his Prince had no power to dispose of him: of course the sale
was void. This right he carries about with him, and is entitled every
where to get it declared. As soon, therefore, as he comes into a
country, in which the Judges are not forgetful of their own humanity,
it is their duty to remember that he is a man, and to declare him to be
free.—This is the Law of Nature, which is obligatory on all men, at all
times, and in all places.—Would not any of us, who should be
snatched by Pirates from his native land, think himself cruelly
abused, and at all times intitled to be free? Have not these
unfortunate Africans, who meet with the same cruel fate, the same
right? are not they men as well as we? and have they not the same
sensibility? Let us not, therefore, defend or support an usage, which
is contrary to all the Laws of Humanity.’
Francis Hutchinson, also in his System of Moral Philosophy,
speaking on the subject of Slavery, says, ‘He who detains another by
force in slavery, is always bound to prove his title. The Slave sold or
carried away into a distant country, must not be obliged to prove a
negative, that he never forfeited his Liberty. The violent possessor
must, in all cases, shew his title, especially where the old proprietor
is well known. In this case each man is the original proprietor of his
own Liberty: The proof of his losing it must be incumbent on those,
who deprived him of it by force. Strange, (says the same author) that
in any nation, where a sense of Liberty prevails, where the Christian
religion is professed, custom and high prospect of gain can so
stupify the consciences of men, and all sense of natural justice, that
they can hear such computation made about the value of their
fellow-men and their Liberty, without abhorrence and indignation.’
The noted Baron Montesquieu gives it, as his opinion, in his Spirit
of Laws, page 348, ‘That nothing more assimilates a man to a beast
than living amongst freemen, himself a slave; such people as these
are the natural enemies of society, and their number must always be
dangerous.’
The Author of a pamphlet, lately printed in London, entituled, An
Essay in Vindication of the continental Colonies of America, writes,
‘That the bondage we have imposed on the Africans, is absolutely
repugnant to justice. That it is highly inconsistent with civil policy:
First, as it tends to suppress all improvements in arts and sciences;
without which it is morally impossible that any nation should be
happy or powerful. Secondly, as it may deprave the minds of the
freemen; steeling their hearts against the laudable feelings of virtue
and humanity. And, lastly, as it endangers the community by the
destructive effects of civil commotions: need I add to these (says that
author) what every heart, which is not callous to all tender feelings,
will readily suggest; that it is shocking to humanity, violative of every
generous sentiment, abhorrent utterly from the Christian Religion:
for, as Montesquieu very justly observes, We must suppose them not
to be men, or a suspicion would follow that we ourselves are not
Christians.——There cannot be a more dangerous maxim, than that
necessity is a plea for injustice. For who shall fix the degree of this
necessity? What villain so atrocious, who may not urge this excuse?
or, as Milton has happily expressed it,

‘—————————And with necessity,


The tyrant’s plea, excuse his dev’lish deed.

‘That our Colonies want people, is a very weak argument for so


inhuman a violation of justice.—Shall a civilized, a Christian nation
encourage Slavery, because the barbarous, savage, lawless African
hath done it? Monstrous thought! To what end do we profess a
religion whose dictates we so flagrantly violate? Wherefore have we
that pattern of goodness and humanity, if we refuse to follow it? How
long shall we continue a practice, which policy rejects, justice
condemns, and piety dissuades? Shall the Americans persist in a
conduct, which cannot be justified; or persevere in oppression from
which their hearts must recoil? If the barbarous Africans shall
continue to enslave each other, let the dæmon slavery remain
among them, that their crime may include its own punishment. Let
not Christians, by administering to their wickedness, confess their
religion to be a useless refinement, their profession vain, and
themselves as inhuman as the savages they detest.’
James Foster, in his Discourses on Natural Religion and Social
Virtue, also shews his just indignation at this wicked practice, which
he declares to be a criminal and outrageous violation of the natural
right of mankind. At page 156, 2d vol. he says, ‘Should we have read
concerning the Greeks or Romans of old, that they traded, with view
to make slaves of their own species, whom they certainly knew that
this would involve in schemes of blood and murder, of destroying or
enslaving each other, that they even fomented wars, and engaged
whole nations and tribes in open hostilities, for their own private
advantage; that they had no detestation of the violence and cruelty,
but only feared the ill success of their inhuman enterprises; that they
carried men like themselves, their brethren, and the offspring of the
same common parent, to be sold like beasts of prey, or beasts of
burden, and put them to the same reproachful trial of their
soundness, strength and capacity for greater bodily service; that
quite forgetting and renouncing the original dignity of human nature,
communicated to all, they treated them with more severity and ruder
discipline, than even the ox or the ass, who are void of
understanding.—Should we not, if this had been the case, have
naturally been led to despise all their pretended refinements of
morality; and to have concluded, that as they were not nations
destitute of politeness, they must have been entire Strangers to
Virtue and Benevolence?
‘But, notwithstanding this, we ourselves (who profess to be
Christians, and boast of the peculiar advantage we enjoy, by means
of an express revelation of our duty from Heaven) are in effect, these
very untaught and rude Heathen countries. With all our superior light,
we instil into those, whom we call savage and barbarous, the most
despicable opinion of human nature. We, to the utmost of our power,
weaken and dissolve the universal tie, that binds and unites
mankind. We practise what we should exclaim against, as the utmost
excess of cruelty and tyranny, if nations of the world, differing in
colour and form of government from ourselves, were so possessed
of empire, as to be able to reduce us to a state of unmerited and
brutish servitude. Of consequence, we sacrifice our reason, our
humanity, our Christianity, to an unnatural sordid gain. We teach
other nations to despise and trample under foot, all the obligations of
social virtue. We take the most effectual method to prevent the
propagation of the Gospel, by representing it as a scheme of power
and barbarous oppression, and an enemy to the natural privileges
and rights of men.
‘Perhaps all that I have now offered, may be of very little weight to
restrain this enormity, this aggravated iniquity. However, I shall still
have the satisfaction, of having entered my private protest against a
practice which, in my opinion, bids that God, who is the God and
Father of the Gentiles unconverted to Christianity, most daring and
bold defiance, and spurns at all the principles both of natural and
revealed Religion.’
How the British nation first came to be concerned in a practice, by
which the rights and liberties of mankind are so violently infringed,
and which is so opposite to the apprehensions Englishmen have
always had of what natural justice requires, is indeed surprising. It
was about the year 1563, in the reign of Queen Elizabeth, that the
English first engaged in the Guinea Trade; when it appears, from an
account in Hill’s Naval History, page 293, That when Captain
Hawkins returned from his first voyage to Africa, that generous
spirited Princess, attentive to the interest of her subjects, sent for the
Commander, to whom she expressed her concern lest any of the
African Negroes should be carried off without their free consent,
declaring it would be detestable, and call down the vengeance of
Heaven upon the undertakers. Captain Hawkins promised to comply
with the Queen’s injunction: nevertheless, we find in the account,
given in the same History, of Hawkins’s second voyage, the author
using these remarkable words, Here began the horrid practice of
forcing the Africans into slavery.
Labat, a Roman Missionary, in his account of the Isles of America,
at page 114, of the 4th vol. mentions, that Lewis the 13th, Father to
the present French King’s Grandfather, was extremely uneasy at a
Law by which all the Negroes of his Colonies were to be made
slaves; but it being strongly urged to him, as the readiest means for
their Conversion to Christianity, he acquiesced therewith.
And although we have not many accounts of the impressions
which this piratical invasion of the rights of mankind gave to serious
minded people, when first engaged in; yet it did not escape the
notice of some, who might be esteemed in a peculiar manner as
watchmen in their day to the different societies of Christians
whereunto they belonged. Richard Baxter, an eminent preacher
amongst the Nonconformists, in the last century, well known and
particularly esteemed by most of the serious Presbyterians and
Independents, in his Christian Directory, mostly wrote about an
hundred Years ago, fully shews his detestation of this practice in the
following words: ‘Do you not mark how God hath followed you with
plagues? And may not conscience tell you, that it is for your
inhumanity to the souls and bodies of men?—To go as pirates and
catch up poor Negroes, or people of another land, that never
forfeited Life or Liberty, and to make them Slaves and sell them, is
one of the worst kind of Thievery in the world; and such persons are
to be taken for the common Enemies of mankind; and they that buy
them, and use them as beasts, for their meer commodity, and betray,
or destroy, or neglect their souls, are fitter to be called devils than
Christians. It is an heinous sin to buy them, unless it be in charity to
deliver them.——Undoubtedly they are presently bound to deliver
them; because by right the man is his own; therefore no man else
can have a just title to him.’
We also find George Fox, a man of exemplary piety, who was the
principal instrument in gathering the religious society of people called
Quakers, expressing his concern and fellow-feeling for the bondage
of the Negroes: In a discourse taken from his mouth, in Barbadoes,
in the Year 1671, says, ‘Consider with yourselves, if you were in the
same condition as the Blacks are,—who came strangers to you, and
were sold to you as slaves. I say, if this should be the condition of
you or yours, you would think it hard measure: Yea, and very great
bondage and cruelty. And, therefore, consider seriously of this, and
do you for and to them, as you would willingly have them, or any
other to do unto you, were you in the like slavish condition; and bring
them to know the Lord Christ.’ And in his journal, page 431, speaking
of the Advice he gave his friends at Barbadoes, he says, ‘I desired
also, that they would cause their Overseers to deal mildly and gently
with their Negroes, and not to use cruelty towards them, as the
manner of some had been; and that after certain years of servitude
they should make them free.’
In a book printed in Leverpool, called The Leverpool
Memorandum-book, which contains, among other things, an account
of the Trade of that port, there is an exact list of the vessels
employed in the Guinea Trade, and of the number of Slaves
imported in each vessel, by which it appears, that in the year 1753,
the number imported to America, by vessels belonging to that port,
amounted to upwards of Thirty Thousand; and from the number of
Vessels employed by the African Company in London and Bristol, we
may, with some degree of certainty conclude, there is, at least, One
Hundred Thousand Negroes purchased and brought on board our
ships yearly from the coast of Africa, on their account. This is
confirmed in Anderson’s History of Trade and Commerce, printed in
1764, where it is said, at page 68 of the Appendix, ‘That England
supplies her American Colonies with Negro-slaves, amounting in
number to above One Hundred Thousand every year.’ When the
vessels are full freighted with slaves, they set out for our plantations
in America, and may be two or three months on the voyage, during
which time, from the filth and stench that is among them, distempers
frequently break out, which carry off a great many, a fifth, a fourth,
yea, sometimes a third of them; so that taking all the slaves together
that are brought on board our ships yearly, one may reasonably
suppose, that at least ten thousand of them die on the voyage. And
in a printed account of the State of the Negroes in our plantations, it
is supposed that a fourth part, more or less, die at the different
Islands, in what is called the seasoning. Hence it may be presumed,
that, at a moderate computation of the slaves, who are purchased by
our African merchants in a year, near thirty thousand die upon the
voyage and in the seasoning. Add to this, the prodigious number
who are killed in the incursions and intestine wars, by which the
Negroes procure the number of slaves wanted to load the vessels.
How dreadful then is this Slave-Trade, whereby so many thousands
of our fellow-creatures, free by nature, endued with the same rational
faculties, and called to be heirs of the same salvation with us, lose
their lives, and are truly, and properly speaking, murdered every
year! For it is not necessary, in order to convict a man of murder, to
make it appear, that he had an intention to commit murder. Whoever
does, by unjust force or violence, deprive another of his Liberty; and,
while he has him in his power, reduces him, by cruel treatment, to
such a condition as evidently endangers his life, and the event
occasions his death, is actually guilty of murder. It is no less
shocking to read the accounts given by Sir Hans Sloane, and others,
of the inhuman and unmerciful treatment those Blacks meet with,
who survive the seasoning in the Islands, often for transgressions, to
which the punishment they receive bears no proportion. ‘And the
horrid executions, which are frequently made there upon discovery
of the plots laid by the Blacks, for the recovery of their liberty; of
some they break the bones, whilst alive, on a wheel; others they
burn or rather roast to death; others they starve to death, with a loaf
hanging before their mouths.’ Thus they are brought to expire, with
frightful agonies, in the most horrid tortures. For negligence only they
are unmercifully whipped, till their backs are raw, and then pepper
and salt is scattered on the wounds to heighten the pain, and
prevent mortification. Is it not a cause of much sorrow and
lamentation, that so many poor creatures should be thus racked with
excruciating tortures, for crimes which often their tormentors have
occasioned? Must not even the common feelings of human nature
have suffered some grievous change in those men, to be capable of
such horrid cruelty towards their fellow-men? If they deserve death,
ought not their judges, in the death decreed them, always to
remember that these their hapless fellow-creatures are men, and
themselves professing Christians? The Mosaic law teaches us our
duty in these cases, in the merciful provision it made in the
punishment of transgressors, Deuter. xxv. 2. And it shall be, if the
wicked man be worthy to be beaten, that the judge shall cause him
to lie down, and to be beaten before his face, according to his fault,
by a certain number; Forty stripes he may give him, and not exceed.
And the reason rendered is out of respect to human nature, viz. Lest
if he should exceed, and beat him above these, with many stripes,
then thy Brother should seem vile unto thee. Britons boast
themselves to be a generous, humane people, who have a true
sense of the importance of Liberty; but is this a true character, whilst
that barbarous, savage Slave-Trade, with all its attendant horrors,
receives countenance and protection from the Legislature, whereby
so many Thousand lives are yearly sacrificed? Do we indeed believe
the truths declared in the Gospel? Are we persuaded that the
threatenings, as well as the promises therein contained, will have
their accomplishment? If indeed we do, must we not tremble to think
what a load of guilt lies upon our Nation generally, and individually so
far as we in any degree abet or countenance this aggravated
iniquity?
We have a memorable Instance in history, which may be fruitful of
Instruction, if timely and properly applied; it is a quotation made by
Sir John Temple, in his history of the Irish rebellion, being an
observation out of Giraldus Cambrensis, a noted author, who lived
about six hundred years ago, concerning the causes of the
prosperity of the English undertakings in Ireland, when they
conquered that Island, he saith, ‘That a synod, or council of the
Clergy, being then assembled at Armagh, and that point fully
debated, it was unanimously agreed, that the sins of the people were
the occasion of that heavy judgment then falling upon their nation;
and that especially their buying of Englishmen from merchants and
pirates, and detaining them under a most miserable hard bondage,
had caused the Lord, by way of just retaliation, to leave them to be
reduced, by the English, to the same state of slavery. Whereupon
they made a publick act in that council, that all the English held in
captivity throughout the whole land, should be presently restored to
their former Liberty.’
I shall now conclude with an extract from an address of a late
author to the merchants, and others, who are concerned in carrying
on the Guinea Trade; which also, in a great measure, is applicable to
others, who, for the love of gain, are in any way concerned in
promoting or maintaining the captivity of the Negroes.
‘As the business, you are publickly carrying on before the world,
has a bad aspect, and you are sensible most men make objection
against it, you ought to justify it to the world, upon principles of
reason, equity, and humanity; to make it appear, that it is no unjust
invasion of the persons, or encroachments on the rights of men; or
for ever to lay it aside.—But laying aside the resentment of men,
which is but of little or no moment, in comparison with that of the
Almighty, think of a future reckoning: consider how you shall come
off in the great and awful Day of account. You now heap up riches
and live in pleasure; but, oh! what will you do in the end thereof? and
that is not far off: what, if death should seize upon you, and hurry
you out of this world, under all that load of blood-guiltiness that now
lies upon your fouls? The gospel expresly declares, that thieves and
murderers shall not inherit the kingdom of God. Consider, that at the
same time, and by the same means, you now treasure up worldly
riches, you are treasuring up to yourselves wrath, against the day of
wrath, and vengeance that shall come upon the workers of iniquity,
unless prevented by a timely repentance.
‘And what greater iniquity, what crime that is more heinous, that
carries in it more complicated guilt, can you name than that, in the
habitual, deliberate practice of which you now live? How can you lift
up your guilty eyes to heaven? How can you pray for mercy to him
that made you, or hope for any favour from him that formed you,
while you go on thus grosly and openly to dishonour him, in
debasing and destroying the noblest workmanship of his hands in
this lower world? He is the Father of men; and do you think he will
not resent such treatment of his offspring, whom he hath so loved,
as to give his only begotten Son, that whosoever believeth in him,
might not perish, but have everlasting life? This love of God to man,
revealed in the gospel, is a great aggravation of your guilt; for if God
so loved us, we ought also to love one another. You remember the
fate of the Servant, who took hold of his fellow-servant, who was in
his debt, by the throat, and cast him into prison: Think then, and
tremble to think, what will be your fate, who take your fellow-servants
by the throat, that owe you not a penny, and make them prisoners for
life.
‘Give yourselves leave to reflect impartially upon, and consider the
nature of, this Man-Trade, which, if you do, your hearts must needs
relent, if you have not lost all sense of humanity, all pity and
companion towards those of your own kind, to think what calamities,
what havock and destruction among them, you have been the
authors of for filthy lucre’s sake. God grant you may be sensible of
your guilt, and repent in time!’

F I N I S.

BOOKS Printed and Sold by J. Phillips,


George-Yard, Lombard-Street.
ESSAY on the Treatment and Conversion of AFRICAN
SLAVES in the British Sugar Colonies. By J. Ramsay, Vicar of
Teston in Kent, who resided many Years in the West-Indies. In One
Volume, Octavo. Price 5s. bound, or 4s. in Boards.
Historical Account of G UI NE A, its Situation, Produce, and the
general Disposition of its Inhabitants. With an Inquiry into the R i s e
and P r o g r e s s of the SLAVE TRADE, its Nature and lamentable
Effects. Also a Republication of the Sentiments of several Authors of
Note on this interesting Subject: Particularly an Extract of a Treatise
written by G r anv i lle S ha r pe. By Anth o ny Benezet. In One
Volume Octavo. Price 2s. 6d. stitched.
THOUGHTS on the Slavery of the NEGROES. Price 4d.

Transcriber’s Notes:
Blank pages have been removed.
A few obvious typographical errors have been silently corrected, otherwise
archaic and inconsistent spellings have been left alone.
*** END OF THE PROJECT GUTENBERG EBOOK A CAUTION TO
GREAT BRITAIN AND HER COLONIES, IN A SHORT
REPRESENTATION OF THE CALAMITOUS STATE OF THE
ENSLAVED NEGROES IN THE BRITISH DOMINIONS ***

Updated editions will replace the previous one—the old editions will
be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States copyright in
these works, so the Foundation (and you!) can copy and distribute it
in the United States without permission and without paying copyright
royalties. Special rules, set forth in the General Terms of Use part of
this license, apply to copying and distributing Project Gutenberg™
electronic works to protect the PROJECT GUTENBERG™ concept
and trademark. Project Gutenberg is a registered trademark, and
may not be used if you charge for an eBook, except by following the
terms of the trademark license, including paying royalties for use of
the Project Gutenberg trademark. If you do not charge anything for
copies of this eBook, complying with the trademark license is very
easy. You may use this eBook for nearly any purpose such as
creation of derivative works, reports, performances and research.
Project Gutenberg eBooks may be modified and printed and given
away—you may do practically ANYTHING in the United States with
eBooks not protected by U.S. copyright law. Redistribution is subject
to the trademark license, especially commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund from
the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law in
the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name
associated with the work. You can easily comply with the terms of
this agreement by keeping this work in the same format with its
attached full Project Gutenberg™ License when you share it without
charge with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.

1.E. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears, or
with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with
this eBook or online at www.gutenberg.org. If you are not located
in the United States, you will have to check the laws of the country
where you are located before using this eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning of
this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1 with
active links or immediate access to the full terms of the Project
Gutenberg™ License.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or expense
to the user, provide a copy, a means of exporting a copy, or a means
of obtaining a copy upon request, of the work in its original “Plain
Vanilla ASCII” or other form. Any alternate format must include the
full Project Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt that
s/he does not agree to the terms of the full Project Gutenberg™
License. You must require such a user to return or destroy all
copies of the works possessed in a physical medium and
discontinue all use of and all access to other copies of Project
Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except


for the “Right of Replacement or Refund” described in paragraph
1.F.3, the Project Gutenberg Literary Archive Foundation, the owner
of the Project Gutenberg™ trademark, and any other party
distributing a Project Gutenberg™ electronic work under this
agreement, disclaim all liability to you for damages, costs and
expenses, including legal fees. YOU AGREE THAT YOU HAVE NO
REMEDIES FOR NEGLIGENCE, STRICT LIABILITY, BREACH OF
WARRANTY OR BREACH OF CONTRACT EXCEPT THOSE
PROVIDED IN PARAGRAPH 1.F.3. YOU AGREE THAT THE
FOUNDATION, THE TRADEMARK OWNER, AND ANY
DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE
TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL,
PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE
NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving it,
you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or entity
that provided you with the defective work may elect to provide a
replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like