Skip to main content
main-content
Top

2019 | Book

Advanced Guide to Python 3 Programming

share
SHARE
insite
SEARCH

About this book

Advanced Guide to Python 3 Programming delves deeply into a host of subjects that you need to understand if you are to develop sophisticated real-world programs. Each topic is preceded by an introduction followed by more advanced topics, along with numerous examples, that take you to an advanced level.

There are nine different sections within the book covering Computer Graphics
(including GUIs), Games, Testing, File Input and Output, Databases Access, Logging, Concurrency and Parallelism, Reactive programming, and Networking. Each section is self-contained and can either be read on its own or as part of the book as a whole.

This book is aimed at the those who have learnt the basics of the Python 3 language
but want to delve deeper into Python’s eco system of additional libraries and modules,
to explore concurrency and parallelism, to create impressive looking graphical interfaces, to work with databases and files and to provide professional logging facilities.

Table of Contents

Frontmatter
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. However, 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.

John Hunt

Computer Graphics

Frontmatter
2. Introduction to Computer Graphics

Computer Graphics are everywhere; they are on your TV, in cinema adverts, the core of many films, on your tablet or mobile phone and certainly on your PC or Mac as well as on the dashboard of your car, on your smart watch and in childrens electronic toys. This chapter introduces this topic.

John Hunt
3. Python Turtle Graphics

Python is very well supported in terms of graphics libraries. One of the most widely used graphics libraries is the Turtle Graphics library introduced in this chapter. This is partly because it is straight forward to use and partly because it is provided by default with the Python environment (and thus you do not need to install any additional libraries to use it).

John Hunt
4. Creating Computer Art

Computer Art is defined as any art that uses a computer. However, in the context of this book we mean it to be art that is generated by a computer or more specifically a computer program. The following example, illustrates how in a very few lines of Python code, using the Turtle graphics library, you can create images that might be considered to be computer art.

John Hunt
5. Introduction to Matplotlib

Matplotlib is a Python graphing and plotting library that can generate a variety of different types of graph or chart in a variety of different formats. It can be used to generate line charts, scatter graphs, heat maps, bar charts, pie charts and 3D plots. It can even support animations and interactive displays. In this chapter we will introduce the Matplotlib library, its architecture, the components that comprise a chart and the pyplot API.

John Hunt
6. Graphing with Matplotlib pyplot

In this chapter we will explore the Matplotlib pyplot API. This is the most common way in which developers generate different types of graphs or plots using Matplotlib.

John Hunt
7. Graphical User Interfaces

A Graphical User Interface can capture the essence of an idea or a situation, often avoiding the need for a long passage of text. Such interfaces can save a user from the need to learn complex commands. They are less likely to intimidate computer users and can provide a large amount of information quickly in a form which can be easily assimilated by the user. This chapter we will first introduce what we mean by a GUI and by WIMP based UIs in particular. We will then consider the range of libraries available for Python before selecting wxPython.

John Hunt
8. The wxPython GUI Library

The wxPython library is a cross platform GUI library (or toolkit) for Python. It allows programmers to develop highly graphical user interfaces for their programs using common concepts such as menu bars, menus, buttons, fields, panels and frames. This chapter provides an overview of the wxPython GUI library.

John Hunt
9. Events in wxPython User Interfaces

Events are an integral part of any GUI; they represent user interactions with the interface such as clicking on a button, entering text into a field, selecting a menu option etc. This chapter introduces event handling in wxPython.

John Hunt
10. PyDraw wxPython Example Application

This chapter builds on the GUI library presented in the last two chapters to illustrate how a larger application can be built. It presents a case study of a drawing tool akin to a tool such as Visio etc.

John Hunt

Computer Games

Frontmatter
11. Introduction to Games Programming

Games programming is performed by developers/coders who implement the logic that drives a game. This chapter introduces Games programming.

John Hunt
12. Building Games with pygame

pygame is a cross-platform, free and Open Source Python library designed to make building multimedia applications such as games easy. This chapter introduces pygame, the key concepts; the key modules, classes and functions and a very simple first pygame application.

John Hunt
13. StarshipMeteors pygame

In this chapter we will create a game in which you pilot a starship through a field of meteors. The longer you play the game the larger the number of meteors you will encounter. We will implement several classes to represent the entities within the game.

John Hunt

Testing

Frontmatter
14. Introduction to Testing

This chapter considers the different types of tests that you might want to perform with the systems you develop in Python. It also introduces Test Driven Development

John Hunt
15. PyTest Testing Framework

There are several testing frameworks available for Python, although only one, unittest comes as part of the typical Python installation. Typical libraries include unittest (which is available within the Python distribution by default) and PyTest. This chapter introduces the PyTest testing framework.

John Hunt
16. Mocking for Testing

Testing software systems is not an easy thing to do; the functions, objects, methods etc. That are involved in any program can be complex things in their own right. In many cases they depend on and interact with other functions, methods and objects; very few functions and methods operate in isolation. Thus the success of failure of a function or method or the overall state of an object is dependent on other program elements.

John Hunt

File Input/Output

Frontmatter
17. Introduction to Files, Paths and IO

The operating system is a critical part of any computer systems. It is comprised of elements that manage the processes that run on the CPU, how memory is utilised and managed, how peripheral devices are used (such as printers and scanners), it allows the computer system to communicate with other systems and it also provide support for the file system used.

John Hunt
18. Reading and Writing Files

Reading data from and writing data to a file is very common within many programs. Python provides a large amount of support for working with files of various types. This chapter introduces you to the core file IO functionality in Python.

John Hunt
19. Stream IO

In this chapter we will explore the Stream I/O model that under pins the way in which data is read from and written to data sources and sinks. One example of a data source or sink is a file but another might be a byte array.

John Hunt
20. Working with CSV Files

This chapter introduces a module that supports the generation of CSV (or Comma Separated Values) files.

John Hunt
21. Working with Excel Files

This chapter introduces the openpyxl module that can be used when working with Excel files. Excel is a software application developed by Microsoft that allows users to work with spreadsheets. It is a very widely used tool and files using the Excel file format are commonly encountered within many organisations. It is in effect the industry standard for spreadsheets and as such is a very useful tool to have in the developers toolbox.

John Hunt
22. Regular Expressions in Python

Regular Expression are a very powerful way of processing text while looking for recurring patterns; they are often used with data held in plain text files (such as log files), CSV files as well as Excel files. This chapter introduces regular expressions, discusses the syntax used to define a regular expression pattern and presents the Python re module and its use.

John Hunt

Database Access

Frontmatter
23. Introduction to Databases

There are several different types of database system in common use today including Object databases, NoSQL databases and (probably the most common) Relational Databases. This chapter focusses on Relational Databases as typified by database systems such as Oracle, Microsoft SQL Server and MySQL. The database we will use in this book is MySQL.

John Hunt
24. Python DB-API

The standard for accessing a database in Python is the Python DB-API. This specifies a set of standard interfaces for modules that wish to allow Python to access a specific database. The standard is described in PEP 249 ( https://www.python.org/dev/peps/pep-0249 )—a PEP is a Python Enhancement Proposal. This chapter introduces the Python DB-API.

John Hunt
25. PyMySQL Module

The PyMySQL module provides access to a MySQL database from Python. It implements the Python DB-API v 2.0. This module is a pure Python database interface implementation meaning that it is portable across different operating systems; this is notable because some database interface modules are merely wrappers around other (native) implementations that may or may not be available on different operating systems.

John Hunt

Logging

Frontmatter
26. Introduction to Logging

Many programming languages have common logging libraries including Java and C# and of course Python also has a logging module. Indeed the Python logging module has been part of the builtin modules since Python 2.3. This chapter introduces logging, what to log, and what not to log.

John Hunt
27. Logging in Python

Python has included a built-in logging module since Python 2.3. This module, the logging module, defines functions and classes which implement a flexible logging framework that can be used in any Python application/script or in Python libraries/modules.

John Hunt
28. Advanced Logging

In this chapter we go further into the configuration and modification of the Python logging module. In particular we will look at Handlers (used to determine the destination fo log messages), Filters which can be used by Handlers to provide finer grained control of log output and logger configuration files. We conclude the chapter by considering performance issues associated with logging.

John Hunt

Concurrency and Parallelism

Frontmatter
29. Introduction to Concurrency and Parallelism

In this chapter we will introduce the concepts of concurrency and parallelism. We will also briefly consider the related topic of distribution. After this we will consider process synchronisation, why object oriented approaches are well suited to concurrency and parallelism before finishing with a short discussion of threads versus processes.

John Hunt
30. Threading

Threading is one of the ways in which Python allows you to write programs that multitask; that is appearing to do more than one thing at a time. This chapter presents the threading module and uses a short example to illustrate how these features can be used.

John Hunt
31. Multiprocessing

The multiprocessing library supports the generation of separate (operating system level) processes to execute behaviour (such as functions or methods) using an API that is similar to the Threading API presented in the last chapter.

John Hunt
32. Inter Thread/Process Synchronisation

In this chapter we will look at several facilities supported by both the threading and multiprocessing libraries that allow for synchronisation and cooperation between Threads or Processes.

John Hunt
33. Futures

A future is a thread (or process) that promises to return a value in the future; once the associated behaviour has completed. It is thus a future value. It provides a very simple way of firing off behaviour that will either be time consuming to execute or which may be delayed due to expensive operations such as Input/Output and which could slow down the execution of other elements of a program. This chapter discusses futures in Python.

John Hunt
34. Concurrency with AsyncIO

The Async IO facilities in Python are relatively recent additions originally introduced in Python 3.4 and evolving up to and including Python 3.7. They are comprised (as of Python 3.7) of two new keywords async and await (introduced in Python 3.7) and the Async IO Python package.

John Hunt

Reactive Programming

Frontmatter
35. Reactive Programming Introduction

In this chapter we will introduce the concept of Reactive Programming. Reactive programming is a way of write programs that allow the system to reactive to data being published to it. We will look at the RxPy library which provides a Python implementation of the ReactiveX approach to Reactive Programming.

John Hunt
36. RxPy Observables, Observers and Subjects

In this chapter we will discuss Observables, Observers and Subjects. We also consider how observers may or may not run concurrently. In the remainder of this chapter we look at RxPy version 3 which is a major update from RxPy version 1.

John Hunt
37. RxPy Operators

In this chapter we will look at the types of operators provided by RxPy that can be applied to the data emitted by an Observable.

John Hunt

Network Programming

Frontmatter
38. Introduction to Sockets and Web Services

In the following two chapters we will explore socket based and web service approaches to inter process communications. These processes may be running on the same computer or different computers on the same local area network or may be geographically far apart. In all cases information is sent by one program running in one process to another program running in a separate process via internet sockets. This chapter introduces the core concepts involved in network programming.

John Hunt
39. Sockets in Python

A Socket is an end point in a communication link between separate processes. In Python sockets are objects which provide a way of exchanging information between two processes in a straight forward and platform independent manner. In this chapter we will introduce the basic idea of socket communications and then presents a simple socket server and client application.

John Hunt
40. Web Services in Python

This chapter looks at RESTful web services as implemented using the Flask framework.

John Hunt
41. Bookshop Web Service

The previous chapter illustrated the basic structure of a very simple web service application. We are now in a position to explore the creation of a set of web services for something a little more realistic; the bookshop web service application.

John Hunt
Metadata
Title
Advanced Guide to Python 3 Programming
Author
Dr. John Hunt
Copyright Year
2019
Electronic ISBN
978-3-030-25943-3
Print ISBN
978-3-030-25942-6
DOI
https://doi.org/10.1007/978-3-030-25943-3

Premium Partner