Open the context
Choose the project folder and name the files or behavior involved.
Evaluate a flat sequence of boolean values joined by AND and OR with AND taking precedence. Check True OR False AND False and reject empty input, misplaced operators, parentheses or non-boolean operands without executing strings.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
def evaluate_flat(tokens):
if not tokens or len(tokens) % 2 == 0:
raise ValueError("logic.shape")
if any(type(x) is not bool for x in tokens[::2]):
raise ValueError("logic.value")
if any(x not in ("AND", "OR") for x in tokens[1::2]):
raise ValueError("logic.operator")
terms, term = [], tokens[0]
for i in range(1, len(tokens), 2):
if tokens[i] == "AND":
term = term and tokens[i + 1]
else:
terms.append(term)
term = tokens[i + 1]
return any([*terms, term])The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.