def next(self): if self._threads is None: raise NotmuchError(STATUS.NOT_INITIALIZED) if not nmlib.notmuch_threads_valid(self._threads): self._threads = None raise StopIteration thread = Thread(Threads._get(self._threads), self) nmlib.notmuch_threads_move_to_next(self._threads) return thread
def __nonzero__(self): """Check if :class:`Threads` contains at least one more valid thread The existence of this function makes 'if Threads: foo' work, as that will implicitely call len() exhausting the iterator if __nonzero__ does not exist. This function makes `bool(Threads())` work repeatedly. :return: True if there is at least one more thread in the Iterator, False if not. None on a "Out-of-memory" error. """ return self._threads is not None and \ nmlib.notmuch_threads_valid(self._threads) > 0
def __len__(self): """len(:class:`Threads`) returns the number of contained Threads .. note:: As this iterates over the threads, we will not be able to iterate over them again! So this will fail:: #THIS FAILS threads = Database().create_query('').search_threads() if len(threads) > 0: #this 'exhausts' threads # next line raises NotmuchError(STATUS.NOT_INITIALIZED)!!! for thread in threads: print thread """ if self._threads is None: raise NotmuchError(STATUS.NOT_INITIALIZED) i = 0 # returns 'bool'. On out-of-memory it returns None while nmlib.notmuch_threads_valid(self._threads): nmlib.notmuch_threads_move_to_next(self._threads) i += 1 # reset self._threads to mark as "exhausted" self._threads = None return i