Change dbhash values in place using next() loop?

Piet van Oostrum piet at cs.uu.nl
Sun May 27 11:22:56 EDT 2001


>>>>> Carsten Gaebler <cg at schlund.de> (CG) writes:

CG> Hi there!
CG> I have a DBM file with values formatted like "a:b" where a and b are
CG> strings. Every once in a while I'd like to set a=b for every item of the
CG> file. I tried the following:

CG> import dbhash
CG> db = dbhash.open("my.db", "w")
CG> while 1:
CG>   (k, v) = db.next()
CG>   (a, b) = v.split(":")
CG>   v = "%s:%s" % (b, b)
CG>   db[k] = v


CG> This works for about half of the items, but the others remain unchanged.
CG> Does changing the values change the ordering of the file?

It could. dbhash uses a form of extensible hashing, which means that
buckets may be reorganised when a value no longer fits in it. In that case
the order will change.

CG> Any ideas besides looping over db.keys() (which at the moment is 16 MB in
CG> size) or creating a temporary file and then os.system("cp -f temp.db
CG> my.db")?

I would not copy, but rename the temporary file. And I think this might
be the most efficient solution. Ny the way, you don't need os.system to
rename.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl



More information about the Python-list mailing list