Remove docstrings, empty line or comments from some code (used to detect if a diff is real or only concern comments or docstrings). Args: content (`str`): The code to clean Returns: `str`: The cleaned code.
(content: str)
| 104 | |
| 105 | |
| 106 | def clean_code(content: str) -> str: |
| 107 | """ |
| 108 | Remove docstrings, empty line or comments from some code (used to detect if a diff is real or only concern |
| 109 | comments or docstrings). |
| 110 | |
| 111 | Args: |
| 112 | content (`str`): The code to clean |
| 113 | |
| 114 | Returns: |
| 115 | `str`: The cleaned code. |
| 116 | """ |
| 117 | # We need to deactivate autoformatting here to write escaped triple quotes (we cannot use real triple quotes or |
| 118 | # this would mess up the result if this function applied to this particular file). |
| 119 | # fmt: off |
| 120 | # Remove docstrings by splitting on triple " then triple ': |
| 121 | splits = content.split('\"\"\"') |
| 122 | content = "".join(splits[::2]) |
| 123 | splits = content.split("\'\'\'") |
| 124 | # fmt: on |
| 125 | content = "".join(splits[::2]) |
| 126 | |
| 127 | # Remove empty lines and comments |
| 128 | lines_to_keep = [] |
| 129 | for line in content.split("\n"): |
| 130 | # remove anything that is after a # sign. |
| 131 | line = re.sub("#.*$", "", line) |
| 132 | # remove white lines |
| 133 | if len(line) != 0 and not line.isspace(): |
| 134 | lines_to_keep.append(line) |
| 135 | return "\n".join(lines_to_keep) |
| 136 | |
| 137 | |
| 138 | def keep_doc_examples_only(content: str) -> str: |
no test coverage detected
searching dependent graphs…