From f86703de9b19f1a0bfc05d7f4b0225fef5fa17c5 Mon Sep 17 00:00:00 2001 From: Dylan Lloyd Date: Sun, 14 Jul 2013 09:40:13 +0000 Subject: [PATCH] add error handling & numpy to readme --- README.md | 2 +- matrix.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) 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])) -- 2.30.2