Skip to content

Commit 3574fa1

Browse files
committed
Codes 4 W05
1 parent e6259fa commit 3574fa1

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

Week05/classes.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class ClassName:
2+
"""This is my first class."""
3+
pass
4+
5+
6+
class Student:
7+
def __init__(
8+
self,
9+
student_id: str,
10+
name: str,
11+
age: int
12+
) -> None:
13+
self.student_id = student_id
14+
self.name = name
15+
self.age = age
16+
self.courses = [] # self.courses = list()
17+
18+
def register(self, course):
19+
if course not in self.courses:
20+
self.courses.append(course)
21+
22+
def drop(self, course):
23+
if course in self.courses:
24+
self.courses.remove(course)
25+
26+
def __str__(self):
27+
return f"We have a student with the following information: {self.student_id}, {self.name}, {self.age}"
28+
29+
def __repr__(self):
30+
return f"Student(\"7\", \"Bora Canbula\", 39)"
31+
32+
33+
if __name__ == "__main__":
34+
object_name = ClassName()
35+
print(object_name)
36+
print(hex(id(object_name)))
37+
print(dir(object_name))
38+
print(object_name.__doc__)
39+
# print(help(object_name))
40+
print(object_name.__class__)
41+
print(object_name.__class__.__name__)
42+
print(object_name.__class__.__bases__)
43+
print(object_name.__class__.__module__)
44+
45+
print(isinstance(object_name, ClassName))
46+
print(isinstance(object_name, int))
47+
print(isinstance(object_name, object))
48+
49+
print(issubclass(ClassName, object))
50+
51+
"""
52+
a = 5
53+
print(isinstance(a, int))
54+
print(isinstance(a, object))
55+
"""
56+
57+
student = Student("7", "Bora Canbula", 39)
58+
print(student.student_id)
59+
print(student.name)
60+
print(student.age)
61+
print(student.courses)
62+
student.register("CSE 3244")
63+
print(student.courses)
64+
student.register("CSE 3237")
65+
print(student.courses)
66+
student.drop("CSE 3237")
67+
print(student.courses)
68+
print(student)
69+
print(student.__repr__())
70+
recreated_student = eval(repr(student))
71+
print(recreated_student)

Week05/inheritance.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from classes import Student
2+
3+
4+
faculty_members = [
5+
"Dr. Bora Canbula",
6+
"Dr. Feza Gursey",
7+
"Dr. Erdal Inonu",
8+
"Dr. Engin Arik"
9+
]
10+
11+
required_keyword = [
12+
"machine learning",
13+
"deep learning"
14+
]
15+
16+
17+
class GraduateStudent(Student):
18+
def __init__(
19+
self,
20+
student_id: str,
21+
name: str,
22+
age: int,
23+
advisor = None,
24+
thesis = None
25+
) -> None:
26+
super().__init__(student_id, name, age)
27+
self.advisor = None
28+
self.thesis = None
29+
if advisor is not None:
30+
self.assign_advisor(advisor)
31+
if thesis is not None:
32+
self.propose_thesis(thesis)
33+
34+
def assign_advisor(self, advisor):
35+
if advisor not in faculty_members:
36+
raise ValueError("The advisor is not a faculty member.")
37+
self.advisor = advisor
38+
39+
def propose_thesis(self, thesis):
40+
if not any(keyword in thesis for keyword in required_keyword):
41+
raise ValueError("The thesis does not contain any of the required keywords.")
42+
self.thesis = thesis
43+
44+
45+
if __name__ == "__main__":
46+
graduate_student = GraduateStudent("7", "Bora Canbula", 39)
47+
print(graduate_student.__class__.__bases__)
48+
print(isinstance(graduate_student, GraduateStudent))
49+
print(isinstance(graduate_student, Student))
50+
print(isinstance(graduate_student, object))
51+
graduate_student.register("CSE 3244")
52+
print(graduate_student.courses)
53+
print(graduate_student)
54+
advisor_choices = ["Dr. Nihat Berker", "Dr. Bora Canbula"]
55+
for advisor in advisor_choices:
56+
try:
57+
graduate_student.assign_advisor(advisor)
58+
except ValueError:
59+
print(f"{advisor} is not a faculty member. Trying the next one.")
60+
else:
61+
print(f"{advisor} is assigned as the advisor.")
62+
break
63+
thesis_choices = [
64+
"new trends in quantum physics",
65+
"deep learning in computer vision"
66+
]
67+
for thesis in thesis_choices:
68+
try:
69+
graduate_student.propose_thesis(thesis)
70+
except ValueError:
71+
print(f"The thesis \"{thesis}\" does not contain any of the required keywords. Trying the next one.")
72+
else:
73+
print(f"The thesis \"{thesis}\" is proposed.")
74+
break

0 commit comments

Comments
 (0)