0x06 Python Classes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

0x06-python-classes

README.md

Python - Classes and Objects


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.

=============================
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."""

def __init__(self, size):


"""Initialize a new 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."""

def __init__(self, size=0):


"""Initialize a new 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."""

def __init__(self, size=0):


"""Initialize a new 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."""

def __init__(self, size=0):


"""Initialize a new 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."""

def __init__(self, size):


"""Initialize a new 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."""

def __init__(self, size=0, position=(0, 0)):


"""Initialize a new 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

[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)]
print("")

=============================
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

def sorted_insert(self, value):


"""Insert a new Node to the SinglyLinkedList.

The node is inserted into the list at the correct


ordered numerical position.

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."""

def __init__(self, size=0, position=(0, 0)):


"""Initialize a new 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

[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)]
print("")

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."""

def __init__(self, size=0):


"""Initialize a new 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 __eq__(self, other):


"""Define the == comparision to a Square."""
return self.area() == other.area()

def __ne__(self, other):


"""Define the != comparison to a Square."""
return self.area() != other.area()

def __lt__(self, other):


"""Define the < comparison to a Square."""
return self.area() < other.area()

def __le__(self, other):


"""Define the <= comparison to a Square."""
return self.area() <= other.area()

def __gt__(self, other):


"""Define the > comparison to a Square."""
return self.area() > other.area()

def __ge__(self, other):


"""Define the >= compmarison to a Square."""
return self.area() >= other.area()

=============================
103-magic_class.py

#!/usr/bin/python3
"""Define a MagicClass matching exactly a bytecode provided by Holberton."""

import math

class MagicClass:
"""Represent a circle."""

def __init__(self, radius=0):


"""Initialize a MagicClass.

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)

=============================

You might also like