Advertisement
gruntfutuk

patients

Dec 30th, 2022
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.64 KB | None | 0 0
  1. from dataclasses import dataclass
  2. from collections import namedtuple
  3.  
  4.  
  5. @dataclass
  6. class Patient:
  7.     name: str
  8.     address: str
  9.     age: int
  10.     sex: str
  11.     disease: str
  12.     room: int
  13.     date: str = None
  14.  
  15.     def __str__(self):  # string representation of a Patient record
  16.         return (f"\nName: {self.name}\n"
  17.                 f"\taddress: {self.address}\n"
  18.                 f"\tage: {self.age}, sex: {self.sex}\n"
  19.                 f"\tdisease: {self.disease}, room: {self.room}"
  20.                 f"{'-' * 30}\n")
  21.  
  22.     def __lt__(self, other):  # less than method provides basis for sorting
  23.         return self.name.casefold() < other.name.casefold()
  24.  
  25. def display_menu(menu, prompt='What option would you like? '):
  26.     print('\n\n')
  27.     for option, details in menu.items():
  28.         print(f'{option:2} - {details.desc}')
  29.     print()
  30.     while True:
  31.         option = input(prompt)
  32.         if option.lower() in menu.keys():
  33.             return menu[option.lower()].func
  34.         if option.upper() in menu.keys():
  35.             return menu[option.upper()].func
  36.         print('Sorry, that option is not available, please make another selection')
  37.  
  38.  
  39. Menu = namedtuple('Menu', ['desc', 'func'])  # use dot notation instead of indexing
  40.  
  41. def get_patient_details(prompt="", edit=False):
  42.     if prompt:
  43.         print(prompt)
  44.     prefix = 'Enter revised' if edit else 'Enter'
  45.     name = input(f"{prefix} name: ")
  46.     address = input(f"{prefix} address: ")
  47.     age = input(f"{prefix} age: ")
  48.     sex = input(f"{prefix} sex: ")
  49.     disease = input(f"{prefix} disease description: ")
  50.     room = input(f"{prefix} specialist room number: ")
  51.     try:
  52.         return Patient(name, address, int(age), sex, disease, int(room))
  53.     except ValueError:
  54.         return None
  55.  
  56.  
  57. def add_record_OPD():
  58.     patient = get_patient_details('Details for OPD patient required\n')
  59.     if patient:
  60.         OPD.append(patient)
  61.         print("Added:", patient)
  62.     else:
  63.         print('Sorry, data could not be processed - unexpected value(s)')
  64.  
  65. def add_record_emergency():
  66.     patient = get_patient_details('Details for emergency patient required\n')
  67.     if patient:
  68.         emergency.append(patient)
  69.         print("Added:", patient)
  70.     else:
  71.         print('Sorry, data could not be processed - unexpected value(s)')
  72.  
  73. def add_record():
  74.     while True:
  75.         option = display_menu(add_record_menu)
  76.         if option:
  77.             status = option()
  78.             rebuild_records()  # rebuild combined list
  79.         else:
  80.             return
  81.  
  82. def find_patient_by_name():
  83.     name = input("Enter the patient's name: ")
  84.     chosen = None
  85.     for idx, patient in enumerate(patients):
  86.         if name.casefold() == patient.name.casefold():
  87.             chosen = idx  # assumes unique names are used
  88.             break
  89.     return chosen
  90.  
  91. def search_record():
  92.     chosen = find_patient_by_name()
  93.  
  94.     if chosen is None:
  95.         print("\n No records were available!")
  96.         return
  97.  
  98.     nxt = input("Press enter to view next record or 2 to edit record: ")
  99.     if nxt == '':
  100.         chosen += 1
  101.         if chosen <= (len(patients) -1):
  102.             print(patients[chosen])
  103.         else:
  104.             print('Sorry, no more records')
  105.     elif nxt == '2':
  106.         patient = get_patient_details("Editing details", edit=True)
  107.         if patient:
  108.             patients[chosen] = patient
  109.         else:
  110.             print('Sorry, data could not be processed - unexpected value(s) - no details changed')
  111.     else:
  112.         print("Press Enter or 2!")
  113.  
  114.  
  115. def list_record():
  116.     choice = display_menu(list_records_menu)
  117.     if choice == 1:
  118.         x = sorted(patients)
  119.         print(*x, sep='\n')
  120.     elif choice == 2:
  121.         print(*emergency, sep='\n')
  122.     elif choice == 3:
  123.         print(*OPD, sep='\n')
  124.     elif choice == 4:
  125.         date = input("Enter month & year (mm-yyyy): ")
  126.         for patient in patients:
  127.             if patient.date == date:
  128.                 print(patient)
  129.                 break
  130.         else:
  131.             print("No patients in this date!")
  132.  
  133.  
  134. def rebuild_records(remove_idx=None):  # remove record idx in relation to patients list
  135.     global OPD
  136.     global emergency
  137.     global patients
  138.     if remove_idx is not None:
  139.         opd_max_idx = len(OPD) - 1
  140.         if 0 <= remove_idx <= opd_max_idx:
  141.             OPD = OPD[:remove_idx] + OPD[remove_idx + 1:]
  142.         elif opd_max_idx < remove_idx < (len(emergency) - 1):
  143.             remove_idx -= opd_max_idx
  144.             emergency = emergency[:remove_idx] + emergency[remove_idx + 1:]
  145.         else:
  146.             raise IndexError(f'Trying to remove record with invalid index of {remove_idx}')
  147.     patients = OPD + emergency
  148.  
  149. def delete_record():
  150.     chosen = find_patient_by_name()
  151.  
  152.     if chosen is None:
  153.         print("\n No records were available!")
  154.         return
  155.  
  156.     x = input("Press enter to delete record: ")
  157.     if x == '':
  158.         rebuild_records(chosen)
  159.  
  160. def menu():
  161.     while True:
  162.         option = display_menu(main_menu)
  163.         if option is None:
  164.             break
  165.         status = option()
  166.  
  167.  
  168.  
  169. main_menu = {'1': Menu('Add new patient record', add_record),
  170.              '2': Menu('Search or edit patient record', search_record),
  171.              '3': Menu('list record of patients', list_record),
  172.              '4': Menu('Delete patient records', delete_record),
  173.              'Q': Menu('Quit', None),
  174.              }
  175.  
  176. add_record_menu = {'1': Menu("Add OPD patient record", add_record_OPD),
  177.                    '2': Menu("Add emergency patient record", add_record_emergency),
  178.                    'B': Menu('back to previous menu', None),
  179.                    }
  180.  
  181. list_records_menu = {"1": Menu("Records of patients in alphabetical order ", 1),
  182.                      "2": Menu("Records of Emergency patients ", 2),
  183.                      "3": Menu("Records of O.P.D. patients ", 3),
  184.                      "4": Menu("Records of patients in a particular date", 4),
  185.                      "B": Menu("back to previous menu", None),
  186.                      }
  187. OPD = [Patient("john", "850-922-5797", 25, "male", "Sore throat", 205),
  188.        Patient("mike", "850-342-6593", 63, "female", "stomachache", 205),
  189.        Patient("sarah", "850-773-6537", 36, "female", "Fever", 205),
  190.        Patient("chen", "850-754-7533", 39, "male", "sprained ankle", 205),
  191.        ]
  192. emergency = [Patient("ben", "850-922-5797", 53, "male", "Malaria", 308),
  193.              Patient("jake", "850-922-5797", 38, "male", "heart attack", 102),
  194.              Patient("alex", "850-922-5797", 29, "male", "Salmonella", 265),
  195.              Patient("chong", "850-922-5797", 44, "male", "Chemical burns", 304),
  196.              ]
  197.  
  198. patients = OPD + emergency
  199.  
  200.  
  201. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement