""" Returns an iterator that iterates over :ref:`profiles`. Each profile is a :class:`Profile` object. """ for did, entry in entries(): yield Profile(entry) # # used by profile scripts # def profile_events(): decoder = Decoder(json_decode=BenJson, lazylist_obj=ChunkedList) def events_iter(it): for did, entry in it: if entry == 'profile_done': return else: yield Event._make(entry) entries_iter = entries(decoder) for did, profile_data in entries_iter: profile = Profile(profile_data) events = events_iter(entries_iter) yield profile, events for event in events: pass profile.close() encoder_alias(Profile, DictType)
# chunk if the predicate fails. Note that since we check only # the first item, the chunk may still contain entries for which # the predicate would fail, so drop_chunks IS NOT guaranteed to # get rid of all the items in the tail which fail the predicate. # # The assumption is that the predicate will change over time and # eventually fail the first item, thus drop the chunk. return not (chunk and pred(self.iter(chunk).next())) def head_predicate(item): return not pred(self._decode(item, 0)[0]) self._tail = list(dropwhile(tail_predicate, self._tail)) if not self._tail: # head is cleaned only if there is nothing left in the tail. self._head = list(dropwhile(head_predicate, self._head)) encoder_alias(ChunkedList, BenLazyList) if __name__ == '__main__': from bencode import bdecode, BenJson def test(): c = ChunkedList(chunk_size=14) c.push(('0')) c.push(('1')) c.push(('2')) c.push(('a', 'b', 'c')) c.push(('d', 'e', 'f')) d = bdecode(bencode(c)) d = ChunkedList(d.encode()) d.push(('g', 'h', 'i')) print list(d) print list(bdecode(bencode(d)))
# get rid of all the items in the tail which fail the predicate. # # The assumption is that the predicate will change over time and # eventually fail the first item, thus drop the chunk. return not (chunk and pred(self.iter(chunk).next())) def head_predicate(item): return not pred(self._decode(item, 0)[0]) self._tail = list(dropwhile(tail_predicate, self._tail)) if not self._tail: # head is cleaned only if there is nothing left in the tail. self._head = list(dropwhile(head_predicate, self._head)) encoder_alias(ChunkedList, BenLazyList) if __name__ == '__main__': from bencode import bdecode, BenJson def test(): c = ChunkedList(chunk_size=14) c.push(('0')) c.push(('1')) c.push(('2')) c.push(('a', 'b', 'c')) c.push(('d', 'e', 'f')) d = bdecode(bencode(c)) d = ChunkedList(d.encode()) d.push(('g', 'h', 'i')) print list(d)