NOTE: FOR TEST 0 TO CHECK, YOU HAVE TO TEST FILES USING THE COMMAND python3 -m unittest tests/test_models/test_base.py
In this project, I encapsulated skills in Python object-oriented programming by coding three connected classes to represent rectangles and squares. I wrote my own test suite using the unittest
module to test all functionality for each class.
The three classes involved utilizing the following Python tools:
- Import
- Exceptions
- Private attributes
- Getter/setter
- Class/static methods
- Inheritance
- File I/O
args
/kwargs
- JSON and CSV serialization/deserialization
- Unittesting
- tests/test_models: Folder containing the following independently-developed test files:
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
id
isNone
, increments__nb_objects
and assigns its value to the public instance attributeid
. - Otherwise, sets the public instance attribute
id
to the providedid
.
- If
- Static method
def to_json_string(list_dictionaries):
that returns the JSON string serialization of a list of dictionaries.- If
list_dictionaries
isNone
or empty, returns the string"[]"
.
- If
- Class method
def save_to_file(cls, list_objs):
that writes the JSON serialization of a list of objects to a file.- The parameter
list_objs
is expected to be a list ofBase
-inherited instances. - If
list_objs
isNone
, the function saves an empty list. - The file is saved with the name
<cls name>.json
(ie.Rectangle.json
) - Overwrites the file if it already exists.
- The parameter
- Static method
def from_json_string(json_string):
that returns a list of objects deserialized from a JSON string.- The parameter
json_string
is expected to be a string representing a list of dictionaries. - If
json_string
isNone
or empty, the function returns an empty list.
- The parameter
- Class method
def create(cls, **dictionary):
that instantiates an object with provided attributes.- 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 objects instantiated from a JSON file.- Reads from the JSON file
<cls name>.json
(ie.Rectangle.json
) - If the file does not exist, the function returns an empty list.
- Reads from the JSON file
- Class method
def save_to_file_csv(cls, list_objs):
that writes the CSV serialization of a list of objects to a file.- The parameter
list_objs
is expected to be a list ofBase
-inherited instances. - If
list_objs
isNone
, the function saves an empty list. - The file is saved with the name
<cls name>.csv
(ie.Rectangle.csv
) - Serializes objects in the format
<id>,<width>,<height>,<x>,<y>
forRectangle
objects and<id>,<size>,<x>,<y>
forSquare
objects.
- The parameter
- Class method
def load_from_file_csv(cls):
that returns a list of objects instantiated from a CSV file.- Reads from the CSV file
<cls name>.csv
(ie.Rectangle.csv
) - If the file does not exist, the function returns an empty list.
- Reads from the CSV file
- Static method
def draw(list_rectangles, list_squares):
that drawsRectangle
andSquare
instances in a GUI window using theturtle
module.- The parameter
list_rectangles
is expected to be a list ofRectangle
objects to print. - The parameter
list_squares
is expected to be a list ofSquare
objects to print.
- The parameter
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
, ory
is not an integer, raises aTypeError
exception with the message<attribute> must be an integer
. - If either of
width
orheight
is >= 0, raises aValueError
exception with the message<attribute> must be > 0
. - If either of
x
ory
is less than 0, raises aValueError
exception with the message<attribute> must be >= 0
.
- If either of
- Public method
def area(self):
that returns the area of theRectangle
instance. - Public method
def display(self):
that prints theRectangle
instance tostdout
using the#
character.- Prints new lines for the
y
coordinate and spaces for thex
coordinate.
- Prints new lines for the
- Overwrite of
__str__
method to print aRectangle
instance in the format[Rectangle] (<id>) <x>/<y>
. - Public method
def update(self, *args, **kwargs):
that updates an instance of aRectangle
with the given attributes.*args
must be supplied in the following order:- 1st:
id
- 2nd:
width
- 3rd:
height
- 4th:
x
- 5th:
y
- 1st:
**kwargs
is expected to be a double pointer to a dictionary of new key/value attributes to update theRectangle
with.**kwargs
is skipped if*args
exists.
- Public method
def to_dictionary(self):
that returns the dictionary representation of aRectangle
instance.
Represents a square. Inherits from Rectangle
with:
- Class constructor
def __init__(self, size, x=0, y=0, id=None):
- The
width
andheight
of theRectangle
superclass are assigned using the value ofsize
.
- The
- Overwrite of
__str__
method to print aSquare
instance in the format[Square] (<id>) <x>/<y>
. - Public method
def update(self, *args, **kwargs):
that updates an instance of aSquare
with the given attributes.*args
must be supplied in the following order:- 1st:
id
- 2nd:
size
- 3rd:
x
- 4th:
y
- 1st:
**kwargs
is expected to be a double pointer to a dictoinary of new key/value attributes to update theSquare
with.**kwargs
is skipped if*args
exists.
- Public method
def to_dictionary(self):
that returns the dictionary representation of aSquare
.