Skip to content

Instantly share code, notes, and snippets.

@terror
Created June 9, 2021 15:59
Show Gist options
  • Save terror/b3dc3e36285684cde643914a92fafcd0 to your computer and use it in GitHub Desktop.
Save terror/b3dc3e36285684cde643914a92fafcd0 to your computer and use it in GitHub Desktop.
Simple python function frequency counter
import ast, collections, argparse, os
class File:
def __init__(self, path):
self.path = path
@property
def last(self):
return os.path.normpath(os.path.basename(self.path))
@property
def ext(self):
_, ext = os.path.splitext(self.last)
return ext
class Visitor(ast.NodeVisitor):
def __init__(self, counter):
self.counter = counter
def visit_Call(self, node):
if isinstance(node.func, ast.Name):
self.counter[node.func.id] += 1
def cli():
parser = argparse.ArgumentParser()
parser.add_argument("--file", "-f", help="Python file.")
return parser.parse_args()
def main(args):
if not args.file:
print("You must provide a valid python file.")
return
file = File(args.file)
if file.ext != ".py":
raise Exception("File must be a valid python file.")
with open(File(args.file).path) as file:
tree = ast.parse(file.read())
visitor = Visitor(collections.Counter())
visitor.visit(tree)
for name, count in visitor.counter.items():
print(name, count)
if __name__ == '__main__':
try: main(cli())
except Exception as error: print(error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment