Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`anydbm` --- Generic access to DBM-style databases |
| 3 | ======================================================= |
| 4 | |
| 5 | .. module:: anydbm |
| 6 | :synopsis: Generic interface to DBM-style database modules. |
| 7 | |
| 8 | |
| 9 | .. index:: |
| 10 | module: dbhash |
| 11 | module: bsddb |
| 12 | module: gdbm |
| 13 | module: dbm |
| 14 | module: dumbdbm |
| 15 | |
| 16 | :mod:`anydbm` is a generic interface to variants of the DBM database --- |
| 17 | :mod:`dbhash` (requires :mod:`bsddb`), :mod:`gdbm`, or :mod:`dbm`. If none of |
| 18 | these modules is installed, the slow-but-simple implementation in module |
| 19 | :mod:`dumbdbm` will be used. |
| 20 | |
| 21 | |
| 22 | .. function:: open(filename[, flag[, mode]]) |
| 23 | |
| 24 | Open the database file *filename* and return a corresponding object. |
| 25 | |
| 26 | If the database file already exists, the :mod:`whichdb` module is used to |
| 27 | determine its type and the appropriate module is used; if it does not exist, the |
| 28 | first module listed above that can be imported is used. |
| 29 | |
| 30 | The optional *flag* argument can be ``'r'`` to open an existing database for |
| 31 | reading only, ``'w'`` to open an existing database for reading and writing, |
| 32 | ``'c'`` to create the database if it doesn't exist, or ``'n'``, which will |
| 33 | always create a new empty database. If not specified, the default value is |
| 34 | ``'r'``. |
| 35 | |
| 36 | The optional *mode* argument is the Unix mode of the file, used only when the |
| 37 | database has to be created. It defaults to octal ``0666`` (and will be modified |
| 38 | by the prevailing umask). |
| 39 | |
| 40 | |
| 41 | .. exception:: error |
| 42 | |
| 43 | A tuple containing the exceptions that can be raised by each of the supported |
| 44 | modules, with a unique exception also named :exc:`anydbm.error` as the first |
| 45 | item --- the latter is used when :exc:`anydbm.error` is raised. |
| 46 | |
| 47 | The object returned by :func:`open` supports most of the same functionality as |
| 48 | dictionaries; keys and their corresponding values can be stored, retrieved, and |
| 49 | deleted, and the :meth:`has_key` and :meth:`keys` methods are available. Keys |
| 50 | and values must always be strings. |
| 51 | |
| 52 | The following example records some hostnames and a corresponding title, and |
| 53 | then prints out the contents of the database:: |
| 54 | |
| 55 | import anydbm |
| 56 | |
| 57 | # Open database, creating it if necessary. |
| 58 | db = anydbm.open('cache', 'c') |
| 59 | |
| 60 | # Record some values |
| 61 | db['www.python.org'] = 'Python Website' |
| 62 | db['www.cnn.com'] = 'Cable News Network' |
| 63 | |
| 64 | # Loop through contents. Other dictionary methods |
| 65 | # such as .keys(), .values() also work. |
| 66 | for k, v in db.iteritems(): |
| 67 | print k, '\t', v |
| 68 | |
| 69 | # Storing a non-string key or value will raise an exception (most |
| 70 | # likely a TypeError). |
| 71 | db['www.yahoo.com'] = 4 |
| 72 | |
| 73 | # Close when done. |
| 74 | db.close() |
| 75 | |
| 76 | |
| 77 | .. seealso:: |
| 78 | |
| 79 | Module :mod:`dbhash` |
| 80 | BSD ``db`` database interface. |
| 81 | |
| 82 | Module :mod:`dbm` |
| 83 | Standard Unix database interface. |
| 84 | |
| 85 | Module :mod:`dumbdbm` |
| 86 | Portable implementation of the ``dbm`` interface. |
| 87 | |
| 88 | Module :mod:`gdbm` |
| 89 | GNU database interface, based on the ``dbm`` interface. |
| 90 | |
| 91 | Module :mod:`shelve` |
| 92 | General object persistence built on top of the Python ``dbm`` interface. |
| 93 | |
| 94 | Module :mod:`whichdb` |
| 95 | Utility module used to determine the type of an existing database. |
| 96 | |