Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions 2016/201612_tdd_workshop/leap_year.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 1 addition & 5 deletions 2017/20170814_tdd_workshop/solutions/fizzbuzzwhiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Author

Choose a reason for hiding this comment

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

Function is_prime refactored with the following changes:

  • Use any() instead of for loop (use-any)
  • Invert any/all to simplify comparisons (invert-any-all)

Choose a reason for hiding this comment

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

Amazing!



def fizzbuzzwhiz(num):
Expand Down
2 changes: 1 addition & 1 deletion 2017/future/first_unique_character_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def first_unique_character(s):
before = ''
for i in range(0, len(s)):
for i in range(len(s)):
Copy link
Author

Choose a reason for hiding this comment

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

Function first_unique_character refactored with the following changes:

  • Replace range(0, x) with range(x) (remove-zero-from-range)

char = s[i]
after = s[i+1:]
if char not in before + after:
Expand Down
6 changes: 1 addition & 5 deletions 2017/future/prime_numbers_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Author

Choose a reason for hiding this comment

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

Function is_prime refactored with the following changes:

  • Use any() instead of for loop (use-any)
  • Invert any/all to simplify comparisons (invert-any-all)



class PrimeNumbersTest(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion 2018/20181019_talks/Celery Schedule Strategy/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@app.task
def show(symbol, time_count):
for m in range(time_count):
for _ in range(time_count):
Copy link
Author

Choose a reason for hiding this comment

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

Function show refactored with the following changes:

  • Replace unused for index with underscore (for-index-underscore)

print(symbol * time_count)
sleep(1)
return symbol
3 changes: 1 addition & 2 deletions 2019/201903_AI_school_meetup/coding_workshop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Author

Choose a reason for hiding this comment

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

Function median refactored with the following changes:

  • Inline variable that is immediately returned (inline-immediately-returned-variable)



print(median(five_numbers))
Expand Down
5 changes: 1 addition & 4 deletions 2019/201904_mob_programming_workshop/odd_even_prime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Author

Choose a reason for hiding this comment

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

Function is_prime refactored with the following changes:

  • Use any() instead of for loop (use-any)
  • Invert any/all to simplify comparisons (invert-any-all)



def odd_or_even(num):
Expand Down
7 changes: 2 additions & 5 deletions 2020/202001_refactoring_workshop/fizzbuzzwhiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Comment on lines -23 to +25
Copy link
Author

Choose a reason for hiding this comment

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

Function is_prime refactored with the following changes:

  • Replace multiple comparisons of same variable with in operator (merge-comparisons)
  • Use any() instead of for loop (use-any)
  • Invert any/all to simplify comparisons (invert-any-all)



def fizzbuzzwhiz(n):
Expand Down
56 changes: 28 additions & 28 deletions 2020/202001_refactoring_workshop/tennis_before.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,81 +66,81 @@ 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

Comment on lines -69 to +131
Copy link
Author

Choose a reason for hiding this comment

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

Function TennisGameDefactored2.score refactored with the following changes:

  • Simplify conditional into switch-like form (switch)

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):
result = "Win for " + self.player2Name
return result

def SetP1Score(self, number):
for i in range(number):
for _ in range(number):
Copy link
Author

Choose a reason for hiding this comment

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

Function TennisGameDefactored2.SetP1Score refactored with the following changes:

  • Replace unused for index with underscore (for-index-underscore)

self.P1Score()

def SetP2Score(self, number):
for i in range(number):
for _ in range(number):
Copy link
Author

Choose a reason for hiding this comment

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

Function TennisGameDefactored2.SetP2Score refactored with the following changes:

  • Replace unused for index with underscore (for-index-underscore)

self.P2Score()

def P1Score(self):
Expand Down
6 changes: 3 additions & 3 deletions 2020/202001_refactoring_workshop/tennis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Comment on lines -49 to +51
Copy link
Author

Choose a reason for hiding this comment

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

Function test_get_score refactored with the following changes:

  • Replace unused for index with underscore (for-index-underscore)

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):
Copy link
Author

Choose a reason for hiding this comment

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

Function test_player_names refactored with the following changes:

  • Replace unused for index with underscore (for-index-underscore)

game.won_point('One')
assert "Win for One" == game.score()