Last active
July 25, 2020 12:38
-
-
Save dmitriiweb/641936a94fa51a0069464854c5f4c031 to your computer and use it in GitHub Desktop.
Decorator, remove non visible characters from string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def clear_text(func): | |
| """Remove non visible characters (\\n, \\r, \\t) | |
| Example: | |
| >>> @clear_text | |
| >>> def foo(): | |
| ... return "blah \\n blah" | |
| ... | |
| >>> foo() | |
| ... blah blah | |
| :param func: function for decoration | |
| """ | |
| def wrapper(*args, **kwargs): | |
| res = func(*args, **kwargs) | |
| return clean_txt(res) | |
| return wrapper | |
| def clean_txt(text: str) -> str: | |
| """Remove non visible characters (\\n, \\r, \\t) | |
| :param str text: some text | |
| :return: cleaned text | |
| """ | |
| trans = { | |
| "\r": "", | |
| "\n": "", | |
| "\t": "", | |
| } | |
| try: | |
| text = text.translate(str.maketrans(trans)) | |
| except AttributeError: | |
| pass | |
| return text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment