[PATCH 10/15] Change filter with lambda to list comprehension

Łukasz Żarnowiecki dolohow at outlook.com
Tue May 10 00:18:32 BST 2016


It is more readable and returns a list therefore it is compatible both
with Python 2 and 3.

Signed-off-by: Łukasz Żarnowiecki <dolohow at outlook.com>
---
 offlineimap/accounts.py       |  6 ++----
 offlineimap/folder/Base.py    | 13 ++++++-------
 offlineimap/folder/Maildir.py |  2 +-
 offlineimap/imapserver.py     |  3 +--
 test/OLItest/TestRunner.py    |  4 +---
 5 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/offlineimap/accounts.py b/offlineimap/accounts.py
index aa97ffe..ab36a8b 100644
--- a/offlineimap/accounts.py
+++ b/offlineimap/accounts.py
@@ -438,8 +438,7 @@ def syncfolder(account, remotefolder, quick):
         # sync to messages with UIDs >= min_uid from this list.
         #
         # local messagelist might contain new messages (with uid's < 0).
-        positive_uids = filter(
-            lambda uid: uid > 0, localfolder.getmessageuidlist())
+        positive_uids = [uid for uid in localfolder.getmessageuidlist() if uid > 0]
         if len(positive_uids) > 0:
             remotefolder.cachemessagelist(min_uid=min(positive_uids))
         else:
@@ -482,8 +481,7 @@ def syncfolder(account, remotefolder, quick):
                 # messagelist.keys() instead of getuidmessagelist() because in
                 # the UID mapped case we want the actual local UIDs, not their
                 # remote counterparts
-                positive_uids = filter(
-                    lambda uid: uid > 0, list(partial.messagelist.keys()))
+                positive_uids = [uid for uid in list(partial.messagelist.keys()) if uid > 0]
                 if len(positive_uids) > 0:
                     min_uid = min(positive_uids)
                 else:
diff --git a/offlineimap/folder/Base.py b/offlineimap/folder/Base.py
index 6243853..f3727f4 100644
--- a/offlineimap/folder/Base.py
+++ b/offlineimap/folder/Base.py
@@ -858,8 +858,7 @@ class BaseFolder(object):
 
         threads = []
 
-        copylist = filter(lambda uid: not statusfolder.uidexists(uid),
-            self.getmessageuidlist())
+        copylist = [uid for uid in self.getmessageuidlist() if not statusfolder.uidexists(uid)]
         num_to_copy = len(copylist)
         if num_to_copy and self.repository.account.dryrun:
             self.ui.info("[DRYRUN] Copy {0} messages from {1}[{2}] to {3}".format(
@@ -909,10 +908,10 @@ class BaseFolder(object):
         # The list of messages to delete. If sync of deletions is disabled we
         # still remove stale entries from statusfolder (neither in local nor
         # remote).
-        deletelist = filter(
-                lambda uid: uid >= 0 and not self.uidexists(uid)
-                    and (self._sync_deletes or not dstfolder.uidexists(uid)),
-                statusfolder.getmessageuidlist())
+        deletelist = [uid for uid in statusfolder.getmessageuidlist()
+                      if uid >= 0 and
+                      not self.uidexists(uid) and
+                      (self._sync_deletes or not dstfolder.uidexists(uid))]
 
         if len(deletelist):
             # Delete in statusfolder first to play safe. In case of abort, we
@@ -921,7 +920,7 @@ class BaseFolder(object):
             # user, or not being tracked (e.g. because of maxage).
             statusfolder.deletemessages(deletelist)
             # Filter out untracked messages
-            deletelist = filter(lambda uid: dstfolder.uidexists(uid), deletelist)
+            deletelist = [uid for uid in deletelist if dstfolder.uidexists(uid)]
             if len(deletelist):
                 self.ui.deletingmessages(deletelist, [dstfolder])
                 if self.repository.account.dryrun:
diff --git a/offlineimap/folder/Maildir.py b/offlineimap/folder/Maildir.py
index ac6908a..6657ec4 100644
--- a/offlineimap/folder/Maildir.py
+++ b/offlineimap/folder/Maildir.py
@@ -204,7 +204,7 @@ class MaildirFolder(BaseFolder):
                 retval[uid]['filename'] = filepath
         if min_date != None:
             # Re-include messages with high enough uid's.
-            positive_uids = filter(lambda uid: uid > 0, retval)
+            positive_uids = [uid for uid in retval if uid > 0]
             if positive_uids:
                 min_uid = min(positive_uids)
                 for uid in list(date_excludees.keys()):
diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py
index ce049e3..6600dc6 100644
--- a/offlineimap/imapserver.py
+++ b/offlineimap/imapserver.py
@@ -433,8 +433,7 @@ class IMAPServer:
 
         if not tried_to_authn:
             methods = ", ".join(map(
-              lambda x: x[5:], filter(lambda x: x[0:5] == "AUTH=",
-               imapobj.capabilities)
+              lambda x: x[5:], [x for x in imapobj.capabilities if x[0:5] == "AUTH="]
             ))
             raise OfflineImapError(u"Repository %s: no supported "
               "authentication mechanisms found; configured %s, "
diff --git a/test/OLItest/TestRunner.py b/test/OLItest/TestRunner.py
index e5fc030..88dade2 100644
--- a/test/OLItest/TestRunner.py
+++ b/test/OLItest/TestRunner.py
@@ -131,9 +131,7 @@ class OLITestLib():
         else:
             sections = [r for r in config.sections() \
                             if r.startswith('Repository')]
-            sections = filter(lambda s: \
-                                  config.get(s, 'Type').lower() == 'imap',
-                              sections)
+            sections = [s for s in sections if config.get(s, 'Type').lower() == 'imap']
         for sec in sections:
             # Connect to each IMAP repo and delete all folders
             # matching the folderfilter setting. We only allow basic
-- 
2.8.2





More information about the OfflineIMAP-project mailing list