def has_parentheses(s): start = s.find("(") if start == -1: raise ex.MyException("Cannot find parentheses") for i, ch in enumerate(s): if ch == '&' or ch == '|': if s[i - 1] != ")" or s[i + 1] != "(": raise ex.MyException("Missing parentheses near %s" % s[i:i + 2])
def is_balance_parentheses(s): my_stack = [] for ch in s: if ch == '(': my_stack.append(ch) elif ch == ')': if len(my_stack) == 0: raise ex.MyException("Parentheses are not balanced") my_stack.pop() if len(my_stack) != 0: raise ex.MyException("Parentheses are not balanced")
def is_valid_window_size(window_size, rule_list): if window_size == -1: return for rule in rule_list: i = 0 for j, ch in enumerate(rule): if ch == '+' or ch == '-': if j - i > int(window_size): raise ex.MyException( "Window size should be larger than all of your searched words" ) i = j + 1 if len(rule[i:]) > int(window_size): raise ex.MyException( "Window size should be larger than all of your searched words")
def is_valid_operator(s): start = 0 while True: start = s.find(')', start) if start == -1: break end = s.find('(', start) if end == -1: break if s[start + 1:end] != "&" and s[start + 1:end] != "|": raise ex.MyException("Operator should be & or |, cannot be %s" % s[start + 1:end]) start = end
def is_positive_window_size(size): if int(size) < 1: raise ex.MyException("Window size should be a positive integer")
def input_is_not_empty(key_words): if len(key_words) == 0: raise ex.MyException("Input cannot be empty")