-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmath.py
133 lines (93 loc) · 3.42 KB
/
math.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Math application
"""
from typing import Union
from twisted.web import http
from twisted.web.iweb import IRequest
from ._main import main
from .klein import Klein, KleinRenderable
__all__ = (
"Application",
)
class Application(object):
"""
Math application.
Application performs some mathematical functions.
"""
router = Klein()
main = classmethod(main) # type: ignore
@router.route("/")
def root(self, request: IRequest) -> KleinRenderable:
"""
Application root resource.
Responds with a message noting the nature of the application.
:param request: The request to respond to.
"""
return "Math happens here."
@router.route("/add/<a>/<b>")
def add(self, request: IRequest, a: str, b: str) -> KleinRenderable:
"""
Addition resource.
Adds the two given numbers and responds with the result.
:param request: The request to respond to.
:param a: A number to add to ``b``.
:param b: A number to add to ``a``.
"""
x = self.numberify(a) + self.numberify(b) # type: ignore #see #21
return "{}".format(x)
@router.route("/subtract/<a>/<b>")
def subtract(self, request: IRequest, a: str, b: str) -> KleinRenderable:
"""
Subtraction resource.
Subtracts one of two given numbers from the other and responds with the
result.
:param request: The request to respond to.
:param a: A number to subtract ``b`` from.
:param b: A number to subtract from ``a``.
"""
x = self.numberify(a) - self.numberify(b) # type: ignore #see #21
return "{}".format(x)
@router.route("/multiply/<a>/<b>")
def multiply(self, request: IRequest, a: str, b: str) -> KleinRenderable:
"""
Multiplication resource.
Multiplies the two given numbers and responds with the result.
:param request: The request to respond to.
:param a: A number to multiply with ``b``.
:param b: A number to multiply with ``a``.
"""
x = self.numberify(a) * self.numberify(b) # type: ignore #see #21
return "{}".format(x)
@router.route("/divide/<a>/<b>")
def divide(self, request: IRequest, a: str, b: str) -> KleinRenderable:
"""
Division resource.
Divides one of two given numbers from the other and responds with the
result.
:param request: The request to respond to.
:param a: A number to divide by ``b``.
:param b: A number to divide ``a`` by.
"""
x = self.numberify(a) / self.numberify(b) # type: ignore #see #21
return "{}".format(x)
@router.handle_errors(ValueError)
def valueError(self, request: IRequest, failure) -> KleinRenderable:
"""
Error handler for :exc:`ValueError`.
:param request: The request to respond to.
:param failure: The failure that occurred.
"""
request.setResponseCode(http.BAD_REQUEST)
return "Invalid inputs provided."
@staticmethod
def numberify(string: str) -> Union[int, float]:
"""
Convert a string into a number.
:param string: A string to convert into a number.
"""
try:
return int(string)
except ValueError:
return float(string)
if __name__ == "__main__": # pragma: no cover
Application.main() # type: ignore