Disclaimer: Packt Publishing sent me the book for free, asking me to write a review.

Introduction

Expert Python Programming is a book by Tarek Ziadé, Zope contributor and known to me from his excellent proposals about setuptools and distutils.

I have to be frank from the beginning --- this is an unusual book. Its title suggests that it's about advanced Python usage, topics that a non-expert Python user would wish to know. While it does cover some of these (but not to the breadth that I expected), it extends to a lot more things as well. I'm not sure whether I should call that a good or a bad decision --- I'll let the reader do that, based on his personal needs.

Overview

In its 350 pages, the Expert Python Programming covers:

  • Getting started
  • Syntax best practices - above and below the class level
  • Choosing good names
  • Writing a package
  • Writing an application
  • Working with zc.buildout
  • Managing code
  • Managing life cycle
  • Documenting your project
  • Test-Driven development
  • Optimization - profiling and solutions
  • Useful design patterns

As you can see, this is a very wide range of topics, each of them potentially worthy of its own book. As the book itself states, some of them are directed to Python programmers who wish to go further, while others are aimed at project leads.

This is, as I said, an unusual decision, which leads to the book not having "flow". The chapters can be easily read standalone, and sometimes the context switch between them is a bit steep. Given that format, I'll present each chapter separately, since I can't find a way to link them together.

The writing is terse and to the point, accompanied with numerous coding examples. Sometimes this is taken to the extreme, and given that it's a book not actually addressed to novices, I think that some more detailed explanation was needed.

There is a distinct preference on Zope related solutions like eggs and zc.buildout. I haven't used either so I can't comment on it. I was also a bit put off by the use of Paste to generate some files.

Chapters

1. Getting Started

This chapter should be all you need to install and create a development environment for Python on the three major platforms. Includes MinGW, tab completion with history, iPython, setuptools and how to setup your editor of choice. Thorough and useful.

2. Syntax Best Practices --- Below the Class Level

Covers list comprehensions, iterators, generators, descriptors and properties, decorators and the with statement. Here is where things become a bit icky. List comprehensions are nicely covered, as are iterators. Generators are a bit all over the place, without a coherent overview. Coroutines are next on the list (since they can be implemented with generators), but rather than showing how to implement them, a non-standard multitask package is demonstrated instead. Some functions from the itertools module are then presented, unfortunately with mediocre examples.

After that, decorators and the with statement are presented. The explanations are a little better, but the code is again needlessly complicated. Strangely, the functools module isn't mentioned at all. with and contextlib are nicely presented.

It's unfortunate that the second chapter of the book has so many shortcomings. Fortunately, things pick up in the coming chapters, so read on!

3. Syntax Best Practices --- Above the Class Level

This chapter covers multiple inheritance, MRO and super, descriptors, slots and metaclasses. Multiple inheritance and MRO are exceptional, with nice small examples that present the problems and solutions sufficiently without complicating things.

Descriptors are also well-explained (although I think that describing how attribute lookups are done should help even more), but a tendency of over-complicating examples is here again (also note that the first edition had horrible typographical mistakes that made following this chapter difficult. However, these should alredy be fixed in the print edition). Properties should probably be put before descriptors, as they are probably the single most common use of descriptors in Python. Metaclasses aren't given much space, and rightly so IMO.

4. Choosing Good Names

A short and pleasant chapter. Covers PEP8, best practices for naming things, how to design an API, how to deprecate an API. Also covers code-quality tools such as Pylint and CloneDigger.

5. Writing a Package

Here Tarek presents how distutils and setuptools work. He has written much about this topic, and it shows. His explanation is top-notch, with very good examples and a nice summary at the end.

We then have a pleasant surprise - a thorough presentation of Python Paste. I've never used Paste before (it's a tool that for generating code and other text files for templates, used extensively in Pylons), so I appreciated that bit (although I don't have a use case yet).

After a brief mention on how to use version numbers, the chapter ends, giving way to...

6. Writing an Application

This is a case study that presents the designing and implementation of 'Atomisator', a small planet-like utility for aggregating feeds. To appreciate this chapter you have to pretend that it's actually a larger and much more complicated project, or else your YAGNI alarm will often beep :). I suppose this is true with every book that tries to present a concept using basic examples though.

This chapter adds more tools to your arsenal: The excellent virtualenv package, and the handy nose test runner. It's also done using a TDD approach, which is always a good thing. I didn't like the use of Paste to create all the templates, but that's probably me being cranky.

Overall, this is a very meaty chapter. I wish I have read this 6 months ago - it would have saved me many headaches. If the book has stumbled at the first two chapters, it's completely redeemed itself by this point. I would go so far as saying that this chapter should be required reading for any developer that wants to publish a Python utility.

7. Working with zc.buildout

Here my eyes glazed over. While Tarek makes a valiant effort of explaining buildout, it still feels very over-engineered and un-Pythonic to me. I really can't say anything more.

8. Managing Code

This chapter detaches from Python momentarily and discusses version control and continuous integration. The two models of VCS are presented (Centralized vs. Distruted), and a thorough tutorial on how to setup and use Mercurial is given.

The book then presents CI using Buildbot. Unfortunately for me, zc.buildout is used again, and no alternative way of using buildbot is given. It makes sense from the author's perspective, since the sample application is managed using buildout, but it's a bit annoying if you don't subscribe to the Zope mentality.

9. Managing Life-Cycle

This is a very small chapter on different types of development (waterfall, spiral and iterative) and a nice tutorial on how to setup Trac (although, much to my chagrin, buildout recipes are used yet again - I guess I should really make an effort to learn this thing).

The last two chapters are very good, and should serve as a decent starting base for any team that hasn't implemented version control, continuous integration or issue tracking yet.

10. Documenting your Project

An interesting deviation from technical issues, where advice is given on how to write good documentation, including pointers on style, structure, language etc. A bit too fluffy for my taste, but still has good, sensible advice.

Also, a primer on reStructured Text and Sphinx, complete with installation and usage instructions. Made me want to play with Sphinx!

11. Test-Driven Development

As a practicioner of TDD, I highly recommend this chapter. It gives a thorough introduction of what is TDD and why you should use it. It mainly focuses on unit tests, presenting unittest, doctest, nose and py.test, but it also gives more information on writing acceptance tests. I realized I have bookmarked a couple of these pages to revisit at some later stage for my own projects, which is a good thing in any book.

It also covers Fakes and Mocks, including Ian Bicking's minimock library.

12. Optimization: General Principles and Profiling Techniques

A very nice chapter - provides solid advice on when to optimise, how to measure, what to optimise. Introduces all the main tools for profiling CPU, memory and other resources. Goes hand in hand with...

13. Optimization: Solutions

Discuess Big-O complexity, and some common Python tricks to optimise (use a set instead of list if testing for membership etc). Also discusses the deque, defaultdict and namedtuple collections and their use cases. I have to confess that named tuples don't seem particularly relevant to the optimisation story, though.

Then the chapter continues to cover multithreading & multiprocessing in a good and thorough way, with numerous examples and use cases. It ends with caching and how to implement memoization.

14. Useful design patterns

Covers Singleton, Borg, Adapter, Proxy, Facade, Observer, Visitor, Template. Most of them can be implemented in 5 lines of Python, but it's a good refresher.

Conclusion

Would I have bought this book? I'm not sure, but probably not. It definitely has its good pieces, and I did learn new things from it, but I think I'm not in the target audience.

However, if it was available 2 years ago, when I was just starting with Python, this book would be very helpful. It can easily be read cover-to-cover, it gives you the necessary code and a lot of helpful hints and pointers to more information. So I would recommend it to any novice programmer that wants to learn more Python, and some development best practices. You can find more here: http://www.packtpub.com/expert-python-programming/book .

Sidenote

Poor Tarek has already apologised for his Frenglish, and as a non-native speaker myself, I blame the editors. Nevertheless, I don't think that there was any place in the book where I couldn't understand what it was talking about, so no reason to worry.

January 23, 2009, 11:25 p.m. More (1677 words) 2 comments Feed
Previous entry: PySmell v0.7.3 released
Next entry: Web Science Conference 2009 - Society On-Line

Comments

1

Comment by Kaiml , 1 year ago :

ah, the norm of reciprocity

2

Comment by Marius Gedminas , 1 year ago :

Having used zc.buildout for a bit (but never written a buildout.cfg from scratch), I can say that it does feel over-engineered and complicated (just like setuptools), but it solves a certain class of problems really well (again, just like setuptools). When you want to work on a package that depends on a number of Python libraries, having zc.buildout build an isolated development sandbox for you sure beats chasing the dependencies manually.


Comments are not allowed in this post