def __init__(self, text): """Create expression from text. :param text: Input text :type text: str :raise: ValueError if not a valid expression """ parts = text.split() if len(parts) != 3: raise ValueError(self._op_errmsg.format(text)) self._field = parts[0] oper_str = parts[1] self._val = parts[2] # if val is a date, then parse it and optionally modify operator _, mdate = parse.guess(self._val, try_num=False) if mdate is not None: self._val = mdate if self._field != common.Keyword.TIME: # time will have the value itself pre-parsed, but other # fields will not, so we indicate this in the operator oper_str = Oper.DATE_VALUE_PREFIX + oper_str elif self._field == common.Keyword.TIME: raise ValueError( "Cannot compare non-date to timestamp field ({}): {}".format( self._field, self._val)) self._op = Oper(oper_str)
def compare(self, lhs, rhs): """Compare two values with this operator. :return: Boolean result of comparison :rtype: bool """ if self._rhs_date: _, rhs_d = parse.guess(rhs) if rhs_d is None: return False # Not a date; exception? rhs = rhs_d elif self._must_be_numeric: lhs = self._number(lhs) rhs = self._number(rhs) if lhs is None or rhs is None: return False # Not a number; exception? return self._opfn(lhs, rhs)