From d342a2fcd53bd3ce170f290670ec4d7c32d77dcc Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Thu, 17 Sep 2020 14:06:51 -0400 Subject: [PATCH 1/4] sp_py230 lesson4, initial with main and rsolv path --- calculator.py | 140 +++++++++++++++++++++++++++++--- goStartWsgiServerCalculator.bat | 3 + 2 files changed, 132 insertions(+), 11 deletions(-) create mode 100644 goStartWsgiServerCalculator.bat diff --git a/calculator.py b/calculator.py index a46affd..4edc4e3 100644 --- a/calculator.py +++ b/calculator.py @@ -1,3 +1,16 @@ +import re +import traceback + +TEST_BODY = """ + +Test Mike - WSGI Assignment + + +

Hello world Mike

+ +""" + + """ For your homework this week, you'll be creating a wsgi application of your own. @@ -41,19 +54,97 @@ """ +# DONE: Include other functions for handling more arithmetic operations. def add(*args): """ Returns a STRING with the sum of the arguments """ - # TODO: Fill sum with the correct value, based on the + # DONE: Fill sum with the correct value, based on the # args provided. - sum = "0" + print (f"ddddddddddd arg1={args[0]} arg2={args[1]}") + oper_a = args[0] + oper_b = args[1] + + sum = str(int(oper_a) + int(oper_b)) + + body = sum + return sum -# TODO: Add functions for handling more arithmetic operations. +def subtract(*args): + """ Returns a STRING with the difference of the arguments """ + + # DONE: Fill difference with the correct value, based on the + # args provided. + + print (f"eeeeeeeeee arg1={args[0]} arg2={args[1]}") + minuend = args[0] + subtrahend = args[1] + + difference = str(int(minuend) - int(subtrahend)) + body = difference + + return difference + + +def divide(*args): + """ Returns a STRING with the difference of the arguments """ + + # DONE: Fill quotient with the correct value, based on the + # args provided. + + print (f"fffffffffff arg1={args[0]} arg2={args[1]}") + dividend = args[0] + divisor = args[1] + + quotient = str(int(dividend) / int(divisor)) + + body = quotient + + return quotient + + def resolve_path(path): + + print ("CCCCCCCCC 111111111111") + + funcs = { + '': add, + 'book': add, + 'add': add, + 'subtract': subtract, + 'divide': divide, + + } + + print("CCCCC 22222222222") + + path = path.strip('/').split('/') + + print("CCCCC 22222222222") + + func_name = path[0] + args = path[1:] + + print("CCCCC 22222222222") + + # DONE: 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. + + try: + func = funcs[func_name] + except KeyError: + raise NameError + + print("CCCCC 33333333") + + return func, args + +def resolve_path_TODO(path): """ Should return two values: a callable and an iterable of arguments. @@ -69,16 +160,43 @@ 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. - # - # TODO (bonus): Add error handling for a user attempting - # to divide by zero. - pass + # Done: + print("BBBBBB") + + pass + + body = "" + headers = [("Content-type", "text/html")] + try: + path = environ.get('PATH_INFO', None) + print(f"BBBBBB path = -{path}-") + if path is None: + raise NameError + body = TEST_BODY #***MMM temp only test + print("BBBBBB calling resolve_path") + func, args = resolve_path(path) + print("BBBBBB return from resolve_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 +if __name__ == '__main__': + from wsgiref.simple_server import make_server + srv = make_server('localhost', 8080, application) + srv.serve_forever() diff --git a/goStartWsgiServerCalculator.bat b/goStartWsgiServerCalculator.bat new file mode 100644 index 0000000..85f8407 --- /dev/null +++ b/goStartWsgiServerCalculator.bat @@ -0,0 +1,3 @@ + +cd C:\A_uwPython\SP_Online_PY230\lesson4\wsgi-calculator\wsgi-calculator +start python -u calculator.py From bf3848dadbe6d95884f9922fadfd37c145ad22f6 Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Thu, 17 Sep 2020 14:59:52 -0400 Subject: [PATCH 2/4] sp_py230 lesson4, include mult divid and refact --- calculator.py | 56 +++++++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/calculator.py b/calculator.py index 4edc4e3..d6628b9 100644 --- a/calculator.py +++ b/calculator.py @@ -62,7 +62,6 @@ def add(*args): # DONE: Fill sum with the correct value, based on the # args provided. - print (f"ddddddddddd arg1={args[0]} arg2={args[1]}") oper_a = args[0] oper_b = args[1] @@ -72,13 +71,27 @@ def add(*args): return sum +def multiply(*args): + """ Returns a STRING with the product of the arguments """ + + # DONE: Fill product with the correct value, based on the + # args provided. + + multiplicand = args[0] + multiplier = args[1] + + product = str(int(multiplicand) * int(multiplier)) + + body = product + + return product + def subtract(*args): """ Returns a STRING with the difference of the arguments """ # DONE: Fill difference with the correct value, based on the # args provided. - print (f"eeeeeeeeee arg1={args[0]} arg2={args[1]}") minuend = args[0] subtrahend = args[1] @@ -95,7 +108,6 @@ def divide(*args): # DONE: Fill quotient with the correct value, based on the # args provided. - print (f"fffffffffff arg1={args[0]} arg2={args[1]}") dividend = args[0] divisor = args[1] @@ -108,28 +120,20 @@ def divide(*args): def resolve_path(path): - print ("CCCCCCCCC 111111111111") - funcs = { '': add, - 'book': add, 'add': add, 'subtract': subtract, + 'multiply': multiply, 'divide': divide, } - print("CCCCC 22222222222") - path = path.strip('/').split('/') - print("CCCCC 22222222222") - func_name = path[0] args = path[1:] - print("CCCCC 22222222222") - # DONE: 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 @@ -140,30 +144,14 @@ def resolve_path(path): except KeyError: raise NameError - print("CCCCC 33333333") - - return func, args - -def resolve_path_TODO(path): - """ - 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. - func = add - args = ['25', '32'] - return func, args def application(environ, start_response): - # Done: + # DONE: 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. - print("BBBBBB") - pass body = "" @@ -193,10 +181,8 @@ def application(environ, start_response): if __name__ == '__main__': - # TODO: Insert the same boilerplate wsgiref simple + # DONE: Insert the same boilerplate wsgiref simple # server creation that you used in the book database. - pass -if __name__ == '__main__': from wsgiref.simple_server import make_server srv = make_server('localhost', 8080, application) srv.serve_forever() From 77500435736f7c72d53d1f1be07786e86f3ff924 Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Thu, 17 Sep 2020 17:40:31 -0400 Subject: [PATCH 3/4] sp_py230 lesson4, add instrs page --- calculator.py | 52 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/calculator.py b/calculator.py index d6628b9..aaf4779 100644 --- a/calculator.py +++ b/calculator.py @@ -1,15 +1,6 @@ import re import traceback -TEST_BODY = """ - -Test Mike - WSGI Assignment - - -

Hello world Mike

- -""" - """ For your homework this week, you'll be creating a wsgi application of @@ -54,8 +45,43 @@ """ -# DONE: Include other functions for handling more arithmetic operations. +dict_instrs = { + 'instrs': { + 'add_description': 'Add two numbers', + 'add_example': 'http://localhost:8080//add/10/5', + 'subtract_description': 'Subtract two numbers', + 'subtract_example': 'http://localhost:8080//subtract/10/5', + 'multiply_description': 'Multiply two numbers', + 'multiply_example': 'http://localhost:8080/multiply//10/5', + 'divide_description': 'Divide two numbers', + 'divide_example': 'http://localhost:8080/divide//10/5', + }, + 'help1': { + }, + 'help2': { + }, + 'help3': { + }, +} + +# DONE: Include usage instructions home page + +def calculator_usage_instructions(*args): + page = """ +

"Calculator Usage Instructions:"

+ + + + + + +
Usage: Operator//arg1//arg2"site: http://localhost:8080"
{add_description}{add_example}
{subtract_description}{subtract_example}
{multiply_description}{multiply_example}
{divide_description}{divide_example}
+""" + instrs = dict_instrs['instrs'] + return page.format(**instrs) +# DONE: Include other functions for handling more arithmetic operations. + def add(*args): """ Returns a STRING with the sum of the arguments """ @@ -121,7 +147,7 @@ def divide(*args): def resolve_path(path): funcs = { - '': add, + '': calculator_usage_instructions, 'add': add, 'subtract': subtract, 'multiply': multiply, @@ -158,13 +184,9 @@ def application(environ, start_response): headers = [("Content-type", "text/html")] try: path = environ.get('PATH_INFO', None) - print(f"BBBBBB path = -{path}-") if path is None: raise NameError - body = TEST_BODY #***MMM temp only test - print("BBBBBB calling resolve_path") func, args = resolve_path(path) - print("BBBBBB return from resolve_path") body = func(*args) status = "200 OK" except NameError: From 4e6427864e94bd0fe99fb28a2a76ec52678d5495 Mon Sep 17 00:00:00 2001 From: mmanc125uw Date: Fri, 18 Sep 2020 07:50:39 -0400 Subject: [PATCH 4/4] sp_py230 lesson4, final refactor and testing --- __pycache__/tests.cpython-37.pyc | Bin 0 -> 3694 bytes goTesting.bat | 4 ++++ testing_results.txt | 11 +++++++++++ 3 files changed, 15 insertions(+) create mode 100644 __pycache__/tests.cpython-37.pyc create mode 100644 goTesting.bat create mode 100644 testing_results.txt diff --git a/__pycache__/tests.cpython-37.pyc b/__pycache__/tests.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..28582db8ad9c57b65321ca95b62f4bb94f2fa137 GIT binary patch literal 3694 zcmdT{-EQ1O6!!RUo&CvANl6P73g%4NYST0CX;i1i3ym5~dt^N~JeyUh`B>+7 znKp0^Gy?P2wWf6lYiX+?%F&}k<2DrW9SEl3+MZ4ctgTk`Olnf=h2~k*rWL4d>d+~u zE3`^$P&>3v8&FTtX*vUSmCn*RsB82LeFN$`orj&z=Cy~cy}^WB4g~96kkAtDR?1zu z!`z4K_a3;*K^$grAX6R^Fa;m1Ehx)S#JeD*_J~kI^=%zms!-!+qp$gZ1ngq2dUkhD z?xabt5thp>cK4ckZVO4%Osbl3-Zb;7$l6_=hD?auT1&eu$<4I~Yb*J*VEifL{&28K za#OH)>lck9T`yOcZ*1Q2v)whd)8_h`|4kA{3G>&!zxw&oW(@14$+gX0u^nAGvf@-< zK>)VpmSFPRE-o#UI}lUXp_rsMH-_1w(mDnWQm3Ff2aUe=NE;BOTD5dVI)Xx0n+A74 zBR3yMaqQK?wA0CwC=4WP5bnY2Lq$ijfPa zO0sT32gw?MD;% zHqUE;0Edw)PqHA+D=-qKl;u_!r-C(21+Zs;QJxvXQn@plcoOwGO!Y^DNU5lU(5yPC z>kgUK9ldvMoPlxm(l3yKl7%0jBf%^1$Qt($f_e+ubAne1j7=3|)B>LD-e)DUcU-_VMtWKSrLCaVcK0F{QvuXma@(ceAqBgl z*^f1?S5=zN_FK>P`3$^WZB}!e2MJ9(c?Ihzfg=m|L?@8^EGY0f6lXx>PJ#P_BsYZQ zxy=#kUm%o#*E`#*Q!hAO66mi^995PeU6_;aU5y>7QTBM#XBf2fEWVI z@mA*Vp>R=5!nmO@9&P*usEs-H5a{#cj(tr3HPAMw3Er$z>y@5uPx9<@r_ru}XFI(+ zlhB5sA$btWV`#fq-1cP^&I0$=n5Qe$qrp!^D@gdm0H2>}m23QaB&dk%k2dzZsfZ-p zmU!QQ`>aePrFn{!SIz(5k#uv6q$6sM==x8Pl(6k!jHHd%CF#Wck|Y}zDq}<&I%9F6 zh!Mk+g!leDL~3=S3>7OdLKuA$Vuht*h26JK#)@H_u*x{`7pI9vPb12XouyWJl0I_V z?w9UTc?u6P)i}f)>4>skqcvRP@17;Qh!}QpB8odr-EJ;!IO$@msf()EJ_IE(4Fck| zUMG$|yx0~_ouQ%rD96QbD#^GC9e@?GbpV(^3j%i^lBhBm8xXc3d4hMt08LG}s;pjC zg0unobd*r`quUL(nHxxzVe>SVZn~w~MRx0o9Sqh{62=+DDG)<5mF9?YEy%TY(p?Pb z86;VlspFc9<@ig|Qc*iMAcC$o?TK;YC}ikGvN;LL1qG$D+aEVY0w4nYRkI-=8@-FC z;5 testing_results.txt 2>&1 +type testing_results.txt + + diff --git a/testing_results.txt b/testing_results.txt new file mode 100644 index 0000000..0230d3b --- /dev/null +++ b/testing_results.txt @@ -0,0 +1,11 @@ +test_add (tests.WebTestCase) ... ok +test_divide (tests.WebTestCase) ... ok +test_index_instructions (tests.WebTestCase) ... ok +test_multiply (tests.WebTestCase) ... ok +test_subtract_negative_result (tests.WebTestCase) ... ok +test_subtract_positive_result (tests.WebTestCase) ... ok + +---------------------------------------------------------------------- +Ran 6 tests in 6.186s + +OK