From 2b62cc931c9a8432d714febb825ed728e3ad350e Mon Sep 17 00:00:00 2001 From: allen maxwell Date: Sun, 20 Jun 2021 15:06:07 -0700 Subject: [PATCH] Update calculator.py --- calculator.py | 92 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 26 deletions(-) diff --git a/calculator.py b/calculator.py index a46affd..983120a 100644 --- a/calculator.py +++ b/calculator.py @@ -40,45 +40,85 @@ """ - +import traceback def add(*args): - """ Returns a STRING with the sum of the arguments """ + """ Returns a STRING with the addition result of the arguments """ + return str(int(args[0]) + int(args[1])) + +def sub(*args): + """ Returns a STRING with the subtraction result of the arguments """ + return str(int(args[0]) - int(args[1])) - # TODO: Fill sum with the correct value, based on the - # args provided. - sum = "0" +def mult(*args): + """ Returns a STRING with the multiplication result of the arguments """ + return str(int(args[0]) * int(args[1])) - return sum +def div(*args): + """ Returns a STRING with the division result of the arguments """ + return str(int(args[0]) / int(args[1])) -# TODO: Add functions for handling more arithmetic operations. +def guide(*args): + """ Returns a STRING with the instructions for producing a result for the arguments """ + body = "

This is an online calculator

"\ + "To get your result, enter the address in the browser in the following format:\n"\ + "operation/number1/number2 (i.e. add/2/4)" + return body def resolve_path(path): """ - Should return two values: a callable and an iterable of + Returns 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'] + funcs = { + "":guide, + "add":add, + "sum":add, + "sub":sub, + "subtract":sub, + "mult":mult, + "multiply":mult, + "div":div, + "divide":div, + } + 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 - # 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 ZeroDivisionError: + status = "500 Inernal Server Error" + body = "

Division by Zero Error

" + 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 [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()