create-list.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python3
  2. import datetime as dt
  3. import os
  4. import yaml
  5. script_path = os.path.realpath(__file__)
  6. data_path = os.path.join(os.path.dirname(script_path), 'patients.yml')
  7. output_path = os.path.join(os.path.dirname(script_path), 'list.html')
  8. def random_date(start, end):
  9. import random
  10. return dt.date.fromordinal(random.randint(
  11. start.toordinal(),
  12. end.toordinal(),
  13. ))
  14. class Patient:
  15. def __init__(self, attr):
  16. self.surname = attr.get('surname', None)
  17. self.forename = attr.get('forename', None)
  18. self.birth_date = attr.get('birth_date', None)
  19. self.admission_date = attr.get('admission_date', None)
  20. self.diagnosis = attr.get('diagnosis', None)
  21. self.room = attr.get('room', None)
  22. self.events = [Event(d) for d in attr.get('events', [])]
  23. @property
  24. def age_years(self):
  25. if self.birth_date is None:
  26. return None
  27. else:
  28. today = dt.date.today()
  29. return today.year - self.birth_date.year \
  30. - ((today.month, today.day) < (self.birth_date.month, self.birth_date.day))
  31. @property
  32. def admission_duration(self):
  33. if self.admission_date is None:
  34. return None
  35. else:
  36. return dt.date.today() - self.admission_date
  37. class Event:
  38. def __init__(self, attr):
  39. self.date = attr['date']
  40. self.title = attr['title']
  41. @property
  42. def date_delta_str(self):
  43. delta = dt.date.today() - self.date
  44. if delta.days == 0:
  45. return 'heute'
  46. elif delta.days < 0:
  47. return '{}d prae'.format(abs(delta.days))
  48. else:
  49. return '{}d post'.format(delta.days)
  50. @property
  51. def label(self):
  52. return '{} {}'.format(self.date_delta_str, self.title)
  53. def main():
  54. with open(data_path, 'r') as f:
  55. patients = yaml.load(f.read())
  56. patients.sort(key=lambda p: p.get('room', 0))
  57. doc = '''
  58. <!DOCTYPE html>
  59. <html>
  60. <head><link rel="stylesheet" href="list.css" /></head>
  61. <body>
  62. <table>
  63. '''
  64. for p in [Patient(d) for d in patients]:
  65. doc += '<tr><td>{}</td></tr>'.format('</td><td>'.join([
  66. '<b>{}</b>'.format(p.room) if p.room else '',
  67. p.surname or '',
  68. p.forename or '',
  69. '{}a'.format(p.age_years) if p.birth_date else '',
  70. '{}d'.format(p.admission_duration.days) if p.admission_date else '',
  71. '; '.join(
  72. ([p.diagnosis] if p.diagnosis else [])
  73. + [e.label for e in sorted(p.events, key=lambda e: e.date)]
  74. )
  75. ]))
  76. doc += '</table></body></html>'
  77. with open(output_path, 'w') as f:
  78. f.write(doc)
  79. if __name__ == '__main__':
  80. main()