From 9420cfeb3b1eb270fad876d002bf92567e619b0c Mon Sep 17 00:00:00 2001 From: Induane Date: Thu, 8 Dec 2022 03:38:13 -0600 Subject: [PATCH] Add more documentation to multi_replace (#306) - Fixed some documentation copypasta - Added example usage to multi_replace function - Removed some old Python 2.6 string formatting --- boltons/strutils.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/boltons/strutils.py b/boltons/strutils.py index f94aefb..5b5b347 100644 --- a/boltons/strutils.py +++ b/boltons/strutils.py @@ -1176,7 +1176,7 @@ class MultiReplace(object): Dictionary Usage:: - from lrmslib import stringutils + from boltons import stringutils s = stringutils.MultiReplace({ 'foo': 'zoo', 'cat': 'hat', @@ -1187,7 +1187,7 @@ class MultiReplace(object): Iterable Usage:: - from lrmslib import stringutils + from boltons import stringutils s = stringutils.MultiReplace([ ('foo', 'zoo'), ('cat', 'hat'), @@ -1239,10 +1239,7 @@ class MultiReplace(object): else: exp = vals[0].pattern - regex_values.append('(?P<{0}>{1})'.format( - group_name, - exp - )) + regex_values.append('(?P<{}>{})'.format(group_name, exp)) self.group_map[group_name] = vals[1] self.combined_pattern = re.compile( @@ -1267,7 +1264,18 @@ class MultiReplace(object): def multi_replace(text, sub_map, **kwargs): - """Shortcut function to invoke MultiReplace in a single call.""" + """ + Shortcut function to invoke MultiReplace in a single call. + + Example Usage:: + + from boltons.stringutils import multi_replace + new = multi_replace( + 'The foo bar cat ate a bat', + {'foo': 'zoo', 'cat': 'hat', 'bat': 'kraken'} + ) + new == 'The zoo bar hat ate a kraken' + """ m = MultiReplace(sub_map, **kwargs) return m.sub(text)