]> git.notmuchmail.org Git - notmuch/blobdiff - bindings/python/notmuch/message.py
python: Implement Message.__cmp__ and __hash__
[notmuch] / bindings / python / notmuch / message.py
index 8fd9eacef0088c84da98e1b15eb34f71dba50a91..d6201ba88b713153ed124b9a3e3d0c22d46afd48 100644 (file)
@@ -70,6 +70,15 @@ class Messages(object):
       print (msglist[0].get_filename())
       print (msglist[1].get_filename())
       print (msglist[0].get_message_id())
+
+
+    As Message() implements both __hash__() and __cmp__(), it is
+    possible to make sets out of Messages() and use set arithmetic::
+
+        s1, s2 = set(msgs1), set(msgs2)
+        s.union(s2)
+        s1 -= s2
+        ...
     """
 
     #notmuch_messages_get
@@ -210,6 +219,11 @@ class Message(object):
     """Represents a single Email message
 
     Technically, this wraps the underlying *notmuch_message_t* structure.
+
+    As this implements both __hash__() and __cmp__(), it is possible to
+    compare 2 Message objects with::
+
+        if msg1 == msg2:
     """
 
     """notmuch_message_get_filename (notmuch_message_t *message)"""
@@ -760,6 +774,23 @@ class Message(object):
 
         return output
 
+    def __hash__(self):
+        """Implement hash(), so we can use Message() sets"""
+        file = self.get_filename()
+        if file is None:
+            return None
+        return hash(file)
+
+    def __cmp__(self, other):
+        """Implement cmp(), so we can compare Message()s
+
+        2 Messages are considered equal if they point to the same
+        Message-Id and if they point to the same file names."""
+        res =  cmp(self.get_message_id(), other.get_message_id())
+        if res:
+            res = cmp(list(self.get_filenames()), list(other.get_filenames()))
+        return res
+
     def __del__(self):
         """Close and free the notmuch Message"""
         if self._msg is not None: