Skip to content

Commit f7bbb7a

Browse files
committed
Add example dir
1 parent 7f45486 commit f7bbb7a

9 files changed

+289
-0
lines changed
File renamed without changes.

examples/2_1_animal_class.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Animal(ABC):
5+
def speed(self):
6+
pass
7+
8+
9+
class Lion(Animal):
10+
def speed(self):
11+
print("The maximum speed: 80km/h")
12+
13+
14+
class Cheeta(Animal):
15+
def speed(self):
16+
print("The maximum speed: 125 km/h")
File renamed without changes.

examples/3_1_rectangle_class.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Rectangle:
2+
def __init__(self, length, width):
3+
self.length = length
4+
self.width = width
5+
6+
def area(self):
7+
return self.length * self.width
8+
9+
def perimeter(self):
10+
return 2 * (self.length + self.width)
11+
12+
def get_detail(self):
13+
return f"Length: {self.length}, Width: {self.width}"
14+
15+
16+
rect1 = Rectangle(5, 4)
17+
print(rect1.area())
18+
print(rect1.perimeter())
19+
print(rect1.get_detail())
File renamed without changes.

examples/3_3_rectangle_class.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# @CodingMantras
2+
class Rectangle:
3+
# Class level variables, shared by all the instances.
4+
num_of_sides = 4
5+
num_of_diagonals = 2
6+
7+
def __init__(self, length, width):
8+
# Instance level variables. Different for all the instances.
9+
# self.length and self.width are instance variables
10+
self.length = length
11+
self.width = width
12+
13+
def area(self):
14+
"""Instance method to calculate the area of individual instance of rectangle
15+
class on the basis of instance variables (self.length and self.width).
16+
"""
17+
return self.length * self.width
18+
19+
def perimeter(self):
20+
"""Instance method to calculate the perimeter of individual instance.
21+
"""
22+
return 2 * (self.length + self.width)
23+
24+
@classmethod
25+
def instantiate_square(cls, side):
26+
"""Using class method as an alternate constructor.
27+
28+
Args:
29+
side (int): side of a square
30+
31+
Returns:
32+
class_instance: Rectangle class instance with both sides equal.
33+
"""
34+
return cls(side, side)
35+
36+
@classmethod
37+
def is_quadrilateral(cls):
38+
# Class method to manipulate a class variable
39+
return cls.num_of_sides == 4
40+
41+
@staticmethod
42+
def rectangle_formulae():
43+
# Static method:
44+
# Display information related to a rectangle. But does not manipulate or work-upon
45+
# any instance or class variables here.
46+
print("Area: length X width")
47+
print("Perimeter: 2(length + width)")
48+
print("Diagonal: square_root(square_of_length + square_of_width)")
49+
50+
51+
# Instantiate an object of the Rectangle class.
52+
rec1 = Rectangle(5, 4)
53+
54+
# Get attributes.
55+
print(rec1.length) # Output: 5
56+
print(rec1.width) # Output: 4
57+
print(rec1.area()) # Output: 20
58+
print(rec1.perimeter()) # Output: 18
59+
60+
61+
# Instantiating a square from the rectangle class.
62+
# Well first of all this is not advisable to do it here in a
63+
# Rectangle class. But this is just for understanding classmethods as
64+
# an alternate constructor.
65+
sq1 = Rectangle.instantiate_square(4)
66+
print(sq1.length)
67+
print(sq1.area())
68+
print(sq1.perimeter())
69+
print(Rectangle.is_quadrilateral())
70+
print(Rectangle.rectangle_formulae())
71+
72+
# Note: - The code represented here is for tutorial and understanding
73+
# purpose only. Not a production-level code.
File renamed without changes.

examples/4_2_car_class.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
from ast import literal_eval
2+
3+
4+
class Car:
5+
'''A base car class.
6+
Store the values of color, year, price and dimension.
7+
Dimention attribute is not necessarily required to instentiate the car class.
8+
'''
9+
# class attribute... applied to all class instances
10+
discount_percentage = 0.1
11+
12+
def __init__(self, color, year, price, dimension=None):
13+
self.color = color
14+
self.year = year
15+
self.price = price
16+
17+
if dimension is None:
18+
self.dimension = (None, None, None)
19+
else:
20+
self.dimension = dimension
21+
assert isinstance(self.dimension, tuple)
22+
assert len(self.dimension) == 3
23+
24+
def display_details(self):
25+
'''Display car details. If not dimention then display none.'''
26+
car = {
27+
'color': self.color,
28+
'year': self.year,
29+
'price': self.price,
30+
'dimension': self.dimension,
31+
}
32+
return car
33+
34+
@property
35+
def discount_amount(self):
36+
'''An attribute of Car class calculated by a method (using property decorator)
37+
A pythonic way of getter.
38+
'''
39+
return self.price * self.discount_percentage
40+
41+
@property
42+
def length(self):
43+
return self.dimension[0]
44+
45+
@property
46+
def width(self):
47+
return self.dimension[1]
48+
49+
@property
50+
def height(self):
51+
return self.dimension[2]
52+
53+
def discount_price(self):
54+
return self.price - self.discount_amount
55+
56+
def is_smallcar(self):
57+
decider_length = 3800
58+
return True if self.length < decider_length else False
59+
60+
@classmethod
61+
def set_discount_percentage(cls, discount_percentage):
62+
cls.discount_percentage = discount_percentage
63+
64+
@classmethod
65+
def from_file(cls, file_data):
66+
created_classes = []
67+
for line in file_data:
68+
year, price, dimension, color = int(line[0]), int(
69+
line[1]), tuple(literal_eval(line[2])), line[3]
70+
created_classes.append(cls(color, year, price, dimension))
71+
72+
return created_classes
73+
74+
75+
class DieselCar(Car):
76+
fuel_type = 'Diesel'
77+
78+
def __init__(self, color, year, price, dimension=None, injector=4):
79+
super().__init__(color, year, price, dimension)
80+
self.injector = injector
81+
82+
83+
class ElectricCar(Car):
84+
pass
85+
86+
87+
if __name__ == '__main__':
88+
dcar_1 = DieselCar('white', 2023, 1500000, (4500, 1200, 1300), 4)
89+
# car1 = Car('black', 2022, 1000000, (4500, 1100, 1200))
90+
# car2 = Car('grey', 2022, 1100000, (3700, 1000, 1100))
91+
# print(car1.discount_percentage)
92+
# print(car2.discount_percentage)
93+
94+
# Car.set_discount_percentage(0.15)
95+
# print(car1.discount_percentage)
96+
97+
# with open('file_data.txt', 'r') as fl:
98+
# lines = fl.readlines()
99+
# lines = [line.rstrip('\n').split("-") for line in lines]
100+
# print(lines)
101+
# created_objects = Car.from_file(lines)
102+
103+
# for obj in created_objects:
104+
# print(obj.display_details())

examples/code_examples.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
class Point:
2+
def __init__(self, x, y):
3+
self._x = x
4+
self._y = y
5+
6+
def get_x(self):
7+
return self._x
8+
9+
def set_x(self, value):
10+
self._x = value
11+
12+
def get_y(self):
13+
return self._y
14+
15+
def set_y(self, value):
16+
self._y = value
17+
18+
19+
class Circle:
20+
def __init__(self, radius):
21+
self._radius = radius
22+
23+
def _get_radius(self):
24+
print("Get radius")
25+
return self._radius
26+
27+
def _set_radius(self, value):
28+
print("Set radius")
29+
self._radius = value
30+
31+
def _del_radius(self):
32+
print("Delete radius")
33+
del self._radius
34+
35+
radius = property(
36+
_get_radius,
37+
_set_radius,
38+
_del_radius,
39+
"The radius properties of the circle class"
40+
)
41+
42+
43+
class Circle2:
44+
def __init__(self, radius):
45+
self._radius = radius
46+
47+
@property
48+
def radius(self):
49+
print("Get radius")
50+
return self._radius
51+
52+
@radius.setter
53+
def radius(self, value):
54+
print("Set radius")
55+
self._radius = value
56+
57+
@radius.deleter
58+
def radius(self):
59+
print("Delete radius")
60+
del self._radius
61+
62+
63+
if __name__ == '__main__':
64+
65+
c = Circle(5)
66+
print(c.radius)
67+
c.radius = 50
68+
print(c.radius)
69+
# del c.radius
70+
# print(c.radius)
71+
print(dir(c))
72+
73+
p = Point(4, 5)
74+
print(p.get_x())
75+
76+
p.set_x(44)
77+
print(p.get_x())

0 commit comments

Comments
 (0)