Kristian Rother Pro Python Best Practices Debugging, Testing and Maintenance Kristian Rother Berlin, Germany Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484222409 . For more detailed information, please visit http://www.apress.com/source-code . ISBN 978-1-48422240-9 e-ISBN 978-1-4842-2241-6 DOI 10.1007/978-1-4842-2241-6 Library of Congress Control Number: 2017936044 © Kristian Rother 2017 author name 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. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Printed on acid-free paper Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800- SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. To my parents Preface Writing good Python programs is difficult. Once your programs grow past a few lines, they are inherently prone to errors of all kind. There are many pitfalls unknown to the apprentice developer, but not because Python as a language is difficult to use. To the contrary, the overabundant opportunities Python provides make it difficult to figure out what matters in your daily practice. Instead of trying to cover the entire world of Python, this book focuses on a few essential practices used by most professional Python programmers. These Best Practices will help you to debug programs, write automated tests for your software, and keep it in a state that can be maintained with reasonable effort. I have picked techniques useful for Python developers in data analysis, web development, and scientific software development alike. If you feel you have mastered the basics of Python and want to improve the way you write programs, this book is here to help you raise the bar. Acknowledgments First of all, I would like to thank my mentors: Wolfgang Vahrson, who introduced me to Python; and Christoph Gille, Cornelius Frömmel, and Ulf Leser, whose guidance helped me to develop my programming skills. I am thankful for the Python projects I had the pleasure to work on with Kaja Milanowska, Anna Philipps, Joanna Kasprzak, Tomasz Puton, Justyna Wojtczak, Pawel Skiba, Marcin Feder, Michal Gajda, Staszek Dunin-Horkawicz, Michal Boniecki, Wojciech Potrzebowski, Natalia Szostak, Michal Pietal, Silke Trissl, Christian Tismer, and Raphael Bauer. Without these people, I would not be in the position to write anything about Python. I am grateful for many opportunities to teach Python provided by Pedro Fernandes, Allegra Via, Jasmin Touati, Veit Schiele, Jose Quesada, Janick Mathys, Martin Hoste, Julia Rose, Eryk Hajecki, and Edward Jenkins. I recall many fruitful conversations inside and outside of training rooms with most of these people about learning programming skills. Credit goes to Dinu Gherman, Sebastian Thieme, Daniel Godoy, and Markus Rother, who reviewed my early notes for the book and provided plenty of valuable feedback on the structure. An especially big “Thank You” is reserved for my Technical Reviewer, Michael Thomas, who undertook the ordeal of reading through the early drafts of all chapters and provided constructive reviews that motivated me to improve the material. This book would not have been possible without the diligence and patience of my Coordinating Editor, Mark Powers, who displayed a great deal of professionalism dealing with both the ups and downs of a manuscript and whose merit it is that the book actually got finished. The support of Development Editor Matthew Moodie and Acquisitions Editor Stephen Anglin was equally indispensable during the last 12 months. Finally, I would like to thank my family for the support that gave me the courage to finish the book. My wife Magdalena Rother, being a fabulous Python developer, gave me counsel throughout the writing process, including critically examining my code, testing Python tools with me, debating programming practices after midnight, and making every other moment with her a moment well spent. Contents Chapter 1: Introduction A Lesson in Humility The Case for Best Practices in Python The Origin of Best Practices Hacking Software Engineering Agile Software Craftsmanship Who This Book Is For What This Book Is About Part 1: Debugging Part 2: Automated Testing Part 3: Maintenance Further Benefits The MazeRun Game How to Use This Book? Install Python 3 Install the Pygame Library Install a Text Editor Download the Source Code Examples Part I: Debugging Chapter 2: Exceptions in Python Exceptions Are Defects We Know About Reading the Code SyntaxError Best Practices for Debugging SyntaxErrors Examining the Error Message The Error Type The Error Description The Traceback Deduction Catching Exceptions Best Practices for Debugging IOErrors Errors and Defects Where Do Defects Come From? The Correct Code Best Practices Chapter 3: Semantic Errors in Python Comparing Expected and Factual Output Defects Defects in Variable Assignments Multiple Initialization Accidental Assignment Accidental Comparison Wrong Variables in an Expression Swapped Literals in Expression Defects in Indexing Creating Wrong Indices Using Wrong Indices Defects in Control Flow Statements Defects in Boolean Expressions Defects with Indentation Defects in Using Functions Omitting a Function Call Missing Return Statement Not Storing the Return Value Error Propagation Best Practices Chapter 4: Debugging with the Scientific Method Applying the Scientific Method Reproducing the Error
Description: