diff numerics/conformity.py @ 186:c2f545f32025

move conformity ensurance to separate function
author Jeff Hammel <k0scist@gmail.com>
date Fri, 21 Jul 2017 12:46:55 -0700
parents
children 100697f7c195
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/numerics/conformity.py	Fri Jul 21 12:46:55 2017 -0700
@@ -0,0 +1,19 @@
+"""
+ensures data is what we assert it to be
+"""
+
+
+class NonConformantRowLengths(Exception):
+    """nested arrays have different lengths"""
+
+
+def ensure_row_length(data):
+    """
+    ensures that all rows of array `data` are the same
+    If so, return that length.
+    If not, raise NonConformantArrayLengths
+    """
+    lengths = [len(i) for i in data]
+    if len(set(lengths)) != 1:
+        raise NonConformantRowLengths("Different lengths to array_mean: {}".format(' '.join(lengths)))
+    return lengths.pop()