Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. from enum import Enum
  2. import math
  3.  
  4.  
  5. class GeneralException(Exception):
  6.     pass
  7.  
  8.  
  9. class UnsupportedOperationException(GeneralException):
  10.     pass
  11.  
  12.  
  13. class UnsupportedTypeOfDataException(GeneralException):
  14.     pass
  15.  
  16.  
  17. class ZeroDivisionException(GeneralException):
  18.     pass
  19.  
  20.  
  21. class LogException(GeneralException):
  22.     pass
  23.  
  24.  
  25. class Operation(Enum):
  26.     plus = '+'
  27.     minus = '-'
  28.     mul = '*'
  29.     div = '/'
  30.     log = 'log'
  31.  
  32.     @classmethod
  33.     def has_value(cls, value):
  34.         return value in Operation.__members__.values()
  35.  
  36.  
  37. def calculate(first_argument: float, second_argument: float, operation: Operation):
  38.     if not Operation.has_value(operation):
  39.         raise UnsupportedOperationException()
  40.     if not isinstance(first_argument, float) and not isinstance(first_argument, int) :
  41.         raise UnsupportedTypeOfDataException()
  42.     if not isinstance(second_argument, float) and not isinstance(second_argument, int):
  43.         raise UnsupportedTypeOfDataException()
  44.     operations = {
  45.         Operation.plus: lambda x, y: x+y,
  46.         Operation.minus: lambda x, y: x-y,
  47.         Operation.mul: lambda x, y: x*y,
  48.         Operation.div: lambda x, y: x/y,
  49.         Operation.log: lambda x, y: math.log(y, x)
  50.     }
  51.     try:
  52.         return operations[operation](first_argument, second_argument)
  53.     except ZeroDivisionError:
  54.         raise ZeroDivisionException()
  55.     except ValueError:
  56.         raise LogException()
  57.     except Exception:
  58.         raise GeneralException()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement