changeset 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 e24eee55c129
children 7c1b4840c2cd
files mercurial/help.py
diffstat 1 files changed, 6 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/help.py	Sat Oct 24 01:54:46 2015 +0200
+++ b/mercurial/help.py	Mon Nov 02 23:37:14 2015 +0800
@@ -115,20 +115,20 @@
             doclines = docs.splitlines()
             if doclines:
                 summary = doclines[0]
-            cmdname = cmd.split('|')[0].lstrip('^')
+            cmdname = cmd.partition('|')[0].lstrip('^')
             results['commands'].append((cmdname, summary))
     for name, docs in itertools.chain(
         extensions.enabled(False).iteritems(),
         extensions.disabled().iteritems()):
         # extensions.load ignores the UI argument
         mod = extensions.load(None, name, '')
-        name = name.split('.')[-1]
+        name = name.rpartition('.')[-1]
         if lowercontains(name) or lowercontains(docs):
             # extension docs are already translated
             results['extensions'].append((name, docs.splitlines()[0]))
         for cmd, entry in getattr(mod, 'cmdtable', {}).iteritems():
             if kw in cmd or (len(entry) > 2 and lowercontains(entry[2])):
-                cmdname = cmd.split('|')[0].lstrip('^')
+                cmdname = cmd.partition('|')[0].lstrip('^')
                 if entry[0].__doc__:
                     cmddoc = gettext(entry[0].__doc__).splitlines()[0]
                 else:
@@ -330,7 +330,7 @@
         h = {}
         cmds = {}
         for c, e in commands.table.iteritems():
-            f = c.split("|", 1)[0]
+            f = c.partition("|")[0]
             if select and not select(f):
                 continue
             if (not select and name != 'shortlist' and
@@ -445,7 +445,7 @@
             head, tail = doc, ""
         else:
             head, tail = doc.split('\n', 1)
-        rst = [_('%s extension - %s\n\n') % (name.split('.')[-1], head)]
+        rst = [_('%s extension - %s\n\n') % (name.rpartition('.')[-1], head)]
         if tail:
             rst.extend(tail.splitlines(True))
             rst.append('\n')
@@ -460,7 +460,7 @@
                 ct = mod.cmdtable
             except AttributeError:
                 ct = {}
-            modcmds = set([c.split('|', 1)[0] for c in ct])
+            modcmds = set([c.partition('|')[0] for c in ct])
             rst.extend(helplist(modcmds.__contains__))
         else:
             rst.append(_('(use "hg help extensions" for information on enabling'