Created
November 12, 2025 00:17
-
-
Save mfurquimdev/3f6d89a5aed7615521fd081d5c812c1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/env python | |
| import time | |
| class CalculatorException(Exception): | |
| """ Exception raised for calculator | |
| Attr: | |
| a (Union[int, float]): Numerator of the operation - e.g., 2 | |
| b (Union[int, float]): Denominator of the operation - e.g., 3.3 | |
| op (str): Operation to execute on calculator - e.g., '+' | |
| message (str): Explanation of the error - e.g., 'Missing calculator operation' | |
| """ | |
| def __init__(self, a, b, operation, message='Missing calculator operation'): | |
| self.a = a | |
| self.b = b | |
| self.operation = operation | |
| self.message = message | |
| super().__init__(self.message) | |
| def __str__(self): | |
| return f"{self.message}. Operation {self.operation} with {self.a} and {self.b}" | |
| class EmptyOperation(CalculatorException): | |
| """ Exception raised for missing operation on calculator | |
| Attr: | |
| a (Union[int, float]): Numerator of the operation - e.g., 2 | |
| b (Union[int, float]): Denominator of the operation - e.g., 3.3 | |
| op (str): Operation to execute on calculator - e.g., '+' | |
| message (str): Explanation of the error - e.g., 'Missing calculator operation' | |
| """ | |
| def __init__(self, a, b, operation=None, message='Missing calculator operation'): | |
| self.a = a | |
| self.b = b | |
| self.operation = operation | |
| self.message = message | |
| super().__init__(self.message) | |
| class InvalidOperation(CalculatorException): | |
| """ Exception raised for invalid or not yet implemented operation on calculator | |
| Attr: | |
| a (Union[int, float]): Numerator of the operation - e.g., 2 | |
| b (Union[int, float]): Denominator of the operation - e.g., 3.3 | |
| op (str): Operation to execute on calculator - e.g., '+' | |
| message (str): Explanation of the error - e.g., 'Invalid or not yet implemented operation' | |
| """ | |
| def __init__(self, a, b, operation, message='Invalid or not yet implemented operation'): | |
| self.a = a | |
| self.b = b | |
| self.operation = operation | |
| self.message = message | |
| super().__init__(self.message) | |
| class CannotDivideByZero(CalculatorException): | |
| """ Exception raised when dividing by zero | |
| Attr: | |
| a (Union[int, float]): Numerator of the operation - e.g., 2 | |
| b (Union[int, float]): Denominator of the operation - e.g., 3.3 | |
| op (str): Operation to execute on calculator - e.g., '+' | |
| message (str): Explanation of the error - e.g., 'Cannot divide by zero' | |
| """ | |
| def __init__(self, a, b, operation, message='Cannot divide by zero'): | |
| self.a = a | |
| self.b = b | |
| self.operation = operation | |
| self.message = message | |
| super().__init__(self.message) | |
| def display_info(func): | |
| def inner(*args, **kwargs): | |
| print("Executing", func.__name__, "function with args:", args, kwargs) | |
| start_time = time.monotonic() | |
| result = func(*args, **kwargs) | |
| end_time = time.monotonic() | |
| elapsed_time = round((end_time - start_time)*1000, 6) | |
| print("Finished execution in", elapsed_time, "miliseconds") | |
| return result | |
| return inner | |
| @display_info | |
| def calculator(a, b, operation=None): | |
| if not operation: | |
| raise(EmptyOperation(a, b)) | |
| if operation not in ['+', '-', '*', '/']: | |
| raise(InvalidOperation(a, b, operation=operation)) | |
| if operation == '+': | |
| return a+b | |
| if operation == '-': | |
| return a-b | |
| if operation == '*': | |
| return a*b | |
| if operation == '/': | |
| if b == 0: | |
| raise(CannotDivideByZero(a, b, operation=operation)) | |
| return a/b | |
| def main(): | |
| print(calculator(2, 4, operation='+')) | |
| print(calculator(2, 4, operation='-')) | |
| print(calculator(2, 4, operation='*')) | |
| print(calculator(2, 4, operation='/')) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment