Skip to content

Commit 7addd64

Browse files
committed
fix bugs
1 parent 521bb42 commit 7addd64

File tree

2 files changed

+100
-134
lines changed

2 files changed

+100
-134
lines changed

0x06-python-classes/101-square.py

Lines changed: 62 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,94 @@
11
#!/usr/bin/python3
2-
"""Defines a class Square"""
2+
"""my square module."""
33

44

55
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+
1112
def __init__(self, size=0, position=(0, 0)):
12-
"""initializes the square
13+
""" initialize the square with this
1314
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)
1817
"""
1918
self.size = size
2019
self.position = position
2120

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-
2921
@property
3022
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.
3427
"""
3528
return self.__size
3629

3730
@size.setter
3831
def size(self, value):
39-
"""setter of __size
32+
""" set the size of square
4033
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
4438
"""
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
6644

6745
@property
6846
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
7251
"""
7352
return self.__position
7453

7554
@position.setter
7655
def position(self, value):
77-
"""setter of __position
56+
"""set the position
7857
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
8262
"""
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
8970

90-
def __str__(self):
91-
"""String representation of a Square instance
71+
def area(self):
72+
""" the area of square
9273
Returns:
93-
Formatted string representing the square
74+
size * size
9475
"""
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."""

0x06-python-classes/6-square.py

Lines changed: 38 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,60 @@
11
#!/usr/bin/python3
2-
"""Defines a class Square"""
2+
"""Square class to represent a square"""
33

44

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-
"""
5+
class Square():
6+
"""square class with it's size and proper validation"""
117

128
def __init__(self, size=0, position=(0, 0)):
13-
"""initializes the square
14-
Args:
15-
size (int): size of a side of the square
16-
position (tuple): positoin of the square in 2D space
17-
Returns:
18-
None
19-
"""
9+
""""Initialize data"""
2010
self.size = size
2111
self.position = position
2212

23-
def area(self):
24-
"""calculates the square's area
25-
Returns:
26-
The area of the square
27-
"""
28-
return (self.__size) ** 2
29-
3013
@property
3114
def size(self):
32-
"""getter of __size
33-
Returns:
34-
The size of the square
35-
"""
15+
""""get size"""
3616
return self.__size
3717

18+
@property
19+
def position(self):
20+
""""get position"""
21+
return self.__position
22+
3823
@size.setter
3924
def size(self, value):
40-
"""setter of __size
41-
Args:
42-
value (int): size of a side of the square
43-
Returns:
44-
None
45-
"""
46-
if type(value) is not int:
25+
""""set size"""
26+
if (type(value) is not int):
4727
raise TypeError("size must be an integer")
28+
elif (value < 0):
29+
raise ValueError("size must be >= 0")
4830
else:
49-
if value < 0:
50-
raise ValueError("size must be >= 0")
51-
else:
52-
self.__size = value
53-
54-
def my_print(self):
55-
"""prints the square
56-
Returns:
57-
None
58-
"""
59-
if self.__size == 0:
60-
print()
61-
return
62-
for i in range(self.__position[1]):
63-
print()
64-
for j in range(self.__size):
65-
print("".join([" " for k in range(self.__position[0])]), end="")
66-
print("".join(["#" for l in range(self.__size)]))
67-
68-
@property
69-
def position(self):
70-
"""getter of __position
71-
Returns:
72-
The position of the square in 2D space
73-
"""
74-
return self.__position
31+
self.__size = value
7532

7633
@position.setter
7734
def position(self, value):
78-
"""setter of __position
79-
Args:
80-
value (tuple): position of the square in 2D space
81-
Returns:
82-
None
83-
"""
84-
if type(value) is not tuple or len(value) != 2 or \
85-
type(value[0]) is not int or value[0] < 0 or \
86-
type(value[1]) is not int or value[1] < 0:
35+
""""set position"""
36+
if (type(value) is not tuple):
37+
raise TypeError("position must be a tuple of 2 positive integers")
38+
elif (len(value) != 2):
39+
raise TypeError("position must be a tuple of 2 positive integers")
40+
elif (type(value[0]) is not int) or (type(value[1]) is not int):
41+
raise TypeError("position must be a tuple of 2 positive integers")
42+
elif (value[0] < 0) or (value[1] < 0):
8743
raise TypeError("position must be a tuple of 2 positive integers")
8844
else:
8945
self.__position = value
46+
47+
def area(self):
48+
""""get area of the square"""
49+
return self.size ** 2
50+
51+
def my_print(self):
52+
"""print the square"""
53+
if self.size == 0:
54+
print()
55+
else:
56+
for i in range(self.position[1]):
57+
print("")
58+
for i in range(self.size):
59+
print(" " * self.position[0], end="")
60+
print("#" * self.size)

0 commit comments

Comments
 (0)