You are previewing Expert Python Programming.
O'Reilly logo
Expert Python Programming

Book Description

This is the perfect book for refining your Python skills through learning best practices and the latest development techniques. Packed with real-world examples and written by an expert, this is the authoritative tutorial.

  • 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

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

    Table of Contents

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