Python bugs

Skip Montanaro skip at mojam.com
Mon Nov 29 22:29:04 CET 1999


    Bjorn> Which is a good thing, but shouldn't preclude it from being used
    Bjorn> as a dictionary in the update statement (although the fix
    Bjorn> probably have to be to Dict.update rahter than os.environ).

You are correct.  It would have to make some assumptions about how to get a
dict from an instance object.  Should it access the instance's "__dict__"
attribute or (in this case) the "data" attribute or try to an "as_dict"
method?  Since no such well-defined interface currently exists, you're
probably better off in the short term simply executing

    d.update(os.environ.data)

    Bjorn> Cool (do you know if you can do db.items() also? -- found that
    Bjorn> one five minutes ago...)

I don't see it, and I doubt it's going to get added any time soon.  One
motivation for using on-disk mappings such as provided by bsddb and (g)dbm
is that they can be really huge (hundreds of thousands or millions of keys
are not out of the question).  Asking for values() or items() in these
situations is much more likely to get you into memory trouble than for
simple dicts which tend to be smaller.

Still, you can have this functionality easily enough:

    class MyShelf(shelve.Shelf):
        def values(self):
	    v = []
	    for k in self.keys():
		v.append(self[k])
	    return v

	def items(self):
	    i = []
	    for k in self.keys():
		i.append((k, self[k]))
	    return i

If your shelve object is really big, figure on getting a cup of coffee while
these methods execute, however...

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/
847-971-7098   | Python: Programming the way Guido indented...




More information about the Python-list mailing list