def test_ensure_int(self): self.assertEqual(5, ensure_int(5)) self.assertEqual(5, ensure_int(float(5.0))) with self.assertRaises(NotIntegerException): ensure_int(0.5) with self.assertRaises(NotIntegerException): ensure_int(None) with self.assertRaises(NotIntegerException): ensure_int([]) with self.assertRaises(NotIntegerException): ensure_int('error') return
def get_numpy_type(type_char: Union[NumpyTypeChars, str, int], size: int) -> np.dtype: """ Gets the numpy data type corresponding to the type char ('i', 'u', 'f') and size in bits of the value. :param type_char: NumpyTypeChars, string, or integer corresponding to char in ('i', 'u', 'f') :param size: number of BITS in the data :return: numpy dtype """ type_char = get_type_char_char(type_char) size = ensure_int(size) if size <= 0: raise DataSizeNotPositiveError( 'Cannot make a negative-sized numpy type') if type_char == NumpyTypeChars.INTEGER.value: if size == 8: return np.int8 elif size == 16: return np.int16 elif size == 32: return np.int32 elif size == 64: return np.int64 elif type_char == NumpyTypeChars.UNSIGNED.value: if size == 8: return np.uint8 elif size == 16: return np.uint16 elif size == 32: return np.uint32 elif size == 64: return np.uint64 elif type_char == NumpyTypeChars.FLOAT.value: if size == 16: return np.float16 elif size == 32: return np.float32 elif size == 64: return np.float64 elif type_char == NumpyTypeChars.STRING.value: try: num_chars = ensure_int(size / 32) except NotIntegerException: raise NumBytesForStringInvalidError( 'Could not get numpy type for string.' ' Number of bits must be multiple of 32') return '<U{}'.format(num_chars) raise ValueError('Could not find match for char {} and size {}'.format( type_char, size))
def get_type_char_char(type_char: Union[NumpyTypeChars, str, int]) -> str: """ Gets the character representation of the char or int. :param type_char: NumpyTypeChars enum, string, or integer to convert into an integer. :return: string corresponding to parameter """ if isinstance(type_char, NumpyTypeChars): return type_char.value if isinstance(type_char, int): return chr(type_char) if isinstance(type_char, float): return chr(ensure_int(type_char)) if isinstance(type_char, str): if len(type_char) == 1: return type_char else: raise CharConversionException( 'Could not convert string \'{}\'into ' 'char because its length was > 1'.format(type_char)) # last ditch, try to cast it as int try: return chr(int(type_char)) except TypeError: raise CharConversionException( 'Could not convert type: {} for value: {}'.format( type(type_char), type_char))
def round_array_returning_integers(arr: array, num_decimals: int) -> array: """ Multiplies the array by 10^num_decimals, rounds the array, and returns an integer array :param arr: source array :param num_decimals: number of decimals to keep :return: 64-bit integer array with rounded data """ try: num_decimals = ensure_int(num_decimals) except NotIntegerException: raise NotIntegerException('Could not round array, parameter ''num_decimals'' was not an integer') if num_decimals < 0: raise ValueError('Could not round array, parameter ''num_decimals'' cannot be negative') rounded_array = arr * pow(10, num_decimals) rounded_array = around(rounded_array) return rounded_array.astype(int64)
def determine_required_bytes_unsigned_integer(value: int) -> int: """ Determines the number of bytes that are required to store value :param value: an UNSIGNED integer :return: 1, 2, 4, or 8 """ value = ensure_int(value) if value < 0: raise IntegerNotUnsignedException if (value >> 8) == 0: return 1 if (value >> 16) == 0: return 2 if (value >> 32) == 0: return 4 if (value >> 64) == 0: return 8 raise IntegerLargerThan64BitsException
def get_type_char_int(type_char: Union[NumpyTypeChars, str, int]) -> int: """ Gets the integer ord() of the character :param type_char: NumpyTypeChars enum, string, or integer to convert into an integer. :return: integer corresponding to the char (or int) """ if isinstance(type_char, NumpyTypeChars): return ord(type_char.value) if isinstance(type_char, str): return ord(type_char) if isinstance(type_char, int): return type_char try: return ensure_int(type_char) except: raise CharConversionException( 'Could not convert type: {} for value: {}'.format( type(type_char), type_char))
def determine_required_bytes_signed_integer(value: int) -> int: """ Determines the number of bytes that are required to store value :param value: a SIGNED integer :return: 1, 2, 4, or 8 """ value = ensure_int(value) if value < 0: value *= -1 value -= 1 if (value >> 7) == 0: return 1 if (value >> 15) == 0: return 2 if (value >> 31) == 0: return 4 if (value >> 63) == 0: return 8 raise IntegerLargerThan64BitsException