Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.00 KB | None | 0 0
  1. """Wizards."""
  2.  
  3.  
  4. class Wand:
  5.     """Wand."""
  6.  
  7.     def __init__(self, wood_type, core):
  8.         self.wood_type = wood_type
  9.         self.core = core
  10.  
  11.     def set_wood_type(self, wood_type):
  12.         self.wood_type = wood_type
  13.  
  14.     def set_core(self, core):
  15.         self.core = core
  16.  
  17.     @staticmethod
  18.     def check_wand(wand):
  19.         if not isinstance(wand, Wand) or wand.wood_type is None or wand.core is None:
  20.             raise MismatchError("The wand like that does not exist!")
  21.  
  22.     def __str__(self):
  23.         return f"{self.wood_type}, {self.core}"
  24.  
  25.  
  26. class Wizard:
  27.     """Wizard."""
  28.  
  29.     def __init__(self, name, wand=None):
  30.         self.name = name
  31.         self.wand = wand
  32.         if wand is not None:
  33.             Wand.check_wand(wand)
  34.  
  35.     def set_wand(self, wand):
  36.         # FEILIB
  37.         if wand is not None and Wand.check_wand(wand) is None:
  38.             self.wand = wand
  39.         else:
  40.             raise MismatchError("The wand like that does not exist!")
  41.  
  42.     def get_wand(self):
  43.         return self.wand
  44.  
  45.     def __str__(self):
  46.         return self.name
  47.  
  48.  
  49. class School:
  50.     """School."""
  51.  
  52.     schools = [
  53.         "Hogwarts School of Witchcraft and Wizardry", "Durmstrang Institute",
  54.         "Ilvermorny School of Witchcraft and Wizardry", "Castelobruxo",
  55.         "Beauxbatons Academy of Magic"
  56.     ]
  57.  
  58.     def __init__(self, name: str):
  59.         self.name = name
  60.         if name not in School.schools:
  61.             raise MismatchError("There is no such school!")
  62.  
  63.     def add_wizard(self, wizard):
  64.         # üks läbib, 2 feilib
  65.         if isinstance(wizard, Wizard) and wizard.name is not None and wizard.wand is not None:
  66.             return f"{wizard.name} is already studying in this school!"
  67.         raise MismatchError("It's a filthy muggle!")
  68.  
  69.     def remove_wizard(self, wizard):
  70.         # FEILIB
  71.         pass
  72.  
  73.     def get_wizards(self):
  74.         # FEILIB
  75.         pass
  76.  
  77.     def get_wizard_by_wand(self, wand):
  78.         # FEILIB
  79.         if wand is None or wand.wood_type is None or wand.core is None:
  80.             raise MismatchError("The wand like that does not exist!")
  81.  
  82.     def __str__(self):
  83.         return self.name
  84.  
  85.  
  86. class MismatchError(Exception):
  87.     def __init__(self, message):
  88.         self.message = message
  89.  
  90.  
  91. if __name__ == '__main__':
  92.  
  93.     wand1 = Wand("Holly", "Phoenix feather")
  94.     wand2 = Wand("Vine", "Dragon heartstring")
  95.     bad_wand = Wand(None, "empty")
  96.     assert str(wand1) == 'Holly, Phoenix feather'
  97.     assert str(wand2) == 'Vine, Dragon heartstring'
  98.  
  99.     wizard1 = Wizard("Harry Potter")
  100.     wizard2 = Wizard("Hermione Granger")
  101.     assert str(wizard1) == 'Harry Potter'
  102.     assert str(wizard2) == 'Hermione Granger'
  103.  
  104.     bad_wizard = Wizard(None, None)
  105.     school = School("Hogwarts School of Witchcraft and Wizardry")
  106.     assert str(school) == 'Hogwarts School of Witchcraft and Wizardry'
  107.  
  108.     assert wizard1.get_wand() is None
  109.     wizard1.set_wand(wand1)
  110.     assert str(wizard1.get_wand()) == 'Holly, Phoenix feather'
  111.     # wizard1.set_wand(bad_wand)  # --> MismatchError: The wand like that does not exist!
  112.  
  113.     assert school.add_wizard(wizard1) == 'Harry Potter started studying in Hogwarts School of Witchcraft and Wizardry.'
  114.     assert school.get_wizards().__len__() == 1
  115.  
  116.     # school.add_wizard(wizard2)  # --> MismatchError: It's a filthy muggle!
  117.     # school.add(bad_wizard)  # --> MismatchError: It's a filthy muggle!
  118.     wizard2.set_wand(wand2)
  119.     assert school.add_wizard(wizard2) == 'Hermione Granger started studying in Hogwarts School of Witchcraft and Wizardry.'
  120.  
  121.     assert school.get_wizards().__len__() == 2
  122.     assert school.add_wizard(wizard1) == 'Harry Potter is already studying in this school!'
  123.  
  124.     assert str(school.get_wizard_by_wand(wand1)) == 'Harry Potter'
  125.     assert str(school.get_wizard_by_wand(wand2)) == 'Hermione Granger'
  126.  
  127.     school.remove_wizard(wizard1)
  128.  
  129.     assert school.get_wizard_by_wand(wand1) is None
  130.  
  131.     School.schools.append("Example school")
  132.     assert "Example school" in School.schools
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement