view numerics/conformity.py @ 188:100697f7c195

fix errors in testing and make exception a better marker
author Jeff Hammel <k0scist@gmail.com>
date Fri, 21 Jul 2017 12:58:40 -0700
parents c2f545f32025
children
line wrap: on
line source

"""
ensures data is what we assert it to be
"""


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):
    """
    ensures that all rows of array `data` are the same
    If so, return that length.
    If not, raise NonConformantArrayLengths
    """
    lengths = set([len(i) for i in data])
    if len(lengths) != 1:
        raise NonConformantRowLengths(lengths)
    return lengths.pop()