Read or watch:
- Allowed editors:
vi,vim,emacs - All your files will be interpreted/compiled on Ubuntu 20.04 LTS using python3 (version 3.8.5)
- All your files should end with a new line
- The first line of all your files should be exactly
#!/usr/bin/python3 - A
README.mdfile, at the root of the folder of the project, is mandatory - Your code should use the pycodestyle (version
2.8.*) - All your files must be executable
- The length of your files will be tested using
wc - All your modules should be documented:
python3 -c 'print(__import__("my_module").__doc__)' - All your classes should be documented:
python3 -c 'print(__import__("my_module").MyClass.__doc__)' - All your functions (inside and outside a class) should be documented:
python3 -c 'print(__import__("my_module").my_function.__doc__)'andpython3 -c 'print(__import__("my_module").MyClass.my_function.__doc__)' - A documentation is not a simple word, it’s a real sentence explaining what’s the purpose of the module, class or method (the length of it will be verified)
- Allowed editors:
vi,vim,emacs - All your files should end with a new line
- All your test files should be inside a folder
tests - You have to use the
unittestmodule - All your test files should be python files (extension:
.py) - All your test files and folders should start with
test_ - Your file organization in the tests folder should be the same as your project: ex: for
models/base.py, unit tests must be in:tests/test_models/test_base.py - All your tests should be executed by using this command:
python3 -m unittest discover tests - You can also test file by file by using this command:
python3 -m unittest tests/test_models/test_base.py - We strongly encourage you to work together on test cases so that you don’t miss any edge case
0. If it's not tested it doesn't work
All your files, classes and methods must be unit tested and be PEP 8 validated.
guillaume@ubuntu:~/$ python3 -m unittest discover tests
...................................................................................
...................................................................................
.......................
----------------------------------------------------------------------
Ran 189 tests in 13.135s
OK
guillaume@ubuntu:~/$
Note that this is just an example. The number of tests you create can be different from the above example.
Write the first class Base:
Create a folder named models with an empty file __init__.py inside - with this file, the folder will become a Python package
Create a file named models/base.py:
- Class
Base:- private class attribute
__nb_objects = 0 - class constructor:
def __init__(self, id=None)::- if
idis notNone, assign the public instance attributeidwith this argument value - you can assumeidis an integer and you don’t need to test the type of it - otherwise, increment
__nb_objectsand assign the new value to the public instance attributeid
- if
- private class attribute
This class will be the base of all other classes in this project. The goal of it is to manage id attribute in all your future classes and to avoid duplicating the same code (by extension, same bugs)
guillaume@ubuntu:~/$ cat 0-main.py
#!/usr/bin/python3
""" 0-main """
from models.base import Base
if __name__ == "__main__":
b1 = Base()
print(b1.id)
b2 = Base()
print(b2.id)
b3 = Base()
print(b3.id)
b4 = Base(12)
print(b4.id)
b5 = Base()
print(b5.id)
guillaume@ubuntu:~/$ ./0-main.py
1
2
3
12
4
guillaume@ubuntu:~/$
Write the class Rectangle that inherits from Base:
- In the file
models/rectangle.py - Class
Rectangleinherits fromBase - Private instance attributes, each with its own public
getterandsetter:__width->width__height->height__x->x__y->y
- Class constructor:
def __init__(self, width, height, x=0, y=0, id=None):- Call the super class with
id- this super call with use the logic of the__init__of theBaseclass - Assign each argument
width,height,xandyto the right attribute
- Call the super class with
Why private attributes with getter/setter? Why not directly public attribute?
Because we want to protect attributes of our class. With a setter, you are able to validate what a developer is trying to assign to a variable. So after, in your class you can “trust” these attributes.
guillaume@ubuntu:~/$ cat 1-main.py
#!/usr/bin/python3
""" 1-main """
from models.rectangle import Rectangle
if __name__ == "__main__":
r1 = Rectangle(10, 2)
print(r1.id)
r2 = Rectangle(2, 10)
print(r2.id)
r3 = Rectangle(10, 2, 0, 0, 12)
print(r3.id)
guillaume@ubuntu:~/$ ./1-main.py
1
2
12
guillaume@ubuntu:~/$
Update the class Rectangle by adding validation of all setter methods and instantiation (id excluded):
- If the input is not an integer, raise the
TypeErrorexception with the message:<name of the attribute> must be an integer. Example:width must be an integer - If
widthorheightis under or equals0, raise theValueErrorexception with the message:<name of the attribute> must be > 0. Example:width must be > 0 - If
xoryis under0, raise theValueErrorexception with the message:<name of the attribute> must be >= 0. Example:x must be >= 0
guillaume@ubuntu:~/$ cat 2-main.py
#!/usr/bin/python3
""" 2-main """
from models.rectangle import Rectangle
if __name__ == "__main__":
try:
Rectangle(10, "2")
except Exception as e:
print("[{}] {}".format(e.__class__.__name__, e))
try:
r = Rectangle(10, 2)
r.width = -10
except Exception as e:
print("[{}] {}".format(e.__class__.__name__, e))
try:
r = Rectangle(10, 2)
r.x = {}
except Exception as e:
print("[{}] {}".format(e.__class__.__name__, e))
try:
Rectangle(10, 2, 3, -1)
except Exception as e:
print("[{}] {}".format(e.__class__.__name__, e))
guillaume@ubuntu:~/$ ./2-main.py
[TypeError] height must be an integer
[ValueError] width must be > 0
[TypeError] x must be an integer
[ValueError] y must be >= 0
guillaume@ubuntu:~/$
Update the class Rectangle by adding the public method def area(self): that returns the area value of the Rectangle instance.
guillaume@ubuntu:~/$ cat 3-main.py
#!/usr/bin/python3
""" 3-main """
from models.rectangle import Rectangle
if __name__ == "__main__":
r1 = Rectangle(3, 2)
print(r1.area())
r2 = Rectangle(2, 10)
print(r2.area())
r3 = Rectangle(8, 7, 0, 0, 12)
print(r3.area())
guillaume@ubuntu:~/$ ./3-main.py
6
20
56
guillaume@ubuntu:~/$
Update the class Rectangle by adding the public method def display(self): that prints in stdout the Rectangle instance with the character # - you don’t need to handle x and y here.
guillaume@ubuntu:~/$ cat 4-main.py
#!/usr/bin/python3
""" 4-main """
from models.rectangle import Rectangle
if __name__ == "__main__":
r1 = Rectangle(4, 6)
r1.display()
print("---")
r1 = Rectangle(2, 2)
r1.display()
guillaume@ubuntu:~/$ ./4-main.py
####
####
####
####
####
####
---
##
##
guillaume@ubuntu:~/$
Update the class Rectangle by overriding the __str__ method so that it returns [Rectangle] (<id>) <x>/<y> - <width>/<height>
guillaume@ubuntu:~/$ cat 5-main.py
#!/usr/bin/python3
""" 5-main """
from models.rectangle import Rectangle
if __name__ == "__main__":
r1 = Rectangle(4, 6, 2, 1, 12)
print(r1)
r2 = Rectangle(5, 5, 1)
print(r2)
guillaume@ubuntu:~/$ ./5-main.py
[Rectangle] (12) 2/1 - 4/6
[Rectangle] (1) 1/0 - 5/5
guillaume@ubuntu:~/$
Update the class Rectangle by improving the public method def display(self): to print in stdout the Rectangle instance with the character # by taking care of x and y
guillaume@ubuntu:~/$ cat 6-main.py
#!/usr/bin/python3
""" 6-main """
from models.rectangle import Rectangle
if __name__ == "__main__":
r1 = Rectangle(2, 3, 2, 2)
r1.display()
print("---")
r2 = Rectangle(3, 2, 1, 0)
r2.display()
guillaume@ubuntu:~/$ ./6-main.py | cat -e
$
$
##$
##$
##$
---$
###$
###$
guillaume@ubuntu:~/$