Skip to content

Ready to review #32

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 1 commit 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
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"python.testing.unittestArgs": [
"-v",
"-s",
".",
"-p",
"*test*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true
}
Binary file added __pycache__/tests.cpython-37.pyc
Binary file not shown.
143 changes: 136 additions & 7 deletions calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,104 @@


"""
import traceback

def html_wrapper(body):
result = f"""
<html>
<head><title>Calculator</title></head>
<body>
{body}
</body>
</html>
"""
return result

def instructions(*args):
"""Index page"""
page = """
<h1>Calculator</h1>

<h2>Instructions:</h2>

<ul>
<li>To access the calculator function in your browser enter http://localhost:8080/.</li>
<li>Enter in the operation you wish to use after the slash "/": Add, Subtract, Multiply, or divide.</li>
<li>After the operation enter slash "/" and the first number in the operation.</li>
<li>Enter another slash "/" followed by the second number in the operation.</li>
<li>Press Enter.</li>
</ul>

<h2>Examples:</h2>

<table>
<tr>
<th>Operation</th>
<th>Example Link</th>
</tr>
<tr>
<td>Add</td>
<td><a href="http://localhost:8080/add/23/42">http://localhost:8080/add/23/42</a></td>
</tr>
<tr>
<td>Subtract</td>
<td><a href="http://localhost:8080/subtract/23/42">http://localhost:8080/subtract/23/42</a></td>
</tr>
<tr>
<td>Multiply</td>
<td><a href="http://localhost:8080/multiply/3/5">http://localhost:8080/multiply/3/5</a></td>
</tr>
<tr>
<td>Divide</td>
<td><a href="http://localhost:8080/divide/22/11">http://localhost:8080/divide/22/11</a></td>
</tr>
</table>

"""
return page


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
# sum = "0"
result = str(sum(args))
result += '</br><a href="/">Back</a>'
return result

# TODO: Add functions for handling more arithmetic operations.


def subtract(*args):
"""Returns a string with the result a-b"""

result = str(args[0] - args[1])
result += '</br><a href="/">Back</a>'

return result

def divide(*args):
"""Return a string with the result of """
try:
result = str(args[0] / args[1])

except ZeroDivisionError:
result = "You can't divide by zero"
result += '</br><a href="/">Back</a>'

return result

def multiply(*args):
"""Returns the result of a*b"""

result = str(args[0] * args[1])
result += '</br><a href="/">Back</a>'

return result


def resolve_path(path):
"""
Should return two values: a callable and an iterable of
Expand All @@ -63,8 +148,30 @@ def resolve_path(path):
# 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 = add
# args = ['25', '32']

funcs = {
"add": add,
"subtract": subtract,
"multiply": multiply,
"divide": divide,
"instructions" : instructions
}
# print(f"!{path}!")
if "favicon.ico" in path or len(path) < 7:
func_name = "instructions"
left = "0"
right = "0"
else:
func_name, left, right = path.split('/')[1:4]

args = [int(left), int(right)]

try:
func = funcs[func_name]
except KeyError:
raise NameError

return func, args

Expand All @@ -76,9 +183,31 @@ def application(environ, start_response):
#
# TODO (bonus): Add error handling for a user attempting
# to divide by zero.
pass
# pass

status = "200 OK"
headers = [('Content-type', 'text/html')]
try:
path = environ.get('PATH_INFO', None)
func, args = resolve_path(path)
body = html_wrapper(func(*args))
except NameError:
status = "404 Not Found"
body = html_wrapper("<h1>Not Found</h1>")
except Exception:
status = "500 Internal Server Error"
body = html_wrapper("<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
# server creation that you used in the book database.
pass
# pass
from wsgiref.simple_server import make_server
srv = make_server('localhost', 8080, application)
srv.serve_forever()
7 changes: 7 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
import http.client
import random

"""
http://localhost:8080/multiply/3/5
http://localhost:8080/add/23/42
http://localhost:8080/subtract/23/42
http://localhost:8080/divide/22/11
http://localhost:8080/
"""

class WebTestCase(unittest.TestCase):
"""tests for the WSGI Calculator"""
Expand Down