diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2016-12-19 16:07:29 -0800 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2016-12-21 17:14:03 -0800 |
commit | 84bee04c1abae4f1832c158b505cb1b724a54f50 (patch) | |
tree | 1f4c107b31e6dbec65dc7f8c3e22d10673db5d90 /alot/db/thread.py | |
parent | 56eb5df98a68a513208ccb837170263d9d78bc3f (diff) |
db/thread: don't create multiple lists to sort
Currently this function takes a list, splits into two lists based on
whether or not a function returns None, sorts the list that isn't None,
and then appends the list of None to the end. This creates 4 new
concrete lists on each method call, requires the use of 3 filter +
lambda pairs, and calls list.sort(). Pretty complicated and inefficient.
This patch replaces that with a single sorted() function call with a kay
function that replaces None with a value that is guaranteed to sort less
than what Message.get_date() will return, but will not cause a
comparison to None (which is an error in Python 3.x). This is all based
on iterators and avoids the need for filter or list concatenation. This
should result in only one new list being created.
Diffstat (limited to 'alot/db/thread.py')
-rw-r--r-- | alot/db/thread.py | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/alot/db/thread.py b/alot/db/thread.py index ddc98d35..ef9a0578 100644 --- a/alot/db/thread.py +++ b/alot/db/thread.py @@ -151,13 +151,12 @@ class Thread(object): :rtype: list of (str, str) """ if self._authors is None: + # Sort messages with date first (by date ascending), and those + # without a date last. + msgs = sorted(self.get_messages().iterkeys(), + key=lambda m: m.get_date() or datetime.max) + seen = {} - msgs = self.get_messages().keys() - msgs_with_date = filter(lambda m: m.get_date() is not None, msgs) - msgs_without_date = filter(lambda m: m.get_date() is None, msgs) - # sort messages with date and append the others - msgs_with_date.sort(None, lambda m: m.get_date()) - msgs = msgs_with_date + msgs_without_date orderby = settings.get('thread_authors_order_by') if orderby == 'latest_message': for i, m in enumerate(msgs): |