Beispiel #1
0
    def validate(self, data):
        if not isinstance(data, dict):
            msg = 'Expected a dictionary object.'
            raise_desc(NotValid, msg, data=describe_value(data))

        for k, v in self.children.items():
            if not k in data:
                msg = 'Expecting key "%s" but not found in %s.' % (k, format_list(data))
                
                raise_desc(NotValid, msg, data=describe_value(data), self=str(self))
            try:
                v.validate(data[k])
            except NotValid as e:
                msg = 'For child "%s":' % k
                raise_wrapped(NotValid, e, msg) 
Beispiel #2
0
 def validate(self, data):
     if self.can_be_none and (data is None):
         return
     
     if not isinstance(data, str):
         msg = 'Expected a string object.'
         raise_desc(NotValid, msg, data=describe_value(data))
Beispiel #3
0
def check_contracts_fail(contract, value, error=ContractNotRespected):
    """ Returns the exception """
    if isinstance(contract, six.string_types):
        contract = [contract]
        value = [value]

    try:
        context = check_contracts(contract, value)

        msg = ('I was expecting that the values would not not'
               ' satisfy the contract.\n')

        for v in value:
            msg += '      value: %s\n' % describe_value(v)

        for c in contract:
            cp = parse_contract_string(c)
            msg += '   contract: %r, parsed as %r (%s)\n' % (c, cp, cp)

        msg += '    context:  %r\n' % context

        raise Exception(msg)

    except error as e:
        # Try generation of strings:
        s = "%r" % e  # @UnusedVariable
        s = "%s" % e  # @UnusedVariable
        return e
def np_zeroshape_array(x):
#     scalars = [ 
#     np.int,  # Platform integer (normally either int32 or int64)
#     np.int8,  # Byte (-128 to 127)
#     np.int16,  # Integer (-32768 to 32767)
#     np.int32,  # Integer (-2147483648 to 2147483647)
#     np.int64,  # Integer (9223372036854775808 to 9223372036854775807)
#     np.uint8,  # Unsigned integer (0 to 255)
#     np.uint16,  # Unsigned integer (0 to 65535)
#     np.uint32,  # Unsigned integer (0 to 4294967295)
#     np.uint64,  # Unsigned integer (0 to 18446744073709551615)
#     np.float,  # Shorthand for float64.
#     np.float16,  #  Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
#     np.float32,  #  Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
#     np.float64,  #  Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
#     np.complex,  #  Shorthand for complex128.
#     np.complex64,  #    Complex number, represented by two 32-bit floats (real and imaginary components)
#     np.complex128
#     ]
#  
#     if isinstance(x, tuple(scalars)):
#         return
#  
#  
    if not isinstance(x, np.ndarray):
        msg = 'Not an array: %s %s ' % (type(x), describe_type(x))
        raise ValueError(msg)
 
    if not x.shape == ():
        msg = 'Not a scalar: %s' % describe_value(x)
        raise ValueError(msg)
def np_zeroshape_array(x):
    #     scalars = [
    #     np.int,  # Platform integer (normally either int32 or int64)
    #     np.int8,  # Byte (-128 to 127)
    #     np.int16,  # Integer (-32768 to 32767)
    #     np.int32,  # Integer (-2147483648 to 2147483647)
    #     np.int64,  # Integer (9223372036854775808 to 9223372036854775807)
    #     np.uint8,  # Unsigned integer (0 to 255)
    #     np.uint16,  # Unsigned integer (0 to 65535)
    #     np.uint32,  # Unsigned integer (0 to 4294967295)
    #     np.uint64,  # Unsigned integer (0 to 18446744073709551615)
    #     np.float,  # Shorthand for float64.
    #     np.float16,  #  Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
    #     np.float32,  #  Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
    #     np.float64,  #  Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
    #     np.complex,  #  Shorthand for complex128.
    #     np.complex64,  #    Complex number, represented by two 32-bit floats (real and imaginary components)
    #     np.complex128
    #     ]
    #
    #     if isinstance(x, tuple(scalars)):
    #         return
    #
    #
    if not isinstance(x, np.ndarray):
        msg = 'Not an array: %s %s ' % (type(x), describe_type(x))
        raise ValueError(msg)

    if not x.shape == ():
        msg = 'Not a scalar: %s' % describe_value(x)
        raise ValueError(msg)
Beispiel #6
0
 def validate(self, data):
     if not isinstance(data, list):
         msg = 'Expected a list object.'
         raise_desc(NotValid, msg, data=describe_value(data))
     
     for i, d in enumerate(data):
         try:
             self.prototype.validate(d)
         except NotValid as e:
             msg = 'For entry %d:' % i
             raise_wrapped(NotValid, e, msg) 
Beispiel #7
0
 def validate(self, data):
     if not isinstance(data, dict):
         msg = 'Expected a dictionary object.'
         raise_desc(NotValid, msg, data=describe_value(data))
     
     for k in data:
         try:
             self.prototype.validate(data[k])
         except NotValid as e:
             msg = 'For entry "%s":' % k
             raise_wrapped(NotValid, e, msg) 
Beispiel #8
0
    def __str__(self):
        if self.block is not None:
            name = self.block.name
        else:
            name = '(unknown)'

        # TODO: add short bad_value
        s = ("Bad input %r for block %r: %s" % 
             (self.input_signal, name, self.error))
        
        if self.block is not None:
            s += '\n value: %s' % describe_value(self.bad_value)
        
        s += format_where(self.block)
        return s
Beispiel #9
0
 def validate(self, data):
     if self.can_be_none and data is None:
         return
     if not isinstance(data, bytes):
         msg = 'Expected a bytes object.'
         raise_desc(NotValid, msg, data=describe_value(data))
Beispiel #10
0
def check_numpy_array(value):
    if not isinstance(value, numpy.ndarray):
        msg = 'Expected array: %s' % describe_value(value)
        raise ValueError(msg)
Beispiel #11
0
def check_numpy_array(value):
    import numpy as np
    if not isinstance(value, np.ndarray):
        msg = 'Expected array: %s' % describe_value(value)
        raise ValueError(msg)