TIENE EN SU CESTA DE LA COMPRA
en total 0,00 €
Master the secret tools every Python programmer needs to know
Professional Python 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. All sample code is available for download from the companion website, and the ´Python 3 first´ approach covers multiple current versions, while ensuring long-term relevance.
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.
Learn advanced Python functions, classes, and libraries
Utilize better development and testing tools
Understand the ´what,´ ´when,´ ´why,´ and ´how´
Download example code to start programming right away
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, Professional Python is the invaluable guide that will get you there.
INTRODUCTION xxv
PART I: FUNCTIONS
CHAPTER 1: DECORATORS 3
Understanding Decorators 3
Decorator Syntax 4
Order of Decorator Application 5
Where Decorators Are Used 6
Why You Should Write Decorators 6
When You Should Write Decorators 7
Additional Functionality 7
Data Sanitization or Addition 7
Function Registration 7
Writing Decorators 7
An Initial Example: A Function Registry 7
Execution-Time Wrapping Code 9
A Simple Type Check 9
Preserving the help 10
User Verification 11
Output Formatting 12
Logging 14
Variable Arguments 15
Decorator Arguments 16
How Does This Work? 17
The Call Signature Matters 18
Decorating Classes 20
Type Switching 22
A Pitfall 24
Summary 25
CHAPTER 2: CONTEXT MANAGERS 27
What Is a Context Manager? 27
Context Manager Syntax 28
The with Statement 28
The enter and exit Methods 28
Exception Handling 29
When You Should Write Context Managers 30
Resource Cleanliness 30
Avoiding Repetition 31
Propagating Exceptions 31
Suppressing Exceptions 32
A Simpler Syntax 37
Summary 38
CHAPTER 3: GENERATORS 41
Understanding What a Generator Is 41
Understanding Generator Syntax 41
The next Function 43
The StopIteration Exception 45
Python 2 46
Python 3 47
Communication with Generators 47
Iterables Versus Iterators 49
Generators in the Standard Library 50
range 50
dict.items and Family 50
zip 51
map 51
File Objects 52
When to Write Generators 53
Accessing Data in Pieces 53
Computing Data in Pieces 54
Sequences Can Be Infi nite 54
When Are Generators Singletons? 54
Generators within Generators 55
Summary 56
PART II: CLASSES
CHAPTER 4: MAGIC METHODS 59
Magic Method Syntax 59
Available Methods 60
Creation and Destruction 61
__init__ 61
__new__ 62
__del__ 62
Type Conversion 63
__str__, __unicode__, and __bytes__ 63
__bool__ 64
__int__, __fl oat__, and __complex__ 65
Comparisons 65
Binary Equality 65
Relative Comparisons 67
Operator Overloading 68
Overloading Common Methods 71
Collections 75
Other Magic Methods 77
Summary 77
CHAPTER 5: METACLASSES 79
Classes and Objects 79
Using type Directly 80
Creating a Class 81
Creating a Subclass 81
The type Chain 82
The Role of type 82
Writing Metaclasses 83
The new Method 83
new Versus init 83
A Trivial Metaclass 84
Metaclass Inheritance 84
Using Metaclasses 87
Python 3 87
Python 2 88
What About Code That Might Run on Either Version? 88
When Is Cross-Compatibility Important? 89
When to Use Metaclasses 89
Declarative Class Declaration 89
An Existing Example 89
How This Works 90
Why This Is a Good Use for Metaclasses 91
Class Verification 91
Non-Inheriting Attributes 93
The Question of Explicit Opt-In 94
Meta-Coding 95
Summary 97
CHAPTER 6: CLASS FACTORIES 99
A Review of type 99
Understanding a Class Factory Function 100
Determining When You Should Write Class Factories 102
Runtime Attributes 102
Understanding Why You Should Do This 103
Attribute Dictionaries 104
Fleshing Out the Credential Class 104
The Form Example 105
Dodging Class Attribute Consistency 106
Class Attributes Versus Instance Attributes 107
The Class Method Limitation 108
Tying This in with Class Factories 109
Answering the Singleton Question 109
Summary 111
CHAPTER 7: ABSTRACT BASE CLASSES 113
Using Abstract Base Classes 113
Declaring a Virtual Subclass 115
Why Declare Virtual Subclasses? 115
Using register as a Decorator 117
__subclasshook__ 117
Declaring a Protocol 119
Other Existing Approaches 119
Using NotImplementedError 120
Using Metaclasses 120
The Value of Abstract Base Classes 122
Abstract Properties 124
Abstract Class or Static Methods 125
Built-in Abstract Base Classes 126
Single-Method ABCs 126
Alternative-Collection ABCs 127
Using Built-In Abstract Base Classes 128
Additional ABCs 128
Summary 128
PART III: DATA
CHAPTER 8: STRINGS AND UNICODE 131
Text String Versus Byte String 131
String Data in Python 132
Python 3 Strings 132
Python 2 Strings 134
six 136
Strings with Non-ASCII Characters 136
Observing the Difference 136
Unicode Is a Superset of ASCII 137
Other Encodings 137
Encodings Are Not Cross-Compatible 138
Reading Files 139
Python 3 139
Specifying Encoding 139
Reading Bytes 140
Python 2 140
Reading Other Sources 141
Specifying Python File Encodings 141
Strict Codecs 143
Suppressing Errors 143
Registering Error Handlers 144
Summary 145
CHAPTER 9: REGULAR EXPRESSIONS 147
Why Use Regular Expressions? 147
Regular Expressions in Python 148
Raw Strings 148
Match Objects 149
Finding More Than One Match 149
Basic Regular Expressions 150
Character Classes 150
Ranges 151
Negation 151
Shortcuts 152
Beginning and End of String 153
Any Character 154
Optional Characters 154
Repetition 155
Repetition Ranges 155
Open-E