diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..d13b3da --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,25 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d1e22ec --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8562918 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/wsgi-calculator.iml b/.idea/wsgi-calculator.iml new file mode 100644 index 0000000..34088bc --- /dev/null +++ b/.idea/wsgi-calculator.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/calculator.py b/calculator.py index a46affd..2400c79 100644 --- a/calculator.py +++ b/calculator.py @@ -1,84 +1,145 @@ -""" -For your homework this week, you'll be creating a wsgi application of -your own. - -You'll create an online calculator that can perform several operations. - -You'll need to support: - - * Addition - * Subtractions - * Multiplication - * Division - -Your users should be able to send appropriate requests and get back -proper responses. For example, if I open a browser to your wsgi -application at `http://localhost:8080/multiple/3/5' then the response -body in my browser should be `15`. - -Consider the following URL/Response body pairs as tests: - -``` - 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/ => Here's how to use this page... -``` - -To submit your homework: - - * Fork this repository (Session03). - * Edit this file to meet the homework requirements. - * Your script should be runnable using `$ python calculator.py` - * When the script is running, I should be able to view your - application in my browser. - * I should also be able to see a home page (http://localhost:8080/) - that explains how to perform calculations. - * Commit and push your changes to your fork. - * Submit a link to your Session03 fork repository! - - -""" +"""a simple math application""" +import traceback 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 + my_sum = 0 + for my_arg in args: + my_sum += int(my_arg) + page = f""" +

Addition

+
{my_sum}
+
Back to index
+ """ + return page + + +def subtract(*args): + """subtract input numbers""" + + my_sum = int(args[0]) + sliced_tuple = args[1:] + print(sliced_tuple) + for my_arg in sliced_tuple: + print(my_sum) + my_sum = my_sum - int(my_arg) + page = f""" +

Subtraction

+
{my_sum}
+
Back to index
+ """ + return page + + +def multiply(*args): + """multiply input numbers""" + + my_sum = int(args[0]) + sliced_tuple = args[1:] + print(sliced_tuple) + for my_arg in sliced_tuple: + print(my_sum) + my_sum = my_sum * int(my_arg) + page = f""" +

Multiplication

+
{my_sum}
+
Back to index
+ """ + return page + + +def divide(*args): + """divide input numbers""" + + try: + my_sum = int(args[0]) + sliced_tuple = args[1:] + print(sliced_tuple) + for my_arg in sliced_tuple: + print(my_sum) + my_sum = my_sum / int(my_arg) + page = f""" +

Division

+
{my_sum}
+
Back to index
+ """ + except ZeroDivisionError: + page = """ +

500 Internal Server Error

+

Divide by zero error

+
Back to index
+ """ + print(traceback.format_exc()) + return page + return page -# TODO: Add functions for handling more arithmetic operations. def resolve_path(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'] - + """ returns two values: a callable and an iterable of arguments. """ + + funcs = {'': index, '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 index(): + """create an entry page for the application""" + + page = """ +

Calculator

+
Directions. This app will calculate 2 or more numbers provided in the url string. To use: +
    +
  1. Type in http://localhost:8080/
  2. +
  3. Type in the arithmetic operation (add, subract, multiply, divide) followed by /
  4. +
  5. Type in numbers. Between each number include a /
  6. +
  7. For example, http://localhost:8080/add/5/10/
  8. +
+

Tests: