-
Notifications
You must be signed in to change notification settings - Fork 78
/
pseudo_calculator.py
62 lines (52 loc) · 1.79 KB
/
pseudo_calculator.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
"""
This pseudo calculator should support the following operations:
* Positive
* Negative
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/positive/5' then the response
body in my browser should be `true`.
Consider the following URL/Response body pairs as tests:
```
http://localhost:8080/positive/5 => 'true'
http://localhost:8080/positive/0 => 'false'
http://localhost:8080/positive/-5 => 'false'
http://localhost:8080/negative/0 => 'false'
http://localhost:8080/negative/-2 => 'true'
```
"""
def resolve_path(path):
"""
Should return two values: a callable and an iterable of
arguments, based on the path.
"""
# 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.
func = some_func
args = ['25', '32']
return func, args
def application(environ, start_response):
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>"
finally:
headers.append(('Content-length', str(len(body))))
start_response(status, headers)
return [body.encode('utf8')]
if __name__ == '__main__':
from wsgiref.simple_server import make_server
srv = make_server('localhost', 8080, application)
srv.serve_forever()