diff --git a/calculator.py b/calculator.py
index a46affd..997588e 100644
--- a/calculator.py
+++ b/calculator.py
@@ -23,7 +23,8 @@
http://localhost:8080/add/23/42 => 65
http://localhost:8080/subtract/23/42 => -19
http://localhost:8080/divide/22/11 => 2
- http://localhost:8080/ => Here's how to use this page...
+ http://localhost:8080/ =>
+ Here's how to use this page...
```
To submit your homework:
@@ -47,12 +48,47 @@ def add(*args):
# TODO: Fill sum with the correct value, based on the
# args provided.
- sum = "0"
-
- return sum
+ final_sum = str(sum(args))
+ return final_sum
# TODO: Add functions for handling more arithmetic operations.
+
+def subtract(*args):
+ difference = args[0]
+ for x in args[1:]:
+ difference -= x
+ return str(difference)
+
+
+def divide(*args):
+ quotient = args[0]
+ for x in args[1:]:
+ quotient /= x
+ return str(quotient)
+
+
+def multiply(*args):
+ product = 1
+ for x in args:
+ product *= x
+ return str(product)
+
+
+def instructions():
+ page = """
+
Here's how to Use this Page!
+This is a basic calculator that can add, subtract, multiply and divide
+- You are going to be using the URL to do math!
+- First input a math operation eg. add. This will look like 'http://localhost:8080/add/'
+- Next input as many numbers as you would like to do the operation upon, separated by '/'.
+- Finally press enter to get the result!
+- A valid URL would look like 'http://localhost:8080/multiply/3/5/6'
+
+ """
+ return page
+
+
def resolve_path(path):
"""
Should return two values: a callable and an iterable of
@@ -63,11 +99,29 @@ def resolve_path(path):
# examples provide the correct *syntax*, but you should
# determine the actual values of func and args using the
# path.
- func = add
- args = ['25', '32']
-
+ # func = add
+ # args = ['25', '32']
+ funcs = {
+ "add": add,
+ "divide": divide,
+ "multiply": multiply,
+ "subtract": subtract,
+ "": instructions
+ }
+ path = path.strip('/').split('/')
+ func_name = path[0]
+ argstrings = path[1:]
+
+ try:
+ func = funcs[func_name]
+ args = [int(x) for x in argstrings]
+ except KeyError:
+ raise NameError
+ except ValueError:
+ raise ValueError
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
@@ -76,9 +130,37 @@ 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 path is None:
+ raise NameError
+ func, args = resolve_path(path)
+ result = func(*args)
+ if func.__name__ == 'instructions':
+ body = result
+ else:
+ body = "The result of your request to {} is: {}".format(
+ func.__name__, result)
+ status = "200 OK"
+ except NameError:
+ status = "404 Not Found"
+ body = "Not Found
"
+ except ValueError:
+ status = "400 Bad Request"
+ body = "Unable to do math on strings
"
+ except ZeroDivisionError:
+ status = "400 Bad Request"
+ body = "Unable to divide by zero
"
+ 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()