Persistent sets

Skip Montanaro skip at pobox.com
Fri Jun 6 14:06:23 CEST 2003


Following up my own post...  I later realized I had to be careful not to
wipe out the persistent set's _data attribute because it might then become
non-persistent.  A quick scan of sets.py revealed that the Set.__iand__
method needed to be changed.

    import sets
    import shelve

    class PersistentSet(sets.Set):
        def __init__(self, file, iterable=None):
            self._data = shelve.open(file)
            if iterable is not None:
                self._update(iterable)

        def __iand__(self, other):
            """Update a set with the intersection of itself and another."""
            self._binary_sanity_check(other)
            ia = (self & other)._data
            self._data.clear()
            self._data.update(ia)
            return self

Again, no tests yet.  Maybe I'll get around to adapting some of the tests
from the sets module unit tests...

Skip





More information about the Python-list mailing list