Mercurial > hg-stable
diff mercurial/commands.py @ 24306:6ddc86eedc3b
style: kill ersatz if-else ternary operators
Although Python supports `X = Y if COND else Z`, this was only
introduced in Python 2.5. Since we have to support Python 2.4, it was
a very common thing to write instead `X = COND and Y or Z`, which is a
bit obscure at a glance. It requires some intricate knowledge of
Python to understand how to parse these one-liners.
We change instead all of these one-liners to 4-liners. This was
executed with the following perlism:
find -name "*.py" -exec perl -pi -e 's,(\s*)([\.\w]+) = \(?(\S+)\s+and\s+(\S*)\)?\s+or\s+(\S*)$,$1if $3:\n$1 $2 = $4\n$1else:\n$1 $2 = $5,' {} \;
I tweaked the following cases from the automatic Perl output:
prev = (parents and parents[0]) or nullid
port = (use_ssl and 443 or 80)
cwd = (pats and repo.getcwd()) or ''
rename = fctx and webutil.renamelink(fctx) or []
ctx = fctx and fctx or ctx
self.base = (mapfile and os.path.dirname(mapfile)) or ''
I also added some newlines wherever they seemd appropriate for readability
There are probably a few ersatz ternary operators still in the code
somewhere, lurking away from the power of a simple regex.
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Fri, 13 Mar 2015 17:00:06 -0400 |
parents | 0b94b68aace9 |
children | fefcafda10b8 |
line wrap: on
line diff
--- a/mercurial/commands.py Fri Mar 13 14:20:13 2015 -0400 +++ b/mercurial/commands.py Fri Mar 13 17:00:06 2015 -0400 @@ -277,7 +277,10 @@ opts['file'] = True fm = ui.formatter('annotate', opts) - datefunc = ui.quiet and util.shortdate or util.datestr + if ui.quiet: + datefunc = util.shortdate + else: + datefunc = util.datestr hexfn = fm.hexfunc opmap = [('user', ' ', lambda x: x[0].user(), ui.shortuser), @@ -664,7 +667,10 @@ # one of the parent was not checked. parents = repo[nodes[0]].parents() if len(parents) > 1: - side = good and state['bad'] or state['good'] + if good: + side = state['bad'] + else: + side = state['good'] num = len(set(i.node() for i in parents) & set(side)) if num == 1: return parents[0].ancestor(parents[1]) @@ -3670,7 +3676,10 @@ def display(fn, ctx, pstates, states): rev = ctx.rev() - datefunc = ui.quiet and util.shortdate or util.datestr + if ui.quiet: + datefunc = util.shortdate + else: + datefunc = util.datestr found = False @util.cachefunc def binary(): @@ -3946,7 +3955,10 @@ raise util.Abort(_("there is no Mercurial repository here " "(.hg not found)")) - hexfunc = ui.debugflag and hex or short + if ui.debugflag: + hexfunc = hex + else: + hexfunc = short default = not (num or id or branch or tags or bookmarks) output = [] revs = [] @@ -4342,7 +4354,10 @@ Returns 0 if a match is found, 1 otherwise. """ - end = opts.get('print0') and '\0' or '\n' + if opts.get('print0'): + end = '\0' + else: + end = '\n' rev = scmutil.revsingle(repo, opts.get('rev'), None).node() ret = 1 @@ -4506,7 +4521,10 @@ rename = getrenamed(fn, rev) if rename: copies.append((fn, rename[0])) - revmatchfn = filematcher and filematcher(ctx.rev()) or None + if filematcher: + revmatchfn = filematcher(ctx.rev()) + else: + revmatchfn = None displayer.show(ctx, copies=copies, matchfn=revmatchfn) if displayer.flush(rev): count += 1 @@ -5550,7 +5568,10 @@ if opts.get('port'): opts['port'] = util.getport(opts.get('port')) - baseui = repo and repo.baseui or ui + if repo: + baseui = repo.baseui + else: + baseui = ui optlist = ("name templates style address port prefix ipv6" " accesslog errorlog certificate encoding") for o in optlist.split(): @@ -5700,15 +5721,25 @@ else: node1, node2 = scmutil.revpair(repo, revs) - cwd = (pats and repo.getcwd()) or '' - end = opts.get('print0') and '\0' or '\n' + if pats: + cwd = repo.getcwd() + else: + cwd = '' + + if opts.get('print0'): + end = '\0' + else: + end = '\n' copy = {} states = 'modified added removed deleted unknown ignored clean'.split() show = [k for k in states if opts.get(k)] if opts.get('all'): show += ui.quiet and (states[:4] + ['clean']) or states if not show: - show = ui.quiet and states[:4] or states[:5] + if ui.quiet: + show = states[:4] + else: + show = states[:5] stat = repo.status(node1, node2, scmutil.match(repo[node2], pats, opts), 'ignored' in show, 'clean' in show, 'unknown' in show, @@ -6029,7 +6060,11 @@ rev_ = opts['rev'] message = opts.get('message') if opts.get('remove'): - expectedtype = opts.get('local') and 'local' or 'global' + if opts.get('local'): + expectedtype = 'local' + else: + expectedtype = 'global' + for n in names: if not repo.tagtype(n): raise util.Abort(_("tag '%s' does not exist") % n)