0x06 Python Classes
0x06 Python Classes
0x06 Python Classes
README.md
=============================
0-square.py
#!/usr/bin/python3
"""Define a class Square."""
class Square:
"""Represent a square."""
pass
=============================
1-square.py
#!/usr/bin/python3
"""Define a class Square."""
class Square:
"""Represent a square."""
Args:
size (int): The size of the new square.
"""
self.__size = size
=============================
2-square.py
#!/usr/bin/python3
"""Define a class Square."""
class Square:
"""Represent a square."""
Args:
size (int): The size of the new square.
"""
if not isinstance(size, int):
raise TypeError("size must be an integer")
elif size < 0:
raise ValueError("size must be >= 0")
self.__size = size
=============================
3-square.py
#!/usr/bin/python3
"""Define a class Square."""
class Square:
"""Represent a square."""
Args:
size (int): The size of the new square.
"""
if not isinstance(size, int):
raise TypeError("size must be an integer")
elif size < 0:
raise ValueError("size must be >= 0")
self.__size = size
def area(self):
"""Return the current area of the square."""
return (self.__size * self.__size)
=============================
4-square.py
#!/usr/bin/python3
"""Define a class Square."""
class Square:
"""Represent a square."""
Args:
size (int): The size of the new square.
"""
self.size = size
@property
def size(self):
"""Get/set the current size of the square."""
return (self.__size)
@size.setter
def size(self, value):
if not isinstance(value, int):
raise TypeError("size must be an integer")
elif value < 0:
raise ValueError("size must be >= 0")
self.__size = value
def area(self):
"""Return the current area of the square."""
return (self.__size * self.__size)
=============================
5-square.py
#!/usr/bin/python3
"""Define a class Square."""
class Square:
"""Represent a square."""
Args:
size (int): The size of the new square.
"""
self.size = size
@property
def size(self):
"""Get/set the current size of the square."""
return (self.__size)
@size.setter
def size(self, value):
if not isinstance(value, int):
raise TypeError("size must be an integer")
elif value < 0:
raise ValueError("size must be >= 0")
self.__size = value
def area(self):
"""Return the current area of the square."""
return (self.__size * self.__size)
def my_print(self):
"""Print the square with the # character."""
for i in range(0, self.__size):
[print("#", end="") for j in range(self.__size)]
print("")
if self.__size == 0:
print("")
=============================
6-square.py
#!/usr/bin/python3
"""Define a class Square."""
class Square:
"""Represent a square."""
Args:
size (int): The size of the new square.
position (int, int): The position of the new square.
"""
self.size = size
self.position = position
@property
def size(self):
"""Get/set the current size of the square."""
return (self.__size)
@size.setter
def size(self, value):
if not isinstance(value, int):
raise TypeError("size must be an integer")
elif value < 0:
raise ValueError("size must be >= 0")
self.__size = value
@property
def position(self):
"""Get/set the current position of the square."""
return (self.__position)
@position.setter
def position(self, value):
if (not isinstance(value, tuple) or
len(value) != 2 or
not all(isinstance(num, int) for num in value) or
not all(num >= 0 for num in value)):
raise TypeError("position must be a tuple of 2 positive integers")
self.__position = value
def area(self):
"""Return the current area of the square."""
return (self.__size * self.__size)
def my_print(self):
"""Print the square with the # character."""
if self.__size == 0:
print("")
return
=============================
100-singly_linked_list.py
#!/usr/bin/python3
"""Define classes for a singly-linked list."""
class Node:
"""Represent a node in a singly-linked list."""
def __init__(self, data, next_node=None):
"""Initialize a new Node.
Args:
data (int): The data of the new Node.
next_node (Node): The next node of the new Node.
"""
self.data = data
self.next_node = next_node
@property
def data(self):
"""Get/set the data of the Node."""
return (self.__data)
@data.setter
def data(self, value):
if not isinstance(value, int):
raise TypeError("data must be an integer")
self.__data = value
@property
def next_node(self):
"""Get/set the next_node of the Node."""
return (self.__next_node)
@next_node.setter
def next_node(self, value):
if not isinstance(value, Node) and value is not None:
raise TypeError("next_node must be a Node object")
self.__next_node = value
class SinglyLinkedList:
"""Represent a singly-linked list."""
def __init__(self):
"""Initalize a new SinglyLinkedList."""
self.__head = None
Args:
value (Node): The new Node to insert.
"""
new = Node(value)
if self.__head is None:
new.next_node = None
self.__head = new
elif self.__head.data > value:
new.next_node = self.__head
self.__head = new
else:
tmp = self.__head
while (tmp.next_node is not None and
tmp.next_node.data < value):
tmp = tmp.next_node
new.next_node = tmp.next_node
tmp.next_node = new
def __str__(self):
"""Define the print() representation of a SinglyLinkedList."""
values = []
tmp = self.__head
while tmp is not None:
values.append(str(tmp.data))
tmp = tmp.next_node
return ('\n'.join(values))
=============================
101-square.py
#!/usr/bin/python3
"""Define a class Square."""
class Square:
"""Represent a square."""
Args:
size (int): The size of the new square.
position (int, int): The position of the new square.
"""
self.size = size
self.position = position
@property
def size(self):
"""Get/set the current size of the square."""
return (self.__size)
@size.setter
def size(self, value):
if not isinstance(value, int):
raise TypeError("size must be an integer")
elif value < 0:
raise ValueError("size must be >= 0")
self.__size = value
@property
def position(self):
"""Get/set the current position of the square."""
return (self.__position)
@position.setter
def position(self, value):
if (not isinstance(value, tuple) or
len(value) != 2 or
not all(isinstance(num, int) for num in value) or
not all(num >= 0 for num in value)):
raise TypeError("position must be a tuple of 2 positive integers")
self.__position = value
def area(self):
"""Return the current area of the square."""
return (self.__size * self.__size)
def my_print(self):
"""Print the square with the # character."""
if self.__size == 0:
print("")
return
def __str__(self):
"""Define the print() representation of a Square."""
if self.__size != 0:
[print("") for i in range(0, self.__position[1])]
for i in range(0, self.__size):
[print(" ", end="") for j in range(0, self.__position[0])]
[print("#", end="") for k in range(0, self.__size)]
if i != self.__size - 1:
print("")
return ("")
=============================
102-square.py
#!/usr/bin/python3
"""Define a class Square."""
class Square:
"""Represent a square."""
Args:
size (int): The size of the new square.
"""
self.size = size
@property
def size(self):
"""Get/set the current size of the square."""
return (self.__size)
@size.setter
def size(self, value):
if not isinstance(value, int):
raise TypeError("size must be an integer")
elif value < 0:
raise ValueError("size must be >= 0")
self.__size = value
def area(self):
"""Return the current area of the square."""
return (self.__size * self.__size)
=============================
103-magic_class.py
#!/usr/bin/python3
"""Define a MagicClass matching exactly a bytecode provided by Holberton."""
import math
class MagicClass:
"""Represent a circle."""
Arg:
radius (float or int): The radius of the new MagicClass.
"""
self.__radius = 0
if type(radius) is not int and type(radius) is not float:
raise TypeError("radius must be a number")
self.__radius = radius
def area(self):
"""Return the area of the MagicClass."""
return (self.__radius ** 2 * math.pi)
def circumference(self):
"""Return The circumference of the MagicClass."""
return (2 * math.pi * self.__radius)
=============================