diff --git a/calculator.py b/calculator.py index a46affd..f794caa 100644 --- a/calculator.py +++ b/calculator.py @@ -1,3 +1,5 @@ +import traceback + """ For your homework this week, you'll be creating a wsgi application of your own. @@ -42,16 +44,49 @@ """ +def home(): + + body = """ +

Calculator Home

+

To use this calculator, input the operator and two "/" separated values in the url path: + """ + + 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" + body = '

Sum of {} and {}: {}

'.format(*args, sum(args)) + return body + + +def subtract(*args): + """ Returns a STRING with the difference of the arguments """ + + body = '

Difference of {} and {}: {}

'.format(*args, args[0]-args[1]) + return body - return sum -# TODO: Add functions for handling more arithmetic operations. +def multiply(*args): + """ Returns a STRING with the product of the arguments """ + + body = '

Product of {} and {}: {}

'.format(*args, args[0]*args[1]) + return body + + +def divide(*args): + """ Returns a STRING with the quotient of the arguments """ + + try: + body = '

Quotient of {} and {}: {}

'.format(*args, args[0]/args[1]) + except ZeroDivisionError: + body = '

Cannot divide by zero!

' + return body + def resolve_path(path): """ @@ -59,26 +94,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, + 'subtract': subtract, + 'multiply': multiply, + 'divide': divide, + } + path = path.strip('/').split('/') + func_name = path[0] + args = path[1:] + args = [int(i) for i in args] + + 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) + print('path: {}'.format(path)) + if path is None: + raise NameError + func, args = resolve_path(path) + body = ['

WSGI Calculator

'] + body.append(func(*args)) + body = '\n'.join(body) + 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) + print(body) + 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()