Design communication handler and exceptions
Implement a Communication Handler with Exceptions
Context
You are tasked with building a single-line communication handler designed to connect exactly two Caller instances at any given time. The environment provides a Caller class, which includes at least a .name attribute, and an CommsHandlerABC abstract base class that defines the required interface for a communication handler.
Your primary goal is to implement a concrete subclass of CommsHandlerABC, named CommsHandler, that adheres to specific behavioral requirements, and to define a custom CommunicationException class.
Requirements
Classes to Implement
CommsHandler(CommsHandlerABC)- Methods:
connect(self, user1: Caller, user2: Caller) -> strhangup(self, user1: Caller, user2: Caller) -> strclear_all(self) -> None
- Behavior for
connect:- If
user1anduser2refer to the sameCallerinstance, raiseCommunicationExceptionwith the exact message: "{user1.name} cannot connect with {user2.name}!" - If
user1anduser2are differentCallerinstances, but the communication line is already in use by another pair, raiseCommunicationExceptionwith the message: "Connection in use. Please try later" - Otherwise (users are different and line is free), store the two connected users and return: "Connection established between {user1.name} and {user2.name}"
- If
- Behavior for
hangup:- If
user1anduser2refer to the sameCallerinstance, raiseCommunicationExceptionwith the message: "{user1.name} cannot hangup with {user2.name}" - If
user1anduser2are differentCallerinstances and are currently communicating via this handler, disconnect them and return: "{user1.name} and {user2.name} are disconnected" - If they are not currently communicating through this handler, raise
CommunicationExceptionwith the message: "{user1.name} and {user2.name} not found in the communication channel"
- If
- Behavior for
clear_all:- This method should clear the communication channel, effectively disconnecting any current users, without returning any value.
- Methods:
CommunicationException(Exception)- This custom exception class must accept an error message in its constructor and be raised as specified in the
CommsHandlerbehaviors.
- This custom exception class must accept an error message in its constructor and be raised as specified in the
Important Notes
- When comparing
Callerinstances, use identity checks (is) rather than equality checks (e.g.,user1 is user2instead ofuser1.name == user2.name). - The handler strictly supports only one active connection at a time.
- Include brief tests or usage examples to demonstrate the specified behaviors of your implementation.
AI interview prep
Core dilemma
The central engineering conflict is precisely managing the state of a single, exclusive communication line under various conditions, ensuring correct exception handling, and maintaining consistency with specific identity-based comparisons for Caller instances. It's about robust state management and error propagation in a highly constrained environment.
2 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design communication handler and exceptions
Implement a Communication Handler with Exceptions
Context
You are tasked with building a single-line communication handler designed to connect exactly two Caller instances at any given time. The environment provides a Caller class, which includes at least a .name attribute, and an CommsHandlerABC abstract base class that defines the required interface for a communication handler.
Your primary goal is to implement a concrete subclass of CommsHandlerABC, named CommsHandler, that adheres to specific behavioral requirements, and to define a custom CommunicationException class.
Requirements
Classes to Implement
CommsHandler(CommsHandlerABC)- Methods:
connect(self, user1: Caller, user2: Caller) -> strhangup(self, user1: Caller, user2: Caller) -> strclear_all(self) -> None
- Behavior for
connect:- If
user1anduser2refer to the sameCallerinstance, raiseCommunicationExceptionwith the exact message: "{user1.name} cannot connect with {user2.name}!" - If
user1anduser2are differentCallerinstances, but the communication line is already in use by another pair, raiseCommunicationExceptionwith the message: "Connection in use. Please try later" - Otherwise (users are different and line is free), store the two connected users and return: "Connection established between {user1.name} and {user2.name}"
- If
- Behavior for
hangup:- If
user1anduser2refer to the sameCallerinstance, raiseCommunicationExceptionwith the message: "{user1.name} cannot hangup with {user2.name}" - If
user1anduser2are differentCallerinstances and are currently communicating via this handler, disconnect them and return: "{user1.name} and {user2.name} are disconnected" - If they are not currently communicating through this handler, raise
CommunicationExceptionwith the message: "{user1.name} and {user2.name} not found in the communication channel"
- If
- Behavior for
clear_all:- This method should clear the communication channel, effectively disconnecting any current users, without returning any value.
- Methods:
CommunicationException(Exception)- This custom exception class must accept an error message in its constructor and be raised as specified in the
CommsHandlerbehaviors.
- This custom exception class must accept an error message in its constructor and be raised as specified in the
Important Notes
- When comparing
Callerinstances, use identity checks (is) rather than equality checks (e.g.,user1 is user2instead ofuser1.name == user2.name). - The handler strictly supports only one active connection at a time.
- Include brief tests or usage examples to demonstrate the specified behaviors of your implementation.