Created
September 4, 2021 19:19
-
-
Save BoredHackerBlog/7d42ac5dade9ec650dfe55b004dfc14a to your computer and use it in GitHub Desktop.
dict rule match - this code essentially takes in a bunch of rules and compares them against an event (dict) and prints if something matches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requires dictquery (pip3 install dictquery or get it from here: https://github.com/cyberlis/dictquery) | |
import dictquery as dq | |
#each rule needs to be a new line | |
#rule format, RULENAME|RULE | |
#rule syntax: https://github.com/cyberlis/dictquery#dictquery | |
rules_file = "rules.txt" | |
rules = {} | |
def loadrules(rules_file): | |
global rules | |
with open(rules_file) as f: | |
for line in f: | |
rule = line.strip().split("|",1) | |
rules[rule[0]] = dq.compile(rule[1]) #this may need some modification, check dictquery docs | |
def checkevent(event): | |
global rules | |
for rule in rules: | |
if rules[rule].match(event): | |
alert(rule,event) | |
def alert(rule, event): | |
match = {"rule": rule, "event":event} | |
print(match) | |
loadrules(rules_file) | |
event = {"name":"John", "age":35} | |
checkevent(event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name is john|name=="John" | |
age more than 30|age > 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dictquery alternative https://github.com/zeroSteiner/rule-engine