Created
July 17, 2023 05:24
-
-
Save isichan0501/07f85d643d6c5ccfb951bb5749a4f1bc to your computer and use it in GitHub Desktop.
get file info
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
| import ast | |
| import os | |
| def get_python_files(path): | |
| python_files = [] | |
| for root, dirs, files in os.walk(path): | |
| for file in files: | |
| if file.endswith('.py'): | |
| python_files.append(os.path.join(root, file)) | |
| return python_files | |
| def get_classes_and_functions(file): | |
| with open(file, 'r') as file: | |
| tree = ast.parse(file.read()) | |
| classes = [(node.name, [n.name for n in node.body if isinstance(n, ast.FunctionDef)]) for node in ast.walk(tree) if isinstance(node, ast.ClassDef)] | |
| functions = [(node.name, [arg.arg for arg in node.args.args]) for node in ast.walk(tree) if isinstance(node, ast.FunctionDef)] | |
| return classes, functions | |
| path = '/path/to/your/directory' | |
| python_files = get_python_files(path) | |
| for file in python_files: | |
| classes, functions = get_classes_and_functions(file) | |
| print(f'File: {file}') | |
| for class_name, methods in classes: | |
| print(f'Class: {class_name}, Methods: {methods}') | |
| for function_name, args in functions: | |
| print(f'Function: {function_name}, Arguments: {args}') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment