Remove docstrings, empty line or comments from some code (used to detect if a diff is real or only concern comments or docstings). Args: content (`str`): The code to clean Returns: `str`: The cleaned code.
(content: str)
| 123 | |
| 124 | |
| 125 | def clean_code(content: str) -> str: |
| 126 | """ |
| 127 | Remove docstrings, empty line or comments from some code (used to detect if a diff is real or only concern |
| 128 | comments or docstings). |
| 129 | |
| 130 | Args: |
| 131 | content (`str`): The code to clean |
| 132 | |
| 133 | Returns: |
| 134 | `str`: The cleaned code. |
| 135 | """ |
| 136 | # We need to deactivate autoformatting here to write escaped triple quotes (we cannot use real triple quotes or |
| 137 | # this would mess up the result if this function applied to this particular file). |
| 138 | # fmt: off |
| 139 | # Remove docstrings by splitting on triple " then triple ': |
| 140 | splits = content.split('\"\"\"') |
| 141 | content = "".join(splits[::2]) |
| 142 | splits = content.split("\'\'\'") |
| 143 | # fmt: on |
| 144 | content = "".join(splits[::2]) |
| 145 | |
| 146 | # Remove empty lines and comments |
| 147 | lines_to_keep = [] |
| 148 | for line in content.split("\n"): |
| 149 | # remove anything that is after a # sign. |
| 150 | line = re.sub("#.*$", "", line) |
| 151 | # remove white lines |
| 152 | if len(line) != 0 and not line.isspace(): |
| 153 | lines_to_keep.append(line) |
| 154 | return "\n".join(lines_to_keep) |
| 155 | |
| 156 | |
| 157 | def keep_doc_examples_only(content: str) -> str: |
no test coverage detected