diff --git a/__pycache__/tests.cpython-37.pyc b/__pycache__/tests.cpython-37.pyc
new file mode 100644
index 0000000..28582db
Binary files /dev/null and b/__pycache__/tests.cpython-37.pyc differ
diff --git a/calculator.py b/calculator.py
index a46affd..aaf4779 100644
--- a/calculator.py
+++ b/calculator.py
@@ -1,3 +1,7 @@
+import re
+import traceback
+
+
"""
For your homework this week, you'll be creating a wsgi application of
your own.
@@ -41,44 +45,166 @@
"""
+dict_instrs = {
+ 'instrs': {
+ 'add_description': 'Add two numbers',
+ 'add_example': 'http://localhost:8080//add/10/5',
+ 'subtract_description': 'Subtract two numbers',
+ 'subtract_example': 'http://localhost:8080//subtract/10/5',
+ 'multiply_description': 'Multiply two numbers',
+ 'multiply_example': 'http://localhost:8080/multiply//10/5',
+ 'divide_description': 'Divide two numbers',
+ 'divide_example': 'http://localhost:8080/divide//10/5',
+ },
+ 'help1': {
+ },
+ 'help2': {
+ },
+ 'help3': {
+ },
+}
+
+# DONE: Include usage instructions home page
+
+def calculator_usage_instructions(*args):
+ page = """
+
"Calculator Usage Instructions:"
+
+ | Usage: Operator//arg1//arg2 | "site: http://localhost:8080" |
+ | {add_description} | {add_example} |
+ | {subtract_description} | {subtract_example} |
+ | {multiply_description} | {multiply_example} |
+ | {divide_description} | {divide_example} |
+
+"""
+ instrs = dict_instrs['instrs']
+ return page.format(**instrs)
+# DONE: Include other functions for handling more arithmetic operations.
+
def add(*args):
""" Returns a STRING with the sum of the arguments """
- # TODO: Fill sum with the correct value, based on the
+ # DONE: Fill sum with the correct value, based on the
# args provided.
- sum = "0"
+ oper_a = args[0]
+ oper_b = args[1]
+
+ sum = str(int(oper_a) + int(oper_b))
+
+ body = sum
+
return sum
-# TODO: Add functions for handling more arithmetic operations.
+def multiply(*args):
+ """ Returns a STRING with the product of the arguments """
+
+ # DONE: Fill product with the correct value, based on the
+ # args provided.
+
+ multiplicand = args[0]
+ multiplier = args[1]
+
+ product = str(int(multiplicand) * int(multiplier))
+
+ body = product
+
+ return product
+
+def subtract(*args):
+ """ Returns a STRING with the difference of the arguments """
+
+ # DONE: Fill difference with the correct value, based on the
+ # args provided.
+
+ minuend = args[0]
+ subtrahend = args[1]
+
+ difference = str(int(minuend) - int(subtrahend))
+ body = difference
+
+ return difference
+
+
+def divide(*args):
+ """ Returns a STRING with the difference of the arguments """
+
+ # DONE: Fill quotient with the correct value, based on the
+ # args provided.
+
+ dividend = args[0]
+ divisor = args[1]
+
+ quotient = str(int(dividend) / int(divisor))
+
+ body = quotient
+
+ return quotient
+
+
def resolve_path(path):
- """
- Should return two values: a callable and an iterable of
- arguments.
- """
- # TODO: Provide correct values for func and args. The
+ funcs = {
+ '': calculator_usage_instructions,
+ 'add': add,
+ 'subtract': subtract,
+ 'multiply': multiply,
+ 'divide': divide,
+
+ }
+
+ path = path.strip('/').split('/')
+
+ func_name = path[0]
+ args = path[1:]
+
+ # DONE: 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']
+
+ 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
+ # DONE: 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
+ pass
+
+ body = ""
+ 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
+ # DONE: 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()
diff --git a/goStartWsgiServerCalculator.bat b/goStartWsgiServerCalculator.bat
new file mode 100644
index 0000000..85f8407
--- /dev/null
+++ b/goStartWsgiServerCalculator.bat
@@ -0,0 +1,3 @@
+
+cd C:\A_uwPython\SP_Online_PY230\lesson4\wsgi-calculator\wsgi-calculator
+start python -u calculator.py
diff --git a/goTesting.bat b/goTesting.bat
new file mode 100644
index 0000000..6497dbb
--- /dev/null
+++ b/goTesting.bat
@@ -0,0 +1,4 @@
+python -m unittest -vv tests.py > testing_results.txt 2>&1
+type testing_results.txt
+
+
diff --git a/testing_results.txt b/testing_results.txt
new file mode 100644
index 0000000..0230d3b
--- /dev/null
+++ b/testing_results.txt
@@ -0,0 +1,11 @@
+test_add (tests.WebTestCase) ... ok
+test_divide (tests.WebTestCase) ... ok
+test_index_instructions (tests.WebTestCase) ... ok
+test_multiply (tests.WebTestCase) ... ok
+test_subtract_negative_result (tests.WebTestCase) ... ok
+test_subtract_positive_result (tests.WebTestCase) ... ok
+
+----------------------------------------------------------------------
+Ran 6 tests in 6.186s
+
+OK