Skip to content

Instantly share code, notes, and snippets.

@Abdur-rahmaanJ
Created October 5, 2020 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Abdur-rahmaanJ/a0ad92869073971d7c260aec57da5a89 to your computer and use it in GitHub Desktop.
Save Abdur-rahmaanJ/a0ad92869073971d7c260aec57da5a89 to your computer and use it in GitHub Desktop.
Count number of classes and functions in the standard library
import ast
import os
stats = {
'funcs':0,
'classes':0,
'analysed':0
}
base_path = 'C:\\Python37-32\\Lib'
excepts = 1
for root, dirs, files in os.walk(base_path):
path = root.split(os.sep)
if 'site-packages' in root: continue
for filename in files:
if filename.endswith('.py'):
#print(len(path) * '---', file)
filepath = os.path.abspath(os.path.join(root, filename))
try:
with open(filepath, encoding='utf8') as f:
tree = ast.parse(f.read())
funcs = sum(isinstance(exp, ast.FunctionDef) for exp in tree.body)
stats['funcs'] += funcs
with open(filepath, encoding='utf8') as f:
tree = ast.parse(f.read())
classes = sum(isinstance(exp, ast.ClassDef) for exp in tree.body)
stats['classes'] += classes
stats['analysed'] += 1
except Exception as e:
print(excepts, filepath, stats, e)
excepts += 1
print(stats)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment