我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用six.advance_iterator()。
def _iter_cached(self): i = 0 gen = self._cache_gen cache = self._cache acquire = self._cache_lock.acquire release = self._cache_lock.release while gen: if i == len(cache): acquire() if self._cache_complete: break try: for j in range(10): cache.append(advance_iterator(gen)) except StopIteration: self._cache_gen = gen = None self._cache_complete = True break release() yield cache[i] i += 1 while i < self._len: yield cache[i] i += 1
def __getitem__(self, item): if self._cache_complete: return self._cache[item] elif isinstance(item, slice): if item.step and item.step < 0: return list(iter(self))[item] else: return list(itertools.islice(self, item.start or 0, item.stop or sys.maxsize, item.step or 1)) elif item >= 0: gen = iter(self) try: for i in range(item+1): res = advance_iterator(gen) except StopIteration: raise IndexError return res else: return list(iter(self))[item]
def __next__(self): try: self.dt = advance_iterator(self.gen) except StopIteration: if self.genlist[0] is self: heapq.heappop(self.genlist) else: self.genlist.remove(self) heapq.heapify(self.genlist)
def interleave(*iterables): """ Return generator that interleaves the elements of the iterables. >>> list(interleave(range(5), 'abc')) [0, 'a', 1, 'b', 2, 'c', 3, 4] >>> list(interleave('12', 'abc', '+-')) ['1', 'a', '+', '2', 'b', '-', 'c'] :param iterable iterables: Collection of iterables, e.g. lists, range, ... :return: Interleaved iterables. :rtype: iterator """ pending = len(iterables) fnext = lambda it: lambda: advance_iterator(it) nexts = itt.cycle(fnext(iter(it)) for it in iterables) while pending: try: for nxt in nexts: yield nxt() except StopIteration: pending -= 1 nexts = itt.cycle(itt.islice(nexts, pending))
def __init__(self, genlist, gen): try: self.dt = advance_iterator(gen) genlist.append(self) except StopIteration: pass self.genlist = genlist self.gen = gen
def __next__(self): try: self.dt = advance_iterator(self.gen) except StopIteration: self.genlist.remove(self)
def _iter(self): rlist = [] self._rdate.sort() self._genitem(rlist, iter(self._rdate)) for gen in [iter(x) for x in self._rrule]: self._genitem(rlist, gen) rlist.sort() exlist = [] self._exdate.sort() self._genitem(exlist, iter(self._exdate)) for gen in [iter(x) for x in self._exrule]: self._genitem(exlist, gen) exlist.sort() lastdt = None total = 0 while rlist: ritem = rlist[0] if not lastdt or lastdt != ritem.dt: while exlist and exlist[0] < ritem: advance_iterator(exlist[0]) exlist.sort() if not exlist or ritem != exlist[0]: total += 1 yield ritem.dt lastdt = ritem.dt advance_iterator(ritem) rlist.sort() self._len = total
def testIterNoKey(self): self.r.request = Mock() self.r.request.return_value = Mock(), {} self.assertRaises(StopIteration, advance_iterator, self.r.iter())
def testRequest(self): self.r.request = Mock() self.r.request.return_value = Mock(), {self.r.key: [{'sid': 'foo'}]} advance_iterator(self.r.iter()) self.r.request.assert_called_with("GET", "https://api.twilio.com/2010-04-01/Resources", params={})
def testIterOneItem(self): self.r.request = Mock() self.r.request.return_value = Mock(), {self.r.key: [{'sid': 'foo'}]} items = self.r.iter() advance_iterator(items) self.assertRaises(StopIteration, advance_iterator, items)
def testIterNoNextPage(self): self.r.request = Mock() self.r.request.return_value = Mock(), {self.r.key: []} self.assertRaises(StopIteration, advance_iterator, self.r.iter())
def test_iter_no_key(self): self.r.request = Mock() self.r.request.return_value = Mock(), {} self.assertRaises(StopIteration, advance_iterator, self.r.iter())
def test_iter_one_item(self): self.r.request = Mock() self.r.request.return_value = Mock(), {'meta': {'key': 'foos', 'next_page_url': None}, 'foos': [{'sid': '123'}]} items = self.r.iter() advance_iterator(items) self.assertRaises(StopIteration, advance_iterator, items)