"Offers experienced coders advice and tips for improving knowledge of Python coding language. Includes interviews with Python experts and covers a wide range of common topics, from scaling and testing code to designing APIs"-- Provided by publisher Brief Contents......Page 3 Contents......Page 4 Introduction......Page 8 About This Book......Page 9 Versions of Python......Page 11 What to Do......Page 13 Version Numbering......Page 14 Coding Style and Automated Checks......Page 16 Tools to Catch Style Errors......Page 17 Tools to Catch Coding Errors......Page 18 Interview with Joshua Harlow......Page 19 2 Modules, Libraries & Frameworks......Page 21 The Import System......Page 22 The sys Module......Page 23 Custom Importers......Page 24 Meta Path Finders......Page 25 Useful Standard Libraries......Page 26 External Libraries......Page 28 Protecting Your Code with an API Wrapper......Page 29 Package Installation: Getting More from pip......Page 30 Using and Choosing Frameworks......Page 32 Doug Hellmann, Python Core Developer, on Python Libraries......Page 33 3 Documentation & Good API Practice......Page 38 Documenting with Sphinx......Page 39 Getting Started with Sphinx and reST......Page 40 Sphinx Modules......Page 41 Writing a Sphinx Extension......Page 44 Managing Changes to Your APIs......Page 45 Documenting Your API Changes......Page 46 Marking Deprecated Functions with the warnings Module......Page 48 Christophe de Vienne on Developing APIs......Page 50 4 Timestamps & Time Zones......Page 53 Building Default datetime Objects......Page 54 Time Zone–Aware Timestamps with dateutil......Page 56 Serializing Time Zone–Aware datetime Objects......Page 58 Solving Ambiguous Times......Page 59 Summary......Page 60 5 Distributing Software......Page 61 A Bit of setup.py History......Page 62 Packaging with setup.cfg......Page 64 The Wheel Format Distribution Standard......Page 65 Sharing Your Work with the World......Page 68 Entry Points......Page 71 Visualizing Entry Points......Page 72 Using Console Scripts......Page 73 Using Plugins and Drivers......Page 75 Summary......Page 77 Nick Coghlan on Packaging......Page 78 6 Unit Testing......Page 79 Some Simple Tests......Page 80 Skipping Tests......Page 82 Running Particular Tests......Page 83 Creating Objects Used in Tests with Fixtures......Page 85 Running Test Scenarios......Page 87 Controlled Tests Using Mocking......Page 88 Revealing Untested Code with coverage......Page 92 Virtual Environments......Page 94 Setting Up a Virtual Environment......Page 95 Using virtualenv with tox......Page 96 Re-creating an Environment......Page 98 Integrating Other Tests......Page 99 Testing Policy ......Page 100 Robert Collins on Testing......Page 101 7 Methods & Decorators......Page 103 Creating Decorators......Page 104 Writing Decorators......Page 105 Stacking Decorators......Page 106 Writing Class Decorators......Page 107 How Methods Work in Python......Page 111 Static Methods......Page 112 Class Methods......Page 113 Abstract Methods......Page 114 Mixing Static, Class, and Abstract Methods......Page 116 The Truth About super......Page 118 Summary......Page 121 8 Functional Programming......Page 122 Creating Pure Functions......Page 123 Creating a Generator......Page 124 Returning and Passing Values with yield......Page 126 Inspecting Generators......Page 127 List Comprehensions......Page 128 Functional Functions Functioning......Page 129 Getting Indexes with enumerate()......Page 130 Finding Items That Satisfy Conditions with any() and all()......Page 131 A Common Problem Solved......Page 132 Useful itertools Functions......Page 135 Summary......Page 137 9 Abstract Syntax Tree, Hy & Lisp-like Attributes......Page 138 Looking at the AST......Page 139 Writing a Program Using the AST......Page 140 The AST Objects......Page 141 Walking Through an AST......Page 142 Extending flake8 with AST Checks......Page 143 Ignoring Irrelevant Code......Page 144 Checking for the Correct Decorator......Page 145 Looking for self......Page 146 A Quick Introduction to Hy......Page 148 Paul Tagliamonte on the AST and Hy......Page 150 10 Performances & Optimizations......Page 153 Data Structures......Page 154 cProfile......Page 156 Disassembling with the dis Module......Page 158 Defining Functions Efficiently......Page 160 Ordered Lists and bisect......Page 161 namedtuple and Slots......Page 164 Memoization......Page 169 Faster Python with PyPy......Page 171 Achieving Zero Copy with the Buffer Protocol......Page 172 Victor Stinner on Optimization......Page 176 11 Scaling & Architecture......Page 179 Multithreading in Python and Its Limitations......Page 180 Multiprocessing vs. Multithreading......Page 181 Event-Driven Architecture......Page 183 Other Options and asyncio......Page 184 Service-Oriented Architecture......Page 186 Interprocess Communication with ZeroMQ......Page 187 Summary......Page 188 RDBMSs, ORMs, and When to Use Them......Page 189 Streaming Data with Flask and PostgreSQL......Page 192 Writing the Data-Streaming Application......Page 193 Building the Application......Page 195 Dimitri Fontaine on Databases......Page 197 Using six for Python 2 and 3 Support......Page 202 Strings and Unicode......Page 203 Using Python Like Lisp to Make a Single Dispatcher......Page 204 Creating Generic Methods in Lisp......Page 205 Generic Methods with Python......Page 206 Context Managers......Page 208 Less Boilerplate with attr......Page 211 Summary......Page 214 Index......Page 215