Skip to content

Commit 1bf2c44

Browse files
committed
Complete programming objects section
1 parent cc106f1 commit 1bf2c44

File tree

3 files changed

+154
-6
lines changed

3 files changed

+154
-6
lines changed

fundamentals/custom_functions.py

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,67 @@
11
# Author: Nguyen Truong Thinh
22
# Contact me: [email protected] || +84393280504
3+
#
4+
# Custom functions section
5+
6+
import os
7+
import pickle
38
from math import sqrt
4-
import pickle, os
59

610

7-
# Custom functions
11+
def indentify(an_object):
12+
"""
13+
[The final cornerstone principle of polymorphism].
14+
A function to take an object of any type & call that object's methods
15+
:param an_object: an object of any type (class, ...)
16+
:return: None
17+
:exception: A run-time error
18+
"""
19+
an_object.who_am_i()
20+
21+
22+
def exam_built_in_attributes(an_object):
23+
"""
24+
Examining built-in attributes
25+
:param an_object: An instance of class
26+
:return: None
27+
"""
28+
print('\nBuilt-in instance attributes...')
29+
for attrib in dir(an_object):
30+
if attrib[0] == '_':
31+
print(attrib)
32+
33+
print("\nInstance Dictionary...")
34+
for i in an_object.__dict__:
35+
print(i, ": ", an_object.__dict__[i])
36+
37+
38+
def exam_built_in_class_dictionary(a_class):
39+
"""
40+
Examining built-in attributes
41+
:param a_class: A class type
42+
:return: None
43+
"""
44+
print('\nClass Dictionary...')
45+
for i in a_class.__dict__:
46+
print(i, ": ", a_class.__dict__[i])
47+
48+
49+
def addressing_class_instance_attributes(an_object):
50+
"""
51+
Addressing class attributes
52+
:param an_object: An instance of class
53+
:return: None
54+
"""
55+
print('\nClass instances of:\n', an_object.__doc__)
56+
print('\nClass instance attributes...')
57+
for attrib in dir(an_object):
58+
if attrib[0] != '_':
59+
print(attrib, ":", getattr(an_object, attrib))
60+
61+
862
def file_with_pickle_data(file_name):
963
"""
10-
Pickling data for more efficient to use a machine-readable binary file.
64+
Pickling data for more efficient to use a machine-readable binary file
1165
:param file_name: Name of the file
1266
:return: None
1367
"""
@@ -138,6 +192,6 @@ def tuple_distance(tuple1, tuple2):
138192
return sqrt((tuple1[0] - tuple2[0]) ** 2 + (tuple1[1] - tuple2[1]) ** 2)
139193

140194

141-
def print_basic_arithmetic(result = 'I would like to have a result of arithmetic expression please.'):
195+
def print_basic_arithmetic(result='I would like to have a result of arithmetic expression please.'):
142196
# Use a breakpoint in the code line below to debug your script.
143197
print(f'Result: {result}') # Press Ctrl+F8 to toggle the breakpoint.

main.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Use a breakpoint in the code line below to debug your script.
66
from fundamentals.custom_functions import *
77
from object_oriented_mindset.cartesian_system_coordinate import Point
8+
from object_oriented_mindset.polygon import *
89
from object_oriented_mindset.util_cartesian_system_coordinate import *
910

1011
# Press the green button in the gutter to run the script.
@@ -71,7 +72,6 @@
7172

7273
cube_nums = [i ** 3 for i in range(10)]
7374
print(cube_nums)
74-
7575
# Custom functions
7676
friends_stock_list = [info_dict_dev, info_dict_tester, info_dict_po, info_dict_boss]
7777
data = get_data_from_list(friends_stock_list)
@@ -87,7 +87,6 @@
8787
update_a_file_with_block(file_name, 'r+', content, 35)
8888
re_write_a_file(file_name)
8989
file_with_pickle_data('pickle.dat')
90-
9190
# Objects
9291
p_x = Point(1, 0)
9392
p_y = Point(5, 3)
@@ -97,4 +96,31 @@
9796
del p_x
9897
del p_y
9998

99+
rect = Rectangle()
100+
rect.name = 'Rectangles'
101+
indentify(rect)
102+
rect.set_values(4, 5)
103+
print("Rectangle Area: ", rect.area())
104+
rect.scale(2)
105+
Polygon.scale(rect, 2)
106+
print("Rectangle Area: ", rect.area())
107+
108+
tria = Triangle()
109+
tria.name = 'Triangles'
110+
indentify(tria)
111+
tria.set_values(4, 5)
112+
print("Triangle Area: ", tria.area())
113+
tria.scale(2)
114+
Polygon.scale(tria, 2)
115+
print("Triangle Area: ", tria.area())
116+
117+
print('The number of polygons: ', tria.count)
118+
119+
addressing_class_instance_attributes(tria)
120+
exam_built_in_attributes(tria)
121+
exam_built_in_class_dictionary(Triangle)
122+
123+
del rect
124+
del tria
125+
100126
# See PyCharm help at https://www.jetbrains.com/help/pycharm/

object_oriented_mindset/polygon.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Author: Nguyen Truong Thinh
2+
# Contact me: [email protected] || +84393280504
3+
# Programming objects:
4+
# Encapsulating data
5+
# Addressing class attributes
6+
# Built-in attributes (on fundamentals/custom_functions.py)
7+
# Collecting garbage
8+
# Inheriting features
9+
# Overriding base methods
10+
# Harnessing polymorphism
11+
12+
class Polygon:
13+
"""A base class to define Polygon properties"""
14+
width = 0
15+
height = 0
16+
count = 0
17+
18+
def __int__(self, name_of_polygon):
19+
"""
20+
Initializer function
21+
:param name_of_polygon: Name of the polygon
22+
:return: None
23+
"""
24+
self.name = name_of_polygon
25+
26+
def who_am_i(self):
27+
print('\nI\'m a polygon')
28+
29+
def set_values(self, width, height):
30+
Polygon.width = width
31+
Polygon.height = height
32+
Polygon.count += 1
33+
34+
def scale(self, number=1):
35+
print(self.name, ': Calling the base class -', number)
36+
37+
def __del__(self):
38+
"""
39+
A destructor method for confirmation when Polygon instances of
40+
class are destroyed.
41+
:return: None
42+
"""
43+
print(f'{self.name} Say Goodbye!')
44+
45+
46+
class Rectangle(Polygon):
47+
""" A derived class that inherits from Polygon class"""
48+
def area(self):
49+
return self.width * self.height
50+
51+
def scale(self, number):
52+
self.width = self.width * number
53+
self.height = self.height * number
54+
55+
def who_am_i(self):
56+
print('\nI\'m a rectangles')
57+
58+
59+
class Triangle(Polygon):
60+
""" A derived class that inherits from Polygon class"""
61+
def area(self):
62+
return (self.width * self.height) / 2
63+
64+
def scale(self, number):
65+
self.width = self.width * number
66+
67+
def who_am_i(self):
68+
print('\nI\'m a triangles')

0 commit comments

Comments
 (0)