From 41ed5b21d77b20cece24483fb1620014bc346e62 Mon Sep 17 00:00:00 2001 From: Benjamin Freeman Date: Wed, 29 Jul 2020 12:03:16 -0700 Subject: [PATCH] updated the calculator.py function with code --- calculator.py | 92 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 81 insertions(+), 11 deletions(-) diff --git a/calculator.py b/calculator.py index a46affd..6a3bb78 100644 --- a/calculator.py +++ b/calculator.py @@ -45,13 +45,46 @@ def add(*args): """ Returns a STRING with the sum of the arguments """ - # TODO: Fill sum with the correct value, based on the + # DONE: Fill sum with the correct value, based on the # args provided. - sum = "0" - + sum = int(args[0])+int(args[1]) return sum -# TODO: Add functions for handling more arithmetic operations. + +def sub(*args): + """ Returns a STRING with the sum of the arguments """ + + # DONE: Fill sum with the correct value, based on the + # args provided. + val = int(args[0])-int(args[1]) + return val + + +def multi(*args): + """ Returns a STRING with the sum of the arguments """ + + # DONE: Fill sum with the correct value, based on the + # args provided. + val = int(args[0])*int(args[1]) + return val + +def div(*args): + """ Returns a STRING with the sum of the arguments """ + + # DONE: Fill sum with the correct value, based on the + # args provided. + try: + val = int(args[0])/int(args[1]) + except Exception: + val = "No you can't just divide by zero!" + + return val +# DONE: Add functions for handling more arithmetic operations. + + +def description(*args): + return "To use calc first type operation type, then two operands, all separated by /. Ex: localhost/add/21/20" + def resolve_path(path): """ @@ -59,26 +92,63 @@ def resolve_path(path): arguments. """ - # TODO: Provide correct values for func and args. The + # DONE: 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'] + funcs = { + '': description, + 'add': add, + "divide": div, + "subtract": sub, + "multiply": multi + } + + 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 + # DONE: 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) + if path is None: + raise NameError + func, args = resolve_path(path) + body = func(*args) + status = "200 OK" + except NameError: + status = "404 Not Found" + body = "

Not Found

" + except Exception: + status = "500 Internal Server Error" + body = "

Internal Server Error

" + print(traceback.format_exc()) + finally: + #headers.append(('Content-length', str(len(body)))) + start_response(status, headers) + return [str(body).encode('utf8')] + if __name__ == '__main__': - # TODO: Insert the same boilerplate wsgiref simple + # DONE: 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()