]> git.notmuchmail.org Git - notmuch/commitdiff
python: cleanup the __nonzero__ implementations
authorJustus Winter <4winter@informatik.uni-hamburg.de>
Mon, 30 Apr 2012 17:12:36 +0000 (19:12 +0200)
committerJustus Winter <4winter@informatik.uni-hamburg.de>
Mon, 30 Apr 2012 17:25:16 +0000 (19:25 +0200)
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>
bindings/python/notmuch/messages.py
bindings/python/notmuch/tag.py
bindings/python/notmuch/threads.py

index 251fa3a3457f77ef42831925b7021264c4d9e3be..59ef40afee1ea2103f8273492c57b5debb4c9479 100644 (file)
@@ -172,11 +172,15 @@ class Messages(object):
     next = __next__ # python2.x iterator protocol compatibility
 
     def __nonzero__(self):
-        """
-        :return: True if there is at least one more thread in the
-            Iterator, False if not."""
-        return self._msgs is not None and \
-            self._valid(self._msgs) > 0
+        '''
+        Implement truth value testing. If __nonzero__ is not
+        implemented, the python runtime would fall back to `len(..) >
+        0` thus exhausting the iterator.
+
+        :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.argtypes = [NotmuchMessagesP]
index e0598139a0d44f7499f4ebaf1d3f6fda3f0d41e2..363c3487fff0a2d8e06c354530f97256da89286b 100644 (file)
@@ -109,15 +109,15 @@ class Tags(Python3StringMixIn):
     next = __next__ # python2.x iterator protocol compatibility
 
     def __nonzero__(self):
-        """Implement bool(Tags) check that can be repeatedly used
-
-        If __nonzero__ is not implemented, "if Tags()"
-        will implicitly call __len__, using up our iterator, so it is
-        important that this function is defined.
-
-        :returns: True if the Tags() iterator has at least one more Tag
-            left."""
-        return self._valid(self._tags) > 0
+        '''
+        Implement truth value testing. If __nonzero__ is not
+        implemented, the python runtime would fall back to `len(..) >
+        0` thus exhausting the iterator.
+
+        :returns: True if the wrapped iterator has at least one more object
+                  left.
+        '''
+        return self._tags and self._valid(self._tags)
 
     def __unicode__(self):
         """string representation of :class:`Tags`: a space separated list of tags
index a6441640605b13bf909d8a7e77c5cc609e98718a..d2e0a910e5794d3bf5b183dc436dfcd38a4005b9 100644 (file)
@@ -157,18 +157,15 @@ class Threads(Python3StringMixIn):
         return i
 
     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 \
-            self._valid(self._threads) > 0
+        '''
+        Implement truth value testing. If __nonzero__ is not
+        implemented, the python runtime would fall back to `len(..) >
+        0` thus exhausting the iterator.
+
+        :returns: True if the wrapped iterator has at least one more object
+                  left.
+        '''
+        return self._threads and self._valid(self._threads)
 
     _destroy = nmlib.notmuch_threads_destroy
     _destroy.argtypes = [NotmuchThreadsP]