class_name = t.__name__ bases = t.__bases__ bases_names = [b.__name__ for b in bases] if not self.name in bases_names + [class_name]: msg = ( 'Failed check isinstance(%s) for type %r and superclasses %r.' % (self.name, class_name, bases_names)) raise ContractNotRespected(self, msg, value, context) def __str__(self): return 'isinstance(%s)' % self.name def __repr__(self): return 'IsInstance(%r)' % self.name @staticmethod def parse_action(s, loc, tokens): where = W(s, loc) name = tokens['name'] return IsInstance(name, where) Identifier = Word(alphanums + '_') isinstance_contract = (Keyword('isinstance') - S('(') - Identifier('name') - S(')')) add_contract(isinstance_contract.setParseAction(IsInstance.parse_action)) add_keyword('isinstance')
import numpy as np np_types = { 'np_int': np.int, # Platform integer (normally either int32 or int64) 'np_int8': np.int8, # Byte (-128 to 127) 'np_int16': np.int16, # Integer (-32768 to 32767) 'np_int32': np.int32, # Integer (-2147483648 to 2147483647) 'np_int64': np.int64, # Integer (9223372036854775808 to 9223372036854775807) 'np_uint8': np.uint8, # Unsigned integer (0 to 255) 'np_uint16': np.uint16, # Unsigned integer (0 to 65535) 'np_uint32': np.uint32, # Unsigned integer (0 to 4294967295) 'np_uint64': np.uint64, # Unsigned integer (0 to 18446744073709551615) 'np_float': np.float, # Shorthand for float64. 'np_float16': np. float16, # Half precision float: sign bit, 5 bits exponent, 10 bits mantissa 'np_float32': np. float32, # Single precision float: sign bit, 8 bits exponent, 23 bits mantissa 'np_float64': np. float64, # Double precision float: sign bit, 11 bits exponent, 52 bits mantissa 'np_complex': np.complex, # Shorthand for complex128. 'np_complex64': np. complex64, # Complex number, represented by two 32-bit floats (real and imaginary components) 'np_complex128': np.complex128 } for k, t in np_types.items(): add_contract(Keyword(k).setParseAction(CheckType.parse_action(t))) add_keyword(k)
if hasattr(value, '__class__'): # old style class klass = value.__class__ class_name = klass.__name__ bases = get_oldstyle_bases(klass) bases_names = [x.__name__ for x in bases] else: # new style t = type(value) class_name = t.__name__ bases_names = [b.__name__ for b in t.mro()] return class_name, bases_names def get_oldstyle_bases(klass): todo = [klass] res = [] while todo: x = todo.pop(0) res.append(x) for b in x.__bases__: if not b in res: todo.append(b) return res Identifier = Word(alphanums + '_') isinstance_contract = (Keyword('isinstance') - S('(') - Identifier('name') - S(')')) add_contract(isinstance_contract.setParseAction(IsInstance.parse_action)) add_keyword('isinstance')
use_dtype = dtype return DType(use_dtype, dtype_string, where) return parse import numpy as np np_types = { 'np_int': np.int, # Platform integer (normally either int32 or int64) 'np_int8': np.int8, # Byte (-128 to 127) 'np_int16': np.int16, # Integer (-32768 to 32767) 'np_int32': np.int32, # Integer (-2147483648 to 2147483647) 'np_int64': np.int64, # Integer (9223372036854775808 to 9223372036854775807) 'np_uint8': np.uint8, # Unsigned integer (0 to 255) 'np_uint16': np.uint16, # Unsigned integer (0 to 65535) 'np_uint32': np.uint32, # Unsigned integer (0 to 4294967295) 'np_uint64': np.uint64, # Unsigned integer (0 to 18446744073709551615) 'np_float': np.float, # Shorthand for float64. 'np_float16': np.float16, # Half precision float: sign bit, 5 bits exponent, 10 bits mantissa 'np_float32': np.float32, # Single precision float: sign bit, 8 bits exponent, 23 bits mantissa 'np_float64': np.float64, # Double precision float: sign bit, 11 bits exponent, 52 bits mantissa 'np_complex': np.complex, # Shorthand for complex128. 'np_complex64': np.complex64, # Complex number, represented by two 32-bit floats (real and imaginary components) 'np_complex128': np.complex128} for k, t in np_types.items(): add_contract(Keyword(k).setParseAction(CheckType.parse_action(t))) add_keyword(k)