diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2016-12-14 14:52:01 -0800 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2016-12-21 17:09:25 -0800 |
commit | 68f64472f69a509b4245a0a2ca491a1256100ab9 (patch) | |
tree | 583f28be3c6ec629354460cf16f87acc06fd0d5d | |
parent | 5745e2291fb4d3c81076256d8bc563d50da16da5 (diff) |
Replace map() and filter() with comprehensions
This had the advantage of being more readable to people without a
functional programming background, and it avoids the need to use lambdas
which are both slow and have many corners in python. In a few cases it
also allows us to use a generator expression instead of a materialized
list, which save some memory.
-rw-r--r-- | alot/buffers.py | 4 | ||||
-rw-r--r-- | alot/commands/__init__.py | 2 | ||||
-rw-r--r-- | alot/commands/envelope.py | 4 | ||||
-rw-r--r-- | alot/commands/globals.py | 2 | ||||
-rw-r--r-- | alot/commands/search.py | 2 | ||||
-rw-r--r-- | alot/commands/thread.py | 8 | ||||
-rw-r--r-- | alot/completion.py | 7 | ||||
-rw-r--r-- | alot/db/envelope.py | 3 | ||||
-rw-r--r-- | alot/db/utils.py | 2 | ||||
-rw-r--r-- | alot/settings/manager.py | 4 | ||||
-rw-r--r-- | alot/ui.py | 2 |
11 files changed, 20 insertions, 20 deletions
diff --git a/alot/buffers.py b/alot/buffers.py index b73b2ecd..e93f9356 100644 --- a/alot/buffers.py +++ b/alot/buffers.py @@ -85,7 +85,7 @@ class BufferlistBuffer(Buffer): self.isinitialized = True lines = list() - displayedbuffers = filter(self.filtfun, self.ui.buffers) + displayedbuffers = [b for b in self.ui.buffers if self.filtfun(b)] for (num, b) in enumerate(displayedbuffers): line = BufferlineWidget(b) if (num % 2) == 0: @@ -634,7 +634,7 @@ class TagListBuffer(Buffer): self.isinitialized = True lines = list() - displayedtags = sorted(filter(self.filtfun, self.tags), + displayedtags = sorted((t for t in self.tags if self.filtfun(t)), key=unicode.lower) for (num, b) in enumerate(displayedtags): if (num % 2) == 0: diff --git a/alot/commands/__init__.py b/alot/commands/__init__.py index 49a096a1..05ce3424 100644 --- a/alot/commands/__init__.py +++ b/alot/commands/__init__.py @@ -176,7 +176,7 @@ def commandfactory(cmdline, mode='global'): args = split_commandstring(cmdline) except ValueError as e: raise CommandParseError(e.message) - args = map(lambda x: string_decode(x, 'utf-8'), args) + args = [string_decode(x, 'utf-8') for x in args] logging.debug('ARGS: %s', args) cmdname = args[0] args = args[1:] diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py index c9d05b0d..8a202ad3 100644 --- a/alot/commands/envelope.py +++ b/alot/commands/envelope.py @@ -48,8 +48,8 @@ class AttachCommand(Command): envelope = ui.current_buffer.envelope if self.path: # TODO: not possible, otherwise argparse error before - files = filter(os.path.isfile, - glob.glob(os.path.expanduser(self.path))) + files = [g for g in glob.glob(os.path.expanduser(self.path)) + if os.path.isfile(g)] if not files: ui.notify('no matches, abort') return diff --git a/alot/commands/globals.py b/alot/commands/globals.py index d2d0285c..2887345f 100644 --- a/alot/commands/globals.py +++ b/alot/commands/globals.py @@ -843,7 +843,7 @@ class ComposeCommand(Command): if settings.get('compose_ask_tags'): comp = TagsCompleter(ui.dbman) tagsstring = yield ui.prompt('Tags', completer=comp) - tags = filter(lambda x: x, tagsstring.split(',')) + tags = [t for t in tagsstring.splie(',') if t] if tags is None: raise CommandCanceled() diff --git a/alot/commands/search.py b/alot/commands/search.py index b8ecef6d..63572e68 100644 --- a/alot/commands/search.py +++ b/alot/commands/search.py @@ -211,7 +211,7 @@ class TagCommand(Command): searchbuffer.result_count += (hitcount_after - hitcount_before) ui.update() - tags = filter(lambda x: x, self.tagsstring.split(',')) + tags = [x for x in self.tagsstring.split(',') if x] try: if self.action == 'add': diff --git a/alot/commands/thread.py b/alot/commands/thread.py index 0c48c174..2aa96cf8 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -73,7 +73,7 @@ def determine_sender(mail, action='reply'): # pick the most important account that has an address in candidates # and use that accounts realname and the address found here for account in my_accounts: - acc_addresses = map(re.escape, account.get_addresses()) + acc_addresses = [re.escape(a) for a in account.get_addresses()] if account.alias_regexp is not None: acc_addresses.append(account.alias_regexp) for alias in acc_addresses: @@ -551,7 +551,7 @@ class ChangeDisplaymodeCommand(Command): msg = msgt.get_message() return msg.matches(self.query) - messagetrees = filter(matches, messagetrees) + messagetrees = [m for m in messagetrees if matches(m)] for mt in messagetrees: # determine new display values for this message @@ -930,7 +930,7 @@ class OpenAttachmentCommand(Command): handler_raw_commandstring = entry['view'] # read parameter part = self.attachment.get_mime_representation() - parms = tuple(map('='.join, part.get_params())) + parms = tuple('='.join(p) for p in part.get_params()) # in case the mailcap defined command contains no '%s', # we pipe the files content to the handling command via stdin @@ -1100,7 +1100,7 @@ class TagCommand(Command): tbuffer.refresh() - tags = filter(lambda x: x, self.tagsstring.split(',')) + tags = [t for t in self.tagsstring.split(',') if t] try: for mt in messagetrees: m = mt.get_message() diff --git a/alot/completion.py b/alot/completion.py index 37861572..2e643e37 100644 --- a/alot/completion.py +++ b/alot/completion.py @@ -150,7 +150,7 @@ class QueryCompleter(Completer): resultlist.append((newtext, newpos)) return resultlist else: - matched = filter(lambda t: t.startswith(myprefix), self.keywords) + matched = (t for t in self.keywords if t.startswith(myprefix)) resultlist = [] for keyword in matched: newprefix = original[:start] + keyword + ':' @@ -412,8 +412,7 @@ class CommandCompleter(Completer): def f((completed, pos)): return ('%s %s' % (header, completed), pos + len(header) + 1) - res = map(f, res) - logging.debug(res) + logging.debug(f(r) for r in res) elif self.mode == 'envelope' and cmd == 'unset': plist = params.split(' ', 1) @@ -532,7 +531,7 @@ class PathCompleter(Completer): escaped_path = escape(path) return escaped_path, len(escaped_path) - return map(prep, glob.glob(deescape(prefix) + '*')) + return [prep(g) for g in glob.glob(deescape(prefix) + '*')] class CryptoKeyCompleter(StringlistCompleter): diff --git a/alot/db/envelope.py b/alot/db/envelope.py index 02a4a212..d45a587d 100644 --- a/alot/db/envelope.py +++ b/alot/db/envelope.py @@ -326,7 +326,8 @@ class Envelope(object): to_attach = [] for line in self.get_all('Attach'): gpath = os.path.expanduser(line.strip()) - to_attach += filter(os.path.isfile, glob.glob(gpath)) + to_attach += [g for g in glob.glob(gpath) + if os.path.isfile(g)] logging.debug('Attaching: %s', to_attach) for path in to_attach: self.attach(path) diff --git a/alot/db/utils.py b/alot/db/utils.py index 71c46931..9ad1f98c 100644 --- a/alot/db/utils.py +++ b/alot/db/utils.py @@ -323,7 +323,7 @@ def extract_body(mail, types=None, field_key='copiousoutput'): stdin = raw_payload # read parameter, create handler command - parms = tuple(map('='.join, part.get_params())) + parms = tuple('='.join(p) for p in part.get_params()) # create and call external command cmd = mailcap.subst(entry['view'], ctype, diff --git a/alot/settings/manager.py b/alot/settings/manager.py index 392f3535..5718193f 100644 --- a/alot/settings/manager.py +++ b/alot/settings/manager.py @@ -318,8 +318,8 @@ class SettingsManager(object): globalmaps, modemaps = self.get_keybindings(mode) candidates = globalmaps.keys() + modemaps.keys() if prefix is not None: - prefixs = prefix + ' ' - cand = filter(lambda x: x.startswith(prefixs), candidates) + prefixes = prefix + ' ' + cand = [c for c in candidates if c.startswith(prefixes)] if prefix in candidates: candidates = cand + [prefix] else: @@ -446,7 +446,7 @@ class UI(object): :type t: alot.buffers.Buffer :rtype: list """ - return filter(lambda x: isinstance(x, t), self.buffers) + return [x for x in self.buffers if isinstance(x, t)] def clear_notify(self, messages): """ |