Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions modules/contributions_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@ def fetch_recent_contributions(username, days=120):
]

contributions[repo].append(contribution)
# clean up empty repositories
# Remove repositories without contributions
contributions = {k: v for k, v in contributions.items() if v}
# get last 5 contributions if more than 5
# sort by most contributions
contributions = dict(sorted(contributions.items(), key=lambda x: len(x[1]), reverse=True))
# get last 5 contributions
contributions = {k: v for k, v in list(contributions.items())[:5]}
# get last 10 contributions
contributions = {k: v for k, v in list(contributions.items())[:10]}

# Sort by most contributions and limit to the top 10 repositories
contributions = dict(
sorted(contributions.items(), key=lambda x: len(x[1]), reverse=True)
)
contributions = {
k: v for k, v in list(contributions.items())[:10]
}
Comment on lines +77 to +82

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current approach for sorting and slicing contributions involves creating an intermediate dictionary after sorting, and then converting its items back to a list for slicing before creating the final dictionary. This can be made more efficient and slightly cleaner.

Consider sorting the contributions.items() once, slicing the resulting list to get the top 10, and then converting this sliced list directly into the final dictionary. This avoids the creation of the intermediate dictionary and the subsequent list(contributions.items()) call.

I've also taken the liberty to rename the lambda variable from x to item for slightly improved readability, which is a common practice in Python (though not strictly a PEP 8 violation for simple lambdas).

This change would improve both performance (by reducing object creation and iteration) and readability.

            sorted_contribution_items = sorted(
                contributions.items(), key=lambda item: len(item[1]), reverse=True
            )
            contributions = dict(sorted_contribution_items[:10])


return contributions
except Exception as e:
Expand Down
Loading