Created
February 9, 2025 07:24
-
-
Save tohskai/c7ca2a513521cdc140efe96602ef707d to your computer and use it in GitHub Desktop.
python script to count classes, functions, and methods in a package
This file contains 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 argparse | |
import importlib | |
import inspect | |
import pkgutil | |
import sys | |
''' | |
Example: | |
python count_package.py torch | |
Returns: | |
---------------------------------------------------- | |
Counts for package 'torch': | |
Classes: 4011 | |
Functions: 11393 | |
Methods: 91812 | |
As of version 2.6.0+cu124 | |
---------------------------------------------------- | |
''' | |
def is_deprecated(obj): | |
if hasattr(obj, '__deprecated__'): | |
return True | |
doc = inspect.getdoc(obj) | |
if doc and 'deprecated' in doc.lower(): | |
return True | |
return False | |
def count_items(module, visited=None): | |
if visited is None: | |
visited = set() | |
if module.__name__ in visited: | |
return 0, 0, 0 | |
visited.add(module.__name__) | |
class_count = 0 | |
function_count = 0 | |
method_count = 0 | |
for name, obj in inspect.getmembers(module): | |
if inspect.isclass(obj) and obj.__module__ == module.__name__: | |
if not is_deprecated(obj): | |
class_count += 1 | |
for mname, member in inspect.getmembers(obj): | |
if (inspect.isfunction(member) or inspect.ismethod(member)) and not is_deprecated(member): | |
method_count += 1 | |
elif inspect.isfunction(obj) and obj.__module__ == module.__name__: | |
if not is_deprecated(obj): | |
function_count += 1 | |
if hasattr(module, '__path__'): | |
for finder, modname, ispkg in pkgutil.walk_packages(module.__path__, module.__name__ + '.'): | |
try: | |
submodule = importlib.import_module(modname) | |
except Exception: | |
continue | |
sub_classes, sub_functions, sub_methods = count_items(submodule, visited) | |
class_count += sub_classes | |
function_count += sub_functions | |
method_count += sub_methods | |
return class_count, function_count, method_count | |
parser = argparse.ArgumentParser() | |
parser.add_argument("package") | |
args = parser.parse_args() | |
try: | |
pkg = importlib.import_module(args.package) | |
except ImportError: | |
print(f"Error: Package '{args.package}' not found.") | |
sys.exit(1) | |
classes, functions, methods = count_items(pkg) | |
print("----------------------------------------------------") | |
print(f"Counts for package '{args.package}':") | |
print(f" Classes: {classes}") | |
print(f" Functions: {functions}") | |
print(f" Methods: {methods}") | |
try: | |
print(f"As of version {pkg.__version__}") | |
except: | |
print("Couldn't get the package version") | |
print("----------------------------------------------------") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment