From c0c83eb522bf7d4adc74455c5e1b188b84d9ed0a Mon Sep 17 00:00:00 2001 From: bplanica Date: Tue, 11 Aug 2020 13:16:44 -0500 Subject: [PATCH] lesson 4 assignment --- __pycache__/tests.cpython-38.pyc | Bin 0 -> 3451 bytes calculator.py | 78 ++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 __pycache__/tests.cpython-38.pyc diff --git a/__pycache__/tests.cpython-38.pyc b/__pycache__/tests.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8da0bd42680fb61e8eead3ae282d50d72360e3b3 GIT binary patch literal 3451 zcmdT{OK;mo5MDk->)}V@!bzWO)3j9*TTYR-L68PXT{rQ?Fc1eR2%`kWUE543QrTT4 zwIZJq^w$3%AN^1I8+hoYm+CL%P;_P`(Uc!Sdnr%~oPBV1IQ-_@nbo6OtxVwheR0`e zsS)xA4vLovgS&8xA3#t-wU~IrR`WD9);&YDrf1$KRHw#kLJdZqSg#DvW+iGq)%kaf z^qm8u!TN`!ZVkW|Sr1VbFC7MV;TAuFU`d)A%vjmfm+8BN@ z4T5NVx@l_Wo9JA^I@BP&JRwZ5UvD z`oN$AZC~%{ec(|~JJ7*Hy>A|neTY=wqDIf?+XrMcUs7LSVD!3=%kJoUz~VOJ?sg{$ zWt1kaOx+zO-N5Bcw9`Z|cblgz81F>SS>kqhe9i5MDA{p$rED*Sal{gtTkB58DFkFU z74p{2<(tc%aesAl@EbT>cQO-Kc=m9KH~nC9b7Lh<5~g14l{kCW0m6C}c>nn2Sp~w* zxLkeK3F52-Pr{V4%nIXFu)4`H#GV08Wo8JbGH1N-IPz6k>W?2{Pz45rSXHg6JKC)7 z=-ms&C=}Hv*nv^VBk>aqByaW;>9(d;wSD#+_umkUn?|&J;1P>SaLvIP-_9{|(;rXW@M#$FYES zs}swp9q&zme&q;qei6U>T@c-wMy_1Ioxg_)mrxvosD^3FAFufuGz#1t;dHUc%?bT~ z36?=kAf!&MGs$V6M$QW-!72eco$kHUz=Cuyc@WA8u-rwrc}*3gz`au-s{(D%|3xDT z!@-{hWd4~(ZsQ+es7jqbUgNh@5lP5bxZ@#ktEXaGKY{64>;IP+-!5XT*m(uXhZN;? z6jxEGoNzWbkKvlIouGj0CRjMh)wke!Z2bhTHW%oyCFG~g#FgQxU+^AGh6L!4S?c`s+35Uukq3mb39qceS zkm{Jp(^R_Yw(1X$cdppQvK1v^+@ZJxs!2x`eUaOOlgDoQ0!v#5jvRPgMVm|c^EX;U zaWylbmaGGcleLO}gy?;Y;u8>cU7>s`oC^w0<+opSMZ!9PBqTPJCZl`l1f(a|tk-Lq z9!~YP%XHh8su_*E{P+UW?h~hem8z_$I zgzDjeg<}j$a0{%^4xDS9vST_k`EO2Xpi`|NN_@XQWSGI Calculator Paths + + + + + +
Addition: add/number/number
Subtraction: subtract/number/number
Multiplication: multiply/number/number
Division: divide/number/number
+ """ + return page 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 + total = str(int(args[0]) + int(args[1])) + return total # TODO: Add functions for handling more arithmetic operations. +def subtract(*args): + total = str(int(args[0]) - int(args[1])) + return total + + +def multiply(*args): + total = str(int(args[0]) * int(args[1])) + return total + + +def divide(*args): + total = str(int(args[0]) / int(args[1])) + return total + + def resolve_path(path): """ Should return two values: a callable and an iterable of @@ -63,11 +90,27 @@ 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'] + 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 + def application(environ, start_response): # TODO: Your application code from the book database # work here as well! Remember that your application must @@ -76,9 +119,30 @@ 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) + 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 + from wsgiref.simple_server import make_server + srv = make_server('localhost', 8080, application) + srv.serve_forever()