Skip to content
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

WSGI Calc Pull #2

Open
wants to merge 6 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 .DS_Store
Binary file not shown.
121 changes: 100 additions & 21 deletions calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,124 @@


"""
def homepage(*args):
page = """
<head>
<title>AWESOME CALCULATOR</title>
</head>
<body>
<h1>Calculator Instructions</h1>
<h2>Please enter a URL containing a mathematical function and two values.</h2>
<h2>Such as this one shown - "http://localhost:8080/divide/22/11"</h2>
<h2>Functions such as "add", "subtract", "multiply" and "divide" are available.</h2>
</body>
"""
return page


def add(*args):
""" Returns a STRING with the sum of the arguments """
output = args[0] + args[1]
print("Output Value: {}".format(output))
returnval = str(output)
print("ReturnVal: {}".format(returnval))
return returnval

# TODO: Fill sum with the correct value, based on the
# args provided.
sum = "0"

return sum
def subtract(*args):
output = args[0] - args[1]
returnval = str(output)
return returnval

# TODO: Add functions for handling more arithmetic operations.

def resolve_path(path):
"""
Should return two values: a callable and an iterable of
arguments.
"""
def multiply(*args):
output = args[0] * args[1]
returnval = str(output)
return returnval


# 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']
def divide(*args):
if (args[1] == 0):
print("Undefined!!! You tried to divide by zero!")
output = "Undefined - You tried to divide by zero!"
else:
output = str(args[0] / args[1])
return output

# TODO: Add functions for handling more arithmetic operations.


def resolve_path(path):
print("Entered resolve_path")
availablefuncs = {"add": add,
"subtract": subtract,
"multiply": multiply,
"divide": divide}
crackedpath = path.split('/')
if crackedpath[1] == '':
func = homepage
args = [0,0]
return func, args
else:
print(crackedpath)
funcname = crackedpath[-3]
arg1 = crackedpath[-2]
arg2 = crackedpath[-1]
print("Vals: {}, {}, {}".format(funcname, arg1, arg2))
print("FunctionCall: {}".format(funcname))
try:
arg1val = int(arg1)
arg2val = int(arg2)
print("Vals OK")
except ValueError:
print("Please enter a valid number.")
return NameError
if funcname in availablefuncs.keys():
print("function name found")
func = availablefuncs.get(funcname)
else:
print("Function name not found")
return NameError
print("Selected function: {}".format(funcname))
args = [arg1val, arg2val]
return func, args

def application(environ, start_response):

#functionoutput = func(args)
# 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
print("About to call path resolve")
func, args = resolve_path(path)
print("CalledResolve_path")
body = func(*args)
print("Called Body")
if body == "Undefined - You tried to divide by zero!":
status = "400 - Bad Request"
else:
status = "200 OK"
except NameError:
print("Triggered Nameerror")
status = "404 Not Found"
body = "<h1>Not Found</h1>"
except Exception:
status = "500 Internal Server Error"
body = "<h1>Internal Server Error</h1>"
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()
9 changes: 9 additions & 0 deletions funkyfunctest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#funkyfunctest.py

def funktest(*args):
print(inputval)

values = funktest
inputval = "blamalama"

thingy = values(inputval)