diff options
author | Sebastian Spaeth <Sebastian@SSpaeth.de> | 2011-06-15 15:05:47 +0200 |
---|---|---|
committer | Sebastian Spaeth <Sebastian@SSpaeth.de> | 2011-06-15 15:05:47 +0200 |
commit | 4557af30649e7fdf11b8194e405cb272f13887da (patch) | |
tree | d396173431e52bd2e875dccac84b74e028956292 /bindings | |
parent | 8866a89e3cff46dbd791a1385ca5c800a5c9091e (diff) |
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 <Sebastian@SSpaeth.de>
Diffstat (limited to 'bindings')
-rw-r--r-- | bindings/python/notmuch/message.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/bindings/python/notmuch/message.py b/bindings/python/notmuch/message.py index 8fd9eac..d6201ba 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: |