forked from fluentpython/example-code-2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplacer.py
36 lines (28 loc) · 842 Bytes
/
replacer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
``zip_replace`` replaces multiple calls to ``str.replace``::
>>> changes = [
... ('(', ' ( '),
... (')', ' ) '),
... (' ', ' '),
... ]
>>> expr = '(+ 2 (* 3 7))'
>>> zip_replace(expr, changes)
' ( + 2 ( * 3 7 ) ) '
"""
# tag::ZIP_REPLACE[]
from collections.abc import Iterable
FromTo = tuple[str, str] # <1>
def zip_replace(text: str, changes: Iterable[FromTo]) -> str: # <2>
for from_, to in changes:
text = text.replace(from_, to)
return text
# end::ZIP_REPLACE[]
def demo() -> None:
import doctest
failed, count = doctest.testmod()
print(f'{count-failed} of {count} doctests OK')
l33t = [(p[0], p[1]) for p in 'a4 e3 i1 o0'.split()]
text = 'mad skilled noob powned leet'
print(zip_replace(text, l33t))
if __name__ == '__main__':
demo()