|
30 | 30 |
|
31 | 31 | """ |
32 | 32 | # |
33 | | -# Copyright © 2018-2020 Dominic Davis-Foster <dominic@davis-foster.co.uk> |
| 33 | +# Copyright © 2018-2022 Dominic Davis-Foster <dominic@davis-foster.co.uk> |
34 | 34 | # |
35 | 35 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
36 | 36 | # of this software and associated documentation files (the "Software"), to deal |
|
84 | 84 | List, |
85 | 85 | Optional, |
86 | 86 | Pattern, |
| 87 | + Set, |
87 | 88 | Tuple, |
88 | 89 | TypeVar, |
89 | 90 | Union, |
|
128 | 129 | "divide", |
129 | 130 | "redivide", |
130 | 131 | "unique_sorted", |
| 132 | + "replace_nonprinting", |
131 | 133 | ] |
132 | 134 |
|
133 | 135 | #: The current major python version. |
@@ -346,6 +348,13 @@ def posargs2kwargs( |
346 | 348 | elif callable(posarg_names): |
347 | 349 | posarg_names = inspect.getfullargspec(posarg_names).args |
348 | 350 |
|
| 351 | + for name, arg_value in zip(posarg_names, args): |
| 352 | + if name in kwargs: |
| 353 | + if isinstance(posarg_names, MethodType): |
| 354 | + raise TypeError(f"{posarg_names.__name__}(): got multiple values for argument '{name}'") |
| 355 | + else: |
| 356 | + raise TypeError(f"got multiple values for argument '{name}'") |
| 357 | + |
349 | 358 | kwargs.update(zip(posarg_names, args)) |
350 | 359 |
|
351 | 360 | if self_arg is not None and self_arg in kwargs: |
@@ -613,3 +622,38 @@ def unique_sorted( |
613 | 622 | """ |
614 | 623 |
|
615 | 624 | return sorted(set(elements), key=key, reverse=reverse) |
| 625 | + |
| 626 | + |
| 627 | +def replace_nonprinting(string: str, exclude: Optional[Set[int]] = None) -> str: |
| 628 | + """ |
| 629 | + Replace nonprinting (control) characters in ``string`` with ``^`` and ``M-`` notation. |
| 630 | +
|
| 631 | + .. versionadded:: 3.3.0 |
| 632 | +
|
| 633 | + :param string: |
| 634 | + :param exclude: A set of codepoints to exclude. |
| 635 | +
|
| 636 | + :rtype: |
| 637 | +
|
| 638 | + .. seealso:: :wikipedia:`C0 and C1 control codes` on Wikipedia |
| 639 | + """ |
| 640 | + |
| 641 | + # https://stackoverflow.com/a/44952259 |
| 642 | + |
| 643 | + if exclude is None: |
| 644 | + exclude = set() |
| 645 | + |
| 646 | + translation_map = {} |
| 647 | + |
| 648 | + for codepoint in range(32): |
| 649 | + if codepoint not in exclude: |
| 650 | + translation_map[codepoint] = f"^{chr(64 + codepoint)}" |
| 651 | + |
| 652 | + if 127 not in exclude: |
| 653 | + translation_map[127] = "^?" |
| 654 | + |
| 655 | + for codepoint in range(128, 256): |
| 656 | + if codepoint not in exclude: |
| 657 | + translation_map[codepoint] = f"M+{chr(codepoint-64)}" |
| 658 | + |
| 659 | + return string.translate(translation_map) |
0 commit comments