def exec_num(x, y): """ Compares whether `x` is strictly lower than `y`. Remarks: - If any operand is None, the return value is None. - If any operand is not a number or temporal string (date, time or date-time), the process returns False. - Temporal strings can not be compared based on their string representation due to the time zone / time-offset representations. Parameters ---------- x : float or int or datetime.datetime First operand. y : float or int or datetime.datetime Second operand. Returns ------- bool : Returns True if `x` is strictly lower than `y`, None if any operand is None, otherwise False. """ if x is None or y is None: return None elif isinstance(x, str) and isinstance(y, str): return str2time(x) < str2time(y) elif isinstance(x, (int, float, datetime.datetime)) and isinstance( y, (int, float, datetime.datetime)): return x < y else: return False
def exec_num(x, y, delta=None, case_sensitive=True): """ Compares whether `x` is strictly equal to `y`. Remarks: - An integer 1 is equal to a floating point number 1.0 as integer is a sub-type of number. - If any operand is None, the return value is None. Therefore, exec_num(None, None) returns None instead of True. - Strings are expected to be encoded in UTF-8 by default. - Temporal strings are differently than other strings and are not be compared based on their string representation due to different possible representations. For example, the UTC time zone representation Z has the same meaning as +00:00. Parameters ---------- x : float or int or str or datetime.datetime First operand. y : float or int or str or datetime.datetime Second operand. delta : float, optional Only applicable for comparing two numbers. If this optional parameter is set to a positive non-zero number the equality of two numbers is checked against a delta value. This is especially useful to circumvent problems with floating point inaccuracy in machine-based computation. case_sensitive : bool, optional Only applicable for comparing two strings. Case sensitive comparison can be disabled by setting this parameter to False. Returns ------- bool : Returns True if `x` is equal to `y`, None if any operand is None, otherwise False. """ if x is None or y is None: return None if (type(x) in [float, int]) and (type(y) in [ float, int ]): # comparison of numbers if type(delta) in [float, int]: return np.isclose(x, y, atol=delta) else: return x == y elif (type(x) == str) and (type(y) == str): # comparison of strings or dates # try to convert the string into a date x_time = str2time(x) y_time = str2time(y) if x_time is None or y_time is None: # comparison of strings if case_sensitive: return x == y else: return x.lower() == y.lower() else: return x_time == y_time # comparison of dates else: return False
def exec_num(x, min, max, exclude_max=False): """ By default this process checks whether `x` is greater than or equal to `min` and lower than or equal to `max`. All definitions from and_, gte and lte apply here as well. If `exclude_max` is set to True the upper bound is excluded so that the process checks whether `x` is greater than or equal to `min` and lower than `max`. Lower and upper bounds are not allowed to be swapped. `min` must be lower than or equal to `max` or otherwise the process always returns False. Parameters ---------- x : float or int or datetime.datetime The value to check. min : float or int or datetime.datetime Lower boundary (inclusive) to check against. max : float or int or datetime.datetime Upper boundary (inclusive) to check against. exclude_max : bool, optional Exclude the upper boundary `max` if set to True. Defaults to False. Returns ------- bool : True if `x` is between the specified bounds, otherwise False. """ if x is None or min is None or max is None: return None if isinstance(x, str): x = str2time(x) if isinstance(min, str): min = str2time(min) if isinstance(max, str): max = str2time(max) if Lt().exec_num(max, min): return False if exclude_max: return Gte.exec_num(x, min) & Lt.exec_num(x, max) else: return Gte.exec_num(x, min) & Lte.exec_num(x, max)