def __div__(self, amount): if isinstance(amount, Duration) and amount.month: m = self.month r = self.milli # DO NOT CONSIDER TIME OF DAY tod = r % MILLI_VALUES.day r = r - tod if m == 0 and r > (MILLI_VALUES.year / 3): m = Math.floor(12 * self.milli / MILLI_VALUES.year) r -= (m / 12) * MILLI_VALUES.year else: r = r - (self.month * MILLI_VALUES.month) if r >= MILLI_VALUES.day * 31: from mo_logs import Log Log.error("Do not know how to handle") r = MIN([29 / 30, (r + tod) / (MILLI_VALUES.day * 30)]) output = Math.floor(m / amount.month) + r return output elif Math.is_number(amount): output = Duration(0) output.milli = self.milli / amount output.month = self.month / amount return output else: return self.milli / amount.milli
def assertAlmostEqualValue(test, expected, digits=None, places=None, msg=None, delta=None): """ Snagged from unittest/case.py, then modified (Aug2014) """ if expected.__class__.__name__ == "NullOp": if test == None: return else: raise AssertionError(expand_template("{{test}} != {{expected}}", locals())) if expected == None: # None has no expectations return if test == expected: # shortcut return if not Math.is_number(expected): # SOME SPECIAL CASES, EXPECTING EMPTY CONTAINERS IS THE SAME AS EXPECTING NULL if isinstance(expected, list) and len(expected) == 0 and test == None: return if isinstance(expected, Mapping) and not expected.keys() and test == None: return if test != expected: raise AssertionError(expand_template("{{test}} != {{expected}}", locals())) return num_param = 0 if digits != None: num_param += 1 if places != None: num_param += 1 if delta != None: num_param += 1 if num_param>1: raise TypeError("specify only one of digits, places or delta") if digits is not None: with suppress_exception: diff = Math.log10(abs(test-expected)) if diff < digits: return standardMsg = expand_template("{{test}} != {{expected}} within {{digits}} decimal places", locals()) elif delta is not None: if abs(test - expected) <= delta: return standardMsg = expand_template("{{test}} != {{expected}} within {{delta}} delta", locals()) else: if places is None: places = 15 with suppress_exception: diff = Math.log10(abs(test-expected)) if diff < Math.ceiling(Math.log10(abs(test)))-places: return standardMsg = expand_template("{{test|json}} != {{expected|json}} within {{places}} places", locals()) raise AssertionError(coalesce(msg, "") + ": (" + standardMsg + ")")
def __new__(cls, value=None, **kwargs): output = object.__new__(cls) if value == None: if kwargs: output.milli = datetime.timedelta( **kwargs).total_seconds() * 1000 output.month = 0 return output else: return None if Math.is_number(value): output._milli = float(value) * 1000 output.month = 0 return output elif isinstance(value, text_type): return parse(value) elif isinstance(value, Duration): output.milli = value.milli output.month = value.month return output elif isinstance(value, float) and Math.is_nan(value): return None else: from mo_logs import Log Log.error("Do not know type of object (" + get_module("mo_json").value2json(value) + ")of to make a Duration")
def quote_value(self, value): """ convert values to mysql code for the same mostly delegate directly to the mysql lib, but some exceptions exist """ try: if value == None: return SQL("NULL") elif isinstance(value, SQL): if not value.param: # value.template CAN BE MORE THAN A TEMPLATE STRING return self.quote_sql(value.template) param = {k: self.quote_sql(v) for k, v in value.param.items()} return SQL(expand_template(value.template, param)) elif isinstance(value, basestring): return SQL(self.db.literal(value)) elif isinstance(value, Mapping): return SQL(self.db.literal(json_encode(value))) elif Math.is_number(value): return SQL(text_type(value)) elif isinstance(value, datetime): return SQL("str_to_date('" + value.strftime("%Y%m%d%H%M%S.%f") + "', '%Y%m%d%H%i%s.%f')") elif isinstance(value, Date): return SQL("str_to_date('" + value.format("%Y%m%d%H%M%S.%f") + "', '%Y%m%d%H%i%s.%f')") elif hasattr(value, '__iter__'): return SQL(self.db.literal(json_encode(value))) else: return self.db.literal(value) except Exception as e: Log.error("problem quoting SQL", e)
def quote_value(self, value): """ convert values to mysql code for the same mostly delegate directly to the mysql lib, but some exceptions exist """ try: if value == None: return SQL("NULL") elif isinstance(value, SQL): if not value.param: # value.template CAN BE MORE THAN A TEMPLATE STRING return self.quote_sql(value.template) param = {k: self.quote_sql(v) for k, v in value.param.items()} return SQL(expand_template(value.template, param)) elif isinstance(value, basestring): return SQL(self.db.literal(value)) elif isinstance(value, Mapping): return SQL(self.db.literal(json_encode(value))) elif Math.is_number(value): return SQL(unicode(value)) elif isinstance(value, datetime): return SQL("str_to_date('" + value.strftime("%Y%m%d%H%M%S.%f") + "', '%Y%m%d%H%i%s.%f')") elif isinstance(value, Date): return SQL("str_to_date('"+value.format("%Y%m%d%H%M%S.%f")+"', '%Y%m%d%H%i%s.%f')") elif hasattr(value, '__iter__'): return SQL(self.db.literal(json_encode(value))) else: return self.db.literal(value) except Exception as e: Log.error("problem quoting SQL", e)
def quote_value(value): """ convert values to mysql code for the same mostly delegate directly to the mysql lib, but some exceptions exist """ try: if value == None: return SQL_NULL elif isinstance(value, SQL): return quote_sql(value.template, value.param) elif isinstance(value, text_type): return SQL("'" + "".join(ESCAPE_DCT.get(c, c) for c in value) + "'") elif isinstance(value, Mapping): return quote_value(json_encode(value)) elif Math.is_number(value): return SQL(text_type(value)) elif isinstance(value, datetime): return SQL("str_to_date('" + value.strftime("%Y%m%d%H%M%S.%f") + "', '%Y%m%d%H%i%s.%f')") elif isinstance(value, Date): return SQL("str_to_date('" + value.format("%Y%m%d%H%M%S.%f") + "', '%Y%m%d%H%i%s.%f')") elif hasattr(value, '__iter__'): return quote_value(json_encode(value)) else: return quote_value(text_type(value)) except Exception as e: Log.error("problem quoting SQL {{value}}", value=repr(value), cause=e)
def convert(self, expr): """ EXPAND INSTANCES OF name TO value """ if expr is True or expr == None or expr is False: return expr elif Math.is_number(expr): return expr elif expr == ".": return "." elif is_variable_name(expr): return coalesce(self.dimensions[expr], expr) elif isinstance(expr, basestring): Log.error("{{name|quote}} is not a valid variable name", name=expr) elif isinstance(expr, Date): return expr elif isinstance(expr, QueryOp): return self._convert_query(expr) elif isinstance(expr, Mapping): if expr["from"]: return self._convert_query(expr) elif len(expr) >= 2: #ASSUME WE HAVE A NAMED STRUCTURE, NOT AN EXPRESSION return wrap({name: self.convert(value) for name, value in expr.leaves()}) else: # ASSUME SINGLE-CLAUSE EXPRESSION k, v = expr.items()[0] return converter_map.get(k, self._convert_bop)(self, k, v) elif isinstance(expr, (list, set, tuple)): return wrap([self.convert(value) for value in expr]) else: return expr
def quote_value(self, value): """ convert values to mysql code for the same mostly delegate directly to the mysql lib, but some exceptions exist """ try: if value == None: return SQL_NULL elif isinstance(value, SQL): return self.quote_sql(value.template, value.param) elif isinstance(value, text_type): return SQL("'" + value.replace("'", "''") + "'") elif isinstance(value, Mapping): return self.quote_value(json_encode(value)) elif Math.is_number(value): return SQL(text_type(value)) elif isinstance(value, datetime): return SQL("str_to_date('" + value.strftime("%Y%m%d%H%M%S.%f") + "', '%Y%m%d%H%i%s.%f')") elif isinstance(value, Date): return SQL("str_to_date('" + value.format("%Y%m%d%H%M%S.%f") + "', '%Y%m%d%H%i%s.%f')") elif hasattr(value, '__iter__'): return self.quote_value(json_encode(value)) else: return self.quote_value(text_type(value)) except Exception as e: Log.error("problem quoting SQL {{value}}", value=repr(value), cause=e)
def __new__(cls, value=None, **kwargs): output = object.__new__(cls) if value == None: if kwargs: output.milli = datetime.timedelta(**kwargs).total_seconds() * 1000 output.month = 0 return output else: return None if Math.is_number(value): output._milli = float(value) * 1000 output.month = 0 return output elif isinstance(value, text_type): return parse(value) elif isinstance(value, Duration): output.milli = value.milli output.month = value.month return output elif isinstance(value, float) and Math.is_nan(value): return None else: from mo_logs import Log Log.error("Do not know type of object (" + get_module("mo_json").value2json(value) + ")of to make a Duration")
def convert(self, expr): """ EXPAND INSTANCES OF name TO value """ if expr is True or expr == None or expr is False: return expr elif Math.is_number(expr): return expr elif expr == ".": return "." elif is_variable_name(expr): return coalesce(self.dimensions[expr], expr) elif isinstance(expr, text_type): Log.error("{{name|quote}} is not a valid variable name", name=expr) elif isinstance(expr, Date): return expr elif isinstance(expr, QueryOp): return self._convert_query(expr) elif isinstance(expr, Mapping): if expr["from"]: return self._convert_query(expr) elif len(expr) >= 2: #ASSUME WE HAVE A NAMED STRUCTURE, NOT AN EXPRESSION return wrap({name: self.convert(value) for name, value in expr.leaves()}) else: # ASSUME SINGLE-CLAUSE EXPRESSION k, v = expr.items()[0] return converter_map.get(k, self._convert_bop)(self, k, v) elif isinstance(expr, (list, set, tuple)): return wrap([self.convert(value) for value in expr]) else: return expr
def value2query(value): if isinstance(value, datetime): return convert.datetime2milli(value) if isinstance(value, Duration): return value.milli if Math.is_number(value): return value return quote(value)
def value2MVEL(value): """ FROM PYTHON VALUE TO MVEL EQUIVALENT """ if isinstance(value, datetime): return str(convert.datetime2milli(value)) + " /*" + value.format("yyNNNdd HHmmss") + "*/" # TIME if isinstance(value, Duration): return str(convert.timedelta2milli(value)) + " /*" + str(value) + "*/" # DURATION if Math.is_number(value): return str(value) return quote(value)
def to_sql(self, schema, not_null=False, boolean=False): value = self.value v = quote_value(value) if v == None: return wrap([{"name": "."}]) elif isinstance(value, text_type): return wrap([{"name": ".", "sql": {"s": quote_value(value)}}]) elif Math.is_number(v): return wrap([{"name": ".", "sql": {"n": quote_value(value)}}]) elif v in [True, False]: return wrap([{"name": ".", "sql": {"b": quote_value(value)}}]) else: return wrap([{"name": ".", "sql": {"j": quote_value(self.json)}}])
def to_sql(self, schema, not_null=False, boolean=False): value = json2value(self.json) v = sql_quote(value) if v == None: return wrap([{"name": "."}]) elif isinstance(value, unicode): return wrap([{"name": ".", "sql": {"s": sql_quote(value)}}]) elif Math.is_number(v): return wrap([{"name": ".", "sql": {"n": sql_quote(value)}}]) elif v in [True, False]: return wrap([{"name": ".", "sql": {"b": sql_quote(value)}}]) else: return wrap([{"name": ".", "sql": {"j": sql_quote(self.json)}}])
def _scrub(r): try: if r == None: return None elif isinstance(r, basestring): if r == "": return None return r elif Math.is_number(r): return convert.value2number(r) elif isinstance(r, Mapping): if isinstance(r, Data): r = object.__getattribute__(r, "_dict") output = {} for k, v in r.items(): v = _scrub(v) if v != None: output[k.lower()] = v if len(output) == 0: return None return output elif hasattr(r, '__iter__'): if isinstance(r, FlatList): r = r.list output = [] for v in r: v = _scrub(v) if v != None: output.append(v) if not output: return None elif len(output) == 1: return output[0] else: return output else: return r except Exception as e: Log.warning("Can not scrub: {{json}}", json=r, cause=e)
def _scrub(r): try: if r == None: return None elif isinstance(r, basestring): if r == "": return None return r elif Math.is_number(r): return convert.value2number(r) elif isinstance(r, Mapping): if isinstance(r, Data): r = object.__getattribute__(r, "_dict") output = {} for k, v in r.items(): v = _scrub(v) if v != None: output[k.lower()] = v if len(output) == 0: return None return output elif hasattr(r, '__iter__'): if isinstance(r, FlatList): r = r.list output = [] for v in r: v = _scrub(v) if v != None: output.append(v) if not output: return None elif len(output) == 1: return output[0] else: return output else: return r except Exception, e: Log.warning("Can not scrub: {{json}}", json=r, cause=e)
def convert(self, expr): """ ADD THE ".$value" SUFFIX TO ALL VARIABLES """ if isinstance(expr, Expression): vars_ = expr.vars() rename = {v: concat_field(v, "$value") for v in vars_} return expr.map(rename) if expr is True or expr == None or expr is False: return expr elif Math.is_number(expr): return expr elif expr == ".": return "." elif is_keyword(expr): #TODO: LOOKUP SCHEMA AND ADD ALL COLUMNS WITH THIS PREFIX return expr + ".$value" elif isinstance(expr, basestring): Log.error("{{name|quote}} is not a valid variable name", name=expr) elif isinstance(expr, Date): return expr elif isinstance(expr, QueryOp): return self._convert_query(expr) elif isinstance(expr, Mapping): if expr["from"]: return self._convert_query(expr) elif len(expr) >= 2: #ASSUME WE HAVE A NAMED STRUCTURE, NOT AN EXPRESSION return wrap({ name: self.convert(value) for name, value in expr.items() }) else: # ASSUME SINGLE-CLAUSE EXPRESSION k, v = expr.items()[0] return self.converter_map.get(k, self._convert_bop)(k, v) elif isinstance(expr, (list, set, tuple)): return wrap([self.convert(value) for value in expr])
def convert(self, expr): """ ADD THE ".$value" SUFFIX TO ALL VARIABLES """ if isinstance(expr, Expression): vars_ = expr.vars() rename = {v: concat_field(v, "$value") for v in vars_} return expr.map(rename) if expr is True or expr == None or expr is False: return expr elif Math.is_number(expr): return expr elif expr == ".": return "." elif is_variable_name(expr): #TODO: LOOKUP SCHEMA AND ADD ALL COLUMNS WITH THIS PREFIX return expr + ".$value" elif isinstance(expr, basestring): Log.error("{{name|quote}} is not a valid variable name", name=expr) elif isinstance(expr, Date): return expr elif isinstance(expr, QueryOp): return self._convert_query(expr) elif isinstance(expr, Mapping): if expr["from"]: return self._convert_query(expr) elif len(expr) >= 2: #ASSUME WE HAVE A NAMED STRUCTURE, NOT AN EXPRESSION return wrap({name: self.convert(value) for name, value in expr.items()}) else: # ASSUME SINGLE-CLAUSE EXPRESSION k, v = expr.items()[0] return self.converter_map.get(k, self._convert_bop)(k, v) elif isinstance(expr, (list, set, tuple)): return wrap([self.convert(value) for value in expr])
def __unicode__(self): if not self.milli: return "zero" output = "" rest = (self.milli - (MILLI_VALUES.month * self.month)) # DO NOT INCLUDE THE MONTH'S MILLIS isNegative = (rest < 0) rest = Math.abs(rest) # MILLI rem = rest % 1000 if rem != 0: output = "+" + text_type(rem) + "milli" + output rest = Math.floor(rest / 1000) # SECOND rem = rest % 60 if rem != 0: output = "+" + text_type(rem) + "second" + output rest = Math.floor(rest / 60) # MINUTE rem = rest % 60 if rem != 0: output = "+" + text_type(rem) + "minute" + output rest = Math.floor(rest / 60) # HOUR rem = rest % 24 if rem != 0: output = "+" + text_type(rem) + "hour" + output rest = Math.floor(rest / 24) # DAY if (rest < 11 and rest != 7) or rest % 10 == 0: rem = rest rest = 0 else: rem = rest % 7 rest = Math.floor(rest / 7) if rem != 0: output = "+" + text_type(rem) + "day" + output # WEEK if rest != 0: output = "+" + text_type(rest) + "week" + output if isNegative: output = output.replace("+", "-") # MONTH AND YEAR if self.month: sign = "-" if self.month < 0 else "+" month = Math.abs(self.month) if month <= 18 and month != 12: output = sign + text_type(month) + "month" + output else: m = month % 12 if m != 0: output = sign + text_type(m) + "month" + output y = Math.floor(month / 12) output = sign + text_type(y) + "year" + output if output[0] == "+": output = output[1::] if output[0] == '1' and not Math.is_number(output[1]): output = output[1::] return output
def assertAlmostEqualValue(test, expected, digits=None, places=None, msg=None, delta=None): """ Snagged from unittest/case.py, then modified (Aug2014) """ if expected.__class__.__name__ == "NullOp": if test == None: return else: raise AssertionError( expand_template("{{test}} != {{expected}}", locals())) if expected == None: # None has no expectations return if test == expected: # shortcut return if not Math.is_number(expected): # SOME SPECIAL CASES, EXPECTING EMPTY CONTAINERS IS THE SAME AS EXPECTING NULL if isinstance(expected, list) and len(expected) == 0 and test == None: return if isinstance(expected, Mapping) and not expected.keys() and test == None: return if test != expected: raise AssertionError( expand_template("{{test}} != {{expected}}", locals())) return num_param = 0 if digits != None: num_param += 1 if places != None: num_param += 1 if delta != None: num_param += 1 if num_param > 1: raise TypeError("specify only one of digits, places or delta") if digits is not None: with suppress_exception: diff = Math.log10(abs(test - expected)) if diff < digits: return standardMsg = expand_template( "{{test}} != {{expected}} within {{digits}} decimal places", locals()) elif delta is not None: if abs(test - expected) <= delta: return standardMsg = expand_template( "{{test}} != {{expected}} within {{delta}} delta", locals()) else: if places is None: places = 15 with suppress_exception: diff = Math.log10(abs(test - expected)) if diff < Math.ceiling(Math.log10(abs(test))) - places: return standardMsg = expand_template( "{{test|json}} != {{expected|json}} within {{places}} places", locals()) raise AssertionError(coalesce(msg, "") + ": (" + standardMsg + ")")
def __unicode__(self): if not self.milli: return "zero" output = "" rest = (self.milli - (MILLI_VALUES.month * self.month) ) # DO NOT INCLUDE THE MONTH'S MILLIS isNegative = (rest < 0) rest = Math.abs(rest) # MILLI rem = rest % 1000 if rem != 0: output = "+" + text_type(rem) + "milli" + output rest = Math.floor(rest / 1000) # SECOND rem = rest % 60 if rem != 0: output = "+" + text_type(rem) + "second" + output rest = Math.floor(rest / 60) # MINUTE rem = rest % 60 if rem != 0: output = "+" + text_type(rem) + "minute" + output rest = Math.floor(rest / 60) # HOUR rem = rest % 24 if rem != 0: output = "+" + text_type(rem) + "hour" + output rest = Math.floor(rest / 24) # DAY if (rest < 11 and rest != 7) or rest % 10 == 0: rem = rest rest = 0 else: rem = rest % 7 rest = Math.floor(rest / 7) if rem != 0: output = "+" + text_type(rem) + "day" + output # WEEK if rest != 0: output = "+" + text_type(rest) + "week" + output if isNegative: output = output.replace("+", "-") # MONTH AND YEAR if self.month: sign = "-" if self.month < 0 else "+" month = Math.abs(self.month) if month <= 18 and month != 12: output = sign + text_type(month) + "month" + output else: m = month % 12 if m != 0: output = sign + text_type(m) + "month" + output y = Math.floor(month / 12) output = sign + text_type(y) + "year" + output if output[0] == "+": output = output[1::] if output[0] == '1' and not Math.is_number(output[1]): output = output[1::] return output