Chapter Persistence and Databases

Serializing Data Using the marshal Module Recipe 7.2. Serializing Data Using the pickle and cPickle Modules Recipe 7.3. Using Compression with Pickling Recipe 7.4. Using the cPickle Module on Classes and Instances Recipe 7.5. Holding Bound Methods in a Picklable Way Recipe 7.6. Pickling Code Objects Recipe 7.7. Mutating Objects with shelve Recipe 7.8. Using the Berkeley DB Database Recipe 7.9. Accesssing a MySQL Database Recipe 7.10. Storing a BLOB in a MySQL Database Recipe 7.11....

Organization

Each chapter is devoted to a particular kind of recipe, such as algorithms, text processing, databases, and so on. The 1st edition had 17 chapters. There have been improvements to Python, both language and library, and to the corpus of recipes the Python community has posted to the cookbook site, that convinced us to add three entirely new chapters on the iterators and generators introduced in Python 2.3 on Python's support for time and money operations, both old and...

The Trouble with Summations

How come a simple summing loop is less than maximally accurate The root of the trouble is that summing two floating-point numbers of very different magnitudes loses accuracy. For example, suppose we used decimal floating-point numbers with a precision of four digits then, summing 1.234 to 123.4 would give 124.6, losing 0.034 from the smaller number. Such artefacts are inevitable, as long as we have finite precision during our computations. Now, imagine we're using a simple loop such as total...

Self

No true difference exists between what I described as the self.something syntax and the theobject.something syntax the former is simply a special case of the latter, when the name of reference theobject happens to be selfl If you're new to OOP in Python, you should try, in an interactive Python environment, the example snippets I have shown so far and those I'm going to show in the rest of this Introduction. One of the best interactive Python environments for such exploration is the GUI shell...