Skip to content

Instantly share code, notes, and snippets.

@BoredHackerBlog
Created September 4, 2021 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BoredHackerBlog/7d42ac5dade9ec650dfe55b004dfc14a to your computer and use it in GitHub Desktop.
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
# 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)
name is john|name=="John"
age more than 30|age > 30
@BoredHackerBlog
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment