best way to store dbm records?

Fredrik Lundh effbot at telia.com
Sat Oct 7 16:12:39 EDT 2000


Michael wrote:
>  dbmrecord = (a,v,d,x,o,r,f)
>  db[p] = `dbmrecord`
>
> Then, to get a record out I eval it like this:
> 
>  dbmrecord = eval(db[key])
>  (a,v,d,x,o,r,f) = dbmrecord
> 
> Will this work reliably or is there a better way?

as long as you stick to simple types, and trust whoever
is putting data in the database...

(if someone can mess with your database, that "eval" call
can do things you didn't expect -- including erasing all your
files, sending nasty mail, etc).

:::

somewhat safer (and more efficient) alternatives include:
http://www.python.org/doc/current/lib/module-pickle.html
http://www.python.org/doc/current/lib/module-marshal.html

e.g.

    db[p] = pickle.dumps(dbmrecord)
    dbmrecord = pickle.loads(db[key])

:::

also see the "shelve" module, which pickles things for
you on the way in to and out from the database:

    import shelve

    db = shelve.open(...)

    db[p] = dbmrecord
    dbmrecord = db[key]

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list