Pro Python (eBook)
XX, 384 Seiten
Apress (Verlag)
978-1-4842-0334-7 (ISBN)
You’ve learned the basics of Python, but how do you take your skills to the next stage? Even if you know enough to be productive, there are a number of features that can take you to the next level in Python. Pro Python, Second Edition explores concepts and features normally left to experimentation, allowing you to be even more productive and creative.
In addition to pure code concerns, Pro Python develops your programming techniques and approaches, which will help make you a better Python programmer. This book will improve not only your code but also your understanding and interaction with the many established Python communities.
This book takes your Python knowledge and coding skills to the next level. It shows you how to write clean, innovative code that will be respected by your peers. With this book, make your code do more with introspection and meta-programming. And learn and later use the nuts and bolts of an application, tier-by-tier as a complex case study along the way.
For more information, including a link to the source code referenced in the book, please visit http://propython.com/.
By day, Marty Alchin works as a senior software engineer at Heroku, and after that, he writes and codes for fun and community. His blog can be found at http://martyalchin.com/ and he has profiles on many other services under the name Gulopine. In particular, his code can be found on GitHub and his random thoughts are on Twitter. He also accepts tips for his open source work at https://gittip.com/gulopine.
You’ve learned the basics of Python, but how do you take your skills to the next stage? Even if you know enough to be productive, there are a number of features that can take you to the next level in Python. Pro Python, Second Edition explores concepts and features normally left to experimentation, allowing you to be even more productive and creative.In addition to pure code concerns, Pro Python develops your programming techniques and approaches, which will help make you a better Python programmer. This book will improve not only your code but also your understanding and interaction with the many established Python communities.This book takes your Python knowledge and coding skills to the next level. It shows you how to write clean, innovative code that will be respected by your peers. With this book, make your code do more with introspection and meta-programming. And learn and later use the nuts and bolts of an application, tier-by-tier as a complex case study along the way.For more information, including a link to the source code referenced in the book, please visit http://propython.com/.
By day, Marty Alchin works as a senior software engineer at Heroku, and after that, he writes and codes for fun and community. His blog can be found at http://martyalchin.com/ and he has profiles on many other services under the name Gulopine. In particular, his code can be found on GitHub and his random thoughts are on Twitter. He also accepts tips for his open source work at https://gittip.com/gulopine.
Contents at a Glance 3
Contents 355
About the Authors 367
About the Technical Reviewer 368
Acknowledgments 369
Introduction 5
Chapter 1: Principles and Philosophy 6
The Zen of Python 6
Beautiful Is Better Than Ugly 7
Explicit Is Better Than Implicit 8
Simple Is Better Than Complex 9
Complex Is Better Than Complicated 9
Flat Is Better Than Nested 10
Sparse Is Better Than Dense 11
Readability Counts 12
Special Cases Aren’t Special Enough to Break the Rules 12
Although Practicality Beats Purity 12
Errors Should Never Pass Silently 13
Unless Explicitly Silenced 15
In the Face of Ambiguity, Refuse the Temptation to Guess 16
There Should Be One—and Preferably Only One—Obvious Way to Do It 16
Although That Way May Not Be Obvious at First Unless You’re Dutch 17
Now Is Better Than Never 17
Although Never Is Often Better Than Right Now 17
If the Implementation Is Hard to Explain, It’s a Bad Idea 17
If the Implementation Is Easy to Explain, It May Be a Good Idea 18
Namespaces Are One Honking Great Idea—Let’s Do More of Those! 18
Don’t Repeat Yourself 18
Loose Coupling 19
The Samurai Principle 19
The Pareto Principle 20
The Robustness Principle 20
Backward Compatibility 21
The Road to Python 3.0 22
Taking It With You 22
Chapter 2: Advanced Basics 23
General Concepts 23
Iteration 23
Caching 24
Transparency 25
Control Flow 26
Catching Exceptions 26
Exception Chains 30
When Everything Goes Right 32
Proceeding Regardless of Exceptions 33
Optimizing Loops 35
The with Statement 36
Conditional Expressions 37
Iteration 39
Sequence Unpacking 40
List Comprehensions 42
Generator Expressions 43
Set Comprehensions 44
Dictionary Comprehensions 44
Chaining Iterables Together 45
Zipping Iterables Together 46
Collections 46
Sets 47
Named Tuples 52
Ordered Dictionaries 52
Dictionaries with Defaults 53
Importing Code 54
Fallback Imports 54
Importing from the Future 55
Using __all __ to Customize Imports 56
Relative Imports 58
The __import__(?) function 58
The importlib module 60
Taking It With You 61
Chapter 3: Functions 62
Arguments 62
Planning for Flexibility 63
Variable Positional Arguments 64
Variable Keyword Arguments 65
Combining Different Kinds of Arguments 67
Invoking Functions with Variable Arguments 69
Preloading Arguments 70
Introspection 71
Example: Identifying Argument Values 72
Example: A More Concise Version 75
Example: Validating Arguments 77
Decorators 79
Closures 81
Wrappers 82
Decorators with Arguments 84
Decorators with—or without—Arguments 86
Example: Memoization 87
Example: A Decorator to Create Decorators 89
Function Annotations 91
Example: Type Safety 91
Factoring Out the Boilerplate 99
Example: Type Coercion 101
Annotating with Decorators 103
Example: Type Safety as a Decorator 104
Generators 108
Lambdas 110
Introspection 112
Identifying Object Types 112
Modules and Packages 113
Docstrings 114
Taking It with You 116
Chapter 4: Classes 117
Inheritance 117
Multiple Inheritance 120
Method Resolution Order 121
Example: C3 Algorithm 125
Using super() to Pass Control to Other Classes 131
Introspection 135
How Classes Are Created 137
Creating Classes at Runtime 137
Metaclasses 139
Example: Plugin Framework 140
Controlling the Namespace 143
Attributes 145
Properties 145
Descriptors 147
Methods 150
Unbound Methods 150
Bound Methods 151
Class Methods 152
Static Methods 153
Assigning Functions to Classes and Instances 154
Magic Methods 154
Creating Instances 155
Example: Automatic Subclasses 156
Dealing with Attributes 157
String Representations 160
Taking It With You 162
Chapter 5: Common Protocols 163
Basic Operations 163
Mathematical Operations 165
Bitwise Operations 170
Variations 171
Numbers 173
Sign Operations 175
Comparison Operations 176
Iterables 177
Example: Repeatable Generators 179
Sequences 181
Mappings 186
Callables 187
Context Managers 188
Taking It With You 190
Chapter 6: Object Management 191
Namespace Dictionary 192
Example: Borg Pattern 192
Example: Self-Caching Properties 196
Garbage Collection 199
Reference Counting 200
Cyclical References 201
Weak References 203
Pickling 205
Copying 211
Shallow Copies 212
Deep Copies 213
Taking It With You 214
Chapter 7: Strings 215
Bytes 215
Simple Conversion: chr(?) and ord(?) 216
Complex Conversion: The Struct Module 217
Text 220
Unicode 221
Encodings 221
Simple Substitution 223
Formatting 226
Looking Up Values Within Objects 228
Distinguishing Types of Strings 228
Standard Format Specification 228
Example: Plain Text Table of Contents 230
Custom Format Specification 232
Taking It With You 233
Chapter 8: Documentation 234
Proper Naming 234
Comments 235
Docstrings 236
Describe What the Function Does 237
Explain the Arguments 237
Don’t Forget the Return Value 237
Include Any Expected Exceptions 237
Documentation Outside the Code 237
Installation and Configuration 238
Tutorials 238
Reference Documents 238
Documentation Utilities 239
Formatting 239
Links 240
Sphinx 242
Taking It With You 243
Chapter 9: Testing 244
Test-Driven Development 244
Doctests 244
Formatting Code 245
Representing Output 246
Integrating With Documentation 247
Running Tests 248
The unittest Module 249
Setting Up 250
Writing Tests 250
Other Comparisons 255
Testing Strings and Other Sequence Content 255
Testing Exceptions 256
Testing Identity 258
Tearing Down 258
Providing a Custom Test Class 259
Changing Test Behavior 259
Taking It With You 259
Chapter 10: Distribution 260
Licensing 260
GNU General Public License 260
Affero General Public License 261
GNU Lesser General Public License 262
Berkeley Software Distribution License 262
Other Licenses 263
Packaging 263
setup.py 264
MANIFEST.in 265
The sdist Command 266
Distribution 268
Taking It With You 269
Chapter 11: Sheets: A CSV Framework 270
Building a Declarative Framework 271
Introducing Declarative Programming 271
To Build or Not to Build? 272
Building the Framework 273
Managing Options 274
Defining Fields 276
Attaching a Field to a Class 278
Adding a Metaclass 280
Bringing It Together 282
Ordering Fields 284
DeclarativeMeta.__prepare__() 284
Column.__init__() 287
Column.__new__() 290
CounterMeta.__call__() 291
Choosing an Option 292
Building a Field Library 293
StringField 294
IntegerColumn 295
FloatColumn 295
DecimalColumn 296
DateColumn 297
Getting Back to CSV 301
Checking Arguments 302
Populating Values 304
The Reader 307
The Writer 312
Taking It With You 316
Appendix A: Style Guide for Python 317
Introduction 317
A Foolish Consistency Is the Hobgoblin of Little Minds 317
Code Layout 318
Indentation 318
Tabs or Spaces ? 318
Maximum Line Length 318
Blank Lines 318
Encodings (PEP 263) 319
Imports 319
Whitespace in Expressions and Statements 320
Pet Peeves 320
Other Recommendations 321
Comments 322
Block Comments 323
Inline Comments 323
Documentation Strings 323
Version Bookkeeping 323
Naming Conventions 324
Descriptive: Naming Styles 324
Prescriptive: Naming Conventions 325
Names to Avoid 325
Package and Module Names 325
Class Names 325
Exception Names 325
Global Variable Names 326
Function Names 326
Function and Method Arguments 326
Method Names and Instance Variables 326
Constants 326
Designing for Inheritance 326
Programming Recommendations 328
Copyright 330
Appendix B: Voting Guidelines 331
Abstract 331
Rationale 331
Voting Scores 331
Copyright 331
Appendix C: The Zen of Python 332
Abstract 332
The Zen of Python 332
Easter Egg 332
Copyright 332
Appendix D: Docstring Conventions 333
Abstract 333
Rationale 333
Specification 333
What Is a Docstring? 333
One-Line Docstrings 334
Multiline Docstrings 335
Handling Docstring Indentation 336
Copyright 337
Acknowledgments 337
Appendix E: Backward Compatibility Policy 338
Abstract 338
Rationale 338
Backward Compatibility Rules 338
Making Incompatible Changes 339
Copyright 339
Appendix F: Python 3000 340
Abstract 340
Naming 340
PEP Numbering 340
Timeline 340
Compatibility and Transition 341
Implementation Language 342
Meta-Contributions 342
Copyright 342
Appendix G: Python Language Moratorium 343
Abstract 343
Rationale 343
Details 344
Cannot Change 344
Case-by-Case Exemptions 344
Allowed to Change 344
Retroactive 345
Extensions 345
Copyright 345
Index 346
Erscheint lt. Verlag | 22.12.2014 |
---|---|
Zusatzinfo | XX, 384 p. 27 illus. |
Verlagsort | Berkeley |
Sprache | englisch |
Themenwelt | Informatik ► Programmiersprachen / -werkzeuge ► Python |
Mathematik / Informatik ► Informatik ► Theorie / Studium | |
Mathematik / Informatik ► Informatik ► Web / Internet | |
ISBN-10 | 1-4842-0334-8 / 1484203348 |
ISBN-13 | 978-1-4842-0334-7 / 9781484203347 |
Haben Sie eine Frage zum Produkt? |
Größe: 2,7 MB
DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasserzeichen und ist damit für Sie personalisiert. Bei einer missbräuchlichen Weitergabe des eBooks an Dritte ist eine Rückverfolgung an die Quelle möglich.
Dateiformat: PDF (Portable Document Format)
Mit einem festen Seitenlayout eignet sich die PDF besonders für Fachbücher mit Spalten, Tabellen und Abbildungen. Eine PDF kann auf fast allen Geräten angezeigt werden, ist aber für kleine Displays (Smartphone, eReader) nur eingeschränkt geeignet.
Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.
Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.
Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.
aus dem Bereich