diff --git a/__pycache__/tests.cpython-38.pyc b/__pycache__/tests.cpython-38.pyc new file mode 100644 index 0000000..ac2ea37 Binary files /dev/null and b/__pycache__/tests.cpython-38.pyc differ diff --git a/calculator.py b/calculator.py index a46affd..c044b3f 100644 --- a/calculator.py +++ b/calculator.py @@ -1,3 +1,5 @@ +import traceback +import numpy as np """ For your homework this week, you'll be creating a wsgi application of your own. @@ -40,18 +42,56 @@ """ - +def index(): + """Instructions for the calc""" + body = """ +
There are 4 methods:
+To use calculator enter operants in address bar
+eg. /add/1/2
+ + + """ + return body 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 = "0" - - return sum - -# TODO: Add functions for handling more arithmetic operations. + """Returns a STRING with the sum of the arguments""" + sumation = np.sum([int(i) for i in args]) + + return str(sumation) + + +def subtract(*args): + """Returns a STRING with the difference of the arguments""" + # np.diff starts with the last number first i guess... + # orig = [1,2,3] 3-2-1 = 0 + # reversed = [1,2,3] 1-2-3 = -4 + diff = int(np.diff([int(i) for i in reversed(args)])) + + return str(diff) + +def multiply(*args): + """Returns a STRING with the product of the arguments""" + product = np.prod([int(i) for i in args]) + + return str(product) + +def divide(*args): + """Returns a STRING with the quotient of the arguments""" + args = [int(i) for i in args] + quotient = args[0] + for num in args[1:]: + if num == 0: + raise ZeroDivisionError + quotient = quotient / num + + return str(quotient) def resolve_path(path): """ @@ -59,26 +99,55 @@ def resolve_path(path): 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'] + func_dict = { + "": index, + "add": add, + "subtract": subtract, + "multiply": multiply, + "divide": divide + } + + path = path.strip("/").split("/") + func = path[0] + args = path[1:] + try: + func = func_dict[func] + 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. - pass + headers = [("Content-type", "text/html")] + try: + path = environ.get('PATH_INFO', None) + print(path) + if path is None: + raise NameError + func, args = resolve_path(path) + body = func(*args) + status = "200 OK" + + except ZeroDivisionError: + status = "404 Bad Request" + body = "