Created
December 17, 2025 05:41
-
-
Save OhadRubin/db48e3a4215a56e014cf25bde5c09fbf to your computer and use it in GitHub Desktop.
ast-grep-delete.py
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
| # /// script | |
| # dependencies = [ | |
| # "ast-grep-py", | |
| # ] | |
| # /// | |
| import sys | |
| from pathlib import Path | |
| from ast_grep_py import SgRoot | |
| def delete_functions(filename: str, func_names: list[str]): | |
| """Delete specified functions from a file.""" | |
| code = Path(filename).read_text() | |
| root = SgRoot(code, "python").root() | |
| edits = [] | |
| for match in root.find_all(pattern="def $NAME($$$): $$$BODY"): | |
| name_node = match.get_match("NAME") | |
| if name_node and name_node.text() in func_names: | |
| edits.append(match.replace("")) | |
| if edits: | |
| return root.commit_edits(edits) | |
| return code | |
| if __name__ == "__main__": | |
| if len(sys.argv) < 3: | |
| print(f"Usage: {sys.argv[0]} <filename> <func1,func2,...>", file=sys.stderr) | |
| sys.exit(1) | |
| filename = sys.argv[1] | |
| func_names = [f.strip() for f in sys.argv[2].split(",")] | |
| new_code = delete_functions(filename, func_names) | |
| with open(filename, "w") as f: | |
| f.write(new_code) | |
| print(f"Deleted {len(func_names)} functions from {filename}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment