diff options
author | Raymond Hettinger <python@rcn.com> | 2004-08-13 07:12:45 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-08-13 07:12:45 (GMT) |
commit | cc0def18685bab5b75e62d3c1780aa921b5f3396 (patch) | |
tree | 0be14fd27a4908a0fb73dc459038081d4cd92073 /Lib | |
parent | bf9ac4bd8931aae6c1aca77e62e0eb15e2ef8a28 (diff) | |
download | cpython-cc0def18685bab5b75e62d3c1780aa921b5f3396.zip cpython-cc0def18685bab5b75e62d3c1780aa921b5f3396.tar.gz cpython-cc0def18685bab5b75e62d3c1780aa921b5f3396.tar.bz2 |
Convert weak iterators to generator form.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/weakref.py | 74 |
1 files changed, 23 insertions, 51 deletions
diff --git a/Lib/weakref.py b/Lib/weakref.py index cfe9456..9373f02 100644 --- a/Lib/weakref.py +++ b/Lib/weakref.py @@ -277,54 +277,26 @@ class WeakKeyDictionary(UserDict.UserDict): self.update(kwargs) -class BaseIter: - def __iter__(self): - return self - - -class WeakKeyedKeyIterator(BaseIter): - def __init__(self, weakdict): - self._next = weakdict.data.iterkeys().next - - def next(self): - while 1: - wr = self._next() - obj = wr() - if obj is not None: - return obj - - -class WeakKeyedItemIterator(BaseIter): - def __init__(self, weakdict): - self._next = weakdict.data.iteritems().next - - def next(self): - while 1: - wr, value = self._next() - key = wr() - if key is not None: - return key, value - - -class WeakValuedValueIterator(BaseIter): - def __init__(self, weakdict): - self._next = weakdict.data.itervalues().next - - def next(self): - while 1: - wr = self._next() - obj = wr() - if obj is not None: - return obj - - -class WeakValuedItemIterator(BaseIter): - def __init__(self, weakdict): - self._next = weakdict.data.itervalues().next - - def next(self): - while 1: - wr = self._next() - value = wr() - if value is not None: - return wr.key, value +def WeakKeyedKeyIterator(weakdict): + for wr in weakdict.data.iterkeys(): + obj = wr() + if obj is not None: + yield obj + +def WeakKeyedItemIterator(weakdict): + for wr, value in weakdict.data.iteritems(): + key = wr() + if key is not None: + yield key, value + +def WeakValuedValueIterator(weakdict): + for wr in weakdict.data.itervalues(): + obj = wr() + if obj is not None: + yield obj + +def WeakValuedItemIterator(weakdict): + for wr in weakdict.data.itervalues(): + value = wr() + if value is not None: + yield wr.key, value |