Example #1
0
def assertAlmostEqualValue(test, expected, digits=None, places=None, msg=None, delta=None):
    """
    Snagged from unittest/case.py, then modified (Aug2014)
    """
    if expected is NULL:
        if test == None:  # pandas dataframes reject any comparision with an exception!
            return
        else:
            raise AssertionError(expand_template("{{test}} != {{expected}}", locals()))

    if expected == None:  # None has no expectations
        return
    if test == expected:
        # shortcut
        return

    if not is_number(expected):
        # SOME SPECIAL CASES, EXPECTING EMPTY CONTAINERS IS THE SAME AS EXPECTING NULL
        if is_list(expected) and len(expected) == 0 and test == None:
            return
        if is_data(expected) 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 = 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 = mo_math.log10(abs(test-expected))
            if diff < mo_math.ceiling(mo_math.log10(abs(test)))-places:
                return

        standardMsg = expand_template("{{test|json}} != {{expected|json}} within {{places}} places", locals())

    raise AssertionError(coalesce(msg, "") + ": (" + standardMsg + ")")
Example #2
0
    def _make_range_domain(self, domain, column_name):
        width = (domain.max - domain.min) / domain.interval
        digits = mo_math.floor(mo_math.log10(width - 1))
        if digits == 0:
            value = "a.value"
        else:
            value = SQL("+").join("1" + ("0" * j) + "*" +
                                  text_type(chr(ord(b'a') + j)) + ".value"
                                  for j in range(digits + 1))

        if domain.interval == 1:
            if domain.min == 0:
                domain = (SQL_SELECT + value + column_name + SQL_FROM +
                          "__digits__ a")
            else:
                domain = (SQL_SELECT + sql_iso(value) + " + " +
                          quote_value(domain.min) + column_name + SQL_FROM +
                          "__digits__ a")
        else:
            if domain.min == 0:
                domain = (SQL_SELECT + value + " * " +
                          quote_value(domain.interval) + column_name +
                          SQL_FROM + "__digits__ a")
            else:
                domain = (
                    SQL_SELECT +
                    sql_iso(value + " * " + quote_value(domain.interval)) +
                    " + " + quote_value(domain.min) + column_name + SQL_FROM +
                    "__digits__ a")

        for j in range(digits):
            domain += SQL_INNER_JOIN + "__digits__" + text_type(
                chr(ord(b'a') + j + 1)) + " ON " + SQL_TRUE
        domain += SQL_WHERE + value + " < " + quote_value(width)
        return domain
Example #3
0
def nice_ceiling(value):
    """
    RETURN A NICE CEILING
    :param value:
    """
    if value == 0:
        return 1
    d = 10 ** (ceiling(log10(value)) - 1)
    norm = value / d
    nice = first(v for v in [1.5, 2, 3, 5, 7.5, 10] if norm <= v)

    return nice * d
Example #4
0
    def _make_range_domain(self, domain, column_name):
        width = (domain.max - domain.min) / domain.interval
        digits = mo_math.floor(mo_math.log10(width - 1))
        if digits == 0:
            value = quote_column("a", "value")
        else:
            value = SQL_PLUS.join("1" + ("0" * j) + SQL_STAR +
                                  text(chr(ord(b'a') + j)) + ".value"
                                  for j in range(digits + 1))
        if domain.interval == 1:
            if domain.min == 0:
                domain = (SQL_SELECT + sql_alias(value, column_name) +
                          SQL_FROM +
                          sql_alias(quote_column(DIGITS_TABLE), "a"))
            else:
                domain = (SQL_SELECT + sql_alias(
                    sql_iso(value) + SQL_PLUS + quote_value(domain.min),
                    column_name) + SQL_FROM +
                          sql_alias(quote_column(DIGITS_TABLE), "a"))
        else:
            if domain.min == 0:
                domain = ConcatSQL(
                    SQL_SELECT,
                    sql_alias(value + SQL_STAR + quote_value(domain.interval),
                              column_name), SQL_FROM,
                    sql_alias(quote_column(DIGITS_TABLE), "a"))
            else:
                domain = ConcatSQL(
                    SQL_SELECT,
                    sql_alias(
                        sql_iso(value, SQL_STAR, quote_value(domain.interval))
                        + SQL_PLUS + quote_value(domain.min), column_name),
                    SQL_FROM, sql_alias(quote_column(DIGITS_TABLE), "a"))

        for j in range(digits):
            domain += SQL_INNER_JOIN + sql_alias(
                quote_column(DIGITS_TABLE), text(
                    chr(ord(b'a') + j + 1))) + SQL_ON + SQL_TRUE
        domain += SQL_WHERE + value + " < " + quote_value(width)
        return domain