# Local import from time_base import comparison_timer int_re = '''\ import re int_regex = re.compile(r'[-+]?\d+$') int_match = int_regex.match def int_re(x): """Function to simulate safe_int but with regular expressions.""" try: if int_match(x): return int(x) else: return x except TypeError: return int(x) ''' int_try = '''\ def int_try(x): """Function to simulate safe_int but with try/except.""" try: return int(x) except ValueError: return x ''' comparison_timer(['int_re', int_re], ['int_try', int_try], ['fast_int', 'from fastnumbers import fast_int'])
try: if int_match(x): return True elif float_match(x): return float(x).is_integer() else: return False except TypeError: return int(x) == x ''' isintlike_try = '''\ def isintlike_try(x): """Function to simulate isintlike but with try/except.""" try: a = int(x) except ValueError: try: a = float(x) except ValueError: return False else: return a.is_integer() else: return a == float(x) ''' comparison_timer(['isintlike_re', isintlike_re], ['isintlike_try', isintlike_try], ['isintlike', 'from fastnumbers import isintlike'])
from __future__ import print_function, division # Local import from time_base import comparison_timer isfloat_re = '''\ import re float_regex = re.compile(r'[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$') float_match = float_regex.match nums = set([float, int]) def isfloat_re(x): """Function to simulate isfloat but with regular expressions.""" t = type(x) return t == float if t in nums else bool(float_match(x)) ''' isfloat_try = '''\ def isfloat_try(x): """Function to simulate isfloat but with try/except.""" try: float(x) except ValueError: return False else: return type(x) != int ''' comparison_timer(['isfloat_re', isfloat_re], ['isfloat_try', isfloat_try], ['isfloat', 'from fastnumbers import isfloat'])
def real_re(x): """Function to simulate safe_real but with regular expressions.""" try: if int_match(x): return int(x) elif real_match(x): return float(x) else: return x except TypeError: if type(x) in set([float, int]): return x else: raise TypeError ''' real_try = '''\ def real_try(x): """Function to simulate safe_real but with try/except.""" try: a = float(x) except ValueError: return x else: b = int(a) return b if a == b else b ''' comparison_timer(['real_re', real_re], ['real_try', real_try], ['fast_real', 'from fastnumbers import fast_real'])
from __future__ import print_function, division from time_base import comparison_timer float_re = '''\ import re float_regex = re.compile(r'[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$') float_match = float_regex.match def float_re(x): """Function to simulate safe_float but with regular expressions.""" try: if float_match(x): return float(x) else: return x except TypeError: return float(x) ''' float_try = '''\ def float_try(x): """Function to simulate safe_float but with try/except.""" try: return float(x) except ValueError: return x ''' comparison_timer(['float_re', float_re], ['float_try', float_try], ['fast_float', 'from fastnumbers import fast_float'])
from __future__ import print_function, division # Local import from time_base import comparison_timer isreal_re = '''\ import re real_regex = re.compile(r'[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$') real_match = real_regex.match nums = set([float, int]) def isreal_re(x): """Function to simulate isreal but with regular expressions.""" return type(x) in nums or bool(real_match(x)) ''' isreal_try = '''\ def isreal_try(x): """Function to simulate isreal but with try/except.""" try: float(x) except ValueError: return False else: return True ''' comparison_timer(['isreal_re', isreal_re], ['isreal_try', isreal_try], ['isreal', 'from fastnumbers import isreal'])