How to use the pycel.excelutil.coerce_to_number function in pycel

To help you get started, we’ve selected a few pycel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dgorissen / pycel / tests / test_excelutil.py View on Github external
def test_coerce_to_number(value, expected, expected_type, convert_all):
    result = coerce_to_number(value, convert_all=convert_all)
    assert result == expected
    assert isinstance(result, expected_type)
github dgorissen / pycel / tests / lib / test_binary.py View on Github external
def compare_result(expected, result):
    expected = coerce_to_number(expected)
    result = coerce_to_number(result)
    if isinstance(expected, (int, float)) and isinstance(result, (int, float)):
        return pytest.approx(expected) == result
    else:
        return expected == result
github dgorissen / pycel / src / pycel / excelformula.py View on Github external
def func_subtotal(self):
        # Excel reference: https://support.office.com/en-us/article/
        #   SUBTOTAL-function-7B027003-F060-4ADE-9040-E478765B9939

        # Note: This does not implement skipping hidden rows.

        func_num = coerce_to_number(self.children[0].emit)
        if func_num not in self.SUBTOTAL_FUNCS:
            if func_num - 100 in self.SUBTOTAL_FUNCS:
                func_num -= 100
            else:
                raise ValueError(
                    "Unknown SUBTOTAL function number: {}".format(func_num))

        func = self.SUBTOTAL_FUNCS[func_num]

        return "{}({})".format(
            func, self.comma_join_emit(fmt_str="{}", to_emit=self.children[1:]))
github dgorissen / pycel / src / pycel / lib / date_time.py View on Github external
def months_inc(start_date, months, eomonth=False):
    if isinstance(start_date, bool) or isinstance(months, bool):
        return VALUE_ERROR
    start_date = coerce_to_number(start_date, convert_all=True)
    months = coerce_to_number(months, convert_all=True)
    if isinstance(start_date, str) or isinstance(months, str):
        return VALUE_ERROR
    if start_date < 0:
        return NUM_ERROR
    y, m, d = date_from_int(start_date)
    if eomonth:
        return date(y, m + months + 1, 1) - 1
    else:
        return date(y, m + months, d)
github dgorissen / pycel / src / pycel / excellib.py View on Github external
def small(array, k):
    # Excel reference: https://support.office.com/en-us/article/
    #   small-function-17da8222-7c82-42b2-961b-14c45384df07
    data = _numerics(array, to_number=coerce_to_number)
    if isinstance(data, str):
        return data

    k = coerce_to_number(k)
    if isinstance(k, str):
        return VALUE_ERROR

    if not data or k is None or k < 1 or k > len(data):
        return NUM_ERROR

    k = math.ceil(k)
    return nsmallest(k, data)[-1]
github dgorissen / pycel / src / pycel / excellib.py View on Github external
def isodd(value):
    # Excel reference: https://support.office.com/en-us/article/
    #   is-functions-0f2d7971-6019-40a0-a171-f2d869135665
    if isinstance(value, bool):
        return VALUE_ERROR
    value = coerce_to_number(value)
    if isinstance(value, str):
        return VALUE_ERROR
    return bool(math.floor(abs(value)) % 2)
github dgorissen / pycel / src / pycel / excellib.py View on Github external
def large(array, k):
    # Excel reference: https://support.office.com/en-us/article/
    #   large-function-3af0af19-1190-42bb-bb8b-01672ec00a64
    data = _numerics(array, to_number=coerce_to_number)
    if isinstance(data, str):
        return data

    k = coerce_to_number(k)
    if isinstance(k, str):
        return VALUE_ERROR

    if not data or k is None or k < 1 or k > len(data):
        return NUM_ERROR

    k = math.ceil(k)
    return nlargest(k, data)[-1]
github dgorissen / pycel / src / pycel / lib / function_helpers.py View on Github external
def wrapper(*args):
        new_args = tuple(coerce_to_number(a, convert_all=True)
                         if i in param_indices else a
                         for i, a in enumerate(args))
        error = next((a for i, a in enumerate(new_args)
                      if i in param_indices and a in ERROR_CODES), None)
        if error:
            return error

        if any(i in param_indices and not is_number(a)
               for i, a in enumerate(new_args)):
            return VALUE_ERROR

        try:
            return f(*new_args)
        except ValueError as exc:
            if "math domain error" in str(exc):
                return NUM_ERROR