Skip to content

Commit f9dfb37

Browse files
committed
Fixing errors
1 parent 351cac4 commit f9dfb37

11 files changed

+119
-83
lines changed

0x0A-python-inheritance/10-square.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#!/usr/bin/python3
2+
"""
3+
Contains the class BaseGeometry and subclass Rectangle
4+
"""
5+
26
Rectangle = __import__('9-rectangle').Rectangle
37

48

59
class Square(Rectangle):
6-
""" Class that defines a Square from Rectangle class """
10+
"""A representation of a square"""
711
def __init__(self, size):
8-
""" Method that initializes a Square """
12+
"""instantiation of the square"""
913
self.integer_validator("size", size)
1014
self.__size = size
11-
super().__init__(self.__size, self.__size)
15+
super().__init__(size, size)
1216

1317
def area(self):
14-
""" Method that returns a string with the area """
15-
return super().area()
18+
""""returns the area of the square"""
19+
return self.__size ** 2

0x0A-python-inheritance/100-my_int.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
#!/usr/bin/python3
2+
"""
3+
Contains the class MyInt
4+
"""
5+
6+
27
class MyInt(int):
3-
""" Class that inherits from class int"""
8+
"""rebel version of an integer, perfect for opposite day!"""
9+
def __new__(cls, *args, **kwargs):
10+
"""create a new instance of the class"""
11+
return super(MyInt, cls).__new__(cls, *args, **kwargs)
412

513
def __eq__(self, other):
6-
""" Method that returns != check """
7-
return int.__ne__(self, other)
14+
"""what was != is now =="""
15+
return int(self) != other
816

917
def __ne__(self, other):
10-
""" Method that returns == check """
11-
return int.__eq__(self, other)
18+
"""what was == is now !="""
19+
return int(self) == other
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
#!/usr/bin/python3
2-
def add_attribute(obj, name, value):
3-
""" Function that adds a new attribute to an object
2+
""" A attribute adding module """
43

5-
Args:
6-
obj: object
7-
name: attribute name
8-
value: attribute value
94

10-
Raises:
11-
TypeError: when the attribute can't be added
12-
13-
"""
14-
15-
if not hasattr(obj, "__dict__"):
5+
def add_attribute(a_class, name, value):
6+
""" Adds a new attribute to an object if it’s possible """
7+
if hasattr(a_class, "__dict__"):
8+
setattr(a_class, name, value)
9+
else:
1610
raise TypeError("can't add new attribute")
17-
setattr(obj, name, value)

0x0A-python-inheritance/11-square.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,53 @@
11
#!/usr/bin/python3
2-
Rectangle = __import__('9-rectangle').Rectangle
2+
"""
3+
Contains the class BaseGeometry and subclass Rectangle
4+
"""
5+
6+
7+
class BaseGeometry:
8+
"""A class with public instance methods area and integer_validator"""
9+
def area(self):
10+
"""raises an exception when called"""
11+
raise Exception("area() is not implemented")
12+
13+
def integer_validator(self, name, value):
14+
"""validates that value is an integer greater than 0"""
15+
if type(value) is not int:
16+
raise TypeError("{:s} must be an integer".format(name))
17+
if value <= 0:
18+
raise ValueError("{:s} must be greater than 0".format(name))
19+
20+
21+
class Rectangle(BaseGeometry):
22+
"""A representation of a rectangle"""
23+
def __init__(self, width, height):
24+
"""instantiation of the rectangle"""
25+
self.integer_validator("width", width)
26+
self.__width = width
27+
self.integer_validator("height", height)
28+
self.__height = height
29+
30+
def area(self):
31+
"""returns the area of the rectangle"""
32+
return self.__width * self.__height
33+
34+
def __str__(self):
35+
"""informal string representation of the rectangle"""
36+
return "[Rectangle] {:d}/{:d}".format(self.__width, self.__height)
337

438

539
class Square(Rectangle):
6-
""" Class that defines a Square from Rectangle class """
40+
"""A representation of a square"""
741
def __init__(self, size):
8-
""" Method that initializes a Square """
42+
"""instantiation of the square"""
943
self.integer_validator("size", size)
1044
self.__size = size
11-
super().__init__(self.__size, self.__size)
45+
super().__init__(size, size)
1246

1347
def area(self):
14-
""" Method that returns a string with the area """
15-
return super().area()
48+
""""returns the area of the square"""
49+
return self.__size ** 2
1650

1751
def __str__(self):
18-
""" Special method that returns a printable string """
19-
return "[Square] {}/{}".format(self.__size, self.__size)
52+
"""informal string reepresentation of the square"""
53+
return "[Square] {:d}/{:d}".format(self.__size, self.__size)
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
#!/usr/bin/python3
2-
def is_same_class(obj, a_class):
3-
""" Function that returns True/False if obj is a type of a_class
2+
"""
3+
This module contains the function is_same_class
4+
"""
45

5-
Args:
6-
obj: object
7-
a_class: class type
86

9-
Returns:
10-
True if type of obj is a_class
11-
False, otherwise
12-
"""
13-
return type(obj) is a_class
7+
def is_same_class(obj, a_class):
8+
"""return true if obj is the exact class a_class, otherwise false"""
9+
return (type(obj) == a_class)
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
#!/usr/bin/python3
2-
def is_kind_of_class(obj, a_class):
3-
""" Function that returns True/False if obj is an instance of a_class
2+
"""
3+
Contains the is_kind_of_class function
4+
"""
45

5-
Args:
6-
obj: object
7-
a_class: class type
86

9-
Returns:
10-
True if obj is an instance of a_class
11-
False, otherwise
12-
"""
13-
return isinstance(obj, a_class)
7+
def is_kind_of_class(obj, a_class):
8+
"""True if obj is an instance or inherited from a_class, else False"""
9+
return (isinstance(obj, a_class))
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
#!/usr/bin/python3
2-
def inherits_from(obj, a_class):
3-
""" Function that returns True/False if obj is an instance of a_class
2+
"""
3+
Contains the inherits_from function
4+
"""
45

5-
Args:
6-
obj: object
7-
a_class: class type
86

9-
Returns:
10-
True if obj is an instance of a_class
11-
False, otherwise
12-
"""
13-
if type(obj) is a_class:
14-
return False
15-
return isinstance(obj, a_class)
7+
def inherits_from(obj, a_class):
8+
"""returns true if obj is a subclass of a_class, otherwise false"""
9+
#!/usr/bin/python3
10+
return(issubclass(type(obj), a_class) and type(obj) != a_class)
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#!/usr/bin/python3
2+
"""
3+
Contains the class BaseGeometry
4+
"""
5+
6+
27
class BaseGeometry:
3-
""" Empty class """
8+
"""An empty class"""
49
pass
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#!/usr/bin/python3
2+
"""
3+
Contains the class BaseGeometry
4+
"""
5+
6+
27
class BaseGeometry:
3-
""" Empty class """
8+
"""A class with public attribute area"""
49
def area(self):
10+
"""raises an exception when called"""
511
raise Exception("area() is not implemented")
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
#!/usr/bin/python3
2-
class BaseGeometry:
3-
""" Class that defines the attributes of Geometric Shapes """
2+
"""
3+
Contains the class BaseGeometry
4+
"""
45

5-
def area(self):
6-
""" Method that defines the area of a geomtric shape """
76

7+
class BaseGeometry:
8+
"""A class with public instance methods area and integer_validator"""
9+
def area(self):
10+
"""raises an exception when called"""
811
raise Exception("area() is not implemented")
912

1013
def integer_validator(self, name, value):
11-
""" Method that recieves the value property
12-
13-
Árgs:
14-
name: name of the object
15-
value: value of the property
16-
17-
"""
18-
14+
"""validates that value is an integer greater than 0"""
1915
if type(value) is not int:
20-
raise TypeError("{} must be an integer".format(name))
16+
raise TypeError("{:s} must be an integer".format(name))
2117
if value <= 0:
22-
raise ValueError("{} must be greater than 0".format(name))
18+
raise ValueError("{:s} must be greater than 0".format(name))
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#!/usr/bin/python3
2+
"""
3+
Contains the class BaseGeometry and subclass Rectangle
4+
"""
5+
26
BaseGeometry = __import__('7-base_geometry').BaseGeometry
37

48

59
class Rectangle(BaseGeometry):
6-
""" Class that defines a rectangle from BaseGeometry Class """
7-
10+
"""A representation of a rectangle"""
811
def __init__(self, width, height):
9-
""" Initializes instance """
12+
"""instantiation of the rectangle"""
1013
self.integer_validator("width", width)
11-
self.integer_validator("height", height)
1214
self.__width = width
15+
self.integer_validator("height", height)
1316
self.__height = height

0 commit comments

Comments
 (0)