Skip to content

Commit e7d6917

Browse files
committed
Initial commit
0 parents  commit e7d6917

File tree

8 files changed

+221
-0
lines changed

8 files changed

+221
-0
lines changed

clones/fib1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def fib(n):
2+
a, b = 0, 1
3+
for _ in range(n):
4+
a, b = b, a + b
5+
return a

clones/fib2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
n = 5
2+
a, b = 0, 1
3+
for _ in range(n):
4+
a, b = b, a + b
5+
print(a)

clones/single_file.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Here's the first example of duplication
2+
with open("file1.py") as f:
3+
for line in f:
4+
print(line)
5+
print()
6+
7+
8+
# Here's the second example of duplication
9+
with open("file1.py") as f:
10+
for line in f:
11+
print(line)
12+
print()

refactorings/basic_examples.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def choose_transport(age, can_drive):
2+
if age > 18:
3+
if can_drive:
4+
return "car"
5+
return "bus"
6+
7+
8+
def even_items(items):
9+
result = []
10+
for i, item in enumerate(items):
11+
if i % 2 == 0:
12+
result.append((item))
13+
return result
14+
15+
16+
def triangular_number(n):
17+
result = 0
18+
for i in range(1, n + 1):
19+
result = result + i
20+
return result
21+
22+
23+
class Point:
24+
def __init__(self, x, y):
25+
self.x = x
26+
self.y = y
27+
28+
def __eq__(self, other):
29+
if self.x == other.x and self.y == other.y:
30+
return True
31+
return False

refactorings/extract_examples.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
DEFAULT_SPEED = 10
2+
DEFAULT_FORCE = 20
3+
HORIZONTAL = 1
4+
VERTICAL = 2
5+
6+
7+
class Sliders:
8+
def __init__(self, steps):
9+
self._steps = steps
10+
11+
def create_speed_slider(self):
12+
speed_slider = Slider(
13+
self._steps, from_=1, to=10, orient=HORIZONTAL, label="Speed"
14+
)
15+
speed_slider.pack()
16+
speed_slider.set(DEFAULT_SPEED)
17+
speed_slider.configure(background="red")
18+
return speed_slider
19+
20+
def create_force_slider(self):
21+
force_slider = Slider(
22+
self._steps, from_=1, to=10, orient=HORIZONTAL, label="Force"
23+
)
24+
force_slider.pack()
25+
force_slider.set(DEFAULT_FORCE)
26+
force_slider.configure(background="green")
27+
return force_slider
28+
29+
30+
def create_sliders():
31+
speed_slider = Slider(5, from_=1, to=10, orient=HORIZONTAL, label="Speed")
32+
speed_slider.pack()
33+
speed_slider.set(DEFAULT_SPEED)
34+
speed_slider.configure(background="red")
35+
36+
force_slider = Slider(5, from_=1, to=10, orient=HORIZONTAL, label="Force")
37+
force_slider.pack()
38+
force_slider.set(DEFAULT_FORCE)
39+
40+
force_slider.configure(background="green")
41+
return speed_slider, force_slider
42+
43+
44+
class Slider:
45+
def __init__(self, steps, from_, to, orient, label):
46+
...
47+
48+
def pack(self):
49+
...
50+
51+
def set(self, position):
52+
...
53+
54+
def configure(self, background):
55+
...

refactorings/scan/fruit.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from dataclasses import dataclass
2+
3+
TASTY_APPLES = ["Gala", "Seneca Crisp", "Braeburn"]
4+
5+
6+
@dataclass
7+
class Fruit:
8+
size: float
9+
ripeness: float
10+
11+
12+
@dataclass
13+
class Apple(Fruit):
14+
variety: str
15+
16+
17+
@dataclass
18+
class Orange(Fruit):
19+
variety: str
20+
21+
22+
def assess_fruit(fruit_bowl):
23+
happiness = 0.0
24+
for fruit in fruit_bowl:
25+
if (fruit, Apple):
26+
yumminess = fruit.size * fruit.ripeness ** 2
27+
if fruit.variety in TASTY_APPLES:
28+
happiness += fruit.size * yumminess
29+
return happiness
30+
31+
32+
class DelMonteMan:
33+
def eat_orange(self, orange):
34+
if not orange.variety == "Clementine":
35+
with self.hands() as hands:
36+
self.happiness = self.happiness + 1
37+
segments = hands.peel(orange)
38+
self.eat(segments)
39+
else:
40+
with self.hands() as hands:
41+
if orange == "Clementine":
42+
self.happiness = self.happiness + 2
43+
segments = hands.peel(orange)
44+
self.eat(segments)

refactorings/scan/gilded_rose.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class GildedRose(object):
2+
def __init__(self, items):
3+
self.items = items
4+
5+
def update_quality(self):
6+
for item in self.items:
7+
if (
8+
item.name != "Aged Brie"
9+
and item.name != "Backstage passes to a TAFKAL80ETC concert"
10+
):
11+
if item.quality > 0:
12+
if item.name != "Sulfuras, Hand of Ragnaros":
13+
item.quality = item.quality - 1
14+
else:
15+
if item.quality < 50:
16+
item.quality = item.quality + 1
17+
if item.name == "Backstage passes to a TAFKAL80ETC concert":
18+
if item.sell_in < 11:
19+
if item.quality < 50:
20+
item.quality = item.quality + 1
21+
if item.sell_in < 6:
22+
if item.quality < 50:
23+
item.quality = item.quality + 1
24+
if item.name != "Sulfuras, Hand of Ragnaros":
25+
item.sell_in = item.sell_in - 1
26+
if item.sell_in < 0:
27+
if item.name != "Aged Brie":
28+
if item.name != "Backstage passes to a TAFKAL80ETC concert":
29+
if item.quality > 0:
30+
if item.name != "Sulfuras, Hand of Ragnaros":
31+
item.quality = item.quality - 1
32+
else:
33+
item.quality = item.quality - item.quality
34+
else:
35+
if item.quality < 50:
36+
item.quality = item.quality + 1
37+
38+
39+
class Item:
40+
def __init__(self, name, sell_in, quality):
41+
self.name = name
42+
self.sell_in = sell_in
43+
self.quality = quality
44+
45+
def __repr__(self):
46+
return "%s, %s, %s" % (self.name, self.sell_in, self.quality)

refactorings/scan/magic.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def find_good_magic():
2+
magics = ["Sourcery", "Mordor", "Voldemort"]
3+
4+
good_magic = []
5+
for magic in magics:
6+
if magic == "Sourcery":
7+
good_magic.append(magic)
8+
9+
print(good_magic)
10+
11+
12+
def sing_happy_birthday(name):
13+
i = 0
14+
while i < 4:
15+
if i == 0:
16+
print("Happy birthday to you")
17+
elif i == 1:
18+
print("Happy birthday to you")
19+
elif i == 2:
20+
print("Happy birthday dear " + name)
21+
else:
22+
print("Happy birthday to you")
23+
i += 1

0 commit comments

Comments
 (0)