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
This commit is contained in:
Induane 2022-12-08 03:38:13 -06:00 committed by GitHub
parent d35693e875
commit 9420cfeb3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 7 deletions

View File

@ -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)