-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathcalculator.py
More file actions
157 lines (119 loc) · 4.44 KB
/
calculator.py
File metadata and controls
157 lines (119 loc) · 4.44 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import traceback
"""
For your homework this week, you'll be creating a wsgi application of
your own.
You'll create an online calculator that can perform several operations.
You'll need to support:
* Addition
* Subtractions
* Multiplication
* Division
Your users should be able to send appropriate requests and get back
proper responses. For example, if I open a browser to your wsgi
application at `http://localhost:8080/multiple/3/5' then the response
body in my browser should be `15`.
Consider the following URL/Response body pairs as tests:
```
http://localhost:8080/multiply/3/5 => 15
http://localhost:8080/add/23/42 => 65
http://localhost:8080/subtract/23/42 => -19
http://localhost:8080/divide/22/11 => 2
http://localhost:8080/ => <html>Here's how to use this page...</html>
```
To submit your homework:
* Fork this repository (Session03).
* Edit this file to meet the homework requirements.
* Your script should be runnable using `$ python calculator.py`
* When the script is running, I should be able to view your
application in my browser.
* I should also be able to see a home page (http://localhost:8080/)
that explains how to perform calculations.
* Commit and push your changes to your fork.
* Submit a link to your Session03 fork repository!
"""
def home():
return 'This site has four functions which add/subtract/multiply/devide two integers. They can be called out by adding the function followed by back slash a number another back slash and another number to the end of http://localhost:8080.'
def add(*args):
""" Returns a STRING with the sum of the arguments """
# TODO: Fill sum with the correct value, based on the
# args provided.
sum = int(args[0])+int(args[1])
return str(sum)
def subtract(*args):
""" Returns a STRING with the sum of the arguments """
# TODO: Fill sum with the correct value, based on the
# args provided.
subtract = int(args[0])-int(args[1])
return str(subtract)
def multiply(*args):
""" Returns a STRING with the sum of the arguments """
# TODO: Fill sum with the correct value, based on the
# args provided.
multiply = int(args[0])*int(args[1])
return str(multiply)
def divide(*args):
""" Returns a STRING with the sum of the arguments """
# TODO: Fill sum with the correct value, based on the
# args provided.
if args[1] == '0':
return "Undefined; attempting to divide by zero."
divide = int(args[0])/int(args[1])
return str(divide)
# TODO: Add functions for handling more arithmetic operations.
def resolve_path(path):
"""
Should return two values: a callable and an iterable of
arguments.
"""
# TODO: Provide correct values for func and args. The
# examples provide the correct *syntax*, but you should
# determine the actual values of func and args using the
# path.
funcs = {
'': home,
'add': add,
'subtract': subtract,
'multiply': multiply,
'divide': divide,
}
path = path.strip('/').split('/')
func_name = path[0]
args = path[1:]
try:
func = funcs[func_name]
except KeyError:
raise NameError
return func, args
def application(environ, start_response):
# TODO: Your application code from the book database
# work here as well! Remember that your application must
# invoke start_response(status, headers) and also return
# the body of the response in BYTE encoding.
#
# TODO (bonus): Add error handling for a user attempting
# to divide by zero.
headers = [("Content-type", "text/html")]
try:
path = environ.get('PATH_INFO', None)
if path is None:
raise NameError
func, args = resolve_path(path)
body = func(*args)
status = "200 OK"
except NameError:
status = "404 Not Found"
body = "<h1>Not Found</h1>"
except Exception:
status = "500 Internal Server Error"
body = "<h1>Internal Server Error</h1>"
print(traceback.format_exc())
finally:
headers.append(('Content-length', str(len(body))))
start_response(status, headers)
return [body.encode('utf8')]
if __name__ == '__main__':
# TODO: Insert the same boilerplate wsgiref simple
# server creation that you used in the book database.
from wsgiref.simple_server import make_server
srv = make_server('localhost', 8080, application)
srv.serve_forever()