python: cleanup the __nonzero__ implementations

Cleanup the code, reword the docstring and use the same implementation
in the Threads, Tags and Messages classes.

__nonzero__ implements truth value testing. If __nonzero__ is not
implemented, the python runtime would fall back to `len(..) > 0` thus
exhausting the iterator.

Signed-off-by: Justus Winter <4winter@informatik.uni-hamburg.de>
This commit is contained in:
Justus Winter 2012-04-30 19:12:36 +02:00
parent 162687a99e
commit 7f74a400d1
3 changed files with 25 additions and 24 deletions

View file

@ -172,11 +172,15 @@ class Messages(object):
next = __next__ # python2.x iterator protocol compatibility next = __next__ # python2.x iterator protocol compatibility
def __nonzero__(self): def __nonzero__(self):
""" '''
:return: True if there is at least one more thread in the Implement truth value testing. If __nonzero__ is not
Iterator, False if not.""" implemented, the python runtime would fall back to `len(..) >
return self._msgs is not None and \ 0` thus exhausting the iterator.
self._valid(self._msgs) > 0
:returns: True if the wrapped iterator has at least one more object
left.
'''
return self._msgs and self._valid(self._msgs)
_destroy = nmlib.notmuch_messages_destroy _destroy = nmlib.notmuch_messages_destroy
_destroy.argtypes = [NotmuchMessagesP] _destroy.argtypes = [NotmuchMessagesP]

View file

@ -109,15 +109,15 @@ class Tags(Python3StringMixIn):
next = __next__ # python2.x iterator protocol compatibility next = __next__ # python2.x iterator protocol compatibility
def __nonzero__(self): def __nonzero__(self):
"""Implement bool(Tags) check that can be repeatedly used '''
Implement truth value testing. If __nonzero__ is not
implemented, the python runtime would fall back to `len(..) >
0` thus exhausting the iterator.
If __nonzero__ is not implemented, "if Tags()" :returns: True if the wrapped iterator has at least one more object
will implicitly call __len__, using up our iterator, so it is left.
important that this function is defined. '''
return self._tags and self._valid(self._tags)
:returns: True if the Tags() iterator has at least one more Tag
left."""
return self._valid(self._tags) > 0
def __unicode__(self): def __unicode__(self):
"""string representation of :class:`Tags`: a space separated list of tags """string representation of :class:`Tags`: a space separated list of tags

View file

@ -157,18 +157,15 @@ class Threads(Python3StringMixIn):
return i return i
def __nonzero__(self): def __nonzero__(self):
"""Check if :class:`Threads` contains at least one more valid thread '''
Implement truth value testing. If __nonzero__ is not
implemented, the python runtime would fall back to `len(..) >
0` thus exhausting the iterator.
The existence of this function makes 'if Threads: foo' work, as :returns: True if the wrapped iterator has at least one more object
that will implicitely call len() exhausting the iterator if left.
__nonzero__ does not exist. This function makes `bool(Threads())` '''
work repeatedly. return self._threads and self._valid(self._threads)
: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 \
self._valid(self._threads) > 0
_destroy = nmlib.notmuch_threads_destroy _destroy = nmlib.notmuch_threads_destroy
_destroy.argtypes = [NotmuchThreadsP] _destroy.argtypes = [NotmuchThreadsP]