Pro Python

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

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/.

Author(s): Marty Alchin; J Burton Browning
Edition: 2
Publisher: Apress
Year: 2014

Language: English
Pages: 384

Pro Python
Contents at a Glance
Contents
About the Authors
About the Technical Reviewer
Acknowledgments
Introduction
Chapter 1: Principles and Philosophy
The Zen of Python
Beautiful Is Better Than Ugly
Explicit Is Better Than Implicit
Simple Is Better Than Complex
Complex Is Better Than Complicated
Flat Is Better Than Nested
Sparse Is Better Than Dense
Readability Counts
Special Cases Aren’t Special Enough to Break the Rules
Although Practicality Beats Purity
Errors Should Never Pass Silently
Unless Explicitly Silenced
In the Face of Ambiguity, Refuse the Temptation to Guess
There Should Be One—and Preferably Only One—Obvious Way to Do It
Although That Way May Not Be Obvious at First Unless You’re Dutch
Now Is Better Than Never
Although Never Is Often Better Than Right Now
If the Implementation Is Hard to Explain, It’s a Bad Idea
If the Implementation Is Easy to Explain, It May Be a Good Idea
Namespaces Are One Honking Great Idea—Let’s Do More of Those!
Don’t Repeat Yourself
Loose Coupling
The Samurai Principle
The Pareto Principle
The Robustness Principle
Backward Compatibility
The Road to Python 3.0
Taking It With You
Chapter 2: Advanced Basics
General Concepts
Iteration
Caching
Transparency
Control Flow
Catching Exceptions
Exception Chains
When Everything Goes Right
Proceeding Regardless of Exceptions
Optimizing Loops
The with Statement
Conditional Expressions
Iteration
Sequence Unpacking
List Comprehensions
Generator Expressions
Set Comprehensions
Dictionary Comprehensions
Chaining Iterables Together
Zipping Iterables Together
Collections
Sets
Named Tuples
Ordered Dictionaries
Dictionaries with Defaults
Importing Code
Fallback Imports
Importing from the Future
Using __all __ to Customize Imports
Relative Imports
The __import__( ) function
The importlib module
Taking It With You
Chapter 3: Functions
Arguments
Planning for Flexibility
Variable Positional Arguments
Variable Keyword Arguments
Combining Different Kinds of Arguments
Invoking Functions with Variable Arguments
Preloading Arguments
Introspection
Example: Identifying Argument Values
Example: A More Concise Version
Example: Validating Arguments
Decorators
Closures
Wrappers
Decorators with Arguments
Decorators with—or without—Arguments
Example: Memoization
Example: A Decorator to Create Decorators
Function Annotations
Example: Type Safety
Factoring Out the Boilerplate
Example: Type Coercion
Annotating with Decorators
Example: Type Safety as a Decorator
Generators
Lambdas
Introspection
Identifying Object Types
Modules and Packages
Docstrings
Taking It with You
Chapter 4: Classes
Inheritance
Multiple Inheritance
Method Resolution Order
Example: C3 Algorithm
Using super() to Pass Control to Other Classes
Introspection
How Classes Are Created
Creating Classes at Runtime
Metaclasses
Example: Plugin Framework
Controlling the Namespace
Attributes
Properties
Descriptors
Methods
Unbound Methods
Bound Methods
Class Methods
Static Methods
Assigning Functions to Classes and Instances
Magic Methods
Creating Instances
Example: Automatic Subclasses
Dealing with Attributes
String Representations
Taking It With You
Chapter 5: Common Protocols
Basic Operations
Mathematical Operations
Bitwise Operations
Variations
Numbers
Sign Operations
Comparison Operations
Iterables
Example: Repeatable Generators
Sequences
Mappings
Callables
Context Managers
Taking It With You
Chapter 6: Object Management
Namespace Dictionary
Example: Borg Pattern
Example: Self-Caching Properties
Garbage Collection
Reference Counting
Cyclical References
Weak References
Pickling
Copying
Shallow Copies
Deep Copies
Taking It With You
Chapter 7: Strings
Bytes
Simple Conversion: chr( ) and ord( )
Complex Conversion: The Struct Module
Text
Unicode
Encodings
Simple Substitution
Formatting
Looking Up Values Within Objects
Distinguishing Types of Strings
Standard Format Specification
Example: Plain Text Table of Contents
Custom Format Specification
Taking It With You
Chapter 8: Documentation
Proper Naming
Comments
Docstrings
Describe What the Function Does
Explain the Arguments
Don’t Forget the Return Value
Include Any Expected Exceptions
Documentation Outside the Code
Installation and Configuration
Tutorials
Reference Documents
Documentation Utilities
Formatting
Links
Sphinx
Taking It With You
Chapter 9: Testing
Test-Driven Development
Doctests
Formatting Code
Representing Output
Integrating With Documentation
Running Tests
The unittest Module
Setting Up
Writing Tests
Other Comparisons
Testing Strings and Other Sequence Content
Testing Exceptions
Testing Identity
Tearing Down
Providing a Custom Test Class
Changing Test Behavior
Taking It With You
Chapter 10: Distribution
Licensing
GNU General Public License
Affero General Public License
GNU Lesser General Public License
Berkeley Software Distribution License
Other Licenses
Packaging
setup.py
MANIFEST.in
The sdist Command
Distribution
Taking It With You
Chapter 11: Sheets: A CSV Framework
Building a Declarative Framework
Introducing Declarative Programming
To Build or Not to Build?
Building the Framework
Managing Options
Defining Fields
Attaching a Field to a Class
Adding a Metaclass
Bringing It Together
Ordering Fields
DeclarativeMeta.__prepare__()
Column.__init__()
Column.__new__()
CounterMeta.__call__()
Choosing an Option
Building a Field Library
StringField
IntegerColumn
FloatColumn
DecimalColumn
DateColumn
Getting Back to CSV
Checking Arguments
Populating Values
The Reader
The Writer
Taking It With You
Appendix A: Style Guide for Python
Introduction
A Foolish Consistency Is the Hobgoblin of Little Minds
Code Layout
Indentation
Tabs or Spaces ?
Maximum Line Length
Blank Lines
Encodings (PEP 263)
Imports
Whitespace in Expressions and Statements
Pet Peeves
Other Recommendations
Comments
Block Comments
Inline Comments
Documentation Strings
Version Bookkeeping
Naming Conventions
Descriptive: Naming Styles
Prescriptive: Naming Conventions
Names to Avoid
Package and Module Names
Class Names
Exception Names
Global Variable Names
Function Names
Function and Method Arguments
Method Names and Instance Variables
Constants
Designing for Inheritance
Programming Recommendations
Copyright
Appendix B: Voting Guidelines
Abstract
Rationale
Voting Scores
Copyright
Appendix C: The Zen of Python
Abstract
The Zen of Python
Easter Egg
Copyright
Appendix D: Docstring Conventions
Abstract
Rationale
Specification
What Is a Docstring?
One-Line Docstrings
Multiline Docstrings
Handling Docstring Indentation
Copyright
Acknowledgments
Appendix E: Backward Compatibility Policy
Abstract
Rationale
Backward Compatibility Rules
Making Incompatible Changes
Copyright
Appendix F: Python 3000
Abstract
Naming
PEP Numbering
Timeline
Compatibility and Transition
Implementation Language
Meta-Contributions
Copyright
Appendix G: Python Language Moratorium
Abstract
Rationale
Details
Cannot Change
Case-by-Case Exemptions
Allowed to Change
Retroactive
Extensions
Copyright
Index