Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from enum import Enum
- import math
- class GeneralException(Exception):
- pass
- class UnsupportedOperationException(GeneralException):
- pass
- class UnsupportedTypeOfDataException(GeneralException):
- pass
- class ZeroDivisionException(GeneralException):
- pass
- class LogException(GeneralException):
- pass
- class Operation(Enum):
- plus = '+'
- minus = '-'
- mul = '*'
- div = '/'
- log = 'log'
- @classmethod
- def has_value(cls, value):
- return value in Operation.__members__.values()
- def calculate(first_argument: float, second_argument: float, operation: Operation):
- if not Operation.has_value(operation):
- raise UnsupportedOperationException()
- if not isinstance(first_argument, float) and not isinstance(first_argument, int) :
- raise UnsupportedTypeOfDataException()
- if not isinstance(second_argument, float) and not isinstance(second_argument, int):
- raise UnsupportedTypeOfDataException()
- operations = {
- Operation.plus: lambda x, y: x+y,
- Operation.minus: lambda x, y: x-y,
- Operation.mul: lambda x, y: x*y,
- Operation.div: lambda x, y: x/y,
- Operation.log: lambda x, y: math.log(y, x)
- }
- try:
- return operations[operation](first_argument, second_argument)
- except ZeroDivisionError:
- raise ZeroDivisionException()
- except ValueError:
- raise LogException()
- except Exception:
- raise GeneralException()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement