Skip to content

Commit

Permalink
Added new 4 patterns to project information expert, composite, bridge…
Browse files Browse the repository at this point in the history
…, high cohesion
  • Loading branch information
itskanny committed Jan 8, 2022
1 parent d1a4895 commit 071f0eb
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ WORKDIR /patterns

#COPY . .

CMD [ "python", "./teacher_controller.py" ]
CMD [ "python", "./information_handler.py" ]
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Singleton
- Factory
- Mediator
- Information Expert
- Bridge
- High Cohesion
- Composite

##### About the code

Expand All @@ -26,14 +30,14 @@ As this code will be later plugged into other code so the error handling is done

##### How to run this code

- The driver class for this code is
`teacher_controller.py`
- The driver file for this code is
`information_handler.py`
- This code requires some configurations to run, so they need to be added correctly in
`config.json` our code provides a sample file with the sample required values named as `config_sample.json`
- You need to do the above step according to the values in the database but sample config file currently contains correct data according to current database file and for now the db file name can only be changed by changing code file
- This code can be run in two ways. First by [`python >= 3`](https://www.python.org/downloads/) (needs to be installed) by using following command in the same directory as the code

`python ./teacher_controller.py`
`python ./information_handler.py`

- The other way this code can be run is by using the [`docker`](https://www.docker.com/) install by clicking on it if not
- First build the image by using following command in the same directory as code
Expand Down
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"email": "teacher1@pucit.edu.pk",
"message": "Bidlksfklsdfion"
"message": "New Update he he again"
}
23 changes: 23 additions & 0 deletions information_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import json
from json import JSONDecodeError

from teacher_controller import TeacherController

if __name__ == "__main__":

try:
with open("config.json", mode="r") as file:
raw_data = file.read()
try:
config_data: dict = json.loads(raw_data)
except JSONDecodeError as e:
raise Exception("The added configuration file has some errors. Consult from sample config file")
except FileNotFoundError:
raise Exception("The provided config file does not exist. Please create a config file using the sample config "
"file")

if len(config_data) == 2 and config_data.keys().__contains__("email") and config_data.keys().__contains__("message"):
TeacherController(config_data['email']).notify(config_data['message'])
else:
raise Exception("Provided config file either does not contains required data and has extra or wrong data. "
"Please consult from sample config file and provide correct data")
10 changes: 10 additions & 0 deletions notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,20 @@ def get_notification(cls, notification_type: NotificationType):
else:
raise Exception("Adapter Type Undefined")


# Mediator Pattern implementation

class NotificationMediator:

def notify(self, sender, receivers, message):
for receiver in receivers:
receiver.get_notified(message, sender)


class NotificationAPIBridge:

def __init__(self):
self.notification_mediator = NotificationMediator()

def send_notification(self, sender, receivers, message):
self.notification_mediator.notify(sender, receivers, message)
25 changes: 25 additions & 0 deletions person.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from abc import ABC, abstractmethod


class Person(ABC):

def __init__(self, id=None, email=None, name=None):
self.id = id
self.name = name
self.email = email

@abstractmethod
def update(self):
pass

@abstractmethod
def create(self):
pass

@abstractmethod
def delete(self):
pass

@abstractmethod
def view(self):
pass
8 changes: 4 additions & 4 deletions student.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@


# Data class for student
from person import Person

class Student:

class Student(Person):

def __init__(self, id=None, email=None, name=None, roll_number=None, batch=None, section=None, cgpa=None,
semester=None, prefered_notification=None):
self.id = id
self.name = name
self.email = email
super().__init__(id=id, email=name, name=email)
self.preferred_notification = prefered_notification
self.batch = batch
self.semester = semester
Expand Down
9 changes: 5 additions & 4 deletions teacher.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
class Teacher:
from person import Person


class Teacher(Person):

# Data class for Teacher

def __init__(self, id=None, name=None, email=None, semester=None, preferred_notification=None):
self.id = id
self.name = name
self.email = email
super().__init__(id=id, email=email, name=name)
self.semester = semester
self.preferred_notification = preferred_notification

Expand Down
25 changes: 4 additions & 21 deletions teacher_controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from json import JSONDecodeError

from database import Database
from notifications import NotificationMediator
from notifications import NotificationAPIBridge
from teacher import Teacher
from student import Student
import json
Expand All @@ -23,36 +23,19 @@ def __init__(self, email):
else:
raise Exception("Teacher not found in the database")

self.notification_mediator = NotificationMediator()
self.notification_api_bridge = NotificationAPIBridge()

def get_students(self):
return Student.from_list(Database().get_generic_data("student", "semester", self.teacher.semester))

def notify(self, message):
if self.teacher is not None:
if len(self.students) != 0:
self.notification_mediator.notify(self.teacher, self.students, message)
self.notification_api_bridge.send_notification(self.teacher, self.students, message)
else:
raise Exception("This teacher does not have any students to send the notification to")
else:
raise Exception("Teacher not found in the database")


if __name__ == "__main__":

try:
with open("config.json", mode="r") as file:
raw_data = file.read()
try:
config_data: dict = json.loads(raw_data)
except JSONDecodeError as e:
raise Exception("The added configuration file has some errors. Consult from sample config file")
except FileNotFoundError:
raise Exception("The provided config file does not exist. Please create a config file using the sample config "
"file")

if len(config_data) == 2 and config_data.keys().__contains__("email") and config_data.keys().__contains__("message"):
TeacherController(config_data['email']).notify(config_data['message'])
else:
raise Exception("Provided config file either does not contains required data and has extra or wrong data. "
"Please consult from sample config file and provide correct data")

0 comments on commit 071f0eb

Please sign in to comment.