[pyar] PyAr tiene plata, ¡gastémosla!

Juanjo Conti jjconti en gmail.com
Lun Feb 7 13:19:35 ART 2011


2011/2/7 Cesar Ballardini <cesar en ballardini.com.ar>:
>
> PD: obpython: ¿cómo hago para acceder al parse tree de una sentencia o
> expresión Python que tengo en un string?


>>> s = "a + 1"
>>> import ast
>>> dir(ast)
['AST', 'Add', 'And', 'Assert', 'Assign', 'Attribute', 'AugAssign',
'AugLoad', 'AugStore', 'BinOp', 'BitAnd', 'BitOr', 'BitXor', 'BoolOp',
'Break', 'Call', 'ClassDef', 'Compare', 'Continue', 'Del', 'Delete',
'Dict', 'Div', 'Ellipsis', 'Eq', 'ExceptHandler', 'Exec', 'Expr',
'Expression', 'ExtSlice', 'FloorDiv', 'For', 'FunctionDef',
'GeneratorExp', 'Global', 'Gt', 'GtE', 'If', 'IfExp', 'Import',
'ImportFrom', 'In', 'Index', 'Interactive', 'Invert', 'Is', 'IsNot',
'LShift', 'Lambda', 'List', 'ListComp', 'Load', 'Lt', 'LtE', 'Mod',
'Module', 'Mult', 'Name', 'NodeTransformer', 'NodeVisitor', 'Not',
'NotEq', 'NotIn', 'Num', 'Or', 'Param', 'Pass', 'Pow', 'Print',
'PyCF_ONLY_AST', 'RShift', 'Raise', 'Repr', 'Return', 'Slice',
'Store', 'Str', 'Sub', 'Subscript', 'Suite', 'TryExcept',
'TryFinally', 'Tuple', 'UAdd', 'USub', 'UnaryOp', 'While', 'With',
'Yield', '__builtins__', '__doc__', '__file__', '__name__',
'__package__', '__version__', 'alias', 'arguments', 'boolop', 'cmpop',
'comprehension', 'copy_location', 'dump', 'excepthandler', 'expr',
'expr_context', 'fix_missing_locations', 'get_docstring',
'increment_lineno', 'iter_child_nodes', 'iter_fields', 'keyword',
'literal_eval', 'mod', 'operator', 'parse', 'slice', 'stmt',
'unaryop', 'walk']
>>> ast.parse(s)
<_ast.Module object at 0x7f3571ed0110>
>>> a = ast.parse(s)
>>> dir(a)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__',
'_attributes', '_fields', 'body']
>>> a.body
[<_ast.Expr object at 0x7f3571edc9d0>]
>>> dir(a.body[0])
['__class__', '__delattr__', '__dict__', '__doc__', '__format__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__',
'_attributes', '_fields', 'col_offset', 'lineno', 'value']
>>> a.body[0].value
<_ast.BinOp object at 0x7f3571edca10>
>>> dir(a.body[0].value)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__',
'_attributes', '_fields', 'col_offset', 'left', 'lineno', 'op',
'right']
>>> a.body[0].value.op
<_ast.Add object at 0x7f3571ed7850>
>>> a.body[0].value.left
<_ast.Name object at 0x7f3571edca50>
>>> a.body[0].value.right
<_ast.Num object at 0x7f3571edca90>

-- 
Juanjo Conti
blog: http://www.juanjoconti.com.ar



More information about the pyar mailing list