Created
March 12, 2020 16:42
-
-
Save JnyJny/39d5568483484405721b2276cb438efc to your computer and use it in GitHub Desktop.
How I would implement 178
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
#!/usr/bin/env python3 | |
""" short module description | |
Longer module description goes here. | |
""" | |
from collections import defaultdict | |
from dateutil.parser import parse | |
def get_min_max_amount_of_commits(commit_log: str, year: int = None) -> (str, str): | |
"""Calculate the amount of inserts / deletes per month from the | |
provided commit log and returns a tuple of the least active month | |
and most active month. | |
:param str commit_log: file path | |
:param int year: optional year to filter for | |
Returns a tuple of (least_active_month, most_active_month) | |
""" | |
counts = defaultdict(int) | |
with open(commit_log) as logdata: | |
for line in logdata: | |
try: | |
date_str, payload = line.strip().split("|") | |
except ValueError as error: | |
# log an error for line | |
continue | |
try: | |
date = parse(date_str, fuzzy=True) | |
except ValueError as error: | |
# log an error for date_str | |
continue | |
if year and date.year != year: | |
continue | |
key = f"{date.year}-{date.month:02d}" | |
words = payload.split() | |
interesting_value_indices = {3, 5} | |
counts = sum( | |
[ | |
int(word) | |
for i, word in enumerate(words) | |
if i in interesting_value_indices | |
] | |
) | |
counts[key] += counts | |
return min(counts), max(counts) | |
if __name__ == "__main__": | |
print(get_min_max_amount_of_commits("log")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment