Created
August 12, 2013 18:24
-
-
Save repustate/6213553 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process(node): | |
if isinstance(node, ast.Num): | |
return str(node.n) | |
elif isinstance(node, ast.Name): | |
return node.id | |
elif isinstance(node, ast.Str): | |
return r"\b%s\b" % node.s | |
elif isinstance(node, ast.Expr): | |
return process(node.value) | |
elif isinstance(node, ast.BinOp): | |
# It's a binary op so we recursively call the binary op methods. | |
if isinstance(node.op, ast.Mult): | |
return and_(process(node.left), process(node.right)) | |
elif isinstance(node.op, ast.Add): | |
return or_(process(node.left), process(node.right)) | |
elif isinstance(node, ast.UnaryOp): | |
# This is actually negations. | |
operand = node.operand | |
if isinstance(operand, ast.Name): | |
return negate(operand.id) | |
elif isinstance(operand, ast.Str): | |
return negate(operand.s) | |
elif isinstance(operand, ast.Num): | |
return negate(operand.n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment