From a6665d625a05763e5fb061295c50526e189c6361 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 21 May 2020 10:52:35 -0700 Subject: [PATCH] finished assignment --- __pycache__/tests.cpython-38.pyc | Bin 0 -> 3470 bytes calculator.py | 123 ++++++++++++++++++++++++------- 2 files changed, 96 insertions(+), 27 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..ac2ea3791421552f760ed0d6951df7ce2b803a1b GIT binary patch literal 3470 zcmdT{OK;mo5awI79=4OXaMCB+G;NiK<)mp51Zj}gag+357>I)ugb{+`u4N_^sq8M3 znvqWldh36XkNy`u7X1ml_EP~Q$a$C=TCMx(00@q6dj zM=K3Q`2!c_!+^ytIQjP=h(gpzaYv)-s&cKlx*QGHxT6q_=&ux_Q{}PwQgtq3*{!U(dNZT8fixr>K>;nNa?rbH@ z5!=*xc>m5lXT^_#H1b8l0u@}D95^@OT!E8+2tp{2RiYBDqrps`ME^zaD;^*MPcqhA ztG6SzlQ?Sxg~_GfPE*S*E=ZCJIa0=&MqcGsWTfia^p!DMR_eqy4^Gm17A=!1J~u7jYJ*QY(af?J9!>2z=|U$6}75n z3uT8UDgpX1IZ(9-*$r4=$Ob^FJ#|;>YXhKGU)|HdLv3K}DZ3E7fl2hfKCt$b@%4&) z104gJJ*+z8<1USQlsQ{z9EdQ99g#R4DjeTol=qUDQ)i1MU08R*XEb(F7F~2w9>yJK zTZrCL5QPwPLA6?gR;;xfw(Wr%i)lh+Q3=%8dX_<@RLbLFDVO z)tlVJpmGd|V%F8VW~=j>t!1anQ7EfVu>+$}MB))F1aJdCo;__>EzRLmA3#j{cpakw zv4B_^FoVy(Zw$-JcFLqlmcG(HIy0%}E;z2!x%nZ$QoT=FvNwO zLV^6u?Gg6(;#}u~J}fR|_0Vcz4rCguQ5I3iR`DAZKUrz260q&9w^U67K9hR5**`07MN; zTi#^P*Pv12<`}0lWo}OC|4XoRVgMmEVjfFQ>nL)bJ`7d`$Z2Qa9t9Ru0Kxn~Oo8QG za@rSVee#`~C9+D;hJ&9~MPfMoQ-@+O*D7rMBMfEr^Co+|Ucy5G`3k>y2;AzCm^Ke# zdffj1CB`?(7)y4ZNAe*>Srf$t6f!3q&&>n4#U{$etSJ+$)i)0&*l}_-i{$zi{*v%nNaz%MTLpK| zIc?_~XSv}05bN|13(+VCAj(;zHMVh(y-jPBH?0vf**Vcf9F;N@>svz_o1T&!dGAcSV1QXsHrf664r;7q9{u`6fkHd;M%xU5d*$> zGfRs%-+(?dj0ye8>G>V%_(FD`EJ=iuY{~hk<8$~nmaQ-jqLknks3xf_`n<3OoyT_a z97|gYEeCe6XmhE!|5|NHuI4(_k~Khax>m7|5WSC4d;+4WNtBO-b5X)6{r1YP2-wGw zgo*{FNzcw6g7om7wPqu?JumjV)bsMH=XDd3MmVo~9=0swI~C6(N#J=5{+cLk9){7y&>bgJuzvF9~sTpK!4 zfiKN4_-?J#x3QCOg}o0Vw>Llo|4B##>?^!xppB#4$n6`WH@%7ck}1GeWSGI Calculator +

There are 4 methods:

+
    +
  • add
  • +
  • subtract
  • +
  • multiply
  • +
  • divide
  • +
+

To use calculator enter operants in address bar

+

eg. /add/1/2

+ + + """ + return body 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. + """Returns a STRING with the sum of the arguments""" + sumation = np.sum([int(i) for i in args]) + + return str(sumation) + + +def subtract(*args): + """Returns a STRING with the difference of the arguments""" + # np.diff starts with the last number first i guess... + # orig = [1,2,3] 3-2-1 = 0 + # reversed = [1,2,3] 1-2-3 = -4 + diff = int(np.diff([int(i) for i in reversed(args)])) + + return str(diff) + +def multiply(*args): + """Returns a STRING with the product of the arguments""" + product = np.prod([int(i) for i in args]) + + return str(product) + +def divide(*args): + """Returns a STRING with the quotient of the arguments""" + args = [int(i) for i in args] + quotient = args[0] + for num in args[1:]: + if num == 0: + raise ZeroDivisionError + quotient = quotient / num + + return str(quotient) def resolve_path(path): """ @@ -59,26 +99,55 @@ def resolve_path(path): 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'] + func_dict = { + "": index, + "add": add, + "subtract": subtract, + "multiply": multiply, + "divide": divide + } + + path = path.strip("/").split("/") + func = path[0] + args = path[1:] + try: + func = func_dict[func] + 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 - # 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 + headers = [("Content-type", "text/html")] + try: + path = environ.get('PATH_INFO', None) + print(path) + if path is None: + raise NameError + func, args = resolve_path(path) + body = func(*args) + status = "200 OK" + + except ZeroDivisionError: + status = "404 Bad Request" + body = "

Cannot Divide by Zero

" + + 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()