Replace characters and patterns in string ``s``. Works similar to :meth:`str.translate`, but replacements and patterns can be full length strings instead of character by character. Arguments: table: A mapping of characters/patterns to their replacement string. s: The st
(table: Mapping, s: str)
| 23 | |
| 24 | |
| 25 | def translate(table: Mapping, s: str) -> str: |
| 26 | """Replace characters and patterns in string ``s``. |
| 27 | |
| 28 | Works similar to :meth:`str.translate`, but replacements and patterns |
| 29 | can be full length strings instead of character by character. |
| 30 | |
| 31 | Arguments: |
| 32 | table: A mapping of characters/patterns to their replacement string. |
| 33 | s: The string to translate |
| 34 | |
| 35 | Note: |
| 36 | Table is the first argument in the signature for compatibility |
| 37 | with :func:`~functools.partial`: |
| 38 | |
| 39 | .. sourcecode:: pycon |
| 40 | |
| 41 | >>> t = partial(translate, {'.': '_'}) |
| 42 | >>> t('foo.bar') |
| 43 | 'foo_bar' |
| 44 | |
| 45 | Examples: |
| 46 | >>> translate('foo.bar@baz', {'.': '_', '@': '.'}) |
| 47 | 'foo_bar.baz' |
| 48 | """ |
| 49 | |
| 50 | def on_reduce(acc: str, kv: Tuple[str, str]) -> str: |
| 51 | return acc.replace(kv[0], kv[1]) # table key/value |
| 52 | |
| 53 | return reduce(on_reduce, table.items(), s) |
no test coverage detected