MCPcopy Create free account
hub / github.com/ModalityDance/Omni-R1 / clean_code

Function clean_code

src/transformers/utils/tests_fetcher.py:125–154  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

123
124
125def 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
157def keep_doc_examples_only(content: str) -> str:

Callers 2

test_clean_codeMethod · 0.90
diff_is_docstring_onlyFunction · 0.85

Calls 1

splitMethod · 0.80

Tested by

no test coverage detected