From 193a37a3c9aadb641b81a2ba6c6984062f5f3321 Mon Sep 17 00:00:00 2001 From: stellie Date: Tue, 15 Dec 2020 14:34:41 -0800 Subject: [PATCH 1/4] update application function status codes using book exercise --- calculator.py | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/calculator.py b/calculator.py index a46affd..d57b3ba 100644 --- a/calculator.py +++ b/calculator.py @@ -41,18 +41,24 @@ """ +# Stella Kim +# Assignemt 4: WSGI Calculator + +import traceback + 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" + sum = '0' return sum # TODO: Add functions for handling more arithmetic operations. + def resolve_path(path): """ Should return two values: a callable and an iterable of @@ -68,17 +74,32 @@ def resolve_path(path): 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. - # + 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')] + # TODO (bonus): Add error handling for a user attempting # to divide by zero. - pass 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() From 0562fb845be1a1870226fe49d265c8b71be54d7f Mon Sep 17 00:00:00 2001 From: stellie Date: Tue, 15 Dec 2020 15:32:29 -0800 Subject: [PATCH 2/4] add functions for different operations, update resolve path function with operation functions --- calculator.py | 90 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 37 deletions(-) diff --git a/calculator.py b/calculator.py index d57b3ba..fcb1dc6 100644 --- a/calculator.py +++ b/calculator.py @@ -1,16 +1,4 @@ """ -For your homework this week, you'll be creating a wsgi application of -your own. - -You'll create an online calculator that can perform several operations. - -You'll need to support: - - * Addition - * Subtractions - * Multiplication - * Division - Your users should be able to send appropriate requests and get back proper responses. For example, if I open a browser to your wsgi application at `http://localhost:8080/multiple/3/5' then the response @@ -26,19 +14,6 @@ http://localhost:8080/ => Here's how to use this page... ``` -To submit your homework: - - * Fork this repository (Session03). - * Edit this file to meet the homework requirements. - * Your script should be runnable using `$ python calculator.py` - * When the script is running, I should be able to view your - application in my browser. - * I should also be able to see a home page (http://localhost:8080/) - that explains how to perform calculations. - * Commit and push your changes to your fork. - * Submit a link to your Session03 fork repository! - - """ # Stella Kim @@ -47,16 +22,45 @@ import traceback +def home(*args): + """Returns homepage that explains how to perform calculations""" + return 'this is the homepage' + + def add(*args): - """ Returns a STRING with the sum of the arguments """ + """Returns a STRING with the sum of the arguments""" + + result = str(int(args[0]) + int(args[1])) + body = 'Your total is: {}'.format(result) + + return body + + +def subtract(*args): + """Returns a STRING with the difference of the arguments""" - # TODO: Fill sum with the correct value, based on the - # args provided. - sum = '0' + result = str(int(args[0]) - int(args[1])) + body = 'Your total is: {}'.format(result) - return sum + return body -# TODO: Add functions for handling more arithmetic operations. + +def multiply(*args): + """Returns a STRING with the product of the arguments""" + + result = str(int(args[0]) * int(args[1])) + body = 'Your total is: {}'.format(result) + + return body + + +def divide(*args): + """Returns a STRING with the quotient of the arguments""" + + result = str(int(args[0]) / int(args[1])) + body = 'Your total is: {}'.format(result) + + return body def resolve_path(path): @@ -65,12 +69,23 @@ 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:] + + try: + func = funcs[func_name] + except KeyError: + raise NameError return func, args @@ -99,6 +114,7 @@ def application(environ, start_response): # TODO (bonus): Add error handling for a user attempting # to divide by zero. + if __name__ == '__main__': from wsgiref.simple_server import make_server srv = make_server('localhost', 8080, application) From f6b649ab878fb3efa652f81a5fc0a05769c6b34d Mon Sep 17 00:00:00 2001 From: stellie Date: Tue, 15 Dec 2020 16:08:46 -0800 Subject: [PATCH 3/4] add homepage text and route to go back to homepage --- calculator.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/calculator.py b/calculator.py index fcb1dc6..e9da30c 100644 --- a/calculator.py +++ b/calculator.py @@ -24,7 +24,26 @@ def home(*args): """Returns homepage that explains how to perform calculations""" - return 'this is the homepage' + + body = """ +

WSGI Calculator

+

Here's how to use this page...

+ +

This application allows you to add, subtract, multiply or divide two + numbers based on the user's URL input. The first part of the path + indicates the operand. The operand is followed by two numbers to solve + the operation.

+ +

Some examples are: +

    +
  • http://localhost:8080/multiply/3/5 yields a result of 15
  • +
  • http://localhost:8080/add/23/42 yields a result of 65
  • +
  • http://localhost:8080/subtract/23/42 yields a result of -19
  • +
  • http://localhost:8080/divide/22/11 yields a result of 2
  • +
+

+ """ + return body def add(*args): @@ -33,7 +52,7 @@ def add(*args): result = str(int(args[0]) + int(args[1])) body = 'Your total is: {}'.format(result) - return body + return(body + '

Back to the homepage

') def subtract(*args): @@ -42,7 +61,7 @@ def subtract(*args): result = str(int(args[0]) - int(args[1])) body = 'Your total is: {}'.format(result) - return body + return(body + '

Back to the homepage

') def multiply(*args): @@ -51,7 +70,7 @@ def multiply(*args): result = str(int(args[0]) * int(args[1])) body = 'Your total is: {}'.format(result) - return body + return(body + '

Back to the homepage

') def divide(*args): @@ -60,7 +79,7 @@ def divide(*args): result = str(int(args[0]) / int(args[1])) body = 'Your total is: {}'.format(result) - return body + return(body + '

Back to the homepage

') def resolve_path(path): From 6c52a9290cfa29b8735531f6d2b57f0e0588401a Mon Sep 17 00:00:00 2001 From: stellie Date: Tue, 15 Dec 2020 16:22:33 -0800 Subject: [PATCH 4/4] add error handling for dividing by zero --- calculator.py | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/calculator.py b/calculator.py index e9da30c..3b5e70d 100644 --- a/calculator.py +++ b/calculator.py @@ -1,21 +1,3 @@ -""" -Your users should be able to send appropriate requests and get back -proper responses. For example, if I open a browser to your wsgi -application at `http://localhost:8080/multiple/3/5' then the response -body in my browser should be `15`. - -Consider the following URL/Response body pairs as tests: - -``` - http://localhost:8080/multiply/3/5 => 15 - http://localhost:8080/add/23/42 => 65 - http://localhost:8080/subtract/23/42 => -19 - http://localhost:8080/divide/22/11 => 2 - http://localhost:8080/ => Here's how to use this page... -``` - -""" - # Stella Kim # Assignemt 4: WSGI Calculator @@ -76,8 +58,11 @@ def multiply(*args): def divide(*args): """Returns a STRING with the quotient of the arguments""" - result = str(int(args[0]) / int(args[1])) - body = 'Your total is: {}'.format(result) + try: + result = str(int(args[0]) / int(args[1])) + body = 'Your total is: {}'.format(result) + except ZeroDivisionError: + body = 'Error dividing by zero, please enter a non-zero value' return(body + '

Back to the homepage

') @@ -130,9 +115,6 @@ def application(environ, start_response): start_response(status, headers) return [body.encode('utf8')] - # TODO (bonus): Add error handling for a user attempting - # to divide by zero. - if __name__ == '__main__': from wsgiref.simple_server import make_server