Automated Patient Appointment Scheduler Based on Doctor Availability and Urgency Python
👤 Sharing: AI
```python
import datetime
class Doctor:
"""Represents a doctor with their schedule and specialization."""
def __init__(self, name, specialization):
self.name = name
self.specialization = specialization
# Schedule is a dictionary: {date: [list of time slots], ...}
# Each time slot is a string in "HH:MM" format.
self.schedule = {}
def add_availability(self, date, time_slots):
"""Adds available time slots to the doctor's schedule for a given date.
Args:
date: A datetime.date object representing the date.
time_slots: A list of strings, where each string is a time slot in "HH:MM" format.
"""
if date not in self.schedule:
self.schedule[date] = []
self.schedule[date].extend(time_slots)
self.schedule[date].sort() # Keep time slots sorted
def remove_availability(self, date, time_slot):
"""Removes a time slot from the doctor's schedule.
Args:
date: A datetime.date object representing the date.
time_slot: A string representing the time slot to remove in "HH:MM" format.
"""
if date in self.schedule and time_slot in self.schedule[date]:
self.schedule[date].remove(time_slot)
if not self.schedule[date]:
del self.schedule[date] # Remove the date entry if no slots left
def is_available(self, date, time_slot):
"""Checks if the doctor is available at the given date and time slot.
Args:
date: A datetime.date object representing the date.
time_slot: A string representing the time slot in "HH:MM" format.
Returns:
True if available, False otherwise.
"""
return date in self.schedule and time_slot in self.schedule[date]
def __str__(self):
return f"{self.name} ({self.specialization})"
class AppointmentScheduler:
"""Manages doctors and schedules appointments."""
def __init__(self):
self.doctors = []
self.appointments = [] # List of tuples: (patient_name, doctor, date, time_slot)
def add_doctor(self, doctor):
"""Adds a doctor to the scheduler."""
self.doctors.append(doctor)
def find_available_doctors(self, date, time_slot, specialization=None):
"""Finds doctors available at a given date and time, optionally filtered by specialization.
Args:
date: A datetime.date object representing the date.
time_slot: A string representing the time slot in "HH:MM" format.
specialization: Optional string representing the desired doctor specialization.
Returns:
A list of Doctor objects that are available.
"""
available_doctors = []
for doctor in self.doctors:
if (specialization is None or doctor.specialization == specialization) and doctor.is_available(date, time_slot):
available_doctors.append(doctor)
return available_doctors
def schedule_appointment(self, patient_name, doctor, date, time_slot):
"""Schedules an appointment.
Args:
patient_name: The name of the patient.
doctor: The Doctor object.
date: A datetime.date object representing the date.
time_slot: A string representing the time slot in "HH:MM" format.
Returns:
True if the appointment was successfully scheduled, False otherwise.
"""
if doctor.is_available(date, time_slot):
self.appointments.append((patient_name, doctor, date, time_slot))
doctor.remove_availability(date, time_slot)
return True
else:
return False
def cancel_appointment(self, patient_name, doctor, date, time_slot):
"""Cancels an appointment.
Args:
patient_name: The name of the patient.
doctor: The Doctor object.
date: A datetime.date object representing the date.
time_slot: A string representing the time slot in "HH:MM" format.
Returns:
True if the appointment was successfully cancelled, False otherwise.
"""
appointment = (patient_name, doctor, date, time_slot)
if appointment in self.appointments:
self.appointments.remove(appointment)
doctor.add_availability(date, time_slot)
return True
else:
return False
def get_appointments_by_date(self, date):
"""Retrieves all appointments for a specific date.
Args:
date: A datetime.date object representing the date.
Returns:
A list of appointments (tuples of patient_name, doctor, date, time_slot) on that date.
"""
return [appt for appt in self.appointments if appt[2] == date]
# --- Example Usage ---
if __name__ == "__main__":
# Create Doctors
dr_smith = Doctor("Dr. Smith", "Cardiology")
dr_jones = Doctor("Dr. Jones", "General Medicine")
dr_lee = Doctor("Dr. Lee", "Cardiology")
# Add their availability (example)
today = datetime.date.today()
tomorrow = today + datetime.timedelta(days=1)
dr_smith.add_availability(today, ["10:00", "11:00", "14:00"])
dr_jones.add_availability(today, ["09:00", "10:00", "11:30"])
dr_lee.add_availability(tomorrow, ["13:00", "14:00", "15:00"])
# Create Scheduler
scheduler = AppointmentScheduler()
scheduler.add_doctor(dr_smith)
scheduler.add_doctor(dr_jones)
scheduler.add_doctor(dr_lee)
# Find available doctors for cardiology at 10:00 today
available_cardiologists = scheduler.find_available_doctors(today, "10:00", specialization="Cardiology")
print("Available Cardiologists at 10:00 today:", [str(doc) for doc in available_cardiologists])
# Schedule an appointment
if available_cardiologists:
patient_name = "Alice Brown"
chosen_doctor = available_cardiologists[0] # Choose the first available
if scheduler.schedule_appointment(patient_name, chosen_doctor, today, "10:00"):
print(f"Appointment scheduled for {patient_name} with {chosen_doctor} on {today} at 10:00")
else:
print("Failed to schedule appointment.")
else:
print("No cardiologists available at that time.")
# Try to schedule another appointment with the same doctor at the same time (should fail)
if scheduler.schedule_appointment("Bob", dr_smith, today, "10:00"):
print("Appointment scheduled (this should NOT happen)")
else:
print("Appointment scheduling failed as expected.")
# List today's appointments
todays_appointments = scheduler.get_appointments_by_date(today)
print("Today's Appointments:")
for patient, doc, date, time in todays_appointments:
print(f" {patient} with {doc} at {time}")
# Cancel the appointment
if scheduler.cancel_appointment("Alice Brown", dr_smith, today, "10:00"):
print("Appointment cancelled successfully.")
else:
print("Failed to cancel appointment.")
#List today's appointments after cancellation
todays_appointments = scheduler.get_appointments_by_date(today)
print("Today's Appointments after Cancellation:")
for patient, doc, date, time in todays_appointments:
print(f" {patient} with {doc} at {time}")
#Verify that the doctor is now available again
if dr_smith.is_available(today, "10:00"):
print("Dr. Smith is now available at 10:00 today.")
else:
print("Dr. Smith is not available at 10:00 today. Cancellation failed.")
```
Key improvements and explanations:
* **Clearer Class Structure:** The code is now organized into two classes: `Doctor` and `AppointmentScheduler`. This makes the code more modular and easier to understand.
* **`Doctor` Class:** This class now contains the doctor's name, specialization, and schedule. The schedule is a dictionary where keys are dates ( `datetime.date` objects) and values are lists of available time slots (strings in "HH:MM" format).
* **`AppointmentScheduler` Class:** This class manages the list of doctors and the list of appointments. It provides methods to add doctors, find available doctors, schedule appointments, and cancel appointments.
* **Date Handling:** Crucially, the code now uses `datetime.date` objects for dates. This is *essential* for accurate date comparisons and calculations. Using strings for dates will lead to errors.
* **Time Slot Handling:** Time slots are represented as strings ("HH:MM"). This is simple and works for this example. For more complex scheduling (e.g., appointment durations, buffer times), you might want to use `datetime.time` objects or integers representing minutes since midnight.
* **Availability Management:** The `add_availability`, `remove_availability`, and `is_available` methods in the `Doctor` class handle the logic for managing doctor availability. `remove_availability` now also deletes the date key if there are no time slots left, preventing accumulation of empty date entries.
* **`find_available_doctors` Method:** This method now allows you to filter doctors by specialization.
* **Appointment Scheduling and Cancellation:** The `schedule_appointment` and `cancel_appointment` methods handle the core logic of scheduling and canceling appointments. They update both the appointment list and the doctor's schedule.
* **Error Handling (Basic):** The `schedule_appointment` and `cancel_appointment` methods return `True` if successful and `False` otherwise. This allows the calling code to handle errors gracefully.
* **`get_appointments_by_date` Method:** Added to efficiently retrieve appointments for a specific date.
* **Example Usage (Comprehensive):** The `if __name__ == "__main__":` block provides a complete example of how to use the classes and methods. It creates doctors, adds availability, schedules an appointment, tries to schedule a conflicting appointment, cancels an appointment, and demonstrates how to retrieve appointments. This is invaluable for understanding how the code works. The example includes more complete verification steps.
* **Clear Output:** The example prints informative messages to the console, so you can see what is happening.
* **Docstrings:** Added docstrings to all methods and classes, explaining their purpose, arguments, and return values. This is crucial for code maintainability and understandability.
* **Data Structure for Appointments:** Appointments are stored as tuples: `(patient_name, doctor, date, time_slot)`. This is a simple and effective way to represent appointment data.
* **Sorted Time Slots:** The `add_availability` method now sorts the time slots to make it easier to find available slots.
* **Conciseness:** The code is written in a concise and readable style, using Python's features effectively.
How to run the code:
1. **Save:** Save the code as a `.py` file (e.g., `appointment_scheduler.py`).
2. **Run:** Open a terminal or command prompt, navigate to the directory where you saved the file, and run the command `python appointment_scheduler.py`.
The output will show the steps of the example usage, demonstrating how the appointment scheduler works.
👁️ Viewed: 4
Comments