diff --git a/.DS_Store b/.DS_Store
new file mode 100644
index 0000000..4c889e0
Binary files /dev/null and b/.DS_Store differ
diff --git a/calculator.py b/calculator.py
index fad858d..d33ef2e 100644
--- a/calculator.py
+++ b/calculator.py
@@ -41,35 +41,90 @@
"""
+def homepage(*args):
+ page = """
+
+ AWESOME CALCULATOR
+
+
+ Calculator Instructions
+ Please enter a URL containing a mathematical function and two values.
+ Such as this one shown - "http://localhost:8080/divide/22/11"
+ Functions such as "add", "subtract", "multiply" and "divide" are available.
+
+ """
+ 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
@@ -77,9 +132,33 @@ def application(environ, start_response):
#
# 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 = "Not Found
"
+ except Exception:
+ status = "500 Internal Server Error"
+ body = "Internal Server Error
"
+ 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()
diff --git a/funkyfunctest.py b/funkyfunctest.py
new file mode 100644
index 0000000..8d96502
--- /dev/null
+++ b/funkyfunctest.py
@@ -0,0 +1,9 @@
+#funkyfunctest.py
+
+def funktest(*args):
+ print(inputval)
+
+values = funktest
+inputval = "blamalama"
+
+thingy = values(inputval)
\ No newline at end of file