Pickled objects
Serialized Python objects saved to files and streams
Shelve files
Pickled Python objects saved in DBM keyed files
Object-oriented databases (OODBs)
Persistent Python objects stored in persistent dictionaries (ZODB, Durus)
SQL relational databases (RDBMSs)
Table-based storage that supports SQL queries (SQLite, MySQL, PostGreSQL,
etc.)
Object relational mappers (ORMs)
Mediators that map Python classes to relational tables (SQLObject, SQLAlchemy)
In some sense, Python’s interfaces to network-based object transmission protocols such
as SOAP, XML-RPC, and CORBA also offer persistence options, but they are beyond
the scope of this chapter. Here, our interest is in techniques that allow a program to
store its data directly and, usually, on the local machine. Although some database
servers may operate on a physically remote machine on a network, this is largely trans-
parent to most of the techniques we’ll study here.
We studied Python’s simple (or “flat”) file interfaces in earnest in Chapter 4, and we
have been using them ever since. Python provides standard access to both the stdio
filesystem (through the built-in open function), as well as lower-level descriptor-based
files (with the built-in os module). For simple data storage tasks, these are all that many
scripts need. To save for use in a future program run, simply write data out to a newly
opened file on your computer in text or binary mode, and read it back from that file
later. As we’ve seen, for more advanced tasks, Python also supports other file-like
interfaces such as pipes, fifos, and sockets.
Since we’ve already explored flat files, I won’t say more about them here. The rest of
this chapter introduces the remaining topics on the preceding list. At the end, we’ll also
meet a GUI program for browsing the contents of things such as shelves and DBM files.
Before that, though, we need to learn what manner of beast these are.
Fourth edition coverage note: The prior edition of this book used the
mysql-python interface to the MySQL relational database system, as well
as the ZODB object database system. As I update this chapter in June
2010, neither of these is yet available for Python 3.X, the version of
Python used in this edition. Because of that, most ZODB information
has been trimmed, and the SQL database examples here were changed
to use the SQLite in-process database system that ships with Python 3.X
as part of its standard library. The prior edition’s ZODB and MySQL
examples and overviews are still available in the examples package, as
described later. Because Python’s SQL database API is portable, though,
the SQLite code here should work largely unchanged on most other
systems.
1304 | Chapter 17: Databases and Persistence