Skip to content

sp_py230 lesson4, assignment wsgi_calculator #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/tests.cpython-37.pyc
Binary file not shown.
160 changes: 143 additions & 17 deletions calculator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import re
import traceback


"""
For your homework this week, you'll be creating a wsgi application of
your own.
Expand Down Expand Up @@ -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 = """
<h1>"Calculator Usage Instructions:"</h1>
<table>
<tr><th>Usage: Operator//arg1//arg2</th><td>"site: http://localhost:8080"</td></tr>
<tr><th>{add_description}</th><td>{add_example}</td></tr>
<tr><th>{subtract_description}</th><td>{subtract_example}</td></tr>
<tr><th>{multiply_description}</th><td>{multiply_example}</td></tr>
<tr><th>{divide_description}</th><td>{divide_example}</td></tr>
</table>
"""
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 = "<h1>Not Found</h1>"
except Exception:
status = "500 Internal Server Error"
body = "<h1>Internal Server Error</h1>"
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()
3 changes: 3 additions & 0 deletions goStartWsgiServerCalculator.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

cd C:\A_uwPython\SP_Online_PY230\lesson4\wsgi-calculator\wsgi-calculator
start python -u calculator.py
4 changes: 4 additions & 0 deletions goTesting.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
python -m unittest -vv tests.py > testing_results.txt 2>&1
type testing_results.txt


11 changes: 11 additions & 0 deletions testing_results.txt
Original file line number Diff line number Diff line change
@@ -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