From 90fe84d52a4a59b951f231cb1e164f8eafbd20da Mon Sep 17 00:00:00 2001
From: Brady James Garvin <bgarvin@cse.unl.edu>
Date: Wed, 13 Feb 2019 10:01:31 -0600
Subject: [PATCH] Added more examples of method overriding, polymorphism, and
 duck typing.

---
 cohorts.py        | 50 +++++++++++++++++++++++++++++++++++++++++++++++
 notes.py          | 29 +++++++++++++++++++++++++++
 person_patient.py |  9 ++++++---
 3 files changed, 85 insertions(+), 3 deletions(-)
 create mode 100644 cohorts.py
 create mode 100644 notes.py

diff --git a/cohorts.py b/cohorts.py
new file mode 100644
index 0000000..fc20f7c
--- /dev/null
+++ b/cohorts.py
@@ -0,0 +1,50 @@
+class Patient(object):
+    def __init__(self, name, region, age):
+        self.name = name
+        self.region = region
+        self.age = age
+
+    def __repr__(self):
+        return f'Person({repr(self.name)}, {repr(self.region)}, {repr(self.age)})'
+
+
+class Cohort(object):
+    def includes(self, patient):
+        raise NotImplementedError('The Cohort class\'s includes method must be overridden to be called.')
+
+    def filter(self, patients):
+        result = []
+        for patient in patients:
+            if self.includes(patient):
+                result.append(patient)
+        return result
+
+
+class RegionalCohort(Cohort):
+    def __init__(self, region):
+        self.region = region
+
+    def includes(self, patient):
+        return patient.region == self.region
+
+
+class AgeCohort(Cohort):
+    def __init__(self, minimum, maximum):
+        self.minimum = minimum
+        self.maximum = maximum
+
+    def includes(self, patient):
+        return self.minimum <= patient.age <= self.maximum
+
+
+if __name__ == '__main__':
+    ada = Patient('Ada', 'UK', 203)
+    charles = Patient('Charles', 'UK', 227)
+    grace = Patient('Grace', 'US', 112)
+    patients = [ada, charles, grace]
+    uk_cohort: Cohort = RegionalCohort('UK')
+    young_cohort: Cohort = AgeCohort(0, 225)
+    print(f'Charles is in the UK region: {uk_cohort.includes(charles)}')
+    print(f'Charles is in the "young" age group: {young_cohort.includes(charles)}')
+    print(f'Everyone in the UK region: {uk_cohort.filter(patients)}')
+    print(f'Everyone in the "young" age group: {young_cohort.filter(patients)}')
diff --git a/notes.py b/notes.py
new file mode 100644
index 0000000..a4b123e
--- /dev/null
+++ b/notes.py
@@ -0,0 +1,29 @@
+class Visit(object):
+    def __init__(self):
+        self.notes = []
+
+    def add_note(self, note):
+        self.notes.append(note)
+
+
+class Appointment(object):
+    def __init__(self):
+        self.note = None
+
+    def add_note(self, note):
+        self.note = note
+
+
+def void(event):
+    event.add_note('Voided.')
+
+
+if __name__ == '__main__':
+    visit = Visit()
+    appointment = Appointment()
+    number = 7
+    void(visit)
+    print(visit.notes)
+    void(appointment)
+    print(appointment.note)
+    void(number)
diff --git a/person_patient.py b/person_patient.py
index 783f288..a909d3a 100644
--- a/person_patient.py
+++ b/person_patient.py
@@ -9,6 +9,9 @@ class Patient(Person):
 
 
 if __name__ == '__main__':
-    print(object())
-    print(Person())
-    print(Patient())
+    example_object: object = object()
+    example_person: object = Person()
+    example_patient: object = Patient()
+    print(example_object)
+    print(example_person)
+    print(example_patient)
-- 
GitLab