#!/usr/bin/env python3 import datetime as dt import os import yaml script_path = os.path.realpath(__file__) data_path = os.path.join(os.path.dirname(script_path), 'patients.yml') output_path = os.path.join(os.path.dirname(script_path), 'list.html') def random_date(start, end): import random return dt.date.fromordinal(random.randint( start.toordinal(), end.toordinal(), )) class Patient: def __init__(self, attr): self.surname = attr.get('surname', None) self.forename = attr.get('forename', None) self.birth_date = attr.get('birth_date', None) self.admission_date = attr.get('admission_date', None) self.diagnosis = attr.get('diagnosis', None) self.room = attr.get('room', None) self.events = [Event(d) for d in attr.get('events', [])] @property def age_years(self): if self.birth_date is None: return None else: today = dt.date.today() return today.year - self.birth_date.year \ - ((today.month, today.day) < (self.birth_date.month, self.birth_date.day)) @property def admission_duration(self): if self.admission_date is None: return None else: return dt.date.today() - self.admission_date class Event: def __init__(self, attr): self.date = attr['date'] self.title = attr['title'] @property def date_delta_str(self): delta = dt.date.today() - self.date if delta.days == 0: return 'heute' elif delta.days < 0: return '{}d prae'.format(abs(delta.days)) else: return '{}d post'.format(delta.days) @property def label(self): return '{} {}'.format(self.date_delta_str, self.title) def main(): with open(data_path, 'r') as f: patients = yaml.load(f.read()) patients.sort(key=lambda p: p.get('room', 0)) doc = ''' ''' for p in [Patient(d) for d in patients]: doc += ''.format('
{}
'.join([ '{}'.format(p.room) if p.room else '', p.surname or '', p.forename or '', '{}a'.format(p.age_years) if p.birth_date else '', '{}d'.format(p.admission_duration.days) if p.admission_date else '', '; '.join( ([p.diagnosis] if p.diagnosis else []) + [e.label for e in sorted(p.events, key=lambda e: e.date)] ) ])) doc += '
' with open(output_path, 'w') as f: f.write(doc) if __name__ == '__main__': main()