comparison contrib/synthrepo.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 a5dbec255f14
children 80c5b2666a96
comparison
equal deleted inserted replaced
24305:867c3649be5d 24306:6ddc86eedc3b
357 message = 'synthesized wide repo with %d files' % (len(files),) 357 message = 'synthesized wide repo with %d files' % (len(files),)
358 mc = context.memctx(repo, [pctx.node(), nullid], message, 358 mc = context.memctx(repo, [pctx.node(), nullid], message,
359 files.iterkeys(), filectxfn, ui.username(), 359 files.iterkeys(), filectxfn, ui.username(),
360 '%d %d' % util.makedate()) 360 '%d %d' % util.makedate())
361 initnode = mc.commit() 361 initnode = mc.commit()
362 hexfn = ui.debugflag and hex or short 362 if ui.debugflag:
363 hexfn = hex
364 else:
365 hexfn = short
363 ui.status(_('added commit %s with %d files\n') 366 ui.status(_('added commit %s with %d files\n')
364 % (hexfn(initnode), len(files))) 367 % (hexfn(initnode), len(files)))
365 368
366 # Synthesize incremental revisions to the repository, adding repo depth. 369 # Synthesize incremental revisions to the repository, adding repo depth.
367 count = int(opts['count']) 370 count = int(opts['count'])
473 renaming regardless of iteration order through the model. 476 renaming regardless of iteration order through the model.
474 ''' 477 '''
475 if dirpath in replacements: 478 if dirpath in replacements:
476 return replacements[dirpath] 479 return replacements[dirpath]
477 head, _ = os.path.split(dirpath) 480 head, _ = os.path.split(dirpath)
478 head = head and rename(head) or '' 481 if head:
482 head = rename(head)
483 else:
484 head = ''
479 renamed = os.path.join(head, wordgen.next()) 485 renamed = os.path.join(head, wordgen.next())
480 replacements[dirpath] = renamed 486 replacements[dirpath] = renamed
481 return renamed 487 return renamed
482 result = [] 488 result = []
483 for dirpath, count in dirs.iteritems(): 489 for dirpath, count in dirs.iteritems():