The Hitchhiker's Guide to Python
Total Page:16
File Type:pdf, Size:1020Kb
The H i t c h h i k e r ’s Guide to P y t h o n BEST PRACTICES FOR DEVELOPMENT Kenneth Reitz & Tanya Schlusser The Hitchhiker’s Guide to Python Best Practices for Development Kenneth Reitz and Tanya Schlusser Beijing Boston Farnham Sebastopol Tokyo The Hitchhiker’s Guide to Python by Kenneth Reitz and Tanya Schlusser Copyright © 2016 Kenneth Reitz, Tanya Schlusser. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected]. Editor: Dawn Schanafelt Indexer: WordCo Indexing Services, Inc. Production Editor: Nicole Shelby, Nicholas Adams Interior Designer: David Futato Copyeditor: Jasmine Kwityn Cover Designer: Randy Comer Proofreader: Amanda Kersey Illustrator: Rebecca Demarest September 2016: First Edition Revision History for the First Edition 2016-08-26: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491933176 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. The Hitchhiker’s Guide to Python, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-93317-6 [LSI] Dedicated to you Table of Contents Preface. xi Part I. Getting Started 1. Picking an Interpreter. 3 The State of Python 2 Versus Python 3 3 Recommendations 4 So…3? 4 Implementations 5 CPython 5 Stackless 5 PyPy 6 Jython 6 IronPython 6 PythonNet 6 Skulpt 7 MicroPython 7 2. Properly Installing Python. 9 Installing Python on Mac OS X 9 Setuptools and pip 11 virtualenv 11 Installing Python on Linux 12 Setuptools and pip 12 Development Tools 13 virtualenv 14 Installing Python on Windows 15 v Setuptools and pip 17 virtualenv 18 Commercial Python Redistributions 18 3. Your Development Environment. 21 Text Editors 22 Sublime Text 23 Vim 23 Emacs 25 TextMate 26 Atom 26 Code 27 IDEs 27 PyCharm/IntelliJ IDEA 29 Aptana Studio 3/Eclipse + LiClipse + PyDev 29 WingIDE 30 Spyder 30 NINJA-IDE 30 Komodo IDE 31 Eric (the Eric Python IDE) 31 Visual Studio 32 Enhanced Interactive Tools 32 IDLE 32 IPython 33 bpython 33 Isolation Tools 33 Virtual Environments 34 pyenv 36 Autoenv 36 virtualenvwrapper 37 Buildout 38 Conda 38 Docker 39 Part II. Getting Down to Business 4. Writing Great Code. 43 Code Style 43 PEP 8 44 PEP 20 (a.k.a. The Zen of Python) 45 General Advice 46 vi | Table of Contents Conventions 52 Idioms 54 Common Gotchas 58 Structuring Your Project 61 Modules 61 Packages 65 Object-Oriented Programming 66 Decorators 67 Dynamic Typing 68 Mutable and Immutable Types 69 Vendorizing Dependencies 71 Testing Your Code 72 Testing Basics 74 Examples 76 Other Popular Tools 80 Documentation 82 Project Documentation 82 Project Publication 83 Docstring Versus Block Comments 84 Logging 84 Logging in a Library 85 Logging in an Application 86 Choosing a License 88 Upstream Licenses 88 Options 88 Licensing Resources 90 5. Reading Great Code. 91 Common Features 92 HowDoI 93 Reading a Single-File Script 93 Structure Examples from HowDoI 96 Style Examples from HowDoI 97 Diamond 99 Reading a Larger Application 100 Structure Examples from Diamond 105 Style Examples from Diamond 109 Tablib 112 Reading a Small Library 112 Structure Examples from Tablib 116 Style Examples from Tablib 124 Requests 126 Table of Contents | vii Reading a Larger Library 126 Structure Examples from Requests 130 Style Examples from Requests 135 Werkzeug 140 Reading Code in a Toolkit 141 Style Examples from Werkzeug 148 Structure Examples from Werkzeug 149 Flask 155 Reading Code in a Framework 156 Style Examples from Flask 162 Structure Examples from Flask 163 6. Shipping Great Code. 167 Useful Vocabulary and Concepts 168 Packaging Your Code 169 Conda 169 PyPI 170 Freezing Your Code 172 PyInstaller 174 cx_Freeze 176 py2app 177 py2exe 178 bbFreeze 178 Packaging for Linux-Built Distributions 179 Executable ZIP Files 180 Part III. Scenario Guide 7. User Interaction. 185 Jupyter Notebooks 185 Command-Line Applications 186 GUI Applications 194 Widget Libraries 194 Game Development 200 Web Applications 200 Web Frameworks/Microframeworks 201 Web Template Engines 204 Web Deployment 209 8. Code Management and Improvement. 213 Continuous Integration 213 viii | Table of Contents System Administration 214 Server Automation 216 System and Task Monitoring 220 Speed 223 Interfacing with C/C++/FORTRAN Libraries 232 9. Software Interfaces. 237 Web Clients 238 Web APIs 238 Data Serialization 243 Distributed Systems 246 Networking 246 Cryptography 251 10. Data Manipulation. 259 Scientific Applications 260 Text Manipulation and Text Mining 264 String Tools in Python’s Standard Library 264 Image Manipulation 267 11. Data Persistence. 271 Structured Files 271 Database Libraries 272 A. Additional Notes. 287 Index. 295 Table of Contents | ix Preface Python is big. Really big. You just won’t believe how vastly hugely mind-bogglingly big it is. This guide is not intended to teach you the Python language (we cite lots of great resources that do that) but is rather an (opinionated) insider’s guide to our communi‐ ty’s favorite tools and best practices. The primary audience is new to mid-level Python programmers who are interested in contributing to open source or in begin‐ ning a career or starting a company using Python, although casual Python users should also find Part I and Chapter 5 helpful. The first part will help you choose the text editor or interactive development environ‐ ment that fits your situation (for example, those using Java frequently may prefer Eclipse with a Python plug-in) and surveys options for other interpreters that may meet needs you don’t yet know Python could address (e.g., there’s a MicroPython implementation based around the ARM Cortex-M4 chip). The second section dem‐ onstrates Pythonic style by highlighting exemplary code in the open source commu‐ nity that will hopefully encourage more in-depth reading and experimentation with open source code. The final section briefly surveys the vast galaxy of libraries most commonly used in the Python community—providing an idea of the scope of what Python can do right now. All of the royalties from the print version of this book will be directly donated to the Django Girls, a giddily joyous global organization dedicated to organizing free Django and Python workshops, creating open-sourced online tutorials, and curating amazing first experiences with technology. Those who wish to contribute to the online version can read more about how to do it at our website. xi Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width Used for program listings, as well as within paragraphs to refer to program ele‐ ments such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold Shows commands or other text that should be typed literally by the user. Constant width italic Shows text that should be replaced with user-supplied values or by values deter‐ mined by context. This element signifies a tip or suggestion. This element signifies a general note. This element indicates a warning or caution. Safari® Books Online Safari Books Online is an on-demand digital library that deliv‐ ers expert content in both book and video form from the world’s leading authors in technology and business. xii | Preface Technology professionals, software developers, web designers, and business and crea‐ tive professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training. Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals. Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kauf‐ mann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online. How to Contact Us Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax) We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/the-hitchhikers-guide-to-python. To comment or ask technical questions about this book, send email to bookques‐ [email protected]. For more information about our books, courses, conferences, and news, see our web‐ site at http://www.oreilly.com. Find us on Facebook: http://facebook.com/oreilly Follow us on Twitter: http://twitter.com/oreillymedia Watch us on YouTube: http://www.youtube.com/oreillymedia Preface | xiii Acknowledgments Welcome, friends, to The Hitchhiker’s Guide to Python.