Details

Professional Python


Professional Python


1. Aufl.

von: Luke Sneeringer

32,99 €

Verlag: Wiley
Format: EPUB
Veröffentl.: 07.10.2015
ISBN/EAN: 9781119070788
Sprache: englisch
Anzahl Seiten: 288

DRM-geschütztes eBook, Sie benötigen z.B. Adobe Digital Editions und eine Adobe ID zum Lesen.

Beschreibungen

<b>Master the secret tools every Python programmer needs to know</b> <p><i>Professional Python</i> goes beyond the basics to teach beginner- and intermediate-level Python programmers the little-known tools and constructs that build concise, maintainable code. Design better architecture and write easy-to-understand code using highly adoptable techniques that result in more robust and efficient applications. Coverage includes Decorators, Context Managers, Magic Methods, Class Factories, Metaclasses, Regular Expressions, and more, including advanced methods for unit testing using asyncio and CLI tools. Each topic includes an explanation of the concept and a discussion on applications, followed by hands-on tutorials based on real-world scenarios. The "Python 3 first" approach covers multiple current versions, while ensuring long-term relevance.</p> <p>Python offers many tools and techniques for writing better code, but often confusing documentation leaves many programmers in the dark about how to use them. This book shines a light on these incredibly useful methods, giving you clear guidance toward building stronger applications.</p> <ul> <li>Learn advanced Python functions, classes, and libraries</li> <li>Utilize better development and testing tools</li> <li>Understand the "what," "when," "why," and "how"</li> </ul> <p>More than just theory or a recipe-style walk-through, this guide helps you learn — and understand — these little-known tools and techniques. You'll streamline your workflow while improving the quality of your output, producing more robust applications with cleaner code and stronger architecture. If you're ready to take your Python skills to the next level, <i>Professional Python</i> is the invaluable guide that will get you there.</p>
<p>Introduction xxv</p> <p><b>Part I: Functions</b></p> <p><b>Chapter 1: Decorators 3</b></p> <p>Understanding Decorators 3</p> <p>Decorator Syntax 4</p> <p>Order of Decorator Application 5</p> <p>Where Decorators Are Used 6</p> <p>Why You Should Write Decorators 6</p> <p>When You Should Write Decorators 7</p> <p>Additional Functionality 7</p> <p>Data Sanitization or Addition 7</p> <p>Function Registration 7</p> <p>Writing Decorators 7</p> <p>An Initial Example: A Function Registry 7</p> <p>Execution-Time Wrapping Code 9</p> <p>A Simple Type Check 9</p> <p>Preserving the help 10</p> <p>User Verification 11</p> <p>Output Formatting 12</p> <p>Logging 14</p> <p>Variable Arguments 15</p> <p>Decorator Arguments 16</p> <p>How Does This Work? 17</p> <p>The Call Signature Matters 18</p> <p>Decorating Classes 20</p> <p>Type Switching 22</p> <p>A Pitfall 24</p> <p>Summary 25</p> <p><b>Chapter 2: Context Managers 27</b></p> <p>What Is a Context Manager? 27</p> <p>Context Manager Syntax 28</p> <p>The with Statement 28</p> <p>The enter and exit Methods 28</p> <p>Exception Handling 29</p> <p>When You Should Write Context Managers 30</p> <p>Resource Cleanliness 30</p> <p>Avoiding Repetition 31</p> <p>Propagating Exceptions 31</p> <p>Suppressing Exceptions 32</p> <p>A Simpler Syntax 37</p> <p>Summary 38</p> <p><b>Chapter 3: Generators 41</b></p> <p>Understanding What a Generator Is 41</p> <p>Understanding Generator Syntax 41</p> <p>The next Function 43</p> <p>The StopIteration Exception 45</p> <p>Python 2 46</p> <p>Python 3 47</p> <p>Communication with Generators 47</p> <p>Iterables Versus Iterators 49</p> <p>Generators in the Standard Library 50</p> <p>range 50</p> <p>dict.items and Family 50</p> <p>zip 51</p> <p>map 51</p> <p>File Objects 52</p> <p>When to Write Generators 53</p> <p>Accessing Data in Pieces 53</p> <p>Computing Data in Pieces 54</p> <p>Sequences Can Be Infinite 54</p> <p>When Are Generators Singletons? 54</p> <p>Generators within Generators 55</p> <p>Summary 56</p> <p><b>Part II: Classes</b></p> <p><b>Chapter 4: Magic Methods 59</b></p> <p>Magic Method Syntax 59</p> <p>Available Methods 60</p> <p>Creation and Destruction 61</p> <p>__init__ 61</p> <p>__new__ 62</p> <p>__del__ 62</p> <p>Type Conversion 63</p> <p>__str__, __unicode__, and __bytes__ 63</p> <p>__bool__ 64</p> <p>__int__, __fl oat__, and __complex__ 65</p> <p>Comparisons 65</p> <p>Binary Equality 65</p> <p>Relative Comparisons 67</p> <p>Operator Overloading 68</p> <p>Overloading Common Methods 71</p> <p>Collections 75</p> <p>Other Magic Methods 77</p> <p>Summary 77</p> <p><b>Chapter 5: Metaclasses 79</b></p> <p>Classes and Objects 79</p> <p>Using type Directly 80</p> <p>Creating a Class 81</p> <p>Creating a Subclass 81</p> <p>The type Chain 82</p> <p>The Role of type 82</p> <p>Writing Metaclasses 83</p> <p>The new Method 83</p> <p>new Versus init 83</p> <p>A Trivial Metaclass 84</p> <p>Metaclass Inheritance 84</p> <p>Using Metaclasses 87</p> <p>Python 3 87</p> <p>Python 2 88</p> <p>What About Code That Might Run on Either Version? 88</p> <p>When Is Cross-Compatibility Important? 89</p> <p>When to Use Metaclasses 89</p> <p>Declarative Class Declaration 89</p> <p>An Existing Example 89</p> <p>How This Works 90</p> <p>Why This Is a Good Use for Metaclasses 91</p> <p>Class Verification 91</p> <p>Non-Inheriting Attributes 93</p> <p>The Question of Explicit Opt-In 94</p> <p>Meta-Coding 95</p> <p>Summary 97</p> <p><b>Chapter 6: Class Factories 99</b></p> <p>A Review of type 99</p> <p>Understanding a Class Factory Function 100</p> <p>Determining When You Should Write Class Factories 102</p> <p>Runtime Attributes 102</p> <p>Understanding Why You Should Do This 103</p> <p>Attribute Dictionaries 104</p> <p>Fleshing Out the Credential Class 104</p> <p>The Form Example 105</p> <p>Dodging Class Attribute Consistency 106</p> <p>Class Attributes Versus Instance Attributes 107</p> <p>The Class Method Limitation 108</p> <p>Tying This in with Class Factories 109</p> <p>Answering the Singleton Question 109</p> <p>Summary 111</p> <p><b>Chapter 7: Abstract Base Classes 113</b></p> <p>Using Abstract Base Classes 113</p> <p>Declaring a Virtual Subclass 115</p> <p>Why Declare Virtual Subclasses? 115</p> <p>Using register as a Decorator 117</p> <p>__subclasshook__ 117</p> <p>Declaring a Protocol 119</p> <p>Other Existing Approaches 119</p> <p>Using NotImplementedError 120</p> <p>Using Metaclasses 120</p> <p>The Value of Abstract Base Classes 122</p> <p>Abstract Properties 124</p> <p>Abstract Class or Static Methods 125</p> <p>Built-in Abstract Base Classes 126</p> <p>Single-Method ABCs 126</p> <p>Alternative-Collection ABCs 127</p> <p>Using Built-In Abstract Base Classes 128</p> <p>Additional ABCs 128</p> <p>Summary 128</p> <p><b>Part III: Data</b></p> <p><b>Chapter 8: Strings And Unicode 131</b></p> <p>Text String Versus Byte String 131</p> <p>String Data in Python 132</p> <p>Python 3 Strings 132</p> <p>Python 2 Strings 134</p> <p>six 136</p> <p>Strings with Non-ASCII Characters 136</p> <p>Observing the Difference 136</p> <p>Unicode Is a Superset of ASCII 137</p> <p>Other Encodings 137</p> <p>Encodings Are Not Cross-Compatible 138</p> <p>Reading Files 139</p> <p>Python 3 139</p> <p>Specifying Encoding 139</p> <p>Reading Bytes 140</p> <p>Python 2 140</p> <p>Reading Other Sources 141</p> <p>Specifying Python File Encodings 141</p> <p>Strict Codecs 143</p> <p>Suppressing Errors 143</p> <p>Registering Error Handlers 144</p> <p>Summary 145</p> <p><b>Chapter 9: Regular Expressions 147</b></p> <p>Why Use Regular Expressions? 147</p> <p>Regular Expressions in Python 148</p> <p>Raw Strings 148</p> <p>Match Objects 149</p> <p>Finding More Than One Match 149</p> <p>Basic Regular Expressions 150</p> <p>Character Classes 150</p> <p>Ranges 151</p> <p>Negation 151</p> <p>Shortcuts 152</p> <p>Beginning and End of String 153</p> <p>Any Character 154</p> <p>Optional Characters 154</p> <p>Repetition 155</p> <p>Repetition Ranges 155</p> <p>Open-Ended Ranges 156</p> <p>Shorthand 156</p> <p>Grouping 157</p> <p>The Zero Group 159</p> <p>Named Groups 159</p> <p>Referencing Existing Groups 160</p> <p>Lookahead 161</p> <p>Flags 163</p> <p>Case Insensitivity 163</p> <p>ASCII and Unicode 163</p> <p>Dot Matching Newline 163</p> <p>Multiline Mode 164</p> <p>Verbose Mode 164</p> <p>Debug Mode 164</p> <p>Using Multiple Flags 165</p> <p>Inline Flags 165</p> <p>Substitution 165</p> <p>Compiled Regular Expressions 166</p> <p>Summary 167</p> <p><b>Part IV: Everything Else</b></p> <p><b>Chapter 10: Python 2 Versus Python 3 171</b></p> <p>Cross-Compatibility Strategies 171</p> <p>The __future__ Module 172</p> <p>2to3 172</p> <p>Writing Changes 173</p> <p>Limitations 174</p> <p>six 174</p> <p>Changes in Python 3 175</p> <p>Strings and Unicode 175</p> <p>The print Function 176</p> <p>Division 176</p> <p>Absolute and Relative Imports 177</p> <p>Removal of “Old-Style” Classes 178</p> <p>Metaclass Syntax 179</p> <p>six.with_metaclass 179</p> <p>six.add_metaclass 180</p> <p>Exception Syntax 180</p> <p>Handling Exceptions 181</p> <p>Exception Chaining 181</p> <p>Dictionary Methods 182</p> <p>Function Methods 183</p> <p>Iterators 183</p> <p>Standard Library Relocations 184</p> <p>Merging “Fast” Modules 184</p> <p>io 184</p> <p>pickle 184</p> <p>The URL Modules 185</p> <p>Renames 185</p> <p>Other Package Reorganizations 185</p> <p>Version Detection 186</p> <p>Summary 186</p> <p><b>Chapter 11: Unit Testing 187</b></p> <p>The Testing Continuum 187</p> <p>The Copied Ecosystem 188</p> <p>The Isolated Environment 188</p> <p>Advantages and Disadvantages 189</p> <p>Speed 189</p> <p>Interactivity 189</p> <p>Testing Code 190</p> <p>Code Layout 190</p> <p>Testing the Function 191</p> <p>The assert Statement 192</p> <p>Unit Testing Frameworks 192</p> <p>Running Unit Tests 193</p> <p>Failures 193</p> <p>Errors 194</p> <p>Skipped Tests 195</p> <p>Loading Tests 196</p> <p>Mocking 197</p> <p>Mocking a Function Call 197</p> <p>Asserting Mocked Calls 199</p> <p>Inspecting Mocks 201</p> <p>Call Count and Status 201</p> <p>Multiple Calls 202</p> <p>Inspecting Calls 203</p> <p>Other Testing Tools 203</p> <p>coverage 203</p> <p>tox 204</p> <p>Other Test Runners 205</p> <p>Summary 205</p> <p><b>Chapter 12: Cli Tools 207</b></p> <p>optparse 207</p> <p>A Simple Argument 207</p> <p>name == ‘ main__’ 208</p> <p>OptionParser 208</p> <p>Options 209</p> <p>Types of Options 209</p> <p>Adding Options to OptionParser 209</p> <p>Options with Values 210</p> <p>Non-String Values 211</p> <p>Specifying Option Values 212</p> <p>Positional Arguments 214</p> <p>Counters 214</p> <p>List Values 215</p> <p>Why Use optparse? 216</p> <p>argparse 216</p> <p>The Bare Bones 217</p> <p>Arguments and Options 217</p> <p>Option Flags 217</p> <p>Alternate Prefixes 218</p> <p>Options with Values 219</p> <p>Positional Arguments 222</p> <p>Reading Files 223</p> <p>Why Use argparse? 224</p> <p>Summary 224</p> <p><b>Chapter 13: Asyncio 225</b></p> <p>The Event Loop 225</p> <p>A Simple Event Loop 226</p> <p>Running the Loop 226</p> <p>Registering Tasks and Running the Loop 227</p> <p>Delaying Calls 227</p> <p>Partials 228</p> <p>Running the Loop until a Task Completes 228</p> <p>Running a Background Loop 229</p> <p>Coroutines 230</p> <p>Nested Coroutines 231</p> <p>Futures and Tasks 232</p> <p>Futures 232</p> <p>Tasks 232</p> <p>Callbacks 234</p> <p>No Guarantee of Success 235</p> <p>Under the Hood 235</p> <p>Callbacks with Arguments 235</p> <p>Task Aggregation 236</p> <p>Gathering Tasks 236</p> <p>Waiting on Tasks 237</p> <p>Timeouts 238</p> <p>Waiting on Any Task 239</p> <p>Waiting on an Exception 239</p> <p>Queues 240</p> <p>Maximum Size 242</p> <p>Servers 242</p> <p>Summary 244</p> <p><b>Chapter 14: STYLE 245</b></p> <p>Principles 245</p> <p>Assume Your Code Will Require Maintenance 245</p> <p>Be Consistent 246</p> <p>Think About Ontology, Especially with Data 246</p> <p>Do Not Repeat Yourself 246</p> <p>Have Your Comments Explain the Story 247</p> <p>Occam’s Razor 247</p> <p>Standards 248</p> <p>Trivial Rules 248</p> <p>Documentation Strings 248</p> <p>Blank Lines 249</p> <p>Imports 249</p> <p>Variables 250</p> <p>Comments 250</p> <p>Line Length 251</p> <p>Summary 251</p> <p>Index 253</p>
<i>“Sneeringer’s book doesn’t duplicate titles which tell you everything there is to know; instead, it takes key powerful features, grouped by sensible examples, then tells you what you need to know about them and shows you how to use them…All in all, a good second Python book if you’re beginning to spend more time with the language.” </i>(MagPi, February 2016)
<p><b>About the author</b></p> <p><b>Luke Sneeringer</b> is a veteran Python developer who has designed, architected, built, and contributed to numerous Python applications for companies including FeedMagnet, May Designs, and Ansible. He is a frequent speaker at Python conferences.
<p><b>Streamline your workflow as you learn to write better code</b></p> <p>Python is powerful, dynamic, and the fastest-growing programming language. While it offers many tools for writing advanced, concise, and maintainable code, a clear explanation of the techniques has been greatly lacking. This guide provides the primer you need to unlock all the power of Python. Full explanations of each concept, discussions about applications, and hands-on tutorials teach you to design better architecture and write code that result in more robust and efficient applications. If you are already familiar with Python and ready to access all it has to offer, this book is for you. <p><i>Professional Python: </i> <ul><li>Covers all the features of the language, including functions and how to apply decorators, context managers, and generators</li> <li>Examines Python’s classes and object model, metaclasses, class factories, and abstract base classes</li> <li>Demonstrates how to navigate using Unicode strings and how strings differ between Python 2 and Python 3</li> <li>Provides an in-depth look at the distinctions between Python 2 and Python 3 and how to write code interoperable with both</li> <li>Explores unit testing, command-line interface tools, and the new asynchronous programming library</li></ul> <p><b>Wrox Professional guides</b> are planned and written by working programmers to meet the real-world needs of programmers, developers, and IT professionals. Focused and relevant, they address the issues technology professionals face every day. They provide examples, practical solutions, and expert education in new technologies, all designed to help programmers do a better job.

Diese Produkte könnten Sie auch interessieren:

PowerPoint 2016 For Dummies
PowerPoint 2016 For Dummies
von: Doug Lowe
EPUB ebook
22,99 €
Gender and the Media
Gender and the Media
von: Rosalind Gill
EPUB ebook
30,99 €
Professional Python
Professional Python
von: Luke Sneeringer
PDF ebook
32,99 €