Skip to content

Instantly share code, notes, and snippets.

@joeflack4
Created May 31, 2024 22:00
Show Gist options
  • Save joeflack4/c784a3dbdaf6d79e18a7f9bda5d3aba8 to your computer and use it in GitHub Desktop.
Save joeflack4/c784a3dbdaf6d79e18a7f9bda5d3aba8 to your computer and use it in GitHub Desktop.
Analyzing which files changed in various build PRs
"""Count file name appearances"""
import os
from collections import defaultdict
from glob import glob
# Path to the Desktop
desktop_path = os.path.expanduser("~/Desktop")
# Filenames
filenames = glob(os.path.join(desktop_path, "*.txt"))
# Print
print('Analyzing which files changed in various build PRs, and how many times they appeared in multiple PRs')
print('Reading: ' + ','.join(os.path.basename(x) for x in filenames) + '\n')
# Initialize a defaultdict to store the counts
line_counts = defaultdict(int)
# Read each file and count the occurrences of each line
for filename in filenames:
file_path = os.path.join(desktop_path, filename)
with open(file_path, 'r') as file:
for line in file:
line = line.strip() # Remove any leading/trailing whitespace
line_counts[line] += 1
# Convert defaultdict to a regular dictionary (optional)
line_counts = dict(line_counts)
grouped_by_n_times = {}
for k, v in line_counts.items():
if v not in grouped_by_n_times:
grouped_by_n_times[v] = []
grouped_by_n_times[v].append(k)
# Print report
print('Results: ')
print('Each number header represents the number of times a file appeared in multiple PRs')
highest_count = max(grouped_by_n_times.keys())
i = highest_count
while i > 0:
v = grouped_by_n_times.get(i, [])
if v:
print(str(i) + ' -------------------')
for filename in v:
print(filename)
print('')
i -= 1
@joeflack4
Copy link
Author

joeflack4 commented May 31, 2024

Was thinking about attaching inputs, but looks like gists don't allow attaching of input files, like .txt even, nor even .zip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment