dc3a336module-07(ex1): refactor and cleanup
5 files changed, +58, -54
module-07/capacitor.py
+35, -35
+35, -35
@@ -1,51 +1,51 @@
1 -from ex1 import HealingCreatureFactory, TransformCreatureFactory
1+from ex0 import Creature, CreatureFactory
2+from ex1 import (
3+ HealCapability,
4+ TransformCapability,
5+ HealingCreatureFactory,
6+ TransformCreatureFactory,
7+)
2 8
3 9
4 -def test_healing(factory: HealingCreatureFactory) -> None:
5 - print("Test creature with healing capability")
10+def show_healer(creature: Creature) -> None:
11+ print(creature.describe())
12+ print(creature.attack())
13+ if isinstance(creature, HealCapability):
14+ print(creature.heal())
615
7 - base = factory.create_base()
8 - evolved = factory.create_evolved()
916
10 - print(" base:")
11 - print(base.describe())
12 - print(base.attack())
13 - print(base.heal())
14 -
15 - print(" evolved:")
16 - print(evolved.describe())
17 - print(evolved.attack())
18 - print(evolved.heal())
17+def show_transformer(creature: Creature) -> None:
18+ print(creature.describe())
19+ print(creature.attack())
20+ if isinstance(creature, TransformCapability):
21+ print(creature.transform())
22+ print(creature.attack())
23+ print(creature.revert())
1924
2025
21 -def test_transform(factory: TransformCreatureFactory) -> None:
22 - print("Test creature with transform capability")
26+def test_healing(factory: CreatureFactory) -> None:
27+ print("Testing Creature with healing capability")
28+ print(" base:")
29+ show_healer(factory.create_base())
30+ print(" evolved:")
31+ show_healer(factory.create_evolved())
2332
24 - base = factory.create_base()
25 - evolved = factory.create_evolved()
2633
34+def test_transform(factory: CreatureFactory) -> None:
35+ print("Testing Creature with transform capability")
2736 print(" base:")
28 - print(base.describe())
29 - print(base.attack())
30 - print(base.transform())
31 - print(base.attack())
32 - print(base.revert())
33 -
37+ show_transformer(factory.create_base())
3438 print(" evolved:")
35 - print(evolved.describe())
36 - print(evolved.attack())
37 - print(evolved.transform())
38 - print(evolved.attack())
39 - print(evolved.revert())
39+ show_transformer(factory.create_evolved())
4040
4141
4242 def main() -> None:
43 - healing = HealingCreatureFactory()
44 - transform = TransformCreatureFactory()
45 -
46 - test_healing(healing)
47 - print()
48 - test_transform(transform)
43+ try:
44+ test_healing(HealingCreatureFactory())
45+ print()
46+ test_transform(TransformCreatureFactory())
47+ except Exception as e:
48+ print("Error:", e)
4949
5050
5151 if __name__ == "__main__":
+3, -0
@@ -1,7 +1,10 @@
1+from .capabilities import HealCapability, TransformCapability
1 2 from .factories import HealingCreatureFactory, TransformCreatureFactory
2 3
3 4
4 5 __all__ = [
6+ "HealCapability",
7+ "TransformCapability",
5 8 "HealingCreatureFactory",
6 9 "TransformCreatureFactory"
710 ]
@@ -1,31 +1,31 @@
1 -from ex0.creature import Creature
1+from ex0 import Creature
2 2 from .capabilities import HealCapability, TransformCapability
3 3
4 4
5 5 class Sproutling(Creature, HealCapability):
6 6 def __init__(self) -> None:
7 - super().__init__("Sproutling", "Grass")
7+ Creature.__init__(self, "Sproutling", "Grass")
8 8
9 9 def attack(self) -> str:
10 - return f"{self._name} uses Vine Whip"
10+ return f"{self._name} uses Vine Whip!"
1111
12 - def heal(self, target: str | None = None) -> str:
12+ def heal(self, target: Creature | None = None) -> str:
1313 if target is not None:
14 - return f"{self._name} heals {target} for a small amount"
14+ return f"{self._name} heals {target._name} for a small amount"
1515 return f"{self._name} heals itself for a small amount"
1616
1717
1818 class Bloomelle(Creature, HealCapability):
1919 def __init__(self) -> None:
20 - super().__init__("Bloomelle", "Grass/Fairy")
20+ Creature.__init__(self, "Bloomelle", "Grass/Fairy")
2121
2222 def attack(self) -> str:
23 - return f"{self._name} uses Petal Dance"
23+ return f"{self._name} uses Petal Dance!"
2424
25 - def heal(self, target: str | None = None) -> str:
25+ def heal(self, target: Creature | None = None) -> str:
2626 if target is not None:
27 - return f"{self._name} heals {target} for a small amount"
28 - return f"{self._name} heals itself and others"
27+ return f"{self._name} heals {target._name} for a small amount"
28+ return f"{self._name} heals itself and others for a large amount"
2929
3030
3131 class Shiftling(Creature, TransformCapability):
@@ -1,9 +1,10 @@
1 1 from abc import ABC, abstractmethod
2+from ex0 import Creature
2 3
3 4
4 5 class HealCapability(ABC):
5 6 @abstractmethod
6 - def heal(self, target: str | None = None) -> str:
7+ def heal(self, target: Creature | None = None) -> str:
7 8 pass
8 9
910
@@ -13,8 +14,8 @@ class TransformCapability(ABC):
1314
1415 @abstractmethod
1516 def transform(self) -> str:
16 - pass
17+ ...
1718
1819 @abstractmethod
1920 def revert(self) -> str:
20 - pass
21+ ...
+6, -6
@@ -1,5 +1,5 @@
1 -from ex0.factories import CreatureFactory
2 -from .creatures import (
1+from ex0 import Creature, CreatureFactory
2+from ._creatures import (
3 3 Sproutling,
4 4 Bloomelle,
5 5 Shiftling,
@@ -8,16 +8,16 @@ from .creatures import (
8 8
9 9
1010 class HealingCreatureFactory(CreatureFactory):
11 - def create_base(self) -> Sproutling:
11+ def create_base(self) -> Creature:
1212 return Sproutling()
1313
14 - def create_evolved(self) -> Bloomelle:
14+ def create_evolved(self) -> Creature:
1515 return Bloomelle()
1616
1717
1818 class TransformCreatureFactory(CreatureFactory):
19 - def create_base(self) -> Shiftling:
19+ def create_base(self) -> Creature:
2020 return Shiftling()
2121
22 - def create_evolved(self) -> Morphagon:
22+ def create_evolved(self) -> Creature:
2323 return Morphagon()