Skip to content

Instantly share code, notes, and snippets.

@Recursing
Created June 12, 2021 08:33
Show Gist options
  • Save Recursing/f4d706c2cf80155e22a4eb921e9950d9 to your computer and use it in GitHub Desktop.
Save Recursing/f4d706c2cf80155e22a4eb921e9950d9 to your computer and use it in GitHub Desktop.
Find the function call with most arguments in a folder and subfolders
#! /bin/python
import ast
import pathlib
from sys import path
from typing import Iterator
def function_calls_arg_numbers(tree: ast.Module) -> Iterator[tuple[int, int]]:
for node in ast.walk(tree):
if not isinstance(node, ast.Call):
continue
yield (len(node.args) + len(node.keywords), node.lineno)
if __name__ == "__main__":
cwd = pathlib.Path(".")
results = []
for path in cwd.rglob("*.py"):
source = path.read_text()
tree = ast.parse(source)
num_args, lineno = max(function_calls_arg_numbers(tree), default=(0, 0))
print(f"{path}:{lineno}", num_args)
results.append((num_args, path, lineno))
print("...")
results.sort(reverse=True)
for (args, path, lineno) in results[:20]:
print(f"{path}:{lineno}", args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment