From b756288bb73d9fda063b60baabbadf6e580d451e Mon Sep 17 00:00:00 2001 From: Dan K Date: Tue, 24 Apr 2018 20:55:09 -0700 Subject: [PATCH 1/6] 1st thing done added boilerplate wsgi thing --- calculator.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/calculator.py b/calculator.py index fad858d..bdc4e46 100644 --- a/calculator.py +++ b/calculator.py @@ -80,6 +80,6 @@ def application(environ, start_response): 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 848dc4f184df9aadc403d3b998b6805bc71abd3b Mon Sep 17 00:00:00 2001 From: Dan K Date: Thu, 26 Apr 2018 08:16:19 -0700 Subject: [PATCH 2/6] blah blah --- calculator.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/calculator.py b/calculator.py index bdc4e46..76e78a1 100644 --- a/calculator.py +++ b/calculator.py @@ -41,7 +41,14 @@ """ - +def home_page(): +

{title}

+ + + + +
Author{author}
Publisher{publisher}
ISBN{isbn}
+ Back to the list def add(*args): """ Returns a STRING with the sum of the arguments """ @@ -55,6 +62,8 @@ def add(*args): # TODO: Add functions for handling more arithmetic operations. def resolve_path(path): + func, arg1, arg2 = path.split('/') + """ Should return two values: a callable and an iterable of arguments. @@ -64,6 +73,12 @@ def resolve_path(path): # examples provide the correct *syntax*, but you should # determine the actual values of func and args using the # path. + + try: + + except NameError: + print("Please select an appropriate function.") + func = add args = ['25', '32'] From cec8c66a9bca01f564f78e25450f423089dc601a Mon Sep 17 00:00:00 2001 From: Dan K Date: Thu, 3 May 2018 22:13:53 -0700 Subject: [PATCH 3/6] made some initial updates --- calculator.py | 43 ++++++++++++++++++++++++++++++++++++------- funkyfunctest.py | 9 +++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 funkyfunctest.py diff --git a/calculator.py b/calculator.py index 76e78a1..3a18dfe 100644 --- a/calculator.py +++ b/calculator.py @@ -56,14 +56,32 @@ def add(*args): # TODO: Fill sum with the correct value, based on the # args provided. sum = "0" + output = args[0] + args[1] + return str.output - return sum + +def subtract(*args): + output = args[0] - args[1] + return str.output + + +def multiply(*args): + output = args[0] * args[1] + return str.output + + +def divide(*args): + if (args[1] == 0): + print("Undefined!!! You tried to divide by zero!") + break + else: + output = args[0] / args[1] + return str.output # TODO: Add functions for handling more arithmetic operations. def resolve_path(path): - func, arg1, arg2 = path.split('/') - + funcname, arg1, arg2 = path.split('/') """ Should return two values: a callable and an iterable of arguments. @@ -73,18 +91,29 @@ def resolve_path(path): # examples provide the correct *syntax*, but you should # determine the actual values of func and args using the # path. - try: + arg1val = int(arg1) + arg2val = int(arg2) + except ValueError: + print("Please enter a valid number.") + + + try: + func = {"add": add, + "subtract": subtract, + "multiply": multiply, + "divide": divide}.get(funcname) except NameError: - print("Please select an appropriate function.") + print("Please select an appropriate function.") - func = add - args = ['25', '32'] + args = [arg1val, arg2val] return func, args def application(environ, start_response): + + functionoutput = func(args) # 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 diff --git a/funkyfunctest.py b/funkyfunctest.py new file mode 100644 index 0000000..8d96502 --- /dev/null +++ b/funkyfunctest.py @@ -0,0 +1,9 @@ +#funkyfunctest.py + +def funktest(*args): + print(inputval) + +values = funktest +inputval = "blamalama" + +thingy = values(inputval) \ No newline at end of file From 5f154b590f510b1ae4c53e19ddfafd3cc2d5deca Mon Sep 17 00:00:00 2001 From: Dan K Date: Mon, 7 May 2018 20:20:47 -0700 Subject: [PATCH 4/6] I think it works It works but I am pretty sure I cheated on the 400 error. --- .DS_Store | Bin 0 -> 6148 bytes calculator.py | 96 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 61 insertions(+), 35 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..4c889e0f4edc24810a0688025fbbaaa03afe6536 GIT binary patch literal 6148 zcmeH~y{ZC1422WjLa^D=avIwUzQG{+1ipZ%AcA0__VehT{2;jAT0~AD`6ihO%bsFq zBOyQ5diE^cEj3d39wiJtjQKc1*XvoMyvW5 zVs&o^OP;IA7L0b$96mIktTx5KG}=WA5|~y80~MeG0|myB_jdoU;s4G5gBGS#fC~JW z0@|%Mt0i74@7A~1v-&oxwr+6Hk0ZSN1R$}icm{XFez65ulP!n}j6VV{0|OQKr~(gE CJ`!~R literal 0 HcmV?d00001 diff --git a/calculator.py b/calculator.py index 3a18dfe..a371dea 100644 --- a/calculator.py +++ b/calculator.py @@ -40,6 +40,7 @@ * Submit a link to your Session03 fork repository! +""" """ def home_page():

{title}

@@ -49,71 +50,72 @@ def home_page(): ISBN{isbn} Back to the list +""" 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" output = args[0] + args[1] - return str.output + print("Output Value: {}".format(output)) + returnval = str(output) + print("ReturnVal: {}".format(returnval)) + return returnval def subtract(*args): output = args[0] - args[1] - return str.output + returnval = str(output) + return returnval def multiply(*args): output = args[0] * args[1] - return str.output + returnval = str(output) + return returnval def divide(*args): if (args[1] == 0): print("Undefined!!! You tried to divide by zero!") - break + output = "Undefined - You tried to divide by zero!" else: - output = args[0] / args[1] - return str.output + output = str(args[0] / args[1]) + return output # TODO: Add functions for handling more arithmetic operations. + def resolve_path(path): - funcname, arg1, arg2 = path.split('/') - """ - Should return 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. + print("Entered resolve_path") + func = {"add": add, + "subtract": subtract, + "multiply": multiply, + "divide": divide} + crackedpath = path.split('/') + print(crackedpath) + funcname = crackedpath[-3] + arg1 = crackedpath[-2] + arg2 = crackedpath[-1] + print("Vals: {}, {}, {}".format(funcname, arg1, arg2)) + print("FunctionCall: {}".format(funcname)) try: arg1val = int(arg1) arg2val = int(arg2) + print("Vals OK") except ValueError: print("Please enter a valid number.") - - - - try: - func = {"add": add, - "subtract": subtract, - "multiply": multiply, - "divide": divide}.get(funcname) - except NameError: - print("Please select an appropriate function.") - + return NameError + if funcname in func.keys(): + print("function name found") + func = func.get(funcname) + else: + print("Function name not found") + return NameError + print("Selected function: {}".format(funcname)) args = [arg1val, arg2val] - return func, args def application(environ, start_response): - functionoutput = func(args) + #functionoutput = func(args) # 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 @@ -121,7 +123,31 @@ 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 + print("About to call path resolve") + func, args = resolve_path(path) + print("CalledResolve_path") + body = func(*args) + print("Called Body") + if body == "Undefined - You tried to divide by zero!": + status = "400 - Bad Request" + else: + status = "200 OK" + except NameError: + print("Triggered Nameerror") + status = "404 Not Found" + body = "

Not Found

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

Internal Server Error

" + finally: + headers.append(('Content-length', str(len(body)))) + start_response(status, headers) + return [body.encode('utf8')] if __name__ == '__main__': from wsgiref.simple_server import make_server From ee65e01088c0a7503134059765b4bf64f08846bf Mon Sep 17 00:00:00 2001 From: Dan K Date: Mon, 7 May 2018 20:32:28 -0700 Subject: [PATCH 5/6] Yeah Homepage doesnt work --- calculator.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/calculator.py b/calculator.py index a371dea..7547eb9 100644 --- a/calculator.py +++ b/calculator.py @@ -41,16 +41,6 @@ """ -""" -def home_page(): -

{title}

- - - - -
Author{author}
Publisher{publisher}
ISBN{isbn}
- Back to the list -""" def add(*args): output = args[0] + args[1] From e4408640ed2a2c400114302ef3c780f7404dc1bc Mon Sep 17 00:00:00 2001 From: Dan K Date: Mon, 7 May 2018 20:59:15 -0700 Subject: [PATCH 6/6] Added a homepage Added a homepage! --- calculator.py | 61 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/calculator.py b/calculator.py index 7547eb9..d33ef2e 100644 --- a/calculator.py +++ b/calculator.py @@ -41,6 +41,20 @@ """ +def homepage(*args): + page = """ + + AWESOME CALCULATOR + + +

Calculator Instructions

+

Please enter a URL containing a mathematical function and two values.

+

Such as this one shown - "http://localhost:8080/divide/22/11"

+

Functions such as "add", "subtract", "multiply" and "divide" are available.

+ + """ + return page + def add(*args): output = args[0] + args[1] @@ -75,32 +89,37 @@ def divide(*args): def resolve_path(path): print("Entered resolve_path") - func = {"add": add, + availablefuncs = {"add": add, "subtract": subtract, "multiply": multiply, "divide": divide} crackedpath = path.split('/') - print(crackedpath) - funcname = crackedpath[-3] - arg1 = crackedpath[-2] - arg2 = crackedpath[-1] - print("Vals: {}, {}, {}".format(funcname, arg1, arg2)) - print("FunctionCall: {}".format(funcname)) - try: - arg1val = int(arg1) - arg2val = int(arg2) - print("Vals OK") - except ValueError: - print("Please enter a valid number.") - return NameError - if funcname in func.keys(): - print("function name found") - func = func.get(funcname) + if crackedpath[1] == '': + func = homepage + args = [0,0] + return func, args else: - print("Function name not found") - return NameError - print("Selected function: {}".format(funcname)) - args = [arg1val, arg2val] + print(crackedpath) + funcname = crackedpath[-3] + arg1 = crackedpath[-2] + arg2 = crackedpath[-1] + print("Vals: {}, {}, {}".format(funcname, arg1, arg2)) + print("FunctionCall: {}".format(funcname)) + try: + arg1val = int(arg1) + arg2val = int(arg2) + print("Vals OK") + except ValueError: + print("Please enter a valid number.") + return NameError + if funcname in availablefuncs.keys(): + print("function name found") + func = availablefuncs.get(funcname) + else: + print("Function name not found") + return NameError + print("Selected function: {}".format(funcname)) + args = [arg1val, arg2val] return func, args def application(environ, start_response):