Skip to content

Commit 4fdb41d

Browse files
author
Kepka
committed
more exceptions, testing exceptions
1 parent aa72306 commit 4fdb41d

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

Game/ducks.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ def fly(self):
77
if self.ratio > 1:
88
print("Weee, this is fun")
99
elif self.ratio == 1:
10-
print("This is hard word, but I'm flying")
10+
print("This is hard work, but I'm flying")
1111
else:
1212
print("I think I'll just walk")
1313

1414

1515
class Duck(object):
1616

17-
def __int__(self):
17+
def __init__(self):
1818
self._wing = Wing(1.8)
1919

2020
def walk(self):
2121
print("Waddle, waddle, waddle")
2222

2323
def swim(self):
24-
print("Come on it, th water's lovely")
24+
print("Come on in, the water's lovely")
2525

2626
def quack(self):
2727
print("Quack quack")
@@ -32,24 +32,60 @@ def fly(self):
3232

3333
class Penguin(object):
3434

35+
def __init__(self):
36+
self.fly = self.aviate
37+
3538
def walk(self):
3639
print("Waddle, waddle, I waddle too")
3740

3841
def swim(self):
39-
print("Come on in, it's a bit chilly this far South")
42+
print("Come on in, but it's a bit chilly this far South")
4043

4144
def quack(self):
42-
print("Are you 'avin' a larf? I'm penguin!")
45+
print("Are you 'avin' a larf? I'm a penguin!")
46+
47+
def aviate(self):
48+
print("I won the lottery and bought a learjet")
4349

4450

4551
# def test_duck(duck):
4652
# duck.walk()
4753
# duck.swim()
4854
# duck.quack()
4955

56+
class Mallard(Duck):
57+
pass
58+
59+
60+
class Flock(object):
61+
62+
def __init__(self):
63+
self.flock = []
64+
65+
def add_duck(self, duck: Duck) -> None:
66+
fly_method = getattr(duck, 'fly', None)
67+
# if isinstance(duck, Duck):
68+
if callable(fly_method):
69+
self.flock.append(duck)
70+
else:
71+
raise TypeError("Cannot add duck, are you sure it's not a " + str(type(duck).__name__))
72+
73+
def migrate(self):
74+
problem = None
75+
for duck in self.flock:
76+
try:
77+
duck.fly()
78+
raise AttributeError("Testing exception handler in migrate") # TODO remove before release
79+
except AttributeError as e:
80+
print('One duck down')
81+
problem = e
82+
if problem:
83+
raise problem
84+
5085

5186
if __name__ == '__main__':
5287
donald = Duck()
88+
donald.fly()
5389

5490
# percy = Penguin()
5591
# test_duck(percy)

Game/migration.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import ducks
2+
3+
flock = ducks.Flock()
4+
donald = ducks.Duck()
5+
daisy = ducks.Duck()
6+
duck3 = ducks.Duck()
7+
duck4 = ducks.Duck()
8+
duck5 = ducks.Duck()
9+
duck6 = ducks.Duck()
10+
duck7 = ducks.Duck()
11+
percy = ducks.Penguin()
12+
13+
flock.add_duck(donald)
14+
flock.add_duck(daisy)
15+
flock.add_duck(duck3)
16+
flock.add_duck(duck4)
17+
flock.add_duck(percy)
18+
flock.add_duck(duck5)
19+
flock.add_duck(duck6)
20+
flock.add_duck(duck7)
21+
22+
flock.migrate()

0 commit comments

Comments
 (0)