0

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

3
  • possible duplicate of Alternative to eval in Python Commented Mar 11, 2015 at 8:17
  • @ChristianStrempfer i want to have multiple conditions, with and/or mixed, with brackets. that alternative you provided only operates on two parameters Commented Mar 11, 2015 at 11:07
  • The linked questions doesn't ask about two parameters, neither does the second answer. Feel free to put a bounty on it, if the answers aren't good enough. Commented Mar 11, 2015 at 11:30

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but one problem with it, i want to have multiple conditions, with and/or mixed, with brackets. how will i achieve that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.