Expert Python Programming


Expert Python Programming
eBook: $26.99
Formats: PDF, PacktLib, ePub and Mobi formats
$18.89
save 30%!
Print + free eBook + free PacktLib access to the book: $71.98    Print cover: $44.99
$44.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Overview
Table of Contents
Author
Reviews
Support
Sample Chapters
  • Learn Python development best practices from an expert, with detailed coverage of naming and coding conventions
  • Apply object-oriented principles, design patterns, and advanced syntax tricks
  • Manage your code with distributed version control
  • Profile and optimize your code
  • Proactive test-driven development and continuous integration
  • Read Chapter 10 Documenting Your Project [PDF 3 MB]

 

Book Details

Language : English
Paperback : 372 pages [ 235mm x 191mm ]
Release Date : September 2008
ISBN : 184719494X
ISBN 13 : 9781847194947
Author(s) : Tarek Ziadé
Topics and Technologies : All Books, Application Development, Open Source, Python


Table of Contents

Preface
Chapter 1: Getting started
Chapter 2: Syntax Best Practices—Below the Class Level
Chapter 3: Syntax Best Practices—Above the Class Level
Chapter 4: Choosing Good Names
Chapter 5: Writing a Package
Chapter 6: Writing an Application
Chapter 7: Working with zc.buildout
Chapter 8: Managing Code
Chapter 9: Managing Life Cycle
Chapter 10: Documenting Your Project
Chapter 11: Test-Driven Development
Chapter 12: Optimization: General Principles and Profiling Techniques
Chapter 13: Optimization: Solutions
Chapter 14: Useful Design Patterns
Index
  • Chapter 1: Getting started
    • Installing Python
      • Python Implementations
        • Jython
        • IronPython
        • PyPy
        • Other Implementations
      • Linux Installation
        • Package Installation
        • Compiling the Sources
      • Windows Installation
        • Installing Python
        • Installing MinGW
        • Installing MSYS
      • Mac OS X Installation
        • Package Installation
        • Compiling the Source
    • The Python Prompt
      • Customizing the Interactive Prompt
        • iPython: An Advanced Prompt
    • Installing setuptools
      • Understanding How It Works
      • setuptools Installation Using EasyInstall
      • Hooking MinGW into distutils
    • Working Environment
      • Using an Editor and Complementary Tools
        • Code Editor
        • Installing and Configuring Vim
        • Using Another Editor
        • Extra Binaries
      • Using an Integrated Development Environment
        • Installing Eclipse with PyDev
    • Summary
  • Chapter 2: Syntax Best Practices—Below the Class Level
    • List Comprehensions
    • Iterators and Generators
      • Generators
      • Coroutines
      • Generator Expressions
      • The itertools Module
        • islice: The Window Iterator
        • tee: The Back and Forth Iterator
        • groupby: The uniq Iterator
        • Other Functions
    • Decorators
      • How to Write a Decorator
      • Argument checking
      • Caching
      • Proxy
      • Context Provider
    • with and contextlib
      • The contextlib Module
      • Context Example
    • Summary
  • Chapter 3: Syntax Best Practices—Above the Class Level
    • Subclassing Built-in Types
    • Accessing Methods from Superclasses
      • Understanding Python's Method Resolution Order (MRO)
      • super Pitfalls
        • Mixing super and classic Calls
        • Heterogeneous Arguments
    • Best Practices
    • Descriptors and Properties
      • Descriptors
        • Introspection Descriptor
        • Meta-descriptor
      • Properties
    • Slots
    • Meta-programming
      • The__new__ Method
      • __metaclass__ Method
    • Summary
  • Chapter 4: Choosing Good Names
    • PEP 8 and Naming Best Practices
    • Naming Styles
      • Variables
        • Constants
        • Public and Private Variables
      • Functions and Methods
        • The Private Controversy
        • Special Methods
        • Arguments
      • Properties
      • Classes
      • Modules and Packages
    • Naming Guide
      • Use "has" or "is" Prefix for Boolean Elements
      • Use Plural for Elements That Are Sequences
      • Use Explicit Names for Dictionaries
      • Avoid Generic Names
      • Avoid Existing Names
    • Best Practices for Arguments
      • Build Arguments by Iterative Design
      • Trust the Arguments and Your Tests
      • Use *args and **kw Magic Arguments Carefully
    • Class Names
    • Module and Package Names
    • Working on APIs
      • Tracking Verbosity
      • Building the Namespace Tree
      • Splitting the Code
      • Using Eggs
      • Using a Deprecation Process
    • Useful Tools
      • Pylint
      • CloneDigger
    • Summary
  • Chapter 5: Writing a Package
    • A Common Pattern for All Packages
      • setup.py, the Script That Controls Everything
        • sdist
        • build and bdist
        • bdist_egg
        • install
        • How to Uninstall a Package
        • develop
        • test
        • register and upload
        • Creating a New Command
        • setup.py Usage Summary
        • Other Important Metadata
    • The Template-Based Approach
      • Python Paste
      • Creating Templates
    • Creating the Package Template
    • Development Cycle
    • Summary
  • Chapter 6: Writing an Application
    • Atomisator: An Introduction
    • Overall Picture
    • Working Environment
      • Adding a Test Runner
      • Adding a packages Structure
    • Writing the Packages
      • atomisator.parser
        • Creating the Initial Package
        • Creating the Initial doctest
        • Building the Test Environment
        • Writing the Code
      • atomisator.db
        • SQLAlchemy
        • Providing the APIs
      • atomisator.feed
      • atomisator.main
    • Distributing Atomisator
    • Dependencies between Packages
    • Summary
  • Chapter 7: Working with zc.buildout
    • zc.buildout Philosophy
      • Configuration File Structure
        • Minimum Configuration File
        • [buildout] Section Options
      • The buildout Command
      • Recipes
        • Notable Recipes
        • Creating Recipes
      • Atomisator buildout Environment
        • buildout Folder Structure
      • Going Further
    • Releasing and Distributing
      • Releasing the Packages
      • Adding a Release Configuration File
      • Building and Releasing the Application
    • Summary
  • Chapter 8: Managing Code
    • Version Control Systems
      • Centralized Systems
      • Distributed Systems
        • Distributed Strategies
      • Centralized or Distributed?
      • Mercurial
      • Project Management with Mercurial
        • Setting Up a Dedicated Folder
        • Configuring hgwebdir
        • Configuring Apache
        • Setting Up Authorizations
        • Setting Up the Client Side
    • Continuous Integration
      • Buildbot
        • Installing Buildbot
        • Hooking Buildbot and Mercurial
        • Hooking Apache and Buildbot
    • Summary
  • Chapter 9: Managing Life Cycle
    • Different Approaches
      • Waterfall Development Model
      • Spiral Development Model
      • Iterative Development Model
    • Defining a Life Cycle
      • Planning
      • Development
      • Global Debug
      • Release
    • Setting Up a Tracking System
      • Trac
        • Installation
        • Apache Settings
        • Permission Settings
      • Project Life Cycle with Trac
        • Planning
        • Development
        • Cleaning
        • Release
    • Summary
  • Chapter 10: Documenting Your Project
    • The Seven Rules of Technical Writing
      • Write in Two Steps
      • Target the Readership
      • Use a Simple Style
      • Limit the Scope of the Information
      • Use Realistic Code Examples
      • Use a Light but Sufficient Approach
      • Use Templates
    • A reStructuredText Primer
      • Section Structure
      • Lists
      • Inline Markup
      • Literal Block
      • Links
    • Building the Documentation
      • Building the Portfolio
        • Design
        • Usage
        • Operations
    • Make Your Own Portfolio
      • Building the Landscape
        • Producer's Layout
        • Consumer's Layout
    • Summary
  • Chapter 11: Test-Driven Development
    • I Don't Test
      • Test-Driven Development Principles
        • Preventing Software Regression
        • Improving Code Quality
        • Providing the Best Developer Documentation
        • Producing Robust Code Faster
      • What Kind of Tests?
        • Acceptance Tests
        • Unit Tests
        • Python Standard Test Tools
    • I Do Test
      • Unittest Pitfalls
      • Unittest Alternatives
        • nose
        • py.test
      • Fakes and Mocks
        • Building a Fake
        • Using Mocks
      • Document-Driven Development
        • Writing a Story
    • Summary
  • Chapter 12: Optimization: General Principles and Profiling Techniques
    • The Three Rules of Optimization
      • Make It Work First
      • Work from the User's Point of View
      • Keep the Code Readable(and thus maintainable)
    • Optimization Strategy
      • Find Another Culprit
      • Scale the Hardware
      • Write a Speed Test
    • Finding Bottlenecks
      • Profiling CPU Usage
        • Macro-Profiling
        • Micro-Profiling
        • Measuring Pystones
      • Profiling Memory Usage
        • How Python Deals with Memory
        • Profiling Memory
      • Profiling Network Usage
    • Summary
  • Chapter 13: Optimization: Solutions
    • Reducing the Complexity
      • Measuring Cyclomatic Complexity
      • Measuring the Big-O Notation
      • Simplifying
        • Searching in a List
        • Using a Set Instead of a List
        • Cut the External Calls, Reduce the Workload
        • Using Collections
    • Multithreading
      • What is Multithreading?
      • How Python Deals with Threads
      • When Should Threading Be Used?
        • Building Responsive Interfaces
        • Delegating Work
        • Multi-User Applications
        • Simple Example
    • Multiprocessing
      • Pyprocessing
    • Caching
      • Deterministic Caching
      • Non-Deterministic Caching
      • Pro-Active Caching
      • Memcached
    • Summary
  • Chapter 14: Useful Design Patterns
    • Creational Patterns
      • Singleton
    • Structural Patterns
      • Adapter
        • Interfaces
      • Proxy
      • Facade
    • Behavioral Patterns
      • Observer
      • Visitor
      • Template
    • Summary

Tarek Ziadé

Tarek Ziadé is CTO at Ingeniweb in Paris, working on Python, Zope, and Plone technology and on Quality Assurance. He has been involved for 5 years in the Zope community and has contributed to the Zope code itself.

Tarek has also created Afpy, the French Python User Group and has written two books in French about Python. He has gave numerous talks and tutorials in French and international events like Solutions Linux, Pycon, OSCON, and EuroPython.


Contact Tarek Ziadé

Code Downloads

Download the code and support files for this book.


Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


Errata

- 2 submitted: last submission 31 Dec 2013

Errata type: Typo | Page number: 13

"tar -xzvf" duplicated in last line.
for first publication

Errata type: Typo | Page number: 16

"Installing MSYS", the URL is the same as the one from "Installing MinGW" on page 15:
http://sourceforge.net/project/showfiles.php?group_id=2435&package_id=240780
for first publication

Errata type: Typo | Page number: 28

The following sentence at the end of the 'Extra Binaries' section is incorrect.
Notice that both grep Under Windows, these are available with MSYS.
for first publication

Errata type: Technical | Page number: 34

In the while loop of that example:
Change: "if i % 2 == 0" to "if i % 2 == 0 and i != 4"
And in the next snippet:
Change: [i for i in range(10) if i % 2 == 0] to [i for i in range(10) if i % 2 == 0 and i != 4]
and Both [0, 2, 4, 6, 8] should be replaced by [0, 2, 6, 8]
for first publication

Errata type: Typo | Page number: 39

Missing quotation mark to mark the end of the string "Don't ask yourself" in line 7 of the psycologist function.
for first publication

Errata type: Technical | Page number: 41

import time
This import is unnecessary. Remove it
for first publication

Errata type: Technical | Page number: 45

In last line: "1g1e1t1 8u1p" should be replaced by "1g1e1t1 18u1p".
for first publication

Errata type: Technical | Page number: 45

Replace: import itertools def with_head(iterable, headsize=1):...
a, b = itertools.tee(iterable)...
return list(itertools.islice(a, headsize)), b
with_head(seq)([1], itertools.tee object at 0x100c698) with_head(seq, 4)([1, 2, 3, 4], itertools.tee object at 0x100c670
With:
import itertools seq = range(10) def with_head(iterable, headsize=1):...
a, b = itertools.tee(iterable)...
return list(itertools.islice(a, headsize)), b...
with_head(seq)([0], itertools.tee object at with_head(seq, 4)([0, 1, 2, 3], itertools.tee object at 0x8a698)
for first publication

Errata type: Typo | Page number: 50

The variable called rpc_info in page is called rpc_infos in page 51.

for first publication

Errata type: Typo | Page number: 55

@locker
...def thread_safe():
...pass...
this should be:
@synchronized&
... def thread_safe():
... pass
for first publication

Errata type: Typo | Page number: 58

there is a grammar mistake at the end of the first textual paragraph:"... it does not returning anything."
for first publication

Errata type: Typo | Page number: 59

"import logging" is unnecessary in context example.
for first publication

Errata type: Typo | Page number: 60

After the word "example" and prior to the comma, there is an space.
for first publication

Errata type: Technical | Page number: 64

The traceback :
"ValueError: This value already exists for 'value'"
Should be:
"DistinctError: This value already exists for 'value'"
for first publication

Errata type: Typo | Page number: 69

"The tail is the first element of a list and head contains rest of the elements.
This should be:
"The head is the first element of a list and tail contains the rest of the elements."
for first publication

Replace:
# 1- looking for definition
if hasattr(MyClass, attribute):
attribute = MyClass.attribute
AttributeClass = attribute.__class__
# 2 - does attribute definition has a setter
if hasattr(AttributeClass, __set__):
# lets use it
AttributeClass.__set__(attribute, instance,value)
# 3 - regular way
instance.__dict__[attribute] = value
# or 'attribute' is not found in __dict__writable = (hasattr(AttributeClass, '__set__') or 'attribute' not in instance.__dict__) if readable and writable:
# 4 let's call the descriptor
return AttributeClass.__get__(attribute,instance, MyClass)
# 5 - regular access with __dict__return instance.__dict__['attribute']
# 1. looking for definition
if hasattr(MyClass, 'attribute'):
attribute = MyClass.attributeAttributeClass = attribute.__class__
# 2. found a getter, using it
if hasattr(AttributeClass, '__get__':
return AttributeClass.__get__(attribute, instance, MyClass)
# 3 - regular access with __dict__
return instance.__dict__['attribute']
Also:
replace "when a class attribute is defined and has a getter and a setter method"
by "when a class attribute is defined and has a getter method"
for first publication

Errata type: Technical | Page number: 76

replace:
instance.__dict__ = {}
instance_of.__dict__{}
for first publication

Errata type: Technical | Page number: 76

The use of MyDescriptor in the code around the center of the table is wrong and should be changed to UpperString (the name of the descriptor actually defined earlier in the code)
for first publication

Errata type: Technical | Page number: 76

MyClass.new_att = MyDescriptor()
should be replaced by
MyClass.new_att = UpperString()
for first publication

Errata type: Technical | Page number: 105

Usage of a variable called "args" which does not exist in the method "sum(sequence)"
replace "args" by "sequence".

Errata type: Technical | Page number: 108

In the call to execute, context should presumably be passed as a second argument (it's currently being ignored).
for first publication

Errata type: Typo | Page number: 111

The correct contents should be "it is more than 500 lines" where that has been substituted by than.
for first publication

Errata type: Typo | Page number: 114

text in the 3rd paragraph mentions .pylinrc -- shd be .pylintrc instead
for first publication

Errata type: Typo | Page number: 121

in the para after the bulleted list, at the end, "it was build on." should be "it was built on."
for first publication

Errata type: Typo | Page number: 137

Instead of "pbp.skeles" it should be "pbp.skels" to use the same name in the code example.
for first publication&

Errata type: Typo | Page number: 192

The picture of the browser is for "http://localhost:8000/?style=gitweb", not the "http://localhost:8000" suggested in in the top line of the page.
for first publication

Errata type: Technical | Page number: 222

change :
$ hg clone
by :
$ hg clone . ../release-atomisator-0.1.0
for first publication

Errata type: Technical | Page number: 258

suite is redefined in test_suite() and that causes an error. TestCases and TestSuites must be instantiated before passing them to addTest(). Here is the modified code:
def test_suite():
"""builds the test suite."""
def _suite(test_class):
return unittest.makeSuite(test_class)
suite = unittest.TestSuite()
suite.addTests((_suite(MyTests), _suite(MyTests2)))
return suite
for first publication

Errata type: Technical | Page number: 289

change:
"all local references in a function are removed after the interpreter:- leaves the function- makes sure the object is not being used anymore"
with :
"all local references in a function are removed after the interpreter leaves the function. If there's no more remaining reference to the object, it is removed from the memory as well."
for first publication

Errata type: Typo | Page number: 300

Broken link:
http://pages.cs.wisc.edu/~hasti/cs367-common/notes/COMPLEXITY.html#bigO
Should be:
http://pages.cs.wisc.edu/~hasti/cs367-common/notes/4.COMPLEXITY.html#bigO
for first publication

Errata type: Typo | Page number: 342

the URL for Behrens' pattern should end with .../8747 and *NOT* with /8747V
for first publication

Errata type: Typo | Page number: 88
In example code, before ">>> thats.contracted_name", remove ">>> thats.score"  
(because neither `MySimpleClass` nor the `thats` instance contain the attribute  
"score").

Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

Frequently bought together

Expert Python Programming +    Python 3 Object Oriented Programming =
50% Off
the second eBook
Price for both: $34.50

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book

  • Set up a productive development environment
  • Customize the Python prompt and deploy setuptools
  • Write efficient syntax: iterators, generators, and decorators
  • Build arguments by design and follow the best practices for working on API
  • Build, release, and distribute your applications
  • Write an application based on several eggs
  • Distribute and deploy your application with zc.buildout
  • Build and release your packages and set up a development cycle
  • Manage your code with distributed version control and continuous integration
  • Use an iterative and incremental approach to write software
  • Practice Test-Driven Development
  • Profile and optimize your code to speed up your programs
  • Apply design patterns to your applications

Chapter 1, Getting Started, explains how to install Python and makes sure all readers have the closest, standardized environment.
 
Chapter 2, Syntax Best Practices—Below the Class Level, presents iterators, generators, descriptors and so on, in an advanced way.

Chapter 3, Syntax Best Practices—Above the Class Level, is also about syntax best practices, but focuses on above the class level.

Chapter 4, Choosing Good Names, is an extension to PEP 8 with naming best practices, but also gives tips on designing good APIs.
  
Chapter 5, Writing a Package, explains how to write a package and how to use code templates, then focuses on how to release and distribute your code.

Chapter 6, Writing an Application, extends Chapter 5 by describing how a full application can be written. It demonstrates it through a small case study called Atomisator.

Chapter 7, Using zc.buildout, is about zc.buildout, a system for managing a development environment and releasing applications, which is widely used in the Zope and Plone community and is starting to be used outside the Zope world.

Chapter 8, Managing Code, shows how your project code base can be managed with distributed instead of centralized version control and explains how to set up continuous integration.

Chapter 9, Managing Life Cycle, presents how to manage software life cycle through an iterative and incremental approach.

Chapter 10, Documenting Your Project, is about documentation and gives tips on technical writing and how Python projects should be documented.

Chapter 11, Test-Driven Development, explains Test-Driven Development and the tools that can be used to do it.

Chapter 12, Optimization—General Principle and Profiling Techniques, gives profiling techniques and an optimization strategy guideline.
 
Chapter 13, Optimization—Solutions, extends Chapter 12 by providing some solutions to speed up your programs.

Chapter 14, Useful Design Patterns, ends the book with a set of design patterns and when to use them.

In Detail

Python is a dynamic programming language, used in a wide range of domains by programmers who find it simple, yet powerful. From the earliest version 15 years ago to the current one, it has constantly evolved with productivity and code readability in mind.

Even if you find writing Python code easy, writing code that is efficient and easy to maintain and reuse is not so straightforward. This book will show you how to do just that:  it will show you how Python development should be done. Python expert Tarek Ziadé takes you on a practical tour of Python application development, beginning with setting up the best development environment, and along the way looking at agile methodologies in Python, and applying proven object-oriented principles to your design.

Approach

This book is an authoritative exploration of Python best practices and applications of agile methodologies to Python, illustrated with practical, real-world examples.

Who this book is for

This book is for Python developers who are already building applications, but want to build better ones by applying best practices and new development techniques to their projects.

The reader is expected to have a sound background in Python programming.

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software