Beispiel #1
0
 def __init__(self, operators=None, functions=None, names=None):
     if operators is None:
         operators = DEFAULT_OPERATORS.copy()
     if functions is None:
         functions = DEFAULT_FUNCTIONS.copy()
     if names is None:
         names = DEFAULT_NAMES.copy()
     super(MathEvaluator, self).__init__(operators, functions, names)
    def __init__(self, ctx, operators=None, functions=None, names=None):
        if operators is None:
            operators = DEFAULT_OPERATORS.copy()
        if functions is None:
            functions = DEFAULT_FUNCTIONS.copy()
        if names is None:
            names = DEFAULT_NAMES.copy()
        super(ScriptingEvaluator, self).__init__(operators, functions, names)

        self.nodes.update({
            ast.JoinedStr: self._eval_joinedstr,  # f-string
            ast.FormattedValue: self._eval_formattedvalue,  # things in f-strings
            ast.ListComp: self._eval_listcomp,
            ast.SetComp: self._eval_setcomp,
            ast.DictComp: self._eval_dictcomp,
            ast.comprehension: self._eval_comprehension
        })

        self.functions.update(  # character-only functions
            get_cc=self.needs_char, set_cc=self.needs_char, get_cc_max=self.needs_char,
            get_cc_min=self.needs_char, mod_cc=self.needs_char,
            cc_exists=self.needs_char, create_cc_nx=self.needs_char,
            get_slots=self.needs_char, get_slots_max=self.needs_char, set_slots=self.needs_char,
            use_slot=self.needs_char,
            get_hp=self.needs_char, set_hp=self.needs_char, mod_hp=self.needs_char, hp_str=self.needs_char,
            get_temphp=self.needs_char, set_temphp=self.needs_char,
            set_cvar=self.needs_char, delete_cvar=self.needs_char, set_cvar_nx=self.needs_char,
            get_raw=self.needs_char
        )

        self.functions.update(
            set=self.set_value, exists=self.exists, combat=self.combat,
            get_gvar=self.get_gvar,
            set_uvar=self.set_uvar, delete_uvar=self.delete_uvar, set_uvar_nx=self.set_uvar_nx,
            uvar_exists=self.uvar_exists,
            chanid=self.chanid, servid=self.servid,
            get=self.get
        )

        self.assign_nodes = {
            ast.Name: self._assign_name,
            ast.Tuple: self._assign_tuple,
            ast.Subscript: self._assign_subscript
        }

        self._loops = 0
        self._cache = {
            "gvars": {},
            "uvars": {}
        }

        self.ctx = ctx
        self.character_changed = False
        self.combat_changed = False
        self.uvars_changed = set()
Beispiel #3
0
def eval_expression(s: str, names: Dict = None) -> str:
    """
    Customizes the functions and names available in
    expression and evaluates an expression.
    """
    functions = DEFAULT_FUNCTIONS.copy()
    functions.update(unixtime=_unixtime,
                     datetime_format=_datetime_format,
                     starts_with=_starts_with,
                     ends_with=_ends_with,
                     start=_start,
                     end=_end,
                     timedelta_days=_timedelta_days,
                     timedelta_hours=_timedelta_hours,
                     timedelta_minutes=_timedelta_minutes,
                     timedelta_seconds=_timedelta_seconds)

    names_combined = DEFAULT_NAMES.copy()
    names_combined.update(now=datetime.datetime.now())

    if names:
        names_combined.update(names)

    return simple_eval(s, functions=functions, names=names_combined)