def conditions_match(conditionlist,t): if isinstance(conditionlist, basestring): #smallest unit of work: non list. eval the expression. return eval(conditionlist) elif len(conditionlist) == 1: #list is a single item, visit its content. return conditions_match(conditionlist[0],t) else: #we know we have "SOMETHING AND/OR/NOT SOMETHINGS" as our input if conditionlist[0] is "NOT": return not conditions_match(conditionlist[1:],t) if conditionlist[1] is "AND": if conditionlist[2] is "NOT": return conditions_match(conditionlist[0],t) and not conditions_match(conditionlist[3:],t) else: return conditions_match(conditionlist[0],t) and conditions_match(conditionlist[2:],t) if conditionlist[1] is "OR": if conditionlist[2] is "NOT": return conditions_match(conditionlist[0],t) or not conditions_match(conditionlist[3:],t) else: return conditions_match(conditionlist[0],t) or conditions_match(conditionlist[2:],t) #here is an example of an expression in this grammar. The graphical interface will generate something in this format. #Data and rule are nonsense in this case just to show the format. conditionlist1=[["t['type']=='CAD'", "AND","t['amount']>50"],"OR",["t['type']=='USD'", "AND","NOT","t['amount']<=(100+(t['amount']/2))"]] #here are some example transactions that we are processing to see if they match user generated ruleslen() t1=[{'amount':45,'type' : "CAD"}, {'amount':95,'type' : "USD"}, {'amount':55,'type' : "CAD"}, {'amount':105,'type' : "USD"}] for t in t1: print conditions_match(conditionlist1,t)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question