Skip to content

Instantly share code, notes, and snippets.

@PierceLBrooks
Created March 23, 2026 20:37
Show Gist options
  • Select an option

  • Save PierceLBrooks/f52421f7c088c67585ae476b5c37067f to your computer and use it in GitHub Desktop.

Select an option

Save PierceLBrooks/f52421f7c088c67585ae476b5c37067f to your computer and use it in GitHub Desktop.
# Author: Pierce Brooks
import os
import ast
import sys
import functools
class ListVisitor(ast.NodeVisitor):
def __init__(self):
self.tier = 0
self.classes = []
self.functions = {}
self.entrypoint = ""
def visit_FunctionDef(self, node):
if (sys.flags.debug):
print((" "*self.tier)+str(node.name))
symbol = ""
if (len(self.entrypoint) > 0):
symbol += str(self.entrypoint)
for i in range(len(self.classes)):
symbol += "::"+self.classes[i]
symbol += "::"+str(node.name)
lines = abs(node.end_lineno-node.lineno)
if not (symbol in self.functions):
self.functions[symbol] = lines
self.tier += 1
self.generic_visit(node)
self.tier -= 1
def visit_ClassDef(self, node):
if (sys.flags.debug):
print((" "*self.tier)+str(node.name))
self.classes.append(str(node.name))
self.tier += 1
self.generic_visit(node)
self.tier -= 1
if (len(self.classes) > 0):
if (len(self.classes) == 1):
self.classes = []
else:
self.classes = self.classes[:(len(self.classes)-1)]
def compare(left, right):
if (left[1] > right[1]):
return 1
if (left[1] < right[1]):
return -1
return 0
def run():
code = 0
visitor = ListVisitor()
for root, folders, files in os.walk(os.getcwd()):
for name in files:
if not (name.endswith(".py")):
continue
path = os.path.join(root, name)
if (sys.flags.debug):
print(path)
visitor.entrypoint = path
try:
with open(path, "r") as descriptor:
visitor.visit(ast.parse(descriptor.read(), filename=name, mode="exec"))
except:
pass
functions = list(sorted(list(visitor.functions.items()), key=functools.cmp_to_key(compare)))
for i in range(len(functions)):
print(str(functions[i][0])+" -> "+str(functions[i][1]))
return code
def launch(arguments):
result = run()
if (sys.flags.debug):
print(str(result))
if not (result == 0):
return False
return True
if (__name__ == "__main__"):
print(str(launch(sys.argv)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment