best way to store dbm records?

Tim Roberts timr at probo.com
Sat Oct 7 16:45:15 EDT 2000


"Michael B. Allen" <mballen at NOSPAM_erols.com> wrote:
>Hi!
>
>I was wondering what the best/simplest way to store dbm records is? This is
>how I am currently adding records:
>
> # create dbm record
> p = '102'
> s = getfield(msg, 'from')
> a = getfield(msg, 'authors')
> v = getfield(msg, 'version')
> d = getfield(msg, 'description')
> x = getfield(msg, 'fixes')
> o = getfield(msg, 'obsoletes')
> r = getfield(msg, 'requires')
> f = getfield(msg, 'flags')
> dbmrecord = (a,v,d,x,o,r,f)
> db[p] = `dbmrecord`

A shelve can do this all for you.  A shelve is just a dbm that holds
pickled objects.  Create a class to hold the data:

  class EMailIndex:
    self.key = ''
    ...

  fields = ( 
    'from',
    'authors',
    'version',
    'description',
    'fixes,'
    'obsoletes',
    'requires',
    'flags'
  )

  idx = EMailIndex()
  idx.key = '102'
  for name in fields:
    idx.__dict__[name] = getfield(msg, name)
  
  db = Shelve.open( dbname, 'w' )
  db[idx.key] = idx
  db.close()

Later, you can get the object back intact.

  >>>db = Shelve.open( dbname, 'r' )
  >>>xxx = db['102']
  >>>xxx.authors
  ds-0.1 

--
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list