Created
October 5, 2020 19:11
-
-
Save Abdur-rahmaanJ/a0ad92869073971d7c260aec57da5a89 to your computer and use it in GitHub Desktop.
Count number of classes and functions in the standard library
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 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