From 4d7ac51d2f3530d0dd415b5d04643f6ee3c75901 Mon Sep 17 00:00:00 2001 From: Sourcery AI Date: Sun, 28 Jun 2020 05:59:10 +0000 Subject: [PATCH] Refactored by Sourcery --- 2016/201612_tdd_workshop/leap_year.py | 4 +- .../solutions/fizzbuzzwhiz.py | 6 +- .../future/first_unique_character_solution.py | 2 +- 2017/future/prime_numbers_solution.py | 6 +- .../Celery Schedule Strategy/tasks.py | 2 +- .../coding_workshop.py | 3 +- .../odd_even_prime.py | 5 +- .../fizzbuzzwhiz.py | 7 +-- .../tennis_before.py | 56 +++++++++---------- .../tennis_test.py | 6 +- 10 files changed, 40 insertions(+), 57 deletions(-) diff --git a/2016/201612_tdd_workshop/leap_year.py b/2016/201612_tdd_workshop/leap_year.py index cb72fde..227a920 100644 --- a/2016/201612_tdd_workshop/leap_year.py +++ b/2016/201612_tdd_workshop/leap_year.py @@ -13,9 +13,7 @@ def is_leap(year): return True if year % 100 == 0: return False - if year % 4 == 0: - return True - return False + return year % 4 == 0 class LeapYearTest(unittest.TestCase): diff --git a/2017/20170814_tdd_workshop/solutions/fizzbuzzwhiz.py b/2017/20170814_tdd_workshop/solutions/fizzbuzzwhiz.py index 700de3f..0655a4c 100644 --- a/2017/20170814_tdd_workshop/solutions/fizzbuzzwhiz.py +++ b/2017/20170814_tdd_workshop/solutions/fizzbuzzwhiz.py @@ -16,11 +16,7 @@ def is_prime(num): if num == 1: return False - for i in range(2, num): - if num % i == 0: - return False - - return True + return all(num % i != 0 for i in range(2, num)) def fizzbuzzwhiz(num): diff --git a/2017/future/first_unique_character_solution.py b/2017/future/first_unique_character_solution.py index 8394f16..15e9d82 100644 --- a/2017/future/first_unique_character_solution.py +++ b/2017/future/first_unique_character_solution.py @@ -8,7 +8,7 @@ def first_unique_character(s): before = '' - for i in range(0, len(s)): + for i in range(len(s)): char = s[i] after = s[i+1:] if char not in before + after: diff --git a/2017/future/prime_numbers_solution.py b/2017/future/prime_numbers_solution.py index 54326c4..be67067 100644 --- a/2017/future/prime_numbers_solution.py +++ b/2017/future/prime_numbers_solution.py @@ -18,11 +18,7 @@ def is_prime(num): end = int(math.sqrt(num)) if num > 10 else num - for i in range(2, end): - if num % i == 0: - return False - - return True + return all(num % i != 0 for i in range(2, end)) class PrimeNumbersTest(unittest.TestCase): diff --git a/2018/20181019_talks/Celery Schedule Strategy/tasks.py b/2018/20181019_talks/Celery Schedule Strategy/tasks.py index 8aac8b0..8d212a0 100644 --- a/2018/20181019_talks/Celery Schedule Strategy/tasks.py +++ b/2018/20181019_talks/Celery Schedule Strategy/tasks.py @@ -5,7 +5,7 @@ @app.task def show(symbol, time_count): - for m in range(time_count): + for _ in range(time_count): print(symbol * time_count) sleep(1) return symbol diff --git a/2019/201903_AI_school_meetup/coding_workshop.py b/2019/201903_AI_school_meetup/coding_workshop.py index ed32c97..db4ad41 100644 --- a/2019/201903_AI_school_meetup/coding_workshop.py +++ b/2019/201903_AI_school_meetup/coding_workshop.py @@ -17,8 +17,7 @@ def median(numbers): length = len(numbers) half = length // 2 - int(length % 2 == 0) middle = sorted(numbers)[half:-half] - median = sum(middle) / len(middle) - return median + return sum(middle) / len(middle) print(median(five_numbers)) diff --git a/2019/201904_mob_programming_workshop/odd_even_prime.py b/2019/201904_mob_programming_workshop/odd_even_prime.py index 3768cfe..9c215a7 100644 --- a/2019/201904_mob_programming_workshop/odd_even_prime.py +++ b/2019/201904_mob_programming_workshop/odd_even_prime.py @@ -13,10 +13,7 @@ def is_prime(num): if num == 1: return False - for i in range(2, num): - if num % i == 0: - return False - return True + return all(num % i != 0 for i in range(2, num)) def odd_or_even(num): diff --git a/2020/202001_refactoring_workshop/fizzbuzzwhiz.py b/2020/202001_refactoring_workshop/fizzbuzzwhiz.py index 602c556..f8d7e0b 100644 --- a/2020/202001_refactoring_workshop/fizzbuzzwhiz.py +++ b/2020/202001_refactoring_workshop/fizzbuzzwhiz.py @@ -20,12 +20,9 @@ def is_prime(n): if n == 1: return False - if n == 2 or n == 3: + if n in [2, 3]: return True - for i in range(2, isqrt(n) + 1): - if n % i == 0: - return False - return True + return all(n % i != 0 for i in range(2, isqrt(n) + 1)) def fizzbuzzwhiz(n): diff --git a/2020/202001_refactoring_workshop/tennis_before.py b/2020/202001_refactoring_workshop/tennis_before.py index e7e3379..86662e9 100644 --- a/2020/202001_refactoring_workshop/tennis_before.py +++ b/2020/202001_refactoring_workshop/tennis_before.py @@ -66,69 +66,69 @@ def won_point(self, playerName): def score(self): result = "" if (self.p1points == self.p2points and self.p1points < 4): - if (self.p1points==0): + if self.p1points == 0: result = "Love" - if (self.p1points==1): + elif self.p1points == 1: result = "Fifteen" - if (self.p1points==2): + elif self.p1points == 2: result = "Thirty" - if (self.p1points==3): + elif self.p1points == 3: result = "Forty" result += "-All" if (self.p1points==self.p2points and self.p1points>3): result = "Deuce" - + P1res = "" P2res = "" if (self.p1points > 0 and self.p2points==0): - if (self.p1points==1): + if self.p1points == 1: P1res = "Fifteen" - if (self.p1points==2): + elif self.p1points == 2: P1res = "Thirty" - if (self.p1points==3): + elif self.p1points == 3: P1res = "Forty" - + P2res = "Love" result = P1res + "-" + P2res if (self.p2points > 0 and self.p1points==0): - if (self.p2points==1): + if self.p2points == 1: P2res = "Fifteen" - if (self.p2points==2): + elif self.p2points == 2: P2res = "Thirty" - if (self.p2points==3): + elif self.p2points == 3: P2res = "Forty" - + P1res = "Love" result = P1res + "-" + P2res - - + + if (self.p1points>self.p2points and self.p1points < 4): - if (self.p1points==2): + if self.p1points == 2: P1res="Thirty" - if (self.p1points==3): + elif self.p1points == 3: P1res="Forty" - if (self.p2points==1): + if self.p2points == 1: P2res="Fifteen" - if (self.p2points==2): + elif self.p2points == 2: P2res="Thirty" result = P1res + "-" + P2res if (self.p2points>self.p1points and self.p2points < 4): - if (self.p2points==2): + if self.p2points == 2: P2res="Thirty" - if (self.p2points==3): + elif self.p2points == 3: P2res="Forty" - if (self.p1points==1): + if self.p1points == 1: P1res="Fifteen" - if (self.p1points==2): + elif self.p1points == 2: P1res="Thirty" result = P1res + "-" + P2res - + if (self.p1points > self.p2points and self.p2points >= 3): result = "Advantage " + self.player1Name - + if (self.p2points > self.p1points and self.p1points >= 3): result = "Advantage " + self.player2Name - + if (self.p1points>=4 and self.p2points>=0 and (self.p1points-self.p2points)>=2): result = "Win for " + self.player1Name if (self.p2points>=4 and self.p1points>=0 and (self.p2points-self.p1points)>=2): @@ -136,11 +136,11 @@ def score(self): return result def SetP1Score(self, number): - for i in range(number): + for _ in range(number): self.P1Score() def SetP2Score(self, number): - for i in range(number): + for _ in range(number): self.P2Score() def P1Score(self): diff --git a/2020/202001_refactoring_workshop/tennis_test.py b/2020/202001_refactoring_workshop/tennis_test.py index 88e698a..bb93f6f 100644 --- a/2020/202001_refactoring_workshop/tennis_test.py +++ b/2020/202001_refactoring_workshop/tennis_test.py @@ -46,16 +46,16 @@ ]) def test_get_score(p1_points, p2_points, score, p1_name='player1', p2_name='player2'): game = TennisGame(p1_name, p2_name) - for i in range(p1_points): + for _ in range(p1_points): game.won_point(p1_name) - for i in range(p2_points): + for _ in range(p2_points): game.won_point(p2_name) assert score == game.score() def test_player_names(): game = TennisGame('One', 'Two') - for i in range(4): + for _ in range(4): game.won_point('One') assert "Win for One" == game.score()