1
+
2
+
3
+ # Sourcery can find all sorts of improvements that make your code more Pythonic
4
+
5
+ MAGICKS = ['Sourcery' , 'Wizardry' , 'Card tricks' ]
6
+
7
+ # Here we can use Python's built-in 'any' function to streamline the method
8
+ def contains_powerful_magic (spells ):
9
+ for spell in spells :
10
+ if is_powerful (spell ):
11
+ return True
12
+ return False
13
+
14
+ # We chain together improvements to get your code to the best possible
15
+ # state with the least effort
16
+
17
+ # Here we can use a list comprehension and then return the answer straight away
18
+ def find_the_best (magicks ):
19
+ powerful_magic = []
20
+ for magic in magicks :
21
+ if is_powerful (magic ):
22
+ powerful_magic .append (magic )
23
+ return powerful_magic
24
+
25
+ # This there are more steps required to put the code in the best state
26
+ def find_more (magicks ):
27
+ powerful_magic = []
28
+ for magic in magicks :
29
+ if not is_powerful (magic ):
30
+ continue
31
+ powerful_magic .append (magicks )
32
+ return powerful_magic
33
+
34
+ # And here we are combining conditionals
35
+ def is_powerful (self , magic ):
36
+ if magic == 'Sourcery' :
37
+ return True
38
+ elif magic == 'More Sourcery' :
39
+ return True
40
+ else :
41
+ return False
42
+
43
+ # We can even identify duplicate code and extract it!
44
+ class ClassWithExample :
45
+ def extraction_example ():
46
+ self .sparkle_slider = Scale (
47
+ self .master , from_ = 1 , to = 10 , orient = HORIZONTAL , label = 'Sparkle' )
48
+ self .sparkle_slider .set (DEFAULT_SPARKLE )
49
+ self .sparkle_slider .configure (background = 'black' )
50
+
51
+ self .whoosh_slider = Scale (
52
+ self .master , from_ = 1 , to = 10 , orient = HORIZONTAL , label = 'Whoosh' )
53
+ self .whoosh_slider .set (DEFAULT_WHOOSH )
54
+ self .whoosh_slider .configure (background = 'purple' )
55
+
56
+
57
+ # We can evem untangle some pretty thorny logic
58
+ class GildedRose :
59
+ def update_quality (self ):
60
+ for item in self .items :
61
+ if item .name != "Aged Brie" and item .name != "Backstage passes to a TAFKAL80ETC concert" :
62
+ if item .quality > 0 :
63
+ if item .name != "Sulfuras, Hand of Ragnaros" :
64
+ item .quality = item .quality - 1
65
+ else :
66
+ if item .quality < 50 :
67
+ item .quality = item .quality + 1
68
+ if item .name == "Backstage passes to a TAFKAL80ETC concert" :
69
+ if item .sell_in < 11 :
70
+ if item .quality < 50 :
71
+ item .quality = item .quality + 1
72
+ if item .sell_in < 6 :
73
+ if item .quality < 50 :
74
+ item .quality = item .quality + 1
75
+ if item .name != "Sulfuras, Hand of Ragnaros" :
76
+ item .sell_in = item .sell_in - 1
77
+ if item .sell_in < 0 :
78
+ if item .name != "Aged Brie" :
79
+ if item .name != "Backstage passes to a TAFKAL80ETC concert" :
80
+ if item .quality > 0 :
81
+ if item .name != "Sulfuras, Hand of Ragnaros" :
82
+ item .quality = item .quality - 1
83
+ else :
84
+ item .quality = item .quality - item .quality
85
+ else :
86
+ if item .quality < 50 :
87
+ item .quality = item .quality + 1
88
+
89
+
90
+ # And do clone detection and whole project scans!
0 commit comments