Skip to content
Snippets Groups Projects
Select Git revision
  • 68f2c3ca4bf942b2067c109d6243c5b0f651c486
  • master default protected
2 results

cohorts.py

Blame
  • cohorts.py 1.55 KiB
    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)
        all_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(all_patients)}')
        print(f'Everyone in the "young" age group: {young_cohort.filter(all_patients)}')