diff --git a/calculator.py b/calculator.py
index a46affd..9105c05 100644
--- a/calculator.py
+++ b/calculator.py
@@ -42,14 +42,37 @@
"""
-def add(*args):
+def add(args):
""" Returns a STRING with the sum of the arguments """
+ return str(int(args[0]) + int(args[1]))
+
+def subtract(args):
+ """ Returns a STRING with the difference of the arguments """
+ return str(int(args[0]) - int(args[1]))
+
+def multiply(args):
+ """ Returns a STRING with the product of the arguments """
+ return str(int(args[0]) * int(args[1]))
+
+def divide(args):
+ """ Returns a STRING with the quotient of the arguments """
+ if args[0] == 0:
+ raise ZeroDivisionError
+ return str(int(args[0]) / int(args[1]))
+
+def index(args):
+ """ Returns instruction page at root """
+ html = """
+
Instructions for WSGI calculator
+Perform calculations on two integer values
+
+
+
+
+"""
+ return html
- # TODO: Fill sum with the correct value, based on the
- # args provided.
- sum = "0"
- return sum
# TODO: Add functions for handling more arithmetic operations.
@@ -58,15 +81,28 @@ 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.
- func = add
- args = ['25', '32']
-
- return func, args
+ funcs = {
+ '': index,
+ 'add': add,
+ 'subtract': subtract,
+ 'multiply': multiply,
+ 'divide': divide
+ }
+ if path == '/':
+ return index, ()
+ # Allows more than or less than two arguments
+ func, args = path.strip('/').split('/', 1)
+ args = args.split('/')
+ if len(args) != 2:
+ raise ValueError("Provide two operands.")
+ try:
+ int(args[0])
+ int(args[1])
+ except ValueError:
+ raise ValueError
+
+
+ return funcs[func], args
def application(environ, start_response):
# TODO: Your application code from the book database
@@ -76,9 +112,34 @@ def application(environ, start_response):
#
# TODO (bonus): Add error handling for a user attempting
# to divide by zero.
- pass
+ headers = [('Content-type', 'text/html')]
+
+ try:
+ path=environ.get('PATH_INFO',None)
+ if not path:
+ raise NameError
+ func, args = resolve_path(path)
+ body = func(args)
+ status = "200 OK"
+ # except NameError:
+ # status = "404 Not Found"
+ # body = "Not Found
"
+ except ValueError:
+ status="400 Bad Request"
+ body="Arguments must be two numbers
"
+ except ZeroDivisionError:
+ status="400 Bad Request"
+ body="Cannot divide by zero
"
+ except:
+ status = "500 Internal Server Error"
+ body = "Intentionally Vague Server Error
"
+ 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.
- pass
+ from wsgiref.simple_server import make_server
+ srv = make_server('localhost', 8080, application)
+ srv.serve_forever()