In [1]:
# https://docs.python.org/2/library/ast.html
import ast

print(list(ast.walk(ast.parse('2 + 3'))))
print(list(ast.walk(ast.parse('2 == 3'))))


[<_ast.Module object at 0x7fb559f2b310>, <_ast.Expr object at 0x7fb559f2b390>, <_ast.BinOp object at 0x7fb559f2b3d0>, <_ast.Num object at 0x7fb559f2b410>, <_ast.Add object at 0x7fb5682cb5d0>, <_ast.Num object at 0x7fb559f2b450>]
[<_ast.Module object at 0x7fb559f2b350>, <_ast.Expr object at 0x7fb559f2b410>, <_ast.Compare object at 0x7fb559f2b450>, <_ast.Num object at 0x7fb559f2b490>, <_ast.Eq object at 0x7fb5682cbe50>, <_ast.Num object at 0x7fb559f2b4d0>]

In [11]:
def validate_number(expression):
    nodes = list(ast.walk(ast.parse(expression)))
    print(nodes)
    assert isinstance(nodes[1], ast.Expr)
    assert isinstance(nodes[2], ast.Compare)
    assert isinstance(nodes[2].ops[0], ast.Eq)
    assert isinstance(nodes[3], ast.Num)
    print(nodes[3].n)
    assert isinstance(nodes[4], ast.Eq)
    print(nodes[5].n)
    
validate_number('2 == 3')


[<_ast.Module object at 0x7fb5596b3d90>, <_ast.Expr object at 0x7fb5596b3e10>, <_ast.Compare object at 0x7fb5596b3e90>, <_ast.Num object at 0x7fb5596b3ed0>, <_ast.Eq object at 0x7fb5682cbe50>, <_ast.Num object at 0x7fb5596b3f10>]
2
3

In [ ]: