From: Dylan Lloyd Date: Sun, 14 Jul 2013 09:40:13 +0000 (+0000) Subject: add error handling & numpy to readme X-Git-Url: https://disinclined.org/git/?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=matrixnullspace.com.git add error handling & numpy to readme --- diff --git a/README.md b/README.md index 5911843..8daf28c 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,4 @@ matrixnullspace.com A [WSGI](http://www.python.org/dev/peps/pep-0333/) application to calculate the the kernel, determinant and eigenvalues of a given matrix. -Requires [SciPy](http://www.scipy.org/). +Requires [SciPy](http://www.scipy.org/) and [NumPy](https://en.wikipedia.org/wiki/NumPy). diff --git a/matrix.py b/matrix.py index 41713e2..f7da2d7 100755 --- a/matrix.py +++ b/matrix.py @@ -13,13 +13,25 @@ def calculate_eigenvalues(A, eps=1e-15): u, s, vh = svd(A) return s +def fail(start_response, message): + start_response('400 Bad Request', []) + return 'error: ' + message + def application(environ, start_response): - parameters = cgi.parse_qs(environ.get('QUERY_STRING', '')) + parameters = cgi.parse_qs(environ.get('QUERY_STRING')) + if not 'matrix' in parameters or parameters['matrix'] == '': + return fail(start_response, 'missing matrix') query_string = str(parameters['matrix'][0]) matrix = query_string.splitlines() for index, row in enumerate(matrix): matrix[index] = row.split() + if len(matrix[index]) != len(matrix[0]): + return fail(start_response, 'ragged matrix') + for index, n in enumerate(matrix[index]): + try: float(n) + except ValueError: + return fail(start_response, 'invalid matrix element') while len(matrix) < len(matrix[0]): matrix.append([0]*len(matrix[0]))