def test_jack_and_jill(self): p = Predicate("name is 'Jack'\r\nand friend is 'Jill'") assert p.is_valid() assert p.evaluate({"name": "Jack", "friend": "Jill"}) res, ctx = p.analyze({"name": "Jack", "friend": "Jill"}) assert res assert p.description() == """AND operator at line: 2, col 1
def main(): import sys if len(sys.argv) == 2: p = Predicate(sys.argv[1]) if not p.is_valid(): for error in p.errors()['errors']: print(error) return else: p = None rows = [] for id, size, date, sender, msg, dest in mailq(): colsObject = { "cols": [{ "value": id }, { "value": size }, { "value": date }, { "value": sender }, { "value": dest }] } rows.append(colsObject) #doc = list(id, size, date, sender, msg, dest) if p == None or p.evaluate(doc): rowsObject = {"rows": rows} hrowsColsObject = { "cols": [{ "value": "ID" }, { "value": "Size" }, { "value": "Date" }, { "value": "Sender" }, { "value": "Recipient" }] } hrowsColsArray = [] hrowsColsArray.append(hrowsColsObject) hrowsObject = {"hrows": hrowsColsArray} returnString = json.dumps(hrowsObject)[1:-1] + ", " + json.dumps( rowsObject)[1:-1] print(returnString)
class TriggerCondition(object): """Represent a trigger condition""" def __init__(self, predicate): from pypred import Predicate self.predicate = Predicate(predicate) if not self.predicate.is_valid(): raise ValueError('Predicate is invalid: ' + str(predicate)) def satisfied_by(self, execution_context): """test the predicate against the execution context""" return self.predicate.evaluate(execution_context)
def test_jack_and_jill(self): p = Predicate("name is 'Jack' and friend is 'Jill'") assert p.is_valid() assert p.evaluate({"name": "Jack", "friend": "Jill"}) res, ctx = p.analyze({"name": "Jack", "friend": "Jill"}) assert res assert ( p.description() == """AND operator at line: 1, col 15 IS comparison at line: 1, col 5 Literal name at line: 1, col 0 Literal 'Jack' at line: 1, col 8 IS comparison at line: 1, col 26 Literal friend at line: 1, col 19 Literal 'Jill' at line: 1, col 29 """ )
def check_condition(self, tracker, condition): if condition is None: return True if isinstance(condition, str): slots = dict() for slotName, slotValue in tracker.slots.items(): slots[slotName] = slotValue.value predicate = Predicate(condition) if predicate.is_valid() is not True: return False result = predicate.evaluate(slots) return result props = condition.get("properties", {}) children = condition.get("children1", {}).values() if condition.get("type") == "rule": return self.check_atomic_condition(tracker, **props) conjunction_operator = any if props.get("conjunction") == "OR" else all polarity = (lambda p: not p) if props.get("not") else (lambda p: p) return polarity( conjunction_operator( self.check_condition(tracker, child) for child in children))