Skip to content

Instantly share code, notes, and snippets.

@matiasmicheletto
Created April 21, 2025 20:40
Show Gist options
  • Save matiasmicheletto/f7002ca77972f1d6fc0711a48e2bfd1f to your computer and use it in GitHub Desktop.
Save matiasmicheletto/f7002ca77972f1d6fc0711a48e2bfd1f to your computer and use it in GitHub Desktop.
Compare LOC for different software projects
import os
import matplotlib.pyplot as plt
# Define your apps and their source directories
dirs = {
"App 1": "/path/to/app1/",
"App 2": "/path/to/app2/",
"App 3": "/path/to/app3/"
}
# Count lines of code
def count_lines(directory):
total_lines = 0
extensions = ('.js', '.jsx', '.json') # For React.js applications. Adapt as needed
for root, _, files in os.walk(directory):
for file in files:
if file.endswith(extensions):
path = os.path.join(root, file)
try:
with open(path, 'r', encoding='utf-8', errors='ignore') as f:
total_lines += sum(1 for _ in f)
except Exception as e:
print(f"Error reading {path}: {e}")
return total_lines
# Compute LOC for each app
loc = {name: count_lines(path) for name, path in dirs.items()}
# Plotting
app_names = list(loc.keys())
app_loc = list(loc.values())
plt.figure(figsize=(10, 6))
colors = plt.cm.tab10.colors # or plt.cm.Set3.colors for more variety
plt.bar(app_names, app_loc, color=colors[:len(app_names)])
plt.xlabel("Applications")
plt.ylabel("Lines of code")
plt.title("Lines of code per application")
plt.xticks(rotation=30, ha='right')
plt.tight_layout()
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment