This project was developed in object-oriented programming with three classes, a base and two other subclasses to represent rectangles and squares. Tests were also created for all classes using the unittest module.
This project contains these topics:
- Imports
- Exceptions
- Private attributes
- Getter/setter
- Class/static methods
- Inheritance
- File I/O
- *args and **kwargs
- JSON serialization/deserialization
- Unittesting
main_files/ - Main functions provided by Holberton School to test the files.
tests/test_models/: Test files usingunittest:
Base
base.py - Represents the "base" class for all other classes in the project. Includes:
- Private class attribute
__nb_objects = 0. - Public instance attribute
id. - Class constructor
def __init__(self, id=None).- If
idisNone, increments__nb_objectsand assigns its value to the public instance attributeid. - Otherwise, sets the public instance attribute
idto the providedid.
- If
- Static method
def to_json_string(list_dictionaries)that returns the JSON string representation oflist_dictionaries.- If
list_dictionariesisNoneor empty, returns the string"[]".
- If
- Class method
def save_to_file(cls, list_objs)that writes the JSON string representation oflist_objsto a file.- The parameter
list_objsis a list of instances who inherits ofBase. - If
list_objsisNone, saves an empty list. - The file is saved with the name
<classname>.json. - Overwrites the file if it already exists.
- The parameter
- Static method
def from_json_string(json_string)that returns the list of the JSON string representationjson_string.- The parameter
json_stringis a string representing a list of dictionaries. - If
json_stringisNoneor empty, the function returns an empty list.
- The parameter
- Class method
def create(cls, **dictionary)that returns an instance with all attributes already set.- Instantiates an object with the attributes given in
**dictionary.
- Instantiates an object with the attributes given in
- Class method
def load_from_file(cls)that returns a list of instances.- Reads from the JSON file
<classname>.json. - If the file doesn't exist, the function returns an empty list.
- Reads from the JSON file
Rectangle
rectangle.py - Represents a rectangle. Inherits from Base with:
- Private instance attributes
__width,__height,__x, and__y.- Each private instance attribute features its own getter/setter.
- Class constructor
def __init__(self, width, height, x=0, y=0, id=None).- If either of
width,height,x, oryis not an integer, raises aTypeErrorexception with the message<attribute> must be an integer. - If either of
widthorheightis <= 0, raises aValueErrorexception with the message<attribute> must be > 0. - If either of
xoryis less than 0, raises aValueErrorexception with the message<attribute> must be >= 0.
- If either of
- Public method
def area(self)that returns the area of theRectangleinstance. - Public method
def display(self)that prints theRectangleinstance tostdoutusing the character#.- Prints new lines for the
ycoordinate and spaces for thexcoordinate.
- Prints new lines for the
- Overwrite the
__str__method to returns aRectangleinstance in the format[Rectangle] (<id>) <x>/<y> - <width>/<height>. - Public method
def update(self, *args, **kwargs)that updates an instance of aRectanglewith the given attributes.*argsis supplied in the following order:- 1st:
id. - 2nd:
width. - 3rd:
height. - 4th:
x. - 5th:
y.
- 1st:
**kwargsis a dictionary of new key/value attributes to update theRectangleinstance.**kwargsis skipped if*argsexists.
- Public method
def to_dictionary(self)that returns the dictionary representation of aRectangleinstance.
Square
square.py - Represents a square. Inherits from Rectangle with:
- Class constructor
def __init__(self, size, x=0, y=0, id=None).- The
widthandheightof theRectanglesuperclass are assigned using the value ofsize.
- The
- Overwrite the
__str__method to print aSquareinstance in the format[Square] (<id>) <x>/<y> - <size>. - Public method
def update(self, *args, **kwargs)that updates an instance of aSquarewith the given attributes.*argsmust be supplied in the following order:- 1st:
id. - 2nd:
size. - 3rd:
x. - 4th:
y.
- 1st:
**kwargsis a dictoinary of new key/value attributes to update theSquareinstance.**kwargsis skipped if*argsexists.
- Public method
def to_dictionary(self)that returns the dictionary representation of aSquareinstance.
- Felipe Villamizar - GitHub