How can I create a rules engine without using eval() or exec()?

Posted by Angela on Stack Overflow See other posts from Stack Overflow or by Angela
Published on 2012-01-12T00:29:56Z Indexed on 2012/07/10 15:16 UTC
Read the original article Hit count: 134

Filed under:
|
|
|

I have a simple rules/conditions table in my database which is used to generate alerts for one of our systems. I want to create a rules engine or a domain specific language.

A simple rule stored in this table would be..(omitting the relationships here)

if temp > 40 send email

Please note there would be many more such rules. A script runs once daily to evaluate these rules and perform the necessary actions. At the beginning, there was only one rule, so we had the script in place to only support that rule. However we now need to make it more scalable to support different conditions/rules. I have looked into rules engines , but I hope to achieve this in some simple pythonic way. At the moment, I have only come up with eval/exec and I know that is not the most recommended approach. So, what would be the best way to accomplish this??

( The rules are stored as data in database so each object like "temperature", condition like ">/=..etc" , value like "40,50..etc" and action like "email, sms, etc.." are stored in the database, i retrieve this to form the condition...if temp > 50 send email, that was my idea to then use exec or eval on them to make it live code..but not sure if this is the right approach )

© Stack Overflow or respective owner

Related posts about python

Related posts about dictionary