From 94019e720df365137f74ab1800d3504bde40fd61 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Mon, 17 Apr 2023 06:51:09 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- welcome-to-sourcery.py | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/welcome-to-sourcery.py b/welcome-to-sourcery.py index 554cf8e0..28fb1bed 100644 --- a/welcome-to-sourcery.py +++ b/welcome-to-sourcery.py @@ -6,10 +6,7 @@ # This means Sourcery has a suggestion. def refactoring_example(spellbook): - result = [] - for spell in spellbook: - if spell.is_awesome: - result.append(spell) + result = [spell for spell in spellbook if spell.is_awesome] print(result) # Hover over the underlined code to see details of the changes including a diff. @@ -27,11 +24,9 @@ def refactoring_example(spellbook): # code quality - hover over the function definition below to see this report. def magical_hoist(magic): - if is_powerful(magic): - result = 'Magic' - else: + if not is_powerful(magic): print("Not powerful.") - result = 'Magic' + result = 'Magic' print(result) # What if we don't want to make the change Sourcery suggests? @@ -72,23 +67,13 @@ def magical_hoist(magic): # up with more powerful refactorings. def find_more(magicks): - powerful_magic = [] - for magic in magicks: - if not is_powerful(magic): - continue - powerful_magic.append(magic) - return powerful_magic + return [magic for magic in magicks if is_powerful(magic)] def is_powerful(magic): - if magic == 'Sourcery': - return True - elif magic == 'More Sourcery': - return True - else: - return False + return magic in ['Sourcery', 'More Sourcery'] def print_all(spells: list): - for i in range(len(spells)): - print(spells[i]) + for spell in spells: + print(spell)