From 7ded18a83d6feb713847abb18b0e97121b6b5a5f Mon Sep 17 00:00:00 2001 From: spark Date: Fri, 23 Oct 2026 09:30:17 +0000 Subject: [PATCH] spark: add code_stats.py - Python code statistics analyzer --- wizard-work/spark/code_stats.py | 80 +++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 wizard-work/spark/code_stats.py diff --git a/wizard-work/spark/code_stats.py b/wizard-work/spark/code_stats.py new file mode 100644 index 0000000..1a621e7 --- /dev/null +++ b/wizard-work/spark/code_stats.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +"""Code statistics analyzer for Python files.""" + +import ast +import os +import sys + + +def analyze_file(filepath: str) -> dict: + """Analyze a single Python file and return stats.""" + stats = { + 'lines': 0, + 'functions': 0, + 'classes': 0, + 'imports': 0 + } + + try: + with open(filepath, 'r', encoding='utf-8') as f: + content = f.read() + stats['lines'] = content.count('\n') + + tree = ast.parse(content) + for node in ast.walk(tree): + if isinstance(node, ast.FunctionDef): + stats['functions'] += 1 + elif isinstance(node, ast.ClassDef): + stats['classes'] += 1 + elif isinstance(node, (ast.Import, ast.ImportFrom)): + stats['imports'] += 1 + except SyntaxError: + pass + + return stats + + +def analyze_directory(directory: str) -> dict: + """Analyze all Python files in a directory recursively.""" + total = { + 'files': 0, + 'lines': 0, + 'functions': 0, + 'classes': 0, + 'imports': 0 + } + + for root, _, files in os.walk(directory): + for filename in files: + if filename.endswith('.py'): + filepath = os.path.join(root, filename) + stats = analyze_file(filepath) + total['files'] += 1 + total['lines'] += stats['lines'] + total['functions'] += stats['functions'] + total['classes'] += stats['classes'] + total['imports'] += stats['imports'] + + return total + + +if __name__ == '__main__': + if len(sys.argv) < 2: + print("Usage: python code_stats.py ") + sys.exit(1) + + target = sys.argv[1] + + if os.path.isfile(target): + stats = analyze_file(target) + stats['files'] = 1 + print(f"File: {target}") + else: + stats = analyze_directory(target) + print(f"Directory: {target}") + + print(f"Files: {stats['files']}") + print(f"Lines: {stats['lines']}") + print(f"Functions: {stats['functions']}") + print(f"Classes: {stats['classes']}") + print(f"Imports: {stats['imports']}") \ No newline at end of file