38,39 €
Take Python beyond scripting to build robust, reusable, and efficient applications
Do you know the basics of Python and object oriented programming? Do you want to go an extra mile and learn techniques to make your Python application robust, extensible, and efficient? Then this book is for you.
Python is one of the most widely used dynamic programming languages, supported by a rich set of libraries and frameworks that enable rapid development. But fast paced development often comes with its own baggage that could bring down the quality, performance, and extensibility of an application. This book will show you ways to handle such problems and write better Python applications.
From the basics of simple command-line applications, develop your skills all the way to designing efficient and advanced Python apps. Guided by a light-hearted fantasy learning theme, overcome the real-world problems of complex Python development with practical solutions. Beginning with a focus on robustness, packaging, and releasing application code, you'll move on to focus on improving application lifetime by making code extensible, reusable, and readable. Get to grips with Python refactoring, design patterns and best practices. Techniques to identify the bottlenecks and improve performance are covered in a series of chapters devoted to performance, before closing with a look at developing Python GUIs.
The book uses a fantasy game theme as a medium to explain various topics. Specific aspects of application development are explained in different chapters. In each chapter the reader is presented with an interesting problem which is then tackled using hands-on examples with easy-to-follow instructions.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 456
Veröffentlichungsjahr: 2016
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: August 2016
Production reference: 1290816
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-919-6
www.packtpub.com
Author
Ninad Sathaye
Reviewer
Will Ware
Commissioning Editor
Priya Singh
Acquisition Editor
Kevin Colaco
Content Development Editor
Deepti Thore
Technical Editors
Kunal Chaudhari
Sunith Shetty
Copy Editor
Zainab Bootwala
Project Coordinator
Shweta H Birwatkar
Proofreader
Safis Editing
Indexer
Tejal Daruwale Soni
Graphics
Abhinash Sahu
Production Coordinator
Melwyn Dsa
Cover Work
Melwyn Dsa
The names, characters, businesses, places, events, and incidents mentioned in this book are either the products of the author's imagination or used in a fictitious manner. Any resemblance to actual persons, living or dead, or actual events is purely coincidental.
The views and opinions expressed in this book are solely of the author and do not reflect those of author's employer or its clients.
For this book, the cartoons representing imaginary game characters, such as the Dwarf, Knight, Orc, Fairy, Elf, and so on, are created and copyrighted by Packt Publishing.
Ninad Sathaye has spent several years of his professional career designing and developing performance-critical engineering applications written in a variety of languages, including Python and C++. He has worked as a software architect in the semiconductor industry, and more recently in the domain of Internet of Things. He holds a master's degree in mechanical engineering.
I would like to thank my wife, Arati, for her creative input on the book's game theme. This book wouldn't have been possible without her continued support. I would also like to express my sincere gratitude to Will Ware for technically reviewing this book. His feedback was valuable and really helped me take this book to the next level! Thank you Deepti, Kunal, Zainab, and the whole Packt Publishing team for your hard work and support. I owe a special thanks to Abhinash Sahu from Packt Publishing for creating the awesome graphics art for all the fictional characters in this book. My sincere thanks to Steve Furkay, Neeshma, and Kevin for their valuable feedback during the initial phase of this book. Finally, I would like to thank my whole family for their encouragement and support!
Will Ware is a software engineer in the Boston area. He has worked with embedded systems, mobile phones, and web development. He received degrees in electrical engineering and mathematics from M.I.T. His interests include STEM education and 3D printing.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
To my daughter, Anvita
Python is one of the most widely used dynamic programming languages. It supports a rich set of libraries and frameworks that enable rapid development. Such fast-paced development often comes with its own baggage that can bring down the overall quality, performance, and extensibility of the application. This book will help you push your Python skill level by teaching you how to build and deploy interesting applications.
Starting with a simple program, the book takes you all the way through designing and developing robust and efficient applications. It touches upon several important topics in an accessible and fun way.
A fantasy theme is used as a vehicle to explain various concepts. During the course of this book, you will meet many fictional game characters. While you learn different topics, these imaginary characters will talk to you, ask questions, and request new features.
Each chapter targets a different aspect of application development. A few initial ones focus on software robustness, packaging, and releasing the application code. The next few chapters are about improving the application's lifetime by making the code extensible, reusable, and readable. You will learn about refactoring, unit testing, design patterns, documentation, and best practices.
Techniques for identifying bottlenecks and improving performance are covered in a series of three chapters devoted to performance. The last chapter introduces you to GUI development.
The code illustrations that you see in this book are actually image files or code snapshots.
The rendering quality of these images will vary depending on your PDF reader's page display resolution and the zoom level.
If you have trouble clearly reading this code, you may try the following in your PDF or e-book reader:
If the problem still persists, you can try with a different resolution.
How do you set this resolution? It will depend on your e-book reader. For example, if you are using Adobe Reader, go to Edit | Preferences and then select Page Display from the left panel. You will see Resolution as an option in the right panel. Select 96 pixels/inch or similar and see if that helps render the images better.
Chapter 1, Developing Simple Applications, starts with installation prerequisites and the theme of the book. The first program is a fantasy text-based game presented as a script. An incremental version of this program with new features is then developed using functions. With more features added, the code becomes difficult to manage. To address this, the game application is redesigned using OOP concepts. This application now becomes the reference version for the next few chapters.
Chapter 2, Dealing with Exceptions, will teach you how to fix the obvious issues that the code written in the previous chapter has. You will learn how to add exception handling code to make the application robust. You will also learn about the try…except…finally clause, raising and re-raising exceptions, creating and using custom exception classes, and so on.
Chapter 3, Modularize, Package, Deploy!, will teach you how to modularize and package the code written in the earlier chapters. After preparing a package, it will show you how to deploy a source distribution, make incremental releases, set up a private Python package repository, and bring the code under version control.
Chapter 4, Documentation and Best Practices, dives into coding standards, which are a set of guidelines that you should follow while developing the code. Complying with these standards can make a significant impact on code readability and the life of the code. In this chapter, you will learn about another important aspect of software development, code documentation, and best practices. It starts with an introduction to the reStructuredText format and uses it to write docstrings. You will create HTML documentation for the code using the Sphinx document generator. The chapter also talks about some important coding standards for writing a Python code and using PyLint to check the code quality.
Chapter 5, Unit Testing and Refactoring, starts with an introduction to the unit testing framework in Python. You will write some unit tests for the game application developed so far. It covers many other topics, such as using Mock library in unit tests and measuring effectiveness of the unit tests with code coverage. The later part of the chapter talks about many code refactoring techniques. This is the last chapter that makes use of the code developed in the earlier chapters. The following chapters will have their own simplified examples tied to the same high-fantasy theme.
Chapter 6, Design Patterns, tells you how, during development, you often encounter a recurring problem. Many times, a general solution or recipe exists, which just works for this problem. This is often referred to as a design pattern. This chapter introduces you to some commonly used design patterns. It covers the strategy, simple and abstract factory, and adapter patterns. For each pattern, a simple game scenario will demonstrate a practical problem. You will learn how the design pattern can help solve this problem. Each of these patterns will be implemented using a Pythonic approach.
Chapter 7, Performance – Identifying Bottlenecks, is the first one in a series of three chapters on performance improvements. You will write a simple program called Gold Hunt that looks harmless until you tweak some parameters. The parameter tweaking reveals performance problems. In this chapter, you will identify the time-consuming blocks of the code. It covers the basic ways to clock the application runtime, profiling the code to identify performance bottlenecks, the basics of memory profiling, and using big-O notation to represent computational complexity.
Chapter 8, Improving Performance – Part One, teaches you how to fix some of the performance bottlenecks identified in the previous chapter. Additionally, you will also learn about several techniques, such as algorithm changes, list comprehension, generator expressions, the right choice of data structures, and so on, to improve the application performance.
Chapter 9, Improving Performance – Part Two, NumPy and Parallelization, is the final chapter on performance improvements, wherein you will drastically improve the performance of the gold hunt application. The chapter will introduce you to the NumPy package. It will also introduce you to parallel processing using Python.
Chapter 10, Simple GUI Applications, is the final chapter and introduces you to simple GUI application development. The chapters so far covered several key aspects of application development using command-line programs. In this chapter, however, you will learn about the Tkinter module, MVC architecture, and develop a GUI version of the first application developed in Chapter 1, Developing Simple Applications.
The code illustrated in this book is compatible with Python version 3.5. The supporting code bundles also provide files compatible with version 2.7.9; however, throughout the book, Python version 3.5 is assumed. See the Installation prerequisites section of Chapter 1, Developing Simple Applications, for details on the basic packages that need to be installed. Additionally, there are some Python package dependencies that need to be installed. Most of these packages can be installed using pip (Python package manager). These dependencies are mentioned in the chapters that require them.
Do you know the basics of Python and object-oriented programming?
Do you wish to go the extra mile and learn techniques to make your Python application robust, extensible, and efficient?
This is the book for you if you answered yes to these questions. It is also for those with a different programming background (for instance, C++ or Java) and wish to get to grips with Python application development.
This book is not for you if either of the following statements apply to you:
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Learning-Python-Application-Development. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from http://www.packtpub.com/sites/default/files/downloads/LearningPythonApplicationDevelopment_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
Python is one of the most widely used dynamic programming languages. It supports a rich set of packages, GUI libraries, and web frameworks that enable you to build efficient cross-platform applications. It is an ideal language for rapid application development. Such fast-paced development often comes with its own baggage that could bring down the overall quality, performance, and extensibility of the code. This book will show you ways to handle such situations and help you develop better Python applications. The key concepts will be explained with the help of command-line applications, which will be progressively improved in subsequent chapters.
This chapter will be an introductory one. It will serve as a refresher to Python programming. That being said, it is expected you have some knowledge of Python language, as well as object-oriented programming (OOP) concepts.
Here is how this chapter is organized:
The code explanation will be a bit verbose. More experienced readers can breeze past the examples and go to the next chapter, but be sure to understand the theme of the book and review the code in the ch01_ex03.py file. In the next few chapters, you will learn techniques to progressively improve this code.
Before diving into the rest of the chapter, let's get some housekeeping out of the way. If you haven't already, you should read the Preface, which documents most of the following things:
Let's make sure that we have installed the prerequisites. Here is a table that summarizes the basic tools we need for this chapter and beyond; more verbose installation instructions follow in the next section:
Tool
Notes
Python 3.5
The code illustrated in this book is compatible with version 3.5. See the next table for available Python distributions. Supporting code bundles also provide 2.7.9 compatible files.
pip (package manager for Python)
The pip is already available in the official distribution for versions 3.5 and 2.7.9.
IPython
Optional installation. IPython is an enhanced Python interpreter.
Integrated development environment (IDE)
Use the Python editor or any IDE of your choice. Some good IDEs are listed in a table later in this chapter.
In subsequent chapters, we will need to install some additional dependencies. The Python package manager (pip) will makes this a trivial task.
Have you already set up the required Python environment or know how to do it? Just skip the setup instructions that follow and move on to the The theme of the book section, where the real action begins!
There are two options to install Python. You can either use the official Python version or one of the freely available bundled distributions.
For Linux or Mac users, Python is probably already installed on your system. If not, you can install it using the package manager of your operating system. Windows OS users can install Python 3.5 by downloading the Python installer from the official Python website:
During the installation process, just make sure to select the option that adds Python 3.5 to the system environment variable, PATH, as shown in the preceding screenshot. You can also visit the official Python website, https://www.python.org/downloads, to get the platform-specific distribution.
Alternatively, there are several freely available Python distributions that bundle together useful Python packages, including pip and IPython. The following table summarizes some of the most popular Python distributions, including the official one:
Distribution
Supported platforms
Notes
Official Python distribution
https://www.python.org
Windows, Linux, Mac
Anaconda
http://continuum.io
Windows, Linux, Mac
Enthought Canopy Express
https://www.enthought.com/canopy-express/
Windows, Linux, Mac
Python(x, y)
https://python-xy.github.io/
Windows
Let's briefly talk about the path where Python is installed, and how to make sure python is available as a command in your terminal window. Of course, things will widely vary, depending on where you install it and which Python distribution you choose.
The official Python documentation page has comprehensive information on setting up the Python environment on different platforms. Here is a link, in case you need further help beyond what we have covered: https://docs.python.org/3/using/index.html.
On a Unix-like operating system such as Linux, the default location is typically /usr/bin/python or /usr/local/bin/python.
If you used your operating system's package manager to install Python, the command python or python3 should be available in the terminal window. If it isn't, you need to update the PATH system environment variable to include the directory path to the Python executable. For example, if you have a Bash shell, add the following to the .bashrc file in your user home directory:
Specify the actual path to your Python installation in place of /usr/bin.
On Windows OS, the default Python installation path is typically the following directory: C:\Users\name\AppData\Local\Programs\Python\Python35-32\python.exe. Replace name with your Windows username. Depending on your installer and system, the Python directory can also be Python35-64. As mentioned earlier, at the time of installation, you should select the option Add Python 3.5 to PATH to make sure python or python.exe are automatically recognized as commands. Alternatively, you can rerun the installer with just this option checked.
Open a terminal window (or command prompt on Windows OS) and type the following command to verify the Python version. This command will work if Python is installed and is available as a command in the terminal window. Otherwise, specify the full path to the Python executable. For instance, on Linux you can specify it as /usr/bin/python, if Python is installed in /usr/bin:
Note that the $ sign in the previous command line belongs to the terminal window and is not part of the command itself! Put another way, the actual command is just python -V. The $ or % sign in the terminal window is a prompt for a normal user on Linux. For a root (admin) user, the sign is #. Likewise, on Windows OS, the corresponding symbol is >. You will type the actual command after this symbol.
The following is just a sample output, if we run the preceding command:
The pip is a software package manager that makes it trivial to install Python packages from the official third party software repository, PyPI. The pip is already installed for Python-2 version 2.7.9 or higher and Python-3 version 3.4 or higher. If you are using a different Python version, check out https://pip.pypa.io/en/stable/installing for the installation instructions.
On Linux OS, the default location for the pip is same as that of the Python executable. For example, if you have /usr/bin/python, then pip should be available as /usr/bin/pip. On Windows OS, the default pip.exe is typically the following: C:\Users\name\AppData\Local\Programs\Python\Python35-32\Scripts\pip.exe. As mentioned earlier, replace name with your Windows username. Depending on your installer and the system, the Python directory can also be Python35-64.
This is an optional installation. IPython is an enhanced version of the Python interpreter. If it is not already bundled in your Python distribution, you can install it with:
After the installation, just type ipython in the terminal to start the IPython interactive shell. Here is a screenshot of the IPython shell using the Anaconda Python 3.5 distribution:
It is often very convenient to use the Jupyter Notebook to write and share interactive programs. It is a web application that enables an interactive environment for writing Python code alongside rich text, images, plots, and so on. For further details, check out the project homepage at http://jupyter.org/. The Jupyter Notebook can be installed with:
Using an IDE for development is a matter of personal preference. Simply put, an IDE is a tool intended to accelerate application development. It enables developers to write efficient code quickly by integrating the most common tools they need. The Python installation comes with a program calledIDLE. It is a basic IDE for Python, which should get you started. For advanced development, you can choose from a number of freely or commercially available tools. Any good Python IDE has the following minimum features:
You can get started by trying out one of the freely available IDEs. Here is a partial list of popular IDEs. If you are just interested in a simple source code editor, you can check out https://wiki.python.org/moin/PythonEditors, for a list of available choices.
Python IDE
Notes
PyCharm Community Edition
https://www.jetbrains.com/pycharm
Has a free community edition. Excellent tool to begin Python development!
Wing IDE 101
http://wingware.com/downloads/wingide-101
Free for non-commercial purposes only. Commercial version available with additional features. Another excellent Python IDE.
Spyder https://pythonhosted.org/spyder
Freely available, open source. Also provided in bundled Python distributions such as Python(x,y) and Anaconda.
Eclipse PyDev
www.pydev.org
Freely available, open source.
Sublime Text 2 or Sublime Text 3 (beta)
http://www.sublimetext.com/2
Free for evaluation purposes only. Highly configurable IDE.
Have you read high fantasy novels, such as The Lord of the Rings or The Hobbit by J. R. R. Tolkien? Or watched the films based on these novels? Well, here is a high fantasy, "Tolkienesque" themed book on Python application development.
To find out more about J.R.R. Tolkien's work, see https://en.wikipedia.org/wiki/J._R._R._Tolkien. The term high fantasy is often used to represent a fantasy theme set in an alternate fictional world. Check out https://en.wikipedia.org/wiki/High_fantasy for more information.
This book takes you to an imaginary world where you will develop a text game based on the aforementioned theme. Yes, you can continue being a developer even in this imaginary world! During the course of the book, you will be accompanied by many fictional characters. While you learn different aspects of Python development, these characters will talk to you, ask questions, request new features, and even fight with the enemy.
It should be noted that this book is not about developing game applications. It uses a simple text-based game just as a medium to learn various development aspects.
Off topic, if you are interested in playing a high fantasy theme game, there are quite a few to choose from. Among the open source ones, Battle for Wesnoth is one of the most highly rated, free, turn-based strategy games with a high fantasy theme. Check out https://www.wesnoth.org, for more details.
Let's meet the imaginary characters who will accompany you in various chapters:
Sir Foo
A human knight who is portrayed as a grand knight guarding the southern plains. He is our main character and will be talking to us throughout the book.
Orc Rider
An Orc is a human-like imaginary creature. Here, it is portrayed as an enemy soldier. The Orc is seen riding a wild boar-like creature. You will see this creature in this chapter.
Elf Rider
An Elf is a supernatural mythical being. The Elf is mounted on an elvish horse. He is portrayed as a friendly. You will meet Mr. Elf in Chapter 6, Design Patterns.
Fairy
An intelligent fairy with an inherent capability for magic. She will use her magic just once while finding her enchanted locket in Chapter 7, Performance Identifying Bottlenecks, (See O(log n)). You will first meet her in Chapter 6, Design Patterns.
Dwarf
A Dwarf is a small human-like mythical being. He is portrayed as "The Great Dwarf" of the Foo mountains. He asks lots of questions. You will see him in the second half of the book, starting with Chapter 6, Design Patterns.
With this fun theme as a vehicle, let's start our journey with a simple command-line application. It will be a text-based game. The complexities added in subsequent chapters will challenge you with interesting problems. The book will show you how to gracefully handle such situations.
In the last section, you wrote a quick set of instructions to create a nice little command-line game. You asked your friends to try it out and they kind of liked it (perhaps they were just trying to be nice!). You received the first feature request for the game.
"I think this game has good potential to grow. How about including combat in the next version of the game? When Sir Foo encounters an enemy, he should not just give up that easily. Fight with the enemy! Let the combat decide the winner. "-your friend
You liked the idea and decided to add this capability to the code in the next version. Additionally, you also want to make it more interactive.
The script you wrote for the first program was small. However, as we go on adding new features, it will soon become a maintenance headache. As a step further, we will wrap the existing code into small functions so that the code is easier to manage. In functional programming, the focus is typically on function arrangement and their composition. For example, you can build complicated logic using a simple set of reusable functions.
Before adding any new features, let's revisit the script that you wrote in the previous version (version 0.0.1). We will identify the blocks of code that can be wrapped into functions. Such code chunks are marked in the two code snippets that follow:
We will wrap most of the highlighted code into individual functions, as follows:
In addition to these six blocks of code, we can also create a few top-level functions to handle all this logic. In Python, the function is created using the def keyword, followed by the function name and arguments in parentheses. For example, the reveal_occupants function requires the information about the huts list. We also need to optionally pass the dotted_line string if we do not want to recreate it in the function. So, we will pass the hut number idx, the huts list, and the dotted_line string as function arguments. This function can be written as follows:
After this initial work, the original script can be rewritten as:
This is much easier to read now. What we just did is also referred to asrefactoring; more on various refactoring techniques in a later chapter. It makes it easier to do changes to the individual methods. For example, if you want to customize the mission statement or scenario description, you do not need to open the main function, run_application. Similarly, occupy_huts can be expanded further without any clutter in the main code.
The initial refactored version of the code is not perfect. There is plenty of room for improvement. Can you reduce the burden of passing the dotted_line parameter or think of some other way to handle the printing of bold text?
In the previous section, we wrapped the game logic into individual functions. This not only improved the code readability, but also made it easier to maintain. Let's move on and include the new attack() function in the game. The following steps show the logic of the game with the attack feature included.
While the user wishes to keep playing the game:
After each attack, update and show the health of Sir Foo, and of the enemy too; if enemy health <= 0: print "You Win".
But, if Sir Foo health <= 0: print "You Lose".
Initially, Sir Foo and the Orc will have full health. To quantify health, let's assign hit points to each of these characters (or the game units). So, when we say the character has full health, it means it has the maximum possible hit points. Depending on the character, the default number of hit points will vary. The following image shows Sir Foo and the Orc with the default number of hit points, indicated by the Health label:
The bar above the Health label in the image represents a health meter. Essentially, it keeps track of the hit points
