# HG changeset patch # User Jeff Hammel # Date 1500667120 25200 # Node ID 100697f7c1959f9ef40f99a05cce409ae655c5c0 # Parent 8aec5ebb2d19aaeb49ce66aa2063a8416952bebd fix errors in testing and make exception a better marker diff -r 8aec5ebb2d19 -r 100697f7c195 numerics/conformity.py --- a/numerics/conformity.py Fri Jul 21 12:50:17 2017 -0700 +++ b/numerics/conformity.py Fri Jul 21 12:58:40 2017 -0700 @@ -6,6 +6,12 @@ class NonConformantRowLengths(Exception): """nested arrays have different lengths""" + _msg_tmpl = "Different lengths to array_mean: {}" + + def __init__(self, lengths): + self.lengths = lengths + Exception.__init__(self, + self._msg_tmpl.format(' '.join([str(l) for l in lengths]))) def ensure_row_length(data): """ @@ -13,7 +19,7 @@ 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))) + lengths = set([len(i) for i in data]) + if len(lengths) != 1: + raise NonConformantRowLengths(lengths) return lengths.pop() diff -r 8aec5ebb2d19 -r 100697f7c195 tests/test_conformity.py --- a/tests/test_conformity.py Fri Jul 21 12:50:17 2017 -0700 +++ b/tests/test_conformity.py Fri Jul 21 12:58:40 2017 -0700 @@ -13,24 +13,24 @@ def test_equal_lengths(self): data = [[1,2,3], - [4,5,6] + [4,5,6], [7,8,9]] assert conformity.ensure_row_length(data) == 3 def test_nonequal_lengths(self): data = [[1,2,3], - [4,5,6] + [4,5,6], [7,8,9, 10] # oops! ] e = None try: conformity.ensure_row_length(data) - except conformity.NonformantRowLengths as e: + except conformity.NonConformantRowLengths as e: pass assert e is not None - assert isinstance(e, NonConformantRowLengths) + assert isinstance(e, conformity.NonConformantRowLengths) if __name__ == '__main__': unittest.main()