diff --git a/__pycache__/tests.cpython-37.pyc b/__pycache__/tests.cpython-37.pyc new file mode 100644 index 0000000..e4680e8 Binary files /dev/null and b/__pycache__/tests.cpython-37.pyc differ diff --git a/calculator.py b/calculator.py index a46affd..28fce9a 100644 --- a/calculator.py +++ b/calculator.py @@ -45,13 +45,92 @@ 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" + if len(args) != 2: + raise NameError + + try: + total = int(args[0]) + int(args[1]) + body = f"{total}" + except (ValueError, TypeError): + body = "Input not valid" + + return f'

{body}

' + +def divide(*args): + """ Returns a STRING with the divide of the arguments """ + + if len(args) != 2: + raise NameError + + try: + total = int(args[0]) / int(args[1]) + body = f"{total}" + except (ValueError, TypeError): + body = "Input not valid" + except (ZeroDivisionError): + body = "Can't Divide by 0" + + return f'

{body}

' + +def multiply(*args): + """ Returns a STRING with the multiply of the arguments """ + + if len(args) != 2: + raise NameError + + try: + total = int(args[0]) * int(args[1]) + body = f"{total}" + except (ValueError, TypeError): + body = "Input not valid" + + return f'

{body}

' + +def subtract(*args): + """ Returns a STRING with the subtract of the arguments """ + + if len(args) != 2: + raise NameError + + try: + total = int(args[0]) - int(args[1]) + body = f"{total}" + except (ValueError, TypeError): + body = "Input not valid" + except (ZeroDivisionError): + body = "Can't Divide by 0" + + return f'

{body}

' + +def home(*args): + """ Returns a STRING with the instuctions for site """ + body = """ +

This is an online calculator that can perform several operations.

+

It supports:

+ +

+ If you open a browser to your wsgi application at + `http://localhost:8080/multiple/3/5' then the response body in my + browser should be `15`. +

+ +

Examples:

+ - return sum + """ -# TODO: Add functions for handling more arithmetic operations. + return body def resolve_path(path): """ @@ -59,26 +138,53 @@ 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'] + funcs = { + '': home, + 'add': add, + 'divide': divide, + 'multiply': multiply, + 'subtract': subtract, + } + + 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 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()