Beispiel #1
0
def evaluate(expression, **kwargs):
    """Evaluates an expression.

    Provides the facility to evaluate mathematical expressions, and to
    substitute variables from dictionaries into those expressions.

    Supports both integer and floating point values, and automatic
    promotion where necessary.
    """
    global _parser
    if _parser is None:
        _parser = _def_parser()

    global _vars
    _vars = kwargs

    # Some reasonable formulas break with the default recursion limit of
    # 1000.  Raise it here and reset it afterward.
    orig_recursion_limit = sys.getrecursionlimit()
    if orig_recursion_limit < 3000:
        sys.setrecursionlimit(3000)

    try:
        result = _parser.parseString(expression, parseAll=True)[0]
    except pyparsing.ParseException as e:
        raise exception.EvaluatorParseException(
            _("ParseException: %s") % e)
    finally:
        sys.setrecursionlimit(orig_recursion_limit)

    return result.eval()
Beispiel #2
0
 def eval(self):
     prod = self.value[0].eval()
     for op, val in _operatorOperands(self.value[1:]):
         try:
             if op == '*':
                 prod *= val.eval()
             elif op == '/':
                 prod /= float(val.eval())
         except ZeroDivisionError as e:
             raise exception.EvaluatorParseException(
                 _("ZeroDivisionError: %s") % e)
     return prod
Beispiel #3
0
    def eval(self):
        result = self.value
        if (isinstance(result, six.string_types)
                and re.match(r"^[a-zA-Z_]+\.[a-zA-Z_]+$", result)):
            (which_dict, entry) = result.split('.')
            try:
                result = _vars[which_dict][entry]
            except KeyError as e:
                raise exception.EvaluatorParseException(_("KeyError: %s") % e)
            except TypeError as e:
                raise exception.EvaluatorParseException(_("TypeError: %s") % e)

        try:
            result = int(result)
        except ValueError:
            try:
                result = float(result)
            except ValueError as e:
                raise exception.EvaluatorParseException(
                    _("ValueError: %s") % e)

        return result
Beispiel #4
0
    def eval(self):
        result = self.value
        if (isinstance(result, str) and
                re.match(r"^[a-zA-Z_]+\.[a-zA-Z_]+$", result)):
            (which_dict, entry) = result.split('.')
            try:
                result = _vars[which_dict][entry]
            except KeyError:
                raise exception.EvaluatorParseException(
                    _("KeyError evaluating string"))
            except TypeError:
                raise exception.EvaluatorParseException(
                    _("TypeError evaluating string"))

        try:
            result = int(result)
        except ValueError:
            try:
                result = float(result)
            except ValueError:
                if isinstance(result, str):
                    result = result.replace('"', '').replace('\'', '')

        return result
Beispiel #5
0
def evaluate(expression, **kwargs):
    """Evaluates an expression.

    Provides the facility to evaluate mathematical expressions, and to
    substitute variables from dictionaries into those expressions.

    Supports both integer and floating point values, and automatic
    promotion where necessary.
    """
    global _parser
    if _parser is None:
        _parser = _def_parser()

    global _vars
    _vars = kwargs

    try:
        result = _parser.parseString(expression, parseAll=True)[0]
    except pyparsing.ParseException as e:
        raise exception.EvaluatorParseException(_("ParseException: %s") % e)

    return result.eval()