66ca814module-07(ex0): refactor and cleanup
5 files changed, +54, -55
+14, -19
@@ -3,41 +3,36 @@ from ex0 import CreatureFactory, AquaFactory, FlameFactory
3 3
4 4 def test_factory(factory: CreatureFactory) -> None:
5 5 print("Testing factory")
6 -
7 6 base = factory.create_base()
7+ print(base.describe())
8+ print(base.attack())
8 9 evolved = factory.create_evolved()
9 -
10 - for c in [base, evolved]:
11 - print(c.describe())
12 - print(c.attack())
13 -
10+ print(evolved.describe())
11+ print(evolved.attack())
1412 print()
1513
1614
17 -def battle(factory1: CreatureFactory, factory2: CreatureFactory):
15+def battle(factory1: CreatureFactory, factory2: CreatureFactory) -> None:
1816 print("Testing battle")
19 -
2017 creature1 = factory1.create_base()
2118 creature2 = factory2.create_base()
22 -
2319 print(creature1.describe())
2420 print(" vs.")
2521 print(creature2.describe())
26 -
2722 print(" fight!")
28 -
2923 print(creature1.attack())
3024 print(creature2.attack())
3125
3226
33 -def main():
34 - flame_factory = FlameFactory()
35 - aqua_factory = AquaFactory()
36 -
37 - test_factory(flame_factory)
38 - test_factory(aqua_factory)
39 -
40 - battle(flame_factory, aqua_factory)
27+def main() -> None:
28+ try:
29+ flame_factory = FlameFactory()
30+ aqua_factory = AquaFactory()
31+ test_factory(flame_factory)
32+ test_factory(aqua_factory)
33+ battle(flame_factory, aqua_factory)
34+ except Exception as e:
35+ print("Error:", e)
4136
4237
4338 if __name__ == "__main__":
+2, -0
@@ -1,7 +1,9 @@
1+from .creature import Creature
12 from .factories import CreatureFactory, FlameFactory, AquaFactory
23
34
45 __all__ = [
6+ "Creature",
57 "CreatureFactory",
68 "FlameFactory",
79 "AquaFactory"
+33, -0
@@ -0,0 +1,33 @@
1+from .creature import Creature
2+
3+
4+class Flameling(Creature):
5+ def __init__(self) -> None:
6+ super().__init__("Flameling", "Fire")
7+
8+ def attack(self) -> str:
9+ return f"{self._name} uses Ember!"
10+
11+
12+class Pyrodon(Creature):
13+ def __init__(self) -> None:
14+ super().__init__("Pyrodon", "Fire/Flying")
15+
16+ def attack(self) -> str:
17+ return f"{self._name} uses Flamethrower!"
18+
19+
20+class Aquabub(Creature):
21+ def __init__(self) -> None:
22+ super().__init__("Aquabub", "Water")
23+
24+ def attack(self) -> str:
25+ return f"{self._name} uses Water Gun!"
26+
27+
28+class Torragon(Creature):
29+ def __init__(self) -> None:
30+ super().__init__("Torragon", "Water")
31+
32+ def attack(self) -> str:
33+ return f"{self._name} uses Hydro Pump!"
+1, -33
@@ -11,36 +11,4 @@ class Creature(ABC):
1111
1212 @abstractmethod
1313 def attack(self) -> str:
14 - pass
15 -
16 -
17 -class Flameling(Creature):
18 - def __init__(self) -> None:
19 - super().__init__("Flameling", "Fire")
20 -
21 - def attack(self) -> str:
22 - return f"{self._name} uses Ember!"
23 -
24 -
25 -class Pyrodon(Creature):
26 - def __init__(self) -> None:
27 - super().__init__("Pyrodon", "Fire/Flying")
28 -
29 - def attack(self) -> str:
30 - return f"{self._name} uses Flamethrower!"
31 -
32 -
33 -class Aquabub(Creature):
34 - def __init__(self) -> None:
35 - super().__init__("Aquabub", "Water")
36 -
37 - def attack(self) -> str:
38 - return f"{self._name} uses Water Gun!"
39 -
40 -
41 -class Torragon(Creature):
42 - def __init__(self) -> None:
43 - super().__init__("Torragon", "Water")
44 -
45 - def attack(self) -> str:
46 - return f"{self._name} uses Hydro Pump!"
14+ ...
+4, -3
@@ -1,15 +1,16 @@
1 1 from abc import ABC, abstractmethod
2 -from .creature import Creature, Flameling, Pyrodon, Aquabub, Torragon
2+from .creature import Creature
3+from ._creatures import Flameling, Pyrodon, Aquabub, Torragon
3 4
4 5
5 6 class CreatureFactory(ABC):
6 7 @abstractmethod
7 8 def create_base(self) -> Creature:
8 - pass
9+ ...
910
1011 @abstractmethod
1112 def create_evolved(self) -> Creature:
12 - pass
13+ ...
1314
1415
1516 class FlameFactory(CreatureFactory):