comparison mercurial/help.py @ 26845:7a77ee434179

help: replace some str.split() calls by str.partition() or str.rpartition() Since Python 2.5 str has new methods: partition and rpartition. They are more specialized than the usual split and rsplit, and they sometimes convey the intent of code better and also are a bit faster (faster than split/rsplit with maxsplit specified). Let's use them in appropriate places for a small speedup. Example performance (partition): $ python -m timeit 'assert "apple|orange|banana".split("|")[0] == "apple"' 1000000 loops, best of 3: 0.376 usec per loop $ python -m timeit 'assert "apple|orange|banana".split("|", 1)[0] == "apple"' 1000000 loops, best of 3: 0.327 usec per loop $ python -m timeit 'assert "apple|orange|banana".partition("|")[0] == "apple"' 1000000 loops, best of 3: 0.214 usec per loop Example performance (rpartition): $ python -m timeit 'assert "apple|orange|banana".rsplit("|")[-1] == "banana"' 1000000 loops, best of 3: 0.372 usec per loop $ python -m timeit 'assert "apple|orange|banana".rsplit("|", 1)[-1] == "banana"' 1000000 loops, best of 3: 0.332 usec per loop $ python -m timeit 'assert "apple|orange|banana".rpartition("|")[-1] == "banana"' 1000000 loops, best of 3: 0.219 usec per loop
author Anton Shestakov <av6@dwimlabs.net>
date Mon, 02 Nov 2015 23:37:14 +0800
parents 56b2bcea2529
children 7625f6387fc4
comparison
equal deleted inserted replaced
26844:e24eee55c129 26845:7a77ee434179
113 docs = _(getattr(entry[0], '__doc__', None)) or '' 113 docs = _(getattr(entry[0], '__doc__', None)) or ''
114 if kw in cmd or lowercontains(summary) or lowercontains(docs): 114 if kw in cmd or lowercontains(summary) or lowercontains(docs):
115 doclines = docs.splitlines() 115 doclines = docs.splitlines()
116 if doclines: 116 if doclines:
117 summary = doclines[0] 117 summary = doclines[0]
118 cmdname = cmd.split('|')[0].lstrip('^') 118 cmdname = cmd.partition('|')[0].lstrip('^')
119 results['commands'].append((cmdname, summary)) 119 results['commands'].append((cmdname, summary))
120 for name, docs in itertools.chain( 120 for name, docs in itertools.chain(
121 extensions.enabled(False).iteritems(), 121 extensions.enabled(False).iteritems(),
122 extensions.disabled().iteritems()): 122 extensions.disabled().iteritems()):
123 # extensions.load ignores the UI argument 123 # extensions.load ignores the UI argument
124 mod = extensions.load(None, name, '') 124 mod = extensions.load(None, name, '')
125 name = name.split('.')[-1] 125 name = name.rpartition('.')[-1]
126 if lowercontains(name) or lowercontains(docs): 126 if lowercontains(name) or lowercontains(docs):
127 # extension docs are already translated 127 # extension docs are already translated
128 results['extensions'].append((name, docs.splitlines()[0])) 128 results['extensions'].append((name, docs.splitlines()[0]))
129 for cmd, entry in getattr(mod, 'cmdtable', {}).iteritems(): 129 for cmd, entry in getattr(mod, 'cmdtable', {}).iteritems():
130 if kw in cmd or (len(entry) > 2 and lowercontains(entry[2])): 130 if kw in cmd or (len(entry) > 2 and lowercontains(entry[2])):
131 cmdname = cmd.split('|')[0].lstrip('^') 131 cmdname = cmd.partition('|')[0].lstrip('^')
132 if entry[0].__doc__: 132 if entry[0].__doc__:
133 cmddoc = gettext(entry[0].__doc__).splitlines()[0] 133 cmddoc = gettext(entry[0].__doc__).splitlines()[0]
134 else: 134 else:
135 cmddoc = _('(no help text available)') 135 cmddoc = _('(no help text available)')
136 results['extensioncommands'].append((cmdname, cmddoc)) 136 results['extensioncommands'].append((cmdname, cmddoc))
328 header = _('list of commands:\n\n') 328 header = _('list of commands:\n\n')
329 329
330 h = {} 330 h = {}
331 cmds = {} 331 cmds = {}
332 for c, e in commands.table.iteritems(): 332 for c, e in commands.table.iteritems():
333 f = c.split("|", 1)[0] 333 f = c.partition("|")[0]
334 if select and not select(f): 334 if select and not select(f):
335 continue 335 continue
336 if (not select and name != 'shortlist' and 336 if (not select and name != 'shortlist' and
337 e[0].__module__ != commands.__name__): 337 e[0].__module__ != commands.__name__):
338 continue 338 continue
443 443
444 if '\n' not in doc: 444 if '\n' not in doc:
445 head, tail = doc, "" 445 head, tail = doc, ""
446 else: 446 else:
447 head, tail = doc.split('\n', 1) 447 head, tail = doc.split('\n', 1)
448 rst = [_('%s extension - %s\n\n') % (name.split('.')[-1], head)] 448 rst = [_('%s extension - %s\n\n') % (name.rpartition('.')[-1], head)]
449 if tail: 449 if tail:
450 rst.extend(tail.splitlines(True)) 450 rst.extend(tail.splitlines(True))
451 rst.append('\n') 451 rst.append('\n')
452 452
453 if not ui.verbose: 453 if not ui.verbose:
458 if mod: 458 if mod:
459 try: 459 try:
460 ct = mod.cmdtable 460 ct = mod.cmdtable
461 except AttributeError: 461 except AttributeError:
462 ct = {} 462 ct = {}
463 modcmds = set([c.split('|', 1)[0] for c in ct]) 463 modcmds = set([c.partition('|')[0] for c in ct])
464 rst.extend(helplist(modcmds.__contains__)) 464 rst.extend(helplist(modcmds.__contains__))
465 else: 465 else:
466 rst.append(_('(use "hg help extensions" for information on enabling' 466 rst.append(_('(use "hg help extensions" for information on enabling'
467 ' extensions)\n')) 467 ' extensions)\n'))
468 return rst 468 return rst