-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators.py
79 lines (61 loc) · 1.63 KB
/
operators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# Add operators here
class Operator(object):
"""
Interface for operators.
Operator must be only one char long.
"""
sign = None
def __init__(self):
if not self.sign:
raise Exception('Operator sign is missing.')
if len(self.sign) != 1:
raise Exception('Operator sign must be 1 char long.')
try:
float(self.sign)
raise Exception('Operator cannot be a number.')
except ValueError:
pass
def calculate(self, first_number, second_number):
pass
class Add(Operator):
"""
Operator responsible for adding
"""
sign = '+'
def calculate(self, first_number, second_number):
"""
Return sum of two numbers.
"""
return first_number + second_number
class Subtract(Operator):
"""
Operator responsible for subtracting.
"""
sign = '-'
def calculate(self, first_number, second_number):
"""
Return distinction of two numbers.
"""
return first_number - second_number
class Multiple(Operator):
"""
Operator responsible for multiplication.
"""
sign = '*'
def calculate(self, first_number, second_number):
"""
Return product of two numbers.
"""
return first_number * second_number
class Divide(Operator):
"""
Operator responsible for dividing.
"""
sign = '/'
def calculate(self, first_number, second_number):
"""
Return quotient of two numbers.
"""
return first_number / second_number