In this project, I began practicing object-oriented programming using classes and objects in Python. I learned about attributes, methods, and properties as well as data abstraction, data encapsulation, and information hiding.
- tests: Folder of test files. Provided by Holberton School.
-
0. My first square
- 0-square.py: Python class
Square
that defines a square.
- 0-square.py: Python class
-
1. Square with size
- 1-square.py: Python class
Square
that defines a square. Builds on 0-square.py with:- Private instance attribute
size
. - Instantiation with
size
.
- Private instance attribute
- 1-square.py: Python class
-
2. Size validation
- 2-square.py: Python class
Square
that defines a square. Builds on 1-square.py with:- Instantiation with optional
size
:def __init__(self, size=0):
- Instantiation with optional
- If a provided
size
attribute is not an integer, aTypeError
exception is raised with the messagemust be an integer
. - If a provided
size
attribute is less than0
, aValueError
exception is raised with the messagesize must be >= 0
.
- 2-square.py: Python class
-
3. Area of a square
- 3-square.py: Python class
Square
that defines a square. Builds on 2-square.py with:- Public instance attribute
def area(self):
that returns the current square area.
- Public instance attribute
- 3-square.py: Python class
-
4. Access and update private attribute
- 4-square.py: Python class
Square
that defines a square. Builds on 3-square.py with:- Property
def size(self):
to retrieve the private instance attributeself
. - Property setter
def size(self, value):
to setself
.
- Property
- 4-square.py: Python class
-
5. Printing a square
- 5-square.py: Python class
Square
that defines a square. Builds on 4-square.py with:- Public instance method
def my_print(self):
that prints the square with the character#
to standard output (ifsize
== 0 -> prints an empty line).
- Public instance method
- 5-square.py: Python class
-
6. Coordinates of a square
- 6-square.py: Python class
Square
that defines a square. Builds on 5-square.py with:- Private instance attribute
position
. - Property
def position(self):
to retreiveposition
. - Property setter
def position(self, value):
to setposition
. - Instantiation with optional
size
andposition
:def __init__(self, size=0, position=(0, 0)):
- Private instance attribute
- If a provided
position
attribute is not a tuple of two integers, aTypeError
exception is raised with the messageposition must be a tuple of 2 positive integers
.
- 6-square.py: Python class
-
7. Singly linked list
- 100-singly_linked_list.py: Python classes
Node
andSinglyLinkedList
that define a node of a singly-linked list and a singly-linked list. The classNode
is defined with:- Private instance attribute
data
. - Property
def data(self):
to setdata
. - Property setter
def data(self, value):
to setdata
. - Private instance attribute
next_node
. - Property
def next_node(self):
to setnext_node
. - Property
def next_node(self, value):
to setnext_node
. - Instantiation with
data
andnext_node
:def __init__(self, data, next_node=None):
- Private instance attribute
- If a provided
data
attribute is not an integer, aTypeError
exception is raised with the messagedata must be an integer
. - If a provided
next_node
attribute is not aNode
orNone
, aTypeError
exception is raised with the messagenext_node must be a Node object
. - The class
SinglyLinkedList
is defined with:- Private instance attribute
head
. - Instantiation
def __init__(self):
- Public instance method
def sorted_insert(self, value):
that inserts a newNode
into the correct sorted position in the list increasing order).
- Private instance attribute
- 100-singly_linked_list.py: Python classes
-
8. Print Square instance
- 101-square.py: Python class
Square
that defines a square. Builds on 6-square.py with:- Method
__str__
to set printing of aSquare
instance equivalent tomy_print()
.
- Method
- 101-square.py: Python class
-
9. Compare 2 squares
- 102-square.py: Python class
Square
that defines a square. Builds on 101-square.py with:- Methods
__eq__
,__ne__
,__lt__
,__le__
,__gt__
, and__ge__
, to enable usage ofSquare
instances with logical operators==
,!=
,<
,<=
,>
, and>=
, respectively, based on the square area.
- Methods
- 102-square.py: Python class
-
10. ByteCode -> Python #5
- 103-magic_class.py: Python function matching exactly a bytecode provided.