From 18b49c1dad76a5b5c6f1ca7cb4de44690da48acd Mon Sep 17 00:00:00 2001 From: Jochen Kupperschmidt Date: Thu, 9 Jun 2016 23:50:22 +0200 Subject: [PATCH 1/5] Clarified names. --- gitfiti.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/gitfiti.py b/gitfiti.py index ac1afcdb5..e83fc5486 100755 --- a/gitfiti.py +++ b/gitfiti.py @@ -207,7 +207,7 @@ def load_images(img_names): return loaded_imgs -def get_calendar(username, base_url): +def retrieve_contributions_calendar(username, base_url): """retrieves the GitHub commit calendar data for a username""" base_url = base_url + 'users/' + username @@ -222,11 +222,11 @@ def get_calendar(username, base_url): return page.read().decode('utf-8').splitlines() -def find_max_commits(calendar): +def find_max_daily_commits(contributions_calendar): """finds the highest number of commits in one day""" output = set() - for line in calendar: + for line in contributions_calendar: for day in line.split(): if 'data-count=' in day: commit = day.split('=')[1] @@ -338,11 +338,11 @@ def main(): git_base = ghe if ghe else GITHUB_BASE_URL - cal = get_calendar(username, git_base) + contributions_calendar = retrieve_contributions_calendar(username, git_base) - max_commits = find_max_commits(cal) + max_daily_commits = find_max_daily_commits(contributions_calendar) - m = calculate_multiplier(max_commits) + m = calculate_multiplier(max_daily_commits) repo = request_user_input( 'Enter the name of the repository to use by gitfiti: ') @@ -361,7 +361,7 @@ def main(): 'Enter the word "gitfiti" to exceed your max\n' '(this option generates WAY more commits)\n' 'Any other input will cause the default matching behavior' - ).format(max_commits)) + ).format(max_daily_commits)) match = request_user_input() match = m if (match == 'gitfiti') else 1 From 4d4af436f4b57067693be6678fd1caa7eea6b04b Mon Sep 17 00:00:00 2001 From: Jochen Kupperschmidt Date: Fri, 10 Jun 2016 00:15:16 +0200 Subject: [PATCH 2/5] Retrieve and return the contributions calender as a string; split into lines later as part of the processing stage. --- gitfiti.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gitfiti.py b/gitfiti.py index e83fc5486..0c5d79aea 100755 --- a/gitfiti.py +++ b/gitfiti.py @@ -219,14 +219,14 @@ def retrieve_contributions_calendar(username, base_url): print(e) raise SystemExit - return page.read().decode('utf-8').splitlines() + return page.read().decode('utf-8') def find_max_daily_commits(contributions_calendar): """finds the highest number of commits in one day""" output = set() - for line in contributions_calendar: + for line in contributions_calendar.splitlines(): for day in line.split(): if 'data-count=' in day: commit = day.split('=')[1] From 2c9c4cba83c36d252a84ef944a4fedba81d41013 Mon Sep 17 00:00:00 2001 From: Jochen Kupperschmidt Date: Fri, 10 Jun 2016 00:07:36 +0200 Subject: [PATCH 3/5] Added test for `find_max_daily_commits`. --- tests/test_find_max_daily_commits.py | 75 ++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/test_find_max_daily_commits.py diff --git a/tests/test_find_max_daily_commits.py b/tests/test_find_max_daily_commits.py new file mode 100644 index 000000000..25acb71dc --- /dev/null +++ b/tests/test_find_max_daily_commits.py @@ -0,0 +1,75 @@ +from gitfiti import find_max_daily_commits + + +CONTRIBUTIONS_CALENDAR_SVG = '''\ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Jun + Jul + Aug + Sep + Oct + Nov + Dec + Jan + Feb + Mar + Apr + May + + M + + W + + F + + + +''' + + +def test_find_max_daily_commits(): + assert find_max_daily_commits(CONTRIBUTIONS_CALENDAR_SVG) == 84 From d23a2d0746e33745164bd88d3aa3033cca9faa1a Mon Sep 17 00:00:00 2001 From: Jochen Kupperschmidt Date: Fri, 10 Jun 2016 00:24:06 +0200 Subject: [PATCH 4/5] Extracted function to parse daily counts from a contributions SVG and added a test for it. --- gitfiti.py | 12 +++++++----- tests/test_find_max_daily_commits.py | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/gitfiti.py b/gitfiti.py index 0c5d79aea..339de6bbb 100755 --- a/gitfiti.py +++ b/gitfiti.py @@ -222,17 +222,19 @@ def retrieve_contributions_calendar(username, base_url): return page.read().decode('utf-8') -def find_max_daily_commits(contributions_calendar): - """finds the highest number of commits in one day""" - output = set() - +def parse_contributions_calendar(contributions_calendar): + """Yield daily counts extracted from the contributions SVG.""" for line in contributions_calendar.splitlines(): for day in line.split(): if 'data-count=' in day: commit = day.split('=')[1] commit = commit.strip('"') - output.add(int(commit)) + yield int(commit) + +def find_max_daily_commits(contributions_calendar): + """finds the highest number of commits in one day""" + output = parse_contributions_calendar(contributions_calendar) output = list(output) output.sort() output.reverse() diff --git a/tests/test_find_max_daily_commits.py b/tests/test_find_max_daily_commits.py index 25acb71dc..d8950db2e 100644 --- a/tests/test_find_max_daily_commits.py +++ b/tests/test_find_max_daily_commits.py @@ -1,4 +1,4 @@ -from gitfiti import find_max_daily_commits +from gitfiti import find_max_daily_commits, parse_contributions_calendar CONTRIBUTIONS_CALENDAR_SVG = '''\ @@ -71,5 +71,19 @@ CONTRIBUTIONS_CALENDAR_SVG = '''\ ''' +def test_parse_contributions_calendar(): + expected = [ + 0, 0, 0, 0, 6, 0, 0, + 0, 0, 0, 0, 0, 0, 6, + 84, 16, 4, 8, 0, 0, 0, + 0, 25, 66, 20, 10, 0, 0, + 33, 9, 0, 0, 7, + ] + + actual = parse_contributions_calendar(CONTRIBUTIONS_CALENDAR_SVG) + + assert list(actual) == expected + + def test_find_max_daily_commits(): assert find_max_daily_commits(CONTRIBUTIONS_CALENDAR_SVG) == 84 From 65b7694662f79e88d63a3a87e5e5186ac55dd9a2 Mon Sep 17 00:00:00 2001 From: Jochen Kupperschmidt Date: Fri, 10 Jun 2016 00:25:44 +0200 Subject: [PATCH 5/5] Simplified finding of highest daily count by using the `max` function. --- gitfiti.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/gitfiti.py b/gitfiti.py index 339de6bbb..b15b75b1c 100755 --- a/gitfiti.py +++ b/gitfiti.py @@ -234,11 +234,8 @@ def parse_contributions_calendar(contributions_calendar): def find_max_daily_commits(contributions_calendar): """finds the highest number of commits in one day""" - output = parse_contributions_calendar(contributions_calendar) - output = list(output) - output.sort() - output.reverse() - return output[0] + daily_counts = parse_contributions_calendar(contributions_calendar) + return max(daily_counts) def calculate_multiplier(max_commits):