Packt Publishing Expert Python Programming (2008).pdf

(6294 KB) Pobierz
Expert Python Programming
Learn best practices to designing, coding, and
distributing your Python software
Tarek Ziadé
BIRMINGHAM - MUMBAI
 
994563025.009.png 994563025.010.png 994563025.011.png 994563025.001.png 994563025.002.png 994563025.003.png
Expert Python Programming
Copyright © 2008 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of
the information presented. However, the information contained in this book is sold
without warranty, either express or implied. Neither the author, Packt Publishing,
nor its dealers or distributors will be held liable for any damages caused or alleged to
be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: September 2008
Production Reference: 1110908
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-847194-94-7
www.packtpub.com
Cover Image by Javier BarrXa C. ( jbarriac@yahoo.com )
1
Chapter 1: Getting started
9
Installing Python
10
Python Implementations
10
Jython
10
IronPython
11
PyPy
11
Other Implementations
11
Linux Installation
12
Package Installation
12
Compiling the Sources
13
Windows Installation
14
Installing Python
14
Installing MinGW
15
Installing MSYS
16
Mac OS X Installation
17
Package Installation
17
Compiling the Source
18
The Python Prompt
18
Customizing the Interactive Prompt
19
iPython: An Advanced Prompt
20
Installing setuptools
21
Understanding How It Works
21
setuptools Installation Using EasyInstall
22
Hooking MinGW into distutils
23
Working Environment
24
Using an Editor and Complementary Tools
24
Code Editor
25
Installing and Configuring Vim
25
Using Another Editor
27
Extra Binaries
28
994563025.004.png
 
Table of Contents
Using an Integrated Development Environment
28
Installing Eclipse with PyDev
29
Summary
32
Chapter 2: Syntax Best Practices—Below the Class Level
33
List Comprehensions
34
Iterators and Generators
36
Generators
37
Coroutines
41
Generator Expressions
43
The itertools Module
44
islice: The Window Iterator
44
tee: The Back and Forth Iterator
45
groupby: The uniq Iterator
45
Other Functions
46
Decorators
47
How to Write a Decorator
48
Argument checking
50
Caching
52
Proxy
54
Context Provider
55
with and contextlib
56
The contextlib Module
58
Context Example
59
Summary
61
Chapter 3: Syntax Best Practices—Above the Class Level
63
Subclassing Built-in Types
63
Accessing Methods from Superclasses
65
Understanding Python's Method Resolution Order (MRO)
66
super Pitfalls
70
Mixing super and classic Calls
70
Heterogeneous Arguments
72
Best Practices
73
Descriptors and Properties
74
Descriptors
74
Introspection Descriptor
77
Meta-descriptor
79
Properties
81
Slots
83
Meta-programming
84
The__new__ Method
84
__metaclass__ Method
86
Summary
89
994563025.005.png 994563025.006.png
 
Table of Contents
Chapter 4: Choosing Good Names
91
PEP 8 and Naming Best Practices
91
Naming Styles
92
Variables
92
Constants
92
Public and Private Variables
95
Functions and Methods
96
The Private Controversy
97
Special Methods
98
Arguments
98
Properties
99
Classes
99
Modules and Packages
99
Naming Guide
100
Use "has" or "is" Prefix for Boolean Elements
100
Use Plural for Elements That Are Sequences
100
Use Explicit Names for Dictionaries
101
Avoid Generic Names
101
Avoid Existing Names
101
Best Practices for Arguments
102
Build Arguments by Iterative Design
102
Trust the Arguments and Your Tests
103
Use *args and **kw Magic Arguments Carefully
104
Class Names
106
Module and Package Names
107
Working on APIs
107
Tracking Verbosity
108
Building the Namespace Tree
108
Splitting the Code
110
Using Eggs
111
Using a Deprecation Process
112
Useful Tools
113
Pylint
113
CloneDigger
115
Summary
116
Chapter 5: Writing a Package
117
A Common Pattern for All Packages
117
setup.py, the Script That Controls Everything
118
sdist
119
build and bdist
121
bdist_egg
122
install
123
994563025.007.png 994563025.008.png
 
Zgłoś jeśli naruszono regulamin