igauravsehrawat
igauravsehrawat

Reputation: 3954

asking for alternate method apart from eval in python

Problem i am solving: i am giving liberty to user to make conditions and actions for making rules while inserting data into a database and evaluate these conditions and action, i could not think anything else from using eval , an example of datastructure i created for such purpose is

action_var = ""
a_hash = {"condition":a_condition,
          "action":a_hash}
a_condition ={"param":"abc",
              "operator":">",
              "value":"cde"}
a_action = {"param":action_var,
            "operation":"=",
            "value":"action

So my plan is to take condition id from user and action id from user and then use eval to evaluate the expression .

Help: Am i going in right direction, is there alternate method to do this ?

P.S: I can't use triggers on database, I am kind of using orm wrapper for lmdb. So i use write command at base level.

Edit: i want to have multiple conditions, with and/or mixed, with brackets.

Thanks

Upvotes: 0

Views: 61

Answers (1)

Dan D.
Dan D.

Reputation: 74655

You don't need to use eval. Map those operators to functions and then apply the functions to the arguments. Consider:

>>> import operator
>>> operators = {}
>>> operators['>'] = operator.gt
>>> operators['>'](*[1, 2])
False
>>> 1 > 2
False

Upvotes: 2

Related Questions