From: Sebastian Spaeth Date: Wed, 15 Jun 2011 13:05:47 +0000 (+0200) Subject: python: Implement Message.__cmp__ and __hash__ X-Git-Tag: debian/0.6_254~38 X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=commitdiff_plain;h=4557af30649e7fdf11b8194e405cb272f13887da python: Implement Message.__cmp__ and __hash__ We can now do: if msg1 == msg2, and we can use set arithmetic on Messages(): s1, s2= msgs1, msgs2 s1.union(s2) s2 -= s1 Signed-off-by: Sebastian Spaeth --- diff --git a/bindings/python/notmuch/message.py b/bindings/python/notmuch/message.py index 8fd9eace..d6201ba8 100644 --- a/bindings/python/notmuch/message.py +++ b/bindings/python/notmuch/message.py @@ -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: