Exemplo n.º 1
0
    def test_readonly_symbols(self):

        def foo():
            return 31


        usersyms = {
            "a": 10,
            "b": 11,
            "c": 12,
            "d": 13,
            "foo": foo,
            "bar": foo,
            "x": 5,
            "y": 7
        }

        aeval = Interpreter(usersyms=usersyms, readonly_symbols={"a", "b", "c", "d", "foo", "bar"})

        aeval("a = 20")
        aeval("def b(): return 100")
        aeval("c += 1")
        aeval("del d")
        aeval("def foo(): return 55")
        aeval("bar = None")
        aeval("x = 21")
        aeval("y += a")

        assert(aeval("a") == 10)
        assert(aeval("b") == 11)
        assert(aeval("c") == 12)
        assert(aeval("d") == 13)
        assert(aeval("foo()") == 31)
        assert(aeval("bar()") == 31)
        assert(aeval("x") == 21)
        assert(aeval("y") == 17)


        assert(aeval("abs(8)") == 8)
        assert(aeval("abs(-8)") == 8)
        aeval("def abs(x): return x*2")
        assert(aeval("abs(8)") == 16)
        assert(aeval("abs(-8)") == -16)

        aeval2 = Interpreter(builtins_readonly=True)

        assert(aeval2("abs(8)") == 8)
        assert(aeval2("abs(-8)") == 8)
        aeval2("def abs(x): return x*2")
        assert(aeval2("abs(8)") == 8)
        assert(aeval2("abs(-8)") == 8)
Exemplo n.º 2
0
def match_rule(rule, expression, pre_context, post_context):
    symbols = {}
    aeval = Interpreter()
    if not obtain_symbols(symbols, rule[1], expression):
        return False
    if not obtain_symbols(symbols, rule[0], pre_context):
        return False
    if not obtain_symbols(symbols, rule[2], post_context):
        return False

    aeval.symtable = symbols

    if rule[3] != None:
        conditions = rule[3].replace(" ", "").split(",")
        for cond in conditions:
            if not aeval(cond):
                return False

    sfinal = ""
    res_split = split_symbols(rule[4])
    for r in res_split:
        if r.find("(") >= 0:
            sfinal += r[:r.find("(") + 1]
            temp = aeval(r[r.find("(") + 1:-1])
            if type(temp) == tuple:
                sfinal += str(temp[0])
                for t in temp[1:]:
                    sfinal += ","
                    sfinal += str(t)
            else:
                sfinal += str(temp)
            sfinal += ")"
        else:
            sfinal += r
    return sfinal
Exemplo n.º 3
0
    def __call__(self, parents, accumulator) -> str:
        """ Execute an action and ship its result """
        parent_symbols = {
            k: ParentSymbol(accumulator[v.id_])
            for k, v in self.format_node_names(parents).items()
        }
        # children_symbols = {k: ChildSymbol(v.id_) for k, v in self.format_node_names(children).items()}
        syms = make_symbol_table(use_numpy=False, **parent_symbols)
        aeval = Interpreter(usersyms=syms,
                            no_while=True,
                            no_try=True,
                            no_functiondef=True,
                            no_ifexp=False,
                            no_augassign=True,
                            no_assert=True,
                            no_delete=True,
                            no_raise=True,
                            no_print=True,
                            use_numpy=False,
                            builtins_readonly=True)

        aeval(self.transform)
        output = aeval.symtable.get("result", None)

        if len(aeval.error) > 0:
            raise TransformException

        return output
Exemplo n.º 4
0
    def determine_category(self, relation_list, app_data_dict,
                           attr_keys_search_mongo, server):
        '''
        determine category by relation
        :param principal: the value user input
        :return: category_id
        '''
        aeval = Interpreter()
        category_id = None
        for rl in relation_list:
            agg_cmp = []
            relation_dict = json.loads(
                rl['relation'])  # convert the 'relation' json to dict

            if set(list(relation_dict.keys())) != set(attr_keys_search_mongo):
                continue

            for kk, vv in relation_dict.items():
                # evaluation the relation using the value of application
                if kk not in app_data_dict:
                    agg_cmp.append(False)
                else:
                    aeval.symtable['VALUE'] = app_data_dict.get(kk)
                    agg_cmp.append(aeval(vv))
            if np.array(agg_cmp).all():
                category_id = rl['category_id']
                break
        if category_id is not None:
            return category_id
        else:
            # cannot match the relation, just return the default one
            logging.warning('cannot match category, load default category_id')
            res_out = self.category_ID_access.read_default_category_id(server)
            return res_out['category_id']
Exemplo n.º 5
0
    def evaluate_dice_list(self):
        """
        Evaluates each dice in the dice list, constructs and evaluates final string
        """
        if self.verbosity:
            print(f"--> evaluate_dice_list")

        self.eval_expression = ""
        for i in range(len(self.dice_list)):
            if isinstance(self.dice_list[i], Die):
                if self.verbosity:
                    print(f"dice_list[{i}] is Die")
                roll_result = self.dice_list[i].evaluate()
                if self.verbosity:
                    print(f"roll_result: {roll_result}")
                self.eval_expression += str(roll_result)
            else:
                if self.verbosity:
                    print(f"dice_list[{i}] is not Die")
                self.eval_expression += self.dice_list[i]

        # Evaluate the constructed string
        aeval = Interpreter()
        self.result = aeval.eval(self.eval_expression)
        if self.verbosity:
            print(f"result: {self.result}")
Exemplo n.º 6
0
 def test_stringio(self):
     """ test using stringio for output/errors """
     out = StringIO()
     err = StringIO()
     intrep = Interpreter(writer=out, err_writer=err)
     intrep("print('out')")
     self.assertEqual(out.getvalue(), 'out\n')
Exemplo n.º 7
0
def _eval(s, error=None, usersyms=None, **kwargs):
    error = error or 'cannot be eval!'
    from asteval import Interpreter
    usersyms = sh.combine_dicts(_usersyms, usersyms or {})
    return Or(And(str, Use(Interpreter(usersyms=usersyms).eval), s),
              s,
              error=error)
Exemplo n.º 8
0
 def _process(self,):
     with open(self.prism_result_file, "r") as fptr:
         lines = fptr.readlines()
     max_param_idx = 0
     aeval = Interpreter()
     for line in lines:
         if len(line) > 7:
             if line[0:6] == 'Result':
                 param_idx, bscc_str = self.process_result_line(line)
                 if param_idx > max_param_idx:
                     max_param_idx = param_idx
                 try:
                     bscc_expr = aeval.parse(bscc_str)
                 except Exception as ex:
                     raise(ex)
                     # sys.exit(ex)
                 self.bscc_str_pfuncs.append(bscc_str)
                 self.bscc_ast_pfuncs.append(bscc_expr)
             elif line[0:30] == 'Parametric model checking: P=?':
                 pattern = re.compile(r'\[ F (.*?)\]')
                 sbscc = re.search(pattern, line).group(1)
                 sbscc = sbscc.rstrip().lstrip()
                 bscc_label, _ = self.process_bscc_label(sbscc)
                 self.bscc_labels.append(bscc_label)
     self.params_count = max_param_idx + 1
Exemplo n.º 9
0
    def __init__(self, asteval=None, usersyms=None):
        """
        Arguments
        ---------
        asteval : :class:`asteval.Interpreter`, optional
            Instance of the `asteval.Interpreter` to use for constraint
            expressions. If None (default), a new interpreter will be
            created. **Warning: deprecated**, use `usersyms` if possible!
        usersyms : dict, optional
            Dictionary of symbols to add to the
            :class:`asteval.Interpreter` (default is None).

        """
        super().__init__(self)

        self._asteval = asteval
        if asteval is None:
            self._asteval = Interpreter()
        else:
            msg = ("The use of the 'asteval' argument for the Parameters class"
                   " was deprecated in lmfit v0.9.12 and will be removed in a "
                   "later release. Please use the 'usersyms' argument instead!")
            warnings.warn(FutureWarning(msg))
            self._asteval = asteval

        _syms = {}
        _syms.update(SCIPY_FUNCTIONS)
        if usersyms is not None:
            _syms.update(usersyms)
        for key, val in _syms.items():
            self._asteval.symtable[key] = val
Exemplo n.º 10
0
    def __init__(self, asteval=None, usersyms=None, *args, **kwds):
        """
        Arguments
        ---------
        asteval : :class:`asteval.Interpreter`, optional
            Instance of the asteval Interpreter to use for constraint
            expressions. If None, a new interpreter will be created.
            Warning: *deprecated, use usersyms if possible*
        usersyms : dictionary of symbols to add to the
            :class:`asteval.Interpreter`.
        *args : optional
            Arguments.
        **kwds : optional
            Keyword arguments.

        """
        super(Parameters, self).__init__(self)

        self._asteval = asteval
        if self._asteval is None:
            self._asteval = Interpreter()

        _syms = {}
        _syms.update(SCIPY_FUNCTIONS)
        if usersyms is not None:
            _syms.update(usersyms)
        for key, val in _syms.items():
            self._asteval.symtable[key] = val

        self.update(*args, **kwds)
Exemplo n.º 11
0
    def post(self, text):

        import pprint
        pp = pprint.PrettyPrinter(indent=4, depth=9)

        odds = self.config.get("behaviour",
                               "reply_to_last_tweet_odds",
                               fallback="0")

        import asteval
        from asteval import Interpreter
        aeval = Interpreter()

        odds = aeval(odds)
        pp.pprint(odds)

        import random

        if random.random() < odds:
            print("replying to last tweet")
            # randomly reply to last tweet
            tweets = self.twitter.statuses.user_timeline(
                screen_name=self.username, count=1)
            last_tweet_id = tweets[0]["id"]
            self.twitter.statuses.update(status=text,
                                         in_reply_to_status_id=last_tweet_id)
        else:
            print("not replying to last tweet")
            self.twitter.statuses.update(status=text)
Exemplo n.º 12
0
 def setUp(self):
     self.interp = Interpreter()
     self.symtable = self.interp.symtable
     self.set_stdout()
     self.set_stderr()
     if not HAS_NUMPY:
         self.interp("arange = range")
Exemplo n.º 13
0
def recalculate(dataset):
    """Recalculate parameterized relationships within a dataset.

    Modifies values in place.

    Creates a ``TolerantParameterSet``, populates it with named parameters with a dataset, and then gets the evaluation order the graph of parameter relationships. After reevaluating all named parameters, creates an ``Interpreter`` with named parameters and all of numpy in its namespace. This interpreter is used to evaluate all other formulas in the dataset.

    Formulas that divide by zero are evaluated to zero.

    Returns the modified dataset."""
    interpreter = Interpreter()
    parameter_set = TolerantParameterSet(extract_named_parameters(dataset))
    for key, value in list(parameter_set.evaluate().items()):
        interpreter.symtable[key] = value

    for exc in iterate_all_parameters(dataset):
        if 'formula' in exc:
            try:
                exc['amount'] = interpreter(exc['formula'])
            except ZeroDivisionError:
                exc['amount'] = 0
        elif 'variable' in exc:
            exc['amount'] = interpreter.symtable[exc['variable']]
        else:
            raise ValueError

    # https://github.com/OcelotProject/Ocelot/issues/111
    for obj in iterate_all_uncertainties(dataset):
        if obj.get('uncertainty'):
            get_uncertainty_class(obj).repair(obj)

    return dataset
Exemplo n.º 14
0
def evaluate(eval_code, input_variables={}, output_variables=[]):
    """Evaluates a given expression, with the timeout given as decorator.

    Args:
        eval_code (str): The code to be evaluated.
        input_variables (dict): dictionary of input variables and their values.
        output_variables (array): array of names of output variables.

    Returns:
        dict: the output variables or empty.

    """
    # FIXME: use_numpy the process blocks infinitely at the return statement
    import time
    sym = make_symbol_table(time=time, use_numpy=False, range=range, **input_variables)
    aeval = Interpreter(
        symtable = sym,
        use_numpy = False,
        no_if = False,
        no_for = False,
        no_while = False,
        no_try = True,
        no_functiondef = True,
        no_ifexp = False,
        no_listcomp = True,
        no_augassign = False, # e.g., a += 1
        no_assert = True,
        no_delete = True,
        no_raise = True,
        no_print = False)
    aeval(eval_code)
    symtable = {x: sym[x] for x in sym if x in output_variables}
    return symtable
Exemplo n.º 15
0
 def eval_bscc_pfuncs(self, ):
     aeval = Interpreter()
     if config.models['use_old_model']:
         aeval.symtable['p'] = self.params
     else:
         aeval.symtable['r'] = self.params
     return [aeval.run(f) for f in self.bscc_ast_pfuncs]
Exemplo n.º 16
0
    def test_custom_symtable(self):
        "test making and using a custom symbol table"

        if HAS_NUMPY:

            def cosd(x):
                "cos with angle in degrees"
                return np.cos(np.radians(x))

            def sind(x):
                "sin with angle in degrees"
                return np.sin(np.radians(x))

            def tand(x):
                "tan with angle in degrees"
                return np.tan(np.radians(x))

            sym_table = make_symbol_table(cosd=cosd, sind=sind, tand=tand)

            aeval = Interpreter(symtable=sym_table)
            aeval("x1 = sind(30)")
            aeval("x2 = cosd(30)")
            aeval("x3 = tand(45)")

            x1 = aeval.symtable['x1']
            x2 = aeval.symtable['x2']
            x3 = aeval.symtable['x3']

            assert_allclose(x1, 0.50, rtol=0.001)
            assert_allclose(x2, 0.866025, rtol=0.001)
            assert_allclose(x3, 1.00, rtol=0.001)
Exemplo n.º 17
0
def test_eval():
    aeval = Interpreter()
    expr_str = "r[0]**4 * r[1]**2 +" * 10000 + '2*r[2]'
    with DeepRecursionCtx():
        expr = aeval.parse(expr_str)
        aeval.symtable['r'] = [1, 2, 3]
        res = aeval.eval(expr)
        assert not math.isnan(res)
Exemplo n.º 18
0
 async def clear(self, ctx):
     """Clear the state of your personal interpreter."""
     try:
         del self.interpreters[ctx.author.id]
     except KeyError:
         await ctx.message.add_reaction("👍")
     self.interpreters[ctx.author.id] = Interpreter(no_print=True)
     await ctx.message.add_reaction("👍")
Exemplo n.º 19
0
 async def calc(self, ctx, *, expression: str):
     """ Evaluates a math expression. """
     terp = Interpreter()
     result = terp.eval(expression)
     if result != '' and result is not None:
         await ctx.send(result)
     else:
         await ctx.send('Empty result.')
Exemplo n.º 20
0
def expr(expression, context=None):
    if context is None:
        context = dict()

    interpreter = Interpreter()
    for variable, value in context.items():
        interpreter.symtable[variable] = value

    return interpreter(expression)
Exemplo n.º 21
0
def do_conversion(in_val, expr):
    aeval = Interpreter()
    try:
        aeval.symtable['x'] = in_val
        aeval(expr)
        value = aeval.symtable['Value']
    except (KeyError, SyntaxError) as e:
        print(str(e))
        value = None
    return value
Exemplo n.º 22
0
    def evaluate(self):
        """A helper method to check if submitted answer for the provided question is correct or not.

        This helper function uses asteval to perform check
        """
        expression_interpreter = Interpreter()
        correct = SubmissionEvaluator.remove_exponent(
            Decimal("{:.3f}".format(expression_interpreter(
                self.expression))[:-1]))
        return str(correct) == self.submitted_result
Exemplo n.º 23
0
 def _objective_function(self, x):
     """
     Abstract Objective Function Call
     :param x:   Input Variable
     :return:    Function Value of Objective
     """
     aeval = Interpreter()
     exprc = aeval.parse(self.objective)
     aeval.symtable['x'] = x
     return aeval.run(exprc)
Exemplo n.º 24
0
    def __init__(self, usersyms=None):
        """
        Arguments
        ---------
        usersyms : dictionary of symbols to add to the
            :class:`asteval.Interpreter`.

        """

        super(Parameters, self).__init__(self)
        self._asteval = Interpreter(usersyms=usersyms)
Exemplo n.º 25
0
 def eval_pmc_pfuncs(self, ):
     aeval = Interpreter()
     if config.models['use_old_model']:
         aeval.symtable['p'] = self.params
     else:
         aeval.symtable['r'] = self.params
     for i in range(0, self.state_count):
         self.init_eval[i] = aeval.run(self.init_ast_pfuncs[i])
     for i in range(0, self.state_count):
         for j in range(0, self.state_count):
             self.trans_eval[i][j] = aeval.run(self.trans_ast_pfuncs[i][j])
Exemplo n.º 26
0
    def __init__(self,
                 first_pass_eq_name = None,
                 eq_name = None,
                 default_eq_name = 'aesthetics',
                 verbose = False,
                 ):
        """
        Basic search results re-ranking model. Allows you to specify a simple custom re-ranking equation.
        
        NOTE: See here for the restrictions / features of asteval: https://newville.github.io/asteval/basics.html
        
        Equation `eq_name` can use any features provided by asteval, in addition to numpy via `np`.
        
        Args:
            first_pass_eq_name:  (Optional) Equation run for first-pass, which can view the whole dataset.
            eq_name:             String name (TODO or python callable?) to use as the re-ranking equation. Operates per-item.
        
        TODO: Intentionally not supporting python callables for now. Strings only.
        """
        
        self.first_pass_eq_name = first_pass_eq_name
        
        if eq_name is None:
            ## Done this way, instead of default args on the function, so that mc_web can pass in `None` to indicate default:
            eq_name = default_eq_name

        self.eq_name = eq_name
            
        if verbose:
            print ('RERANK_EQUATION', first_pass_eq_name, eq_name)

        if eq_name in ranking_prebuilt_equations:
            eq = ranking_prebuilt_equations[eq_name]
        else:
            eq = eq_name
        
        self.eq = eq

        self.aeval = Interpreter()

        ## Access all functions of these modules:

        ## Turns out that asteval already imports all of `math`.

        for mod in []:
            for attr in dir(mod):
                if attr.startswith('_'):
                    continue
                self.aeval.symtable[attr] = getattr(math, attr)

        ## numpy:

        self.aeval.symtable['np'] = np
Exemplo n.º 27
0
    def recalculate_exchanges(self, group: str, global_params: dict = None) -> Iterable[Tuple[int, float]]:
        """ Constructs a list of exc.id/amount tuples for the
        ParameterizedExchanges in the given group.
        """
        params = self.initial.exc_by_group(group)
        if not params:
            return []

        glo = global_params or {}
        interpreter = Interpreter()
        interpreter.symtable.update(glo)
        return [(k, interpreter(v)) for k, v in params.items()]
Exemplo n.º 28
0
    def run(self, script,data):
        for k,v in self.stateData.items():
            data[k] = v
        data['stateData'] = self.stateData
        interpreter = Interpreter(data)
        interpreter.eval(script,0,True)
        for k,v in interpreter.symtable.items():
            if type(v) in [str,int,float,bool]:
                #print 'saving {} in stateData'.format(k)
                self.stateData[k] = v

        interpreter = None
Exemplo n.º 29
0
def button_equal():
    equation = e.get()
    e.delete(0, END)
    my_eval = Interpreter()

    messsage = my_eval(equation)
    if len(my_eval.error) > 0:
        for err in my_eval.error:
            e.insert(0, err.get_error())
        messsage = "Invalid operation"
    else:
        e.insert(0, messsage)
Exemplo n.º 30
0
    def get_interpreter(self, evaluate_first=True):
        """Get an instance of ``asteval.Interpreter`` that is prepopulated with global and local symbol names and values."""
        if evaluate_first:
            self.evaluate_and_set_amount_field()

        interpreter = Interpreter()
        for key, value in self.global_params.items():
            interpreter.symtable[key] = value
        for key, value in self.params.items():
            interpreter.symtable[key] = value['amount']

        return interpreter