From fa5f2938eac25a143f4c055717e72340eaf87848 Mon Sep 17 00:00:00 2001 From: Jason Jenkins Date: Thu, 28 Jan 2021 15:21:11 -0600 Subject: [PATCH] Ready for review --- __pycache__/tests.cpython-37.pyc | Bin 0 -> 3742 bytes calculator.py | 150 ++++++++++++++++++++++++++----- 2 files changed, 128 insertions(+), 22 deletions(-) create mode 100644 __pycache__/tests.cpython-37.pyc diff --git a/__pycache__/tests.cpython-37.pyc b/__pycache__/tests.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e4680e88b44674efd10f119807b9309f98e4fa51 GIT binary patch literal 3742 zcmdT{&2QX96d!-Cv%C3f18ISRp%mECW<%jnp;B7XDlHNrQd0^(V7#8m#;Mod&WuyC z-JDu+gj? zcfZ#O`3)aNmkFI4aElcXlu#`uUeRivrk-`rP_5~isXb*|eV;UWGZ- ze5mu!85vCM5)JI!BMoa0Y?74_<>=C(a|3SiH3&v<4Ns>Utf4Hz8YZ=#5znGFtw3v2 zhfY9Sp;cOg)}eJe3GD=(qSMe;=?tBPwnk6U)6mxG9IX6OUVFe=YfQ+cK(O992|eMi zr`(kr%zbcg`L4SZ#9tk&4ESD zT@AjJ;Fqp=c6VEDq)BfwEDbMow;OtH3rW*Vs+Muy2D@r4fk=~0mOP9S z(Y%$0S%)Q3G?ycJCu=qDt@>Xkag;EB^_wdnE;b*Fc652))uuuS9EL@hyx*;XWr602 zX6icJrdHQ`vtytxD7^#NA-4*H8hSZs}C#0Bq}2j(u>0bYSqqi+oCT{0Z6DE|%c+Uz~3y2I-Zi@S`w>sb=YC{0|M zx@{)iz~xMI(?l?Lou?gmZbw@zaWfuYax)PnZFfV;?m`$xz)WeaHx;Kq#6~LQ$5$7x zE_%lD?X|sMz~P3In>fR>iz(jJowc>qr8G&HTG*>__9%nU_G+;HXz{!j2nYyy`%xCe zc?I5tDTUw(<5aMQsQ~s2aLO}7FqJ!_iHA|I!&HCt@)hu>2neeVq2OpUx}*2bj59E< zUd9D7P_po?Vqu{6)ULjxNr({KU(ko$KL9@Z*heOS2jIzs9-Ih$*lj~j$iS8r)SD+y zwSlus;2oSe$QQMMC%gASiR?`maE+0kW%|VpGGkUB6kYh?c+=rXv@le1$-7J+4V9w7XHW+jKRTKx2=C9${ITWv>cmu=`VD93;n<(Bw zaU8~z3ggkjS3&J4&}YX3`+)unplv{gz{s{vjBNY3$Ubuv?FvM;)4O>b+K@sd4?=kW zZTGUr;G9NPH1d&w>`hF$E4;!JaKtrHKnnmVY8vhf1hZ$u4$Q~;6 zOEU1AN;0m%1HcO9JOHdg50Z5sil{Of8<4i4c!IHEf~F=MRaP%6L0W@?J4z_~-t7i$ z<_1!g;5<#Go35*VQR}*51Cw=>gmFf33gpmCl{uoc1+~^j`WO>>21OQ@@@R9ReE(c) zs;r$GkU>`(_OW?mKV|4ev3XpMm{(9Lzx{Dnwg3=;@k(q+$VTt{5jc;|S!qn>j_)Ty zhxvY9_5Dstvl#nz-^U_t_@?6fG!1>9BW-*Jh01&9vG+EL3n<<}aS_F36j-E>39Oen za)=eSz`DeN>fNb1rZZjsW|amy)d`}+_ZwBufeKvWM{^|KtBqBv{1l878F>PFxw8gp m_`gb7;R`rsqM-g_VMWmw*f5xdNj`k;Y-f^e^ literal 0 HcmV?d00001 diff --git a/calculator.py b/calculator.py index a46affd..28fce9a 100644 --- a/calculator.py +++ b/calculator.py @@ -45,13 +45,92 @@ 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" + if len(args) != 2: + raise NameError + + try: + total = int(args[0]) + int(args[1]) + body = f"{total}" + except (ValueError, TypeError): + body = "Input not valid" + + return f'

{body}

' + +def divide(*args): + """ Returns a STRING with the divide of the arguments """ + + if len(args) != 2: + raise NameError + + try: + total = int(args[0]) / int(args[1]) + body = f"{total}" + except (ValueError, TypeError): + body = "Input not valid" + except (ZeroDivisionError): + body = "Can't Divide by 0" + + return f'

{body}

' + +def multiply(*args): + """ Returns a STRING with the multiply of the arguments """ + + if len(args) != 2: + raise NameError + + try: + total = int(args[0]) * int(args[1]) + body = f"{total}" + except (ValueError, TypeError): + body = "Input not valid" + + return f'

{body}

' + +def subtract(*args): + """ Returns a STRING with the subtract of the arguments """ + + if len(args) != 2: + raise NameError + + try: + total = int(args[0]) - int(args[1]) + body = f"{total}" + except (ValueError, TypeError): + body = "Input not valid" + except (ZeroDivisionError): + body = "Can't Divide by 0" + + return f'

{body}

' + +def home(*args): + """ Returns a STRING with the instuctions for site """ + body = """ +

This is an online calculator that can perform several operations.

+

It supports:

+
    +
  • Addition
  • +
  • Subtractions
  • +
  • Multiplication
  • +
  • Division
  • +
+

+ If you open a browser to your wsgi application at + `http://localhost:8080/multiple/3/5' then the response body in my + browser should be `15`. +

+ +

Examples:

+
    +
  • http://localhost:8080/multiply/3/5 => 15
  • +
  • http://localhost:8080/add/23/42 => 65
  • +
  • http://localhost:8080/subtract/23/42 => -19
  • +
  • http://localhost:8080/divide/22/11 => 2
  • + http://localhost:8080/ => instuctions +
- return sum + """ -# TODO: Add functions for handling more arithmetic operations. + return body def resolve_path(path): """ @@ -59,26 +138,53 @@ 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'] + funcs = { + '': home, + 'add': add, + 'divide': divide, + 'multiply': multiply, + 'subtract': subtract, + } + + 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 - # 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) + + 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()