From 4d54755b4987e950ef8376c6dc5bdffc1639ed02 Mon Sep 17 00:00:00 2001 From: zyang2020 <62482157+zyang2020@users.noreply.github.com> Date: Sat, 13 Jun 2020 20:46:14 -0700 Subject: [PATCH 1/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8bc427c..116a97c 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ You'll also have to create one more page: an index page at the address http://lo ## How to Know When You're Done When you have completed the TODOs, you should be able to visit the following pages and see a page with the indicated content.: - * http://localhost:8080/multiply/3/5  => 15 + * [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 From 4d18f706314616d8fd59b51318c5150b9ecd4273 Mon Sep 17 00:00:00 2001 From: zyang2020 <62482157+zyang2020@users.noreply.github.com> Date: Sat, 13 Jun 2020 20:46:43 -0700 Subject: [PATCH 2/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 116a97c..8bc427c 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ You'll also have to create one more page: an index page at the address http://lo ## How to Know When You're Done When you have completed the TODOs, you should be able to visit the following pages and see a page with the indicated content.: - * [http://localhost:8080/multiply/3/5]  => 15 + * 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 From 0c3106d8f80f4a55cc5b71bae39e8d3b9ab88838 Mon Sep 17 00:00:00 2001 From: zyang2020 <62482157+zyang2020@users.noreply.github.com> Date: Sat, 13 Jun 2020 20:59:02 -0700 Subject: [PATCH 3/4] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8bc427c..2165854 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,15 @@ Your assignment is to create a WSGI calculator that you can use to add, subtract You'll use the calculator by visiting an address like: [http://localhost:8080/multiply/3/5](http://localhost:8080/multiply/3/5). Once you've completed the assignment, if you were to run the program and visit this page then you would expect to see a page in your browser with the text "15". -You'll also have to create one more page: an index page at the address http://localhost:8080/ with some text instructions that explain (in just a few sentences) how to use the site. +You'll also have to create one more page: an index page at the address [http://localhost:8080/](http://localhost:8080/) with some text instructions that explain (in just a few sentences) how to use the site. ## How to Know When You're Done When you have completed the TODOs, you should be able to visit the following pages and see a page with the indicated content.: - * 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... (etc.) + * [httpL//localhost:8080/add/3/5](http://localhost:8080/multiply/3/5)  => 15 + * [http://localhost:8080/add/23/42](http://localhost:8080/add/23/42)  => 65 + * [http://localhost:8080/subtract/23/42](http://localhost:8080/subtract/23/42)  => -19 + * [http://localhost:8080/divide/22/11](http://localhost:8080/divide/22/11)  => 2 + * [http://localhost:8080/](http://localhost:8080/)  => Here's how to use this page... (etc.) There is also a set of tests for you to run, using `python tests.py`. From a403a762016ebfa79577dfa121d1434c1ad4ed4b Mon Sep 17 00:00:00 2001 From: Zhen Yang Date: Sat, 13 Jun 2020 21:11:40 -0700 Subject: [PATCH 4/4] finished assignment for lesson04 --- calculator.py | 106 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 94 insertions(+), 12 deletions(-) diff --git a/calculator.py b/calculator.py index a46affd..9cdab27 100644 --- a/calculator.py +++ b/calculator.py @@ -1,3 +1,6 @@ +import re +import traceback + """ For your homework this week, you'll be creating a wsgi application of your own. @@ -44,14 +47,50 @@ 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" - - return sum - -# TODO: Add functions for handling more arithmetic operations. + try: + res = str(int(args[0]) + int(args[1])) + return '{} + {} = {}'.format(args[0], args[1], res) + except ValueError: + raise ValueError + +def substract(*args): + """ Returns a STRING with the subtract of the arguments """ + try: + res = str(int(args[0]) - int(args[1])) + return '{} - {} = {}'.format(args[0], args[1], res) + except ValueError: + raise ValueError + +def multiply(*args): + """ Returns a STRING with the multiply of the arguments """ + try: + res = str(int(args[0]) * int(args[1])) + return '{} * {} = {}'.format(args[0], args[1], res) + except ValueError: + raise ValueError + +def divide(*args): + """ Returns a STRING with the divide of the arguments """ + try: + res = str(int(args[0]) / int(args[1])) + return '{} / {} = {}'.format(args[0], args[1], res) + except ValueError: + if args[1] == 0: + raise ZeroDivisionError + raise ValueError + +def instructions(): + """ Returns a STRING about how to use the calculator """ + ins = """ +

Here is how to use the calculator:

+
  • Input URL http://localhoat:8080/add/2/3 for 2 + 3
  • +
  • Input URL http://localhoat:8080/multiply/2/3 for 2 * 3
  • +
  • Input URL http://localhoat:8080/subtract/2/3 for 2 - 3
  • +
  • Input URL http://localhoat:8080/divide/2/3 for 2 / 3
  • + """ + return ins def resolve_path(path): """ @@ -63,10 +102,23 @@ def resolve_path(path): # examples provide the correct *syntax*, but you should # determine the actual values of func and args using the # path. - func = add - args = ['25', '32'] - - return func, args + functions = { + '': instructions, + 'add': add, + 'subtract': substract, + 'multiply': multiply, + 'divide': divide + } + path = path.strip('/').split('/') + func_name = path[0] + func_args = path[1:] + + try: + func = functions[func_name] + except KeyError: + raise NameError + finally: + return func, func_args def application(environ, start_response): # TODO: Your application code from the book database @@ -76,9 +128,39 @@ def application(environ, start_response): # # 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) + #if len(args) == 1 or len(args) > 2: + # raise ValueError + body = func(*args) + status = '200 OK' + except ValueError: + status = '500 Internal Server Error' + body = '

    Unable to finish the calculation. Please provide two numbers.

    ' + except NameError: + status = '404 Not Found' + body = '

    404 Operation Method Not Found

    ' + except ZeroDivisionError: + status = '500 Internal Server Error' + body = '

    Zero Division Error!

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

    Internal Server Error

    ' + print(traceback.format_exc()) + finally: + headers.append(('Content-len', 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('127.0.0.1', 8080, application) + #srv = make_server('localhost', 8080, application) + srv.serve_forever()