def round(value, ndigits=0) :
    """
    round(number[, ndigits]) -> float
    Round a number to a given precision in decimal digits (default 0 digits).
    This always returns a floating point number.  Precision may be negative.
    This builtin function was overloaded in mathutils to work on complex numbers,
    in that case rel and imaginary values are rounded separately

    """
    ndigits = int(ndigits)
    if isinstance(value, complex) :
        return complex(_round(value.real, ndigits), _round(value.imag, ndigits))
    else :
        return _round(value, ndigits)
Example #2
0
def round(value, ndigits=0):
    """
    round(number[, ndigits]) -> float
    Round a number to a given precision in decimal digits (default 0 digits).
    This always returns a floating point number.  Precision may be negative.
    This builtin function was overloaded in mathutils to work on complex numbers,
    in that case rel and imaginary values are rounded separately

    """
    ndigits = int(ndigits)
    if isinstance(value, complex):
        return complex(_round(value.real, ndigits),
                       _round(value.imag, ndigits))
    else:
        return _round(value, ndigits)
Example #3
0
    def replacer(found):
        ops = found.group(1).split("|")

        path = ops[0]
        var = path.lstrip(".")
        depth = min(len(seq), max(1, len(path) - len(var)))
        try:
            val = seq[-depth]
            if var:
                if isinstance(val, (list, tuple)) and float(var) == _round(
                        float(var), 0):
                    val = val[int(var)]
                else:
                    val = val[var]
            for func_name in ops[1:]:
                parts = func_name.split('(')
                if len(parts) > 1:
                    val = eval(parts[0] + "(val, " + ("(".join(parts[1::])))
                else:
                    val = globals()[func_name](val)
            val = toString(val)
            return val
        except Exception as e:
            try:
                if e.message.find("is not JSON serializable"):
                    # WORK HARDER
                    val = toString(val)
                    return val
            except Exception, f:
                if not _Log:
                    _late_import()

                _Log.warning("Can not expand " + "|".join(ops) +
                             " in template: {{template_|json}}",
                             template_=template,
                             cause=e)
            return "[template expansion error: (" + str(e.message) + ")]"
Example #4
0
    def replacer(found):
        ops = found.group(1).split("|")

        path = ops[0]
        var = path.lstrip(".")
        depth = min(len(seq), max(1, len(path) - len(var)))
        try:
            val = seq[-depth]
            if var:
                if isinstance(val, (list, tuple)) and float(var) == _round(float(var), 0):
                    val = val[int(var)]
                else:
                    val = val[var]
            for func_name in ops[1:]:
                parts = func_name.split('(')
                if len(parts) > 1:
                    val = eval(parts[0] + "(val, " + ("(".join(parts[1::])))
                else:
                    val = globals()[func_name](val)
            val = toString(val)
            return val
        except Exception, e:
            try:
                if e.message.find("is not JSON serializable"):
                    # WORK HARDER
                    val = toString(val)
                    return val
            except Exception, f:
                if not _Log:
                    _late_import()

                _Log.warning(
                    "Can not expand " + "|".join(ops) + " in template: {{template_|json}}",
                    template_=template,
                    cause=e
                )