← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3only-simplify-text-replaced into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3only-simplify-text-replaced into launchpad:master.

Commit message:
Simplify text_replaced for Python 3

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/413592

In Python 3, `list_item` and `join_char` take the same values regardless of the type of `text`, so simplify this.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3only-simplify-text-replaced into launchpad:master.
diff --git a/lib/lp/services/helpers.py b/lib/lp/services/helpers.py
index 254675b..1a56835 100644
--- a/lib/lp/services/helpers.py
+++ b/lib/lp/services/helpers.py
@@ -56,16 +56,10 @@ def text_replaced(text, replacements, _cache={}):
     cachekey = tuple(replacements.items())
     if cachekey not in _cache:
         L = []
-        if isinstance(text, six.text_type):
-            list_item = u'(%s)'
-            join_char = u'|'
-        else:
-            list_item = '(%s)'
-            join_char = '|'
         for find, replace in sorted(replacements.items(),
                                     key=lambda item: len(item[0]),
                                     reverse=True):
-            L.append(list_item % re.escape(find))
+            L.append('(%s)' % re.escape(find))
         # Make a copy of the replacements dict, as it is mutable, but we're
         # keeping a cached reference to it.
         replacements_copy = dict(replacements)
@@ -73,7 +67,7 @@ def text_replaced(text, replacements, _cache={}):
         def matchobj_replacer(matchobj):
             return replacements_copy[matchobj.group()]
 
-        regexsub = re.compile(join_char.join(L)).sub
+        regexsub = re.compile('|'.join(L)).sub
 
         def replacer(s):
             return regexsub(matchobj_replacer, s)