[spambayes-dev] improving dumbdbm's survival chances...

Richie Hindle richie at entrian.com
Mon Jul 14 00:32:56 EDT 2003


[Tim]
> BTW, this code in the spambayes storage.py is revolting (having one module
> change the documented default behavior of another module is almost always
> indefensible -- I can't see any reason for this abuse in spambayes):
> 
> """
> # Make shelve use binary pickles by default.
> oldShelvePickler = shelve.Pickler
> def binaryDefaultPickler(f, binary=1):
>     return oldShelvePickler(f, binary)
> shelve.Pickler = binaryDefaultPickler
> """

I'll hold up my hand and admit to writing that code, in the interests of
education.  I know it's bad, but I'd like to know the 'right' way to do this.
In 2.2, shelve used text pickles and had no interface to changing that (it's
been enhanced in 2.3, but this code predates 2.3). 

Subclassing shelve.Shelf and re-implementing the pieces that pickle things
would break when a new version of shelve is released - as happened between 2.2
and 2.3.  2.3's new writeback feature would have quietly failed had I
re-implemented the 2.2 version of __setitem__ like this:

class OurShelf(shelve.Shelf)
    def __setitem__(self, key, value):
        f = StringIO()
        p = Pickler(f, binary=1)   # This line modified from shelve.Shelf
        p.dump(value)
        self.dict[key] = f.getvalue()

Yes, the offending code made me go and shower, but I didn't see a better way
of doing it.  I'm keen to learn.  8-)

-- 
Richie Hindle
richie at entrian.com




More information about the spambayes-dev mailing list