|
1 | 1 | #!/usr/bin/python3 |
2 | | -"""Defines a class Square""" |
| 2 | +"""my square module.""" |
3 | 3 |
|
4 | 4 |
|
5 | 5 | class Square: |
6 | | - """Represents a square |
7 | | - Attributes: |
8 | | - __size (int): size of a size of the square |
9 | | - __position (tuple): position of the square in 2D space |
10 | | - """ |
| 6 | + """define a Square.""" |
| 7 | + |
| 8 | + def __str__(self): |
| 9 | + """teach python to print the square my way""" |
| 10 | + return self.pos_print()[:-1] |
| 11 | + |
11 | 12 | def __init__(self, size=0, position=(0, 0)): |
12 | | - """initializes the square |
| 13 | + """ initialize the square with this |
13 | 14 | Args: |
14 | | - size (int): size of a side of the square |
15 | | - position (tuple): positoin of the square in 2D space |
16 | | - Returns: |
17 | | - None |
| 15 | + size: a side of square |
| 16 | + position: where the square is (coordinates) |
18 | 17 | """ |
19 | 18 | self.size = size |
20 | 19 | self.position = position |
21 | 20 |
|
22 | | - def area(self): |
23 | | - """calculates the square's area |
24 | | - Returns: |
25 | | - The area of the square |
26 | | - """ |
27 | | - return (self.__size) ** 2 |
28 | | - |
29 | 21 | @property |
30 | 22 | def size(self): |
31 | | - """getter of __size |
32 | | - Returns: |
33 | | - The size of the square |
| 23 | + """property of the length of a side of square |
| 24 | + Raises: |
| 25 | + TypeError: if size is not an int. |
| 26 | + ValueError: if size is < 0. |
34 | 27 | """ |
35 | 28 | return self.__size |
36 | 29 |
|
37 | 30 | @size.setter |
38 | 31 | def size(self, value): |
39 | | - """setter of __size |
| 32 | + """ set the size of square |
40 | 33 | Args: |
41 | | - value (int): size of a side of the square |
42 | | - Returns: |
43 | | - None |
| 34 | + value: the size |
| 35 | + Raises: |
| 36 | + TypeError: if value is not int |
| 37 | + ValueError: if valie < 0 |
44 | 38 | """ |
45 | | - if type(value) is not int: |
46 | | - raise TypeError("size must be an integer") |
47 | | - else: |
48 | | - if value < 0: |
49 | | - raise ValueError("size must be >= 0") |
50 | | - else: |
51 | | - self.__size = value |
52 | | - |
53 | | - def my_print(self): |
54 | | - """prints the square |
55 | | - Returns: |
56 | | - None |
57 | | - """ |
58 | | - if self.__size == 0: |
59 | | - print() |
60 | | - return |
61 | | - for i in range(self.__position[1]): |
62 | | - print() |
63 | | - for j in range(self.__size): |
64 | | - print("".join([" " for k in range(self.__position[0])]), end="") |
65 | | - print("".join(["#" for l in range(self.__size)])) |
| 39 | + if not isinstance(value, int): |
| 40 | + raise TypeError('size must be an integer') |
| 41 | + if value < 0: |
| 42 | + raise ValueError('size must be >= 0') |
| 43 | + self.__size = value |
66 | 44 |
|
67 | 45 | @property |
68 | 46 | def position(self): |
69 | | - """getter of __position |
70 | | - Returns: |
71 | | - The position of the square in 2D space |
| 47 | + """property of the position of square |
| 48 | + Raises: |
| 49 | + TypeError: if value != tuple of 2 ints >= 0. |
| 50 | + Returns: the position |
72 | 51 | """ |
73 | 52 | return self.__position |
74 | 53 |
|
75 | 54 | @position.setter |
76 | 55 | def position(self, value): |
77 | | - """setter of __position |
| 56 | + """set the position |
78 | 57 | Args: |
79 | | - value (tuple): position of the square in 2D space |
80 | | - Returns: |
81 | | - None |
| 58 | + value: where |
| 59 | + Raises: |
| 60 | + TypeError: if not tuple, ints, positive |
| 61 | + Returns: the position |
82 | 62 | """ |
83 | | - if type(value) is not tuple or len(value) != 2 or \ |
84 | | - type(value[0]) is not int or value[0] < 0 or \ |
85 | | - type(value[1]) is not int or value[1] < 0: |
86 | | - raise TypeError("position must be a tuple of 2 positive integers") |
87 | | - else: |
88 | | - self.__position = value |
| 63 | + if not isinstance(value, tuple): |
| 64 | + raise TypeError('position must be a tuple of 2 positive integers') |
| 65 | + if len(value) != 2: |
| 66 | + raise TypeError('position must be a tuple of 2 positive integers') |
| 67 | + if len([i for i in value if isinstance(i, int) and i >= 0]) != 2: |
| 68 | + raise TypeError('position must be a tuple of 2 positive integers') |
| 69 | + self.__position = value |
89 | 70 |
|
90 | | - def __str__(self): |
91 | | - """String representation of a Square instance |
| 71 | + def area(self): |
| 72 | + """ the area of square |
92 | 73 | Returns: |
93 | | - Formatted string representing the square |
| 74 | + size * size |
94 | 75 | """ |
95 | | - if self.size == 0: |
96 | | - return "" |
97 | | - string = "\n" * self.position[1] + (" " * self.position[0] + |
98 | | - "#" * self.size + "\n") * self.size |
99 | | - return string[:-1] |
| 76 | + return self.__size * self.__size |
| 77 | + |
| 78 | + def pos_print(self): |
| 79 | + """returns the printed square with position""" |
| 80 | + pos = "" |
| 81 | + if not self.size: |
| 82 | + return "\n" |
| 83 | + for w in range(self.position[1]): |
| 84 | + pos += "\n" |
| 85 | + for w in range(self.size): |
| 86 | + for i in range(self.position[0]): |
| 87 | + pos += " " |
| 88 | + for j in range(self.size): |
| 89 | + pos += "#" |
| 90 | + pos += "\n" |
| 91 | + return pos |
| 92 | + |
| 93 | + def my_print(self): |
| 94 | + """print square.""" |
0 commit comments