Untitled

 avatar
unknown
python
5 months ago
6.2 kB
4
Indexable
class PatientRecord:
    def __init__(self, patient_name, patient_id):
        # Public attributes
        self.patient_name = patient_name
        self.patient_id = patient_id
        
        # Private attributes
        self.__medical_history = []
        self.__current_medications = []
        self.__contact_info = {}

        # Protected attributes
        self._general_health_conditions = []
        self._emergency_contact_info = {}

    # Private method to validate doctor
    def __validate_doctor(self, doctor):
        # Simulate a validation check (e.g., checking doctor's credentials)
        authorized_doctors = ["Dr. John", "Dr. Smith"]
        return doctor in authorized_doctors

    # Public method to view the patient record
    def view_record(self, doctor):
        if self.__validate_doctor(doctor):
            return {
                "patient_name": self.patient_name,
                "patient_id": self.patient_id,
                "medical_history": self.__medical_history,
                "current_medications": self.__current_medications,
                "general_health_conditions": self._general_health_conditions,
                "emergency_contact_info": self._emergency_contact_info
            }
        else:
            return "Unauthorized access. Doctor validation failed."

    # Public method to update the patient record
    def update_record(self, doctor, updates):
        if self.__validate_doctor(doctor):
            # Update private attributes if provided
            if 'medical_history' in updates:
                self.__medical_history = updates['medical_history']
            if 'current_medications' in updates:
                self.__current_medications = updates['current_medications']
            if 'contact_info' in updates:
                self.__contact_info = updates['contact_info']
            
            return "Record updated successfully."
        else:
            return "Unauthorized update attempt."

    # Protected method to add a new health condition
    def _add_health_condition(self, condition):
        self._general_health_conditions.append(condition)

    # Protected method to update emergency contact information
    def _update_emergency_contact(self, contact_info):
        self._emergency_contact_info = contact_info

# Subclass inheriting from PatientRecord
class SpecializedPatientRecord(PatientRecord):
    def __init__(self, patient_name, patient_id, specialist_name):
        super().__init__(patient_name, patient_id)
        self.specialist_name = specialist_name

    # Public method demonstrating access to protected attributes
    def add_condition_and_contact(self, condition, contact_info):
        self._add_health_condition(condition)
        self._update_emergency_contact(contact_info)
        return "Condition and emergency contact updated by specialist."

# Demonstration
if __name__ == "__main__":
    # Creating a PatientRecord object
    patient = PatientRecord("John Doe", "12345")
    
    # Attempt to view the record by an authorized doctor
    print(patient.view_record("Dr. John"))  # Should show patient details
    
    # Attempt to update the record by an authorized doctor
    update_info = {
        "medical_history": ["Diabetes", "Hypertension"],
        "current_medications": ["Insulin", "Blood Pressure Medication"]
    }
    print(patient.update_record("Dr. John", update_info))  # Should update successfully
    
    # Attempt to update the record by an unauthorized doctor
    print(patient.update_record("Dr. Alice", update_info))  # Unauthorized access
    
    # SpecializedPatientRecord subclass demonstrating access to protected attributes
    specialized_patient = SpecializedPatientRecord("Jane Doe", "67890", "Dr. House")
    print(specialized_patient.add_condition_and_contact("Asthma", {"name": "Jane's Mother", "phone": "555-5555"}))
    
    # View updated patient record
    print(specialized_patient.view_record("Dr. John"))



# Explanation
# Public Attributes:

# patient_name and patient_id are public, meaning they can be accessed from outside the class.
# Private Attributes:

# __medical_history, __current_medications, and __contact_info are private. Private attributes are prefixed with __ and are only accessible within the class. Attempting to access them outside the class will result in an AttributeError.
# Protected Attributes:

# _general_health_conditions and _emergency_contact_info are protected, indicated by a single underscore _. These attributes can be accessed within the class, its subclasses, but not directly from outside the class.
# Methods:

# __validate_doctor is a private method to check if a doctor is authorized to view or update patient records.
# view_record and update_record are public methods that allow authorized doctors to view or update the patient’s details.
# _add_health_condition and _update_emergency_contact are protected methods to modify health conditions and emergency contacts. These are intended to be accessed by subclasses like SpecializedPatientRecord.
# Subclass:

# SpecializedPatientRecord inherits from PatientRecord and demonstrates how protected attributes like _general_health_conditions and _emergency_contact_info can be accessed and updated from within the subclass using the protected methods.
# Private vs Protected in Python
# Private (__attribute): Private attributes or methods are intended for internal use only within the class. In Python, private attributes are not strictly inaccessible but are name-mangled to avoid accidental access. For example, __medical_history is renamed internally to _PatientRecord__medical_history to prevent access from outside.

# Protected (_attribute): Protected attributes or methods are intended to be accessible from within the class and its subclasses. It is a convention in Python (as there is no strict enforcement like in some other languages). Developers are expected to respect the protected access modifier and not access protected attributes from outside the class hierarchy.
Editor is loading...
Leave a Comment