Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. """PR11 Drawing."""
  2.  
  3.  
  4. # TODO: You should create the exception classes here.
  5.  
  6.  
  7. class FigureDoesNotExistError(Exception):
  8. """IF figure does not exist"""
  9.  
  10. pass
  11.  
  12.  
  13. class DrawingFullError(Exception):
  14. """IF drawing figures are full."""
  15.  
  16. pass
  17.  
  18.  
  19. class DrawingCanvas:
  20. """A drawing canvas where one can draw some simple figures."""
  21.  
  22. def __init__(self, max_figures: int, author: str):
  23. """
  24. Initialize the canvas.
  25.  
  26. You should initialize your variables (properties) here.
  27.  
  28. :param max_figures: The maxim amount of figures on the drawing.
  29. :param author: The author of the drawing.
  30. """
  31. self.max_figures = max_figures
  32. self.author = author
  33. self.figures = []
  34. self.figures_count = 0
  35.  
  36. def draw_figure(self, figure: str) -> str or None:
  37. """
  38. Draw a new figure.
  39.  
  40. If the drawing has already reached maximum amount of figures
  41. don't add this figure and throw a DrawingFullError with the
  42. message "The drawing is full".
  43.  
  44. There can be only unique figures on the drawing.
  45. This means that there is no way that, for example, two or more
  46. circles are on the drawing.
  47. In this case method does nothing and returns None.
  48.  
  49. :param figure: A figure to draw.
  50. :return: The newly drawn figure.
  51. """
  52. self.figures_count += 1
  53. if self.figures_count > self.max_figures:
  54. self.figures_count -= 1
  55. raise DrawingFullError("The drawing is full")
  56. for i in self.figures:
  57. if i == figure:
  58. self.figures_count =- 1
  59. return None
  60. return figure
  61.  
  62. def erase_figure(self, figure: str) -> str:
  63. """
  64. Erase the figure.
  65.  
  66. If there is no such figure throw a FigureDoesNotExistError
  67. with the message "There is no such figure on the drawing".
  68.  
  69. :return: The erased figure.
  70. """
  71. try:
  72. self.figures.index(figure)
  73. except ValueError:
  74. raise FigureDoesNotExistError("There is no such figure on the drawing")
  75. self.figures.remove(figure)
  76. self.figures_count -= 1
  77. return figure
  78.  
  79. def is_empty(self) -> bool:
  80. """
  81. Find out whether the drawing is empty or not.
  82.  
  83. Empty means there are not any figures drawn on it.
  84.  
  85. :return: True if empty, False otherwise.
  86. """
  87. if self.figures == []:
  88. return True
  89. else:
  90. return False
  91.  
  92. def size(self) -> int:
  93. """Return the amount of figures on the drawing."""
  94. return len(self.figures)
  95.  
  96. def __str__(self):
  97. """
  98. A string representation of the drawing.
  99.  
  100. The returned string must follow this pattern:
  101. "The drawing painted by {author}. Contains {current amount of figures} figure(s)"
  102.  
  103. :return: A correct string representing the drawing.
  104. """
  105. return "The drawing painted by " + self.author + ". " + "Contains " + str(self.figures_count) + " figure(s)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement