comparison tests/run-tests.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 4bf484276787
children e7ca4d4b10e1
comparison
equal deleted inserted replaced
24305:867c3649be5d 24306:6ddc86eedc3b
1834 vlog("# Performing temporary installation of HG") 1834 vlog("# Performing temporary installation of HG")
1835 installerrs = os.path.join("tests", "install.err") 1835 installerrs = os.path.join("tests", "install.err")
1836 compiler = '' 1836 compiler = ''
1837 if self.options.compiler: 1837 if self.options.compiler:
1838 compiler = '--compiler ' + self.options.compiler 1838 compiler = '--compiler ' + self.options.compiler
1839 pure = self.options.pure and "--pure" or "" 1839 if self.options.pure:
1840 pure = "--pure"
1841 else:
1842 pure = ""
1840 py3 = '' 1843 py3 = ''
1841 if sys.version_info[0] == 3: 1844 if sys.version_info[0] == 3:
1842 py3 = '--c2to3' 1845 py3 = '--c2to3'
1843 1846
1844 # Run installer in hg root 1847 # Run installer in hg root