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.
Showing
- contrib/synthrepo.py 8 additions, 2 deletionscontrib/synthrepo.py
- hgext/convert/common.py 4 additions, 1 deletionhgext/convert/common.py
- hgext/convert/convcmd.py 5 additions, 1 deletionhgext/convert/convcmd.py
- hgext/convert/gnuarch.py 4 additions, 1 deletionhgext/convert/gnuarch.py
- hgext/convert/hg.py 4 additions, 1 deletionhgext/convert/hg.py
- hgext/convert/subversion.py 14 additions, 3 deletionshgext/convert/subversion.py
- hgext/hgcia.py 8 additions, 2 deletionshgext/hgcia.py
- hgext/keyword.py 4 additions, 1 deletionhgext/keyword.py
- hgext/mq.py 23 additions, 5 deletionshgext/mq.py
- hgext/notify.py 4 additions, 1 deletionhgext/notify.py
- hgext/patchbomb.py 4 additions, 1 deletionhgext/patchbomb.py
- i18n/polib.py 9 additions, 2 deletionsi18n/polib.py
- mercurial/bookmarks.py 4 additions, 1 deletionmercurial/bookmarks.py
- mercurial/bundlerepo.py 4 additions, 1 deletionmercurial/bundlerepo.py
- mercurial/byterange.py 5 additions, 1 deletionmercurial/byterange.py
- mercurial/cmdutil.py 25 additions, 6 deletionsmercurial/cmdutil.py
- mercurial/commands.py 46 additions, 11 deletionsmercurial/commands.py
- mercurial/context.py 10 additions, 2 deletionsmercurial/context.py
- mercurial/dagutil.py 8 additions, 2 deletionsmercurial/dagutil.py
- mercurial/discovery.py 4 additions, 1 deletionmercurial/discovery.py
Loading
Please register or sign in to comment