From c11b9209dc647aa7c604325cd5f555bfa4f7f034 Mon Sep 17 00:00:00 2001 From: Erica Edwards Date: Sun, 10 Jan 2021 18:49:02 -0800 Subject: [PATCH] Ready to review added additional html to response page added response if user tries to divide by zero added a back button to all response pages. add the html for the index page --- .vscode/settings.json | 12 +++ __pycache__/tests.cpython-37.pyc | Bin 0 -> 3673 bytes calculator.py | 143 +++++++++++++++++++++++++++++-- tests.py | 7 ++ 4 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 __pycache__/tests.cpython-37.pyc diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5b2aee6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,12 @@ +{ + "python.testing.unittestArgs": [ + "-v", + "-s", + ".", + "-p", + "*test*.py" + ], + "python.testing.pytestEnabled": false, + "python.testing.nosetestsEnabled": false, + "python.testing.unittestEnabled": true +} \ No newline at end of file diff --git a/__pycache__/tests.cpython-37.pyc b/__pycache__/tests.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e256127980aa3ee4eb434db15617b0e03fa30ce3 GIT binary patch literal 3673 zcmdT{-EJF26yDiCZ~T*=l9CoEEEEK$Z5#?0rE06Tp|q7y6sf6-mcVMgGjTRu@47SN zmbeZ#t+?YIoJ-z>XV_bA$O~{$Za8Pw-q?wYR3Q+owP$8_&di+ozB6-eTt>6F0SYDUkbCbgbxo<(h1g3_iA zoq@7M%d`TeL#wm~Qb%Q&gk(m#p zIQA-G+HQ9fFjBHQ;WN;qwvdYoDmsz{{EPHS@+PzdI-N$!q_;S3&PXLhEATWb-x%2Y+VFcx*>50gy$5A?_}pP}hjDkS zn}jk-6IZ5gi%BJr-Ic@1+Zs;QJxvXRGBjxcog+24E0By?|?m`3PQ80q^dh) zUU&4~sc{Cz)eFBs1_~CwgN6jJz|*m>?~$=OoZ|uT(Z}z|1n>YnnNWkX-8To;kP~fS z%M$7>Y0n7W8qhbB_fZRYvU{Ht$lh=P*BI$(1e7*{M$_GmSWE>_bIxrRdIuEjisnDm zv|d?hKHY0P-Q#ocb*)~`Y#t;uZD%DcqXbSTTnX(!@{^#z@tp7zATlS%{ce&OLh{UJ zi2~l_ETDBu;hvEoBhAe01ag}%;z&GUlb$Z557sWj_c6SydKnNu>hZbiKacg|%dq~e zz;F!fb~lz$C*GX^{NqEo`RlN^9PYVBp*(>(UqV>$8sVo=Od`$Sz;AdL@V8K$0Wk!a z;|0voS-Fd18pbue>HN{cS3&JK(5J^8dqV#e&^D+E-mFvWrJik1^XyZ{(Jp~!JG~pz z(1!RRc@WA8wA~AC^Sp{&fqQMt(-rE`;76k6B>ZN8&(AfAIsQEoRB-i23;Uf^L=tXG zyl=pLzM4u({TL}P8~?u}>B<;MhtwR>^`9UqVXa_{q;Fo8q$A@Cl5Ci$h!IU_jKzgK zMhs69-usIXsnv-xRII=VVf0Oi6_$z>cHcT0D~55xD&oXnoF*DQjwqWrOO4_rec(3T ztL{p13J);VI8ZmZy3*8Xsx^jl{N1x;=MlrsPegvFsoTwkRi|ET6?IYN%kO}am<0jx zTCWmEA4ZU+V`nJWFUY_>6=Yn52EYo5IRH$c27$W|NmLPx4G7ziJi)hNfTku~RaUQC zfV2UrbCgi_z1s;|%nhVUrg@r5H{DY8Jdt(9HU{e`3F9usArM2mD$NnaT###Rr%y1T zcOl8bq#Wm5F8W`PhKkym0TFbqZcmLHhap4Hlg(){Vo5=%?Doe^c?y6C^jFP>fNb>6 z9)t7vn6-K>b9_Gu+RXQ}vhTN3+KsVZ^?gjzhCL9Mde0+~xMai?#PqoJH|I zijPp7Lvffus2b+W967}C0=MQs?(UQw)0r!N3rYixY6nr``}LX3fec*Yra6-DRmL(^ tJ`Z11M9!QI(7-=OY65;9Ix-XIm}W-iT*-HO4gVsk80!%AE1&2& +Calculator + +{body} + + +""" + return result + +def instructions(*args): + """Index page""" + page = """ +

Calculator

+ +

Instructions:

+ +
    +
  • To access the calculator function in your browser enter http://localhost:8080/.
  • +
  • Enter in the operation you wish to use after the slash "/": Add, Subtract, Multiply, or divide.
  • +
  • After the operation enter slash "/" and the first number in the operation.
  • +
  • Enter another slash "/" followed by the second number in the operation.
  • +
  • Press Enter.
  • +
+ +

Examples:

+ + + + + + + + + + + + + + + + + + + + + + +
OperationExample Link
Addhttp://localhost:8080/add/23/42
Subtracthttp://localhost:8080/subtract/23/42
Multiplyhttp://localhost:8080/multiply/3/5
Dividehttp://localhost:8080/divide/22/11
+ +""" + return page def add(*args): @@ -47,12 +102,42 @@ def add(*args): # TODO: Fill sum with the correct value, based on the # args provided. - sum = "0" - - return sum + # sum = "0" + result = str(sum(args)) + result += '
Back' + return result # TODO: Add functions for handling more arithmetic operations. + +def subtract(*args): + """Returns a string with the result a-b""" + + result = str(args[0] - args[1]) + result += '
Back' + + return result + +def divide(*args): + """Return a string with the result of """ + try: + result = str(args[0] / args[1]) + + except ZeroDivisionError: + result = "You can't divide by zero" + result += '
Back' + + return result + +def multiply(*args): + """Returns the result of a*b""" + + result = str(args[0] * args[1]) + result += '
Back' + + return result + + def resolve_path(path): """ Should return two values: a callable and an iterable of @@ -63,8 +148,30 @@ 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'] + # func = add + # args = ['25', '32'] + + funcs = { + "add": add, + "subtract": subtract, + "multiply": multiply, + "divide": divide, + "instructions" : instructions + } + # print(f"!{path}!") + if "favicon.ico" in path or len(path) < 7: + func_name = "instructions" + left = "0" + right = "0" + else: + func_name, left, right = path.split('/')[1:4] + + args = [int(left), int(right)] + + try: + func = funcs[func_name] + except KeyError: + raise NameError return func, args @@ -76,9 +183,31 @@ def application(environ, start_response): # # TODO (bonus): Add error handling for a user attempting # to divide by zero. - pass + # pass + + status = "200 OK" + headers = [('Content-type', 'text/html')] + try: + path = environ.get('PATH_INFO', None) + func, args = resolve_path(path) + body = html_wrapper(func(*args)) + except NameError: + status = "404 Not Found" + body = html_wrapper("

Not Found

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

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 + # pass + from wsgiref.simple_server import make_server + srv = make_server('localhost', 8080, application) + srv.serve_forever() \ No newline at end of file diff --git a/tests.py b/tests.py index c2a8dcb..b7c435f 100644 --- a/tests.py +++ b/tests.py @@ -3,6 +3,13 @@ import http.client import random +""" +http://localhost:8080/multiply/3/5 +http://localhost:8080/add/23/42 +http://localhost:8080/subtract/23/42 +http://localhost:8080/divide/22/11 +http://localhost:8080/ +""" class WebTestCase(unittest.TestCase): """tests for the WSGI Calculator"""