[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

DBM Details: Files, Portability, and Close


Despite the dictionary-like interface, DBM files really do map to one or more external
files. For instance, the underlying default dbm i n t e r f a c e u s e d b y P y t h o n 3. 1 o n W i n d o w s
writes two files—movie.dir a n d movie.dat—when a DBM file called movie i s m a d e , a n d
saves a movie.bak on later opens. If your Python has access to a different underlying
keyed-file interface, different external files might show up on your computer.
Technically, the module dbm i s r e a l l y a n i n t e r f a c e t o w h a t e v e r D B M - l i k e f i l e s y s t e m y o u
have available in your Python:


  • When opening an already existing DBM file, dbm t r i e s t o d e t e r m i n e t h e s y s t e m t h a t
    created it with the dbm.whichdb f u n c t i o n i n s t e a d. T h i s d e t e r m i n a t i o n i s b a s e d u p o n
    the content of the database itself.

  • When creating a new file, dbm today tries a set of keyed-file interface modules in a
    fixed order. According to its documentation, it attempts to import the interfaces
    dbm.bsd, dbm.gnu, dbm.ndbm, or dbm.dumb, and uses the first that succeeds. Pythons
    without any of these automatically fall back on an all-Python and always-present
    implementation called dbm.dumb, which is not really “dumb,” or course, but may
    not be as fast or robust as other options.
    Future Pythons are free to change this selection order, and may even add additional
    alternatives to it. You normally don’t need to care about any of this, though, unless you
    delete any of the files your DBM creates, or transfer them between machines with dif-
    ferent configurations—if you need to care about the portability o f y o u r D B M f i l e s ( a n d
    as we’ll see later, by proxy, that of your shelve files), you should configure machines
    such that all have the same DBM interface installed or rely upon the dumb f a l l b a c k. F o r
    example, the Berkeley DB package (a.k.a. bsddb) used by dbm.bsd is widely available
    and portable.
    Note that DBM files may or may not need to be explicitly closed, per the last entry in
    Table 17-1. Some DBM files don’t require a close call, but some depend on it to flush
    changes out to disk. On such systems, your file may be corrupted if you omit the close
    call. Unfortunately, the default DBM in some older Windows Pythons, dbhash (a.k.a.
    bsddb), is one of the DBM systems that requires a close call to avoid data loss. As a rule
    of thumb, always close your DBM files explicitly after making changes and before your
    program exits to avoid potential problems; it’s essential a “commit” operation for these
    files. This rule extends by proxy to shelves, a topic we’ll meet later in this chapter.


1308 | Chapter 17: Databases and Persistence

Do


wnload from Wow! eBook <www.wowebook.com>

Free download pdf