Change dbhash values in place using next() loop?

Thomas Wouters thomas at xs4all.net
Sun May 27 10:21:26 EDT 2001


On Sun, May 27, 2001 at 02:27:49PM +0200, Carsten Gaebler wrote:

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

[ Code snippet that modifies the dbm it iterates over ]

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

It can, yes. So don't do that. Instead, build a list of new values (for
instance, in a separate dict, or in a second dbm file) and write it out
later (or move the new dbm over the old one.)

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

On UNIX, you can use 'os.rename' to make it an atomic move ;) Not on
Windows, though, as it'll throw an exception if 'my.db' still exists.

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list