Skip to content

Instantly share code, notes, and snippets.

@isichan0501
Created July 17, 2023 05:24
Show Gist options
  • Select an option

  • Save isichan0501/07f85d643d6c5ccfb951bb5749a4f1bc to your computer and use it in GitHub Desktop.

Select an option

Save isichan0501/07f85d643d6c5ccfb951bb5749a4f1bc to your computer and use it in GitHub Desktop.
get file info
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