mirror of https://github.com/mahmoud/boltons.git
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:
parent
d35693e875
commit
9420cfeb3b
|
@ -1176,7 +1176,7 @@ class MultiReplace(object):
|
||||||
|
|
||||||
Dictionary Usage::
|
Dictionary Usage::
|
||||||
|
|
||||||
from lrmslib import stringutils
|
from boltons import stringutils
|
||||||
s = stringutils.MultiReplace({
|
s = stringutils.MultiReplace({
|
||||||
'foo': 'zoo',
|
'foo': 'zoo',
|
||||||
'cat': 'hat',
|
'cat': 'hat',
|
||||||
|
@ -1187,7 +1187,7 @@ class MultiReplace(object):
|
||||||
|
|
||||||
Iterable Usage::
|
Iterable Usage::
|
||||||
|
|
||||||
from lrmslib import stringutils
|
from boltons import stringutils
|
||||||
s = stringutils.MultiReplace([
|
s = stringutils.MultiReplace([
|
||||||
('foo', 'zoo'),
|
('foo', 'zoo'),
|
||||||
('cat', 'hat'),
|
('cat', 'hat'),
|
||||||
|
@ -1239,10 +1239,7 @@ class MultiReplace(object):
|
||||||
else:
|
else:
|
||||||
exp = vals[0].pattern
|
exp = vals[0].pattern
|
||||||
|
|
||||||
regex_values.append('(?P<{0}>{1})'.format(
|
regex_values.append('(?P<{}>{})'.format(group_name, exp))
|
||||||
group_name,
|
|
||||||
exp
|
|
||||||
))
|
|
||||||
self.group_map[group_name] = vals[1]
|
self.group_map[group_name] = vals[1]
|
||||||
|
|
||||||
self.combined_pattern = re.compile(
|
self.combined_pattern = re.compile(
|
||||||
|
@ -1267,7 +1264,18 @@ class MultiReplace(object):
|
||||||
|
|
||||||
|
|
||||||
def multi_replace(text, sub_map, **kwargs):
|
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)
|
m = MultiReplace(sub_map, **kwargs)
|
||||||
return m.sub(text)
|
return m.sub(text)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue