Convert weak iterators to generator form.

This commit is contained in:
Raymond Hettinger 2004-08-13 07:12:45 +00:00
parent bf9ac4bd89
commit cc0def1868
1 changed files with 20 additions and 48 deletions

View File

@ -277,54 +277,26 @@ class WeakKeyDictionary(UserDict.UserDict):
self.update(kwargs) self.update(kwargs)
class BaseIter: def WeakKeyedKeyIterator(weakdict):
def __iter__(self): for wr in weakdict.data.iterkeys():
return self 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
class WeakKeyedKeyIterator(BaseIter): def WeakValuedValueIterator(weakdict):
def __init__(self, weakdict): for wr in weakdict.data.itervalues():
self._next = weakdict.data.iterkeys().next obj = wr()
if obj is not None:
yield obj
def next(self): def WeakValuedItemIterator(weakdict):
while 1: for wr in weakdict.data.itervalues():
wr = self._next() value = wr()
obj = wr() if value is not None:
if obj is not None: yield wr.key, value
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