Last active
July 21, 2021 17:15
-
-
Save belmarca/c4400e9ee5808353b75c5824fb0b9cc0 to your computer and use it in GitHub Desktop.
exploration for ober
This file contains 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
import ast | |
t = ast.parse(""" | |
def my_func(n): | |
return n | |
x = 0 | |
x += 1 | |
print("Hello, world!") | |
y = [1, 2, "abc", x] | |
z = b'\x11' | |
for i in range(len(y)): | |
print(i) | |
for i in range(100): | |
do_something() | |
""") | |
def ast_to_sexp(node, _ast=''): | |
if isinstance(node, ast.AST): | |
_ast += '(ast::' + node.__class__.__name__ + ' ' | |
_fields = node._fields | |
for field in _fields: | |
f = getattr(node, field) | |
_ast += '(' + field + ' ' | |
_ast += ast_to_sexp(f, '') + ')' | |
_ast += ')' | |
elif isinstance(node, list): | |
_ast += '(py::list' | |
for n in node: | |
_ast += ' ' + ast_to_sexp(n, '') | |
_ast += ')' | |
elif isinstance(node, tuple): | |
_ast += '(py::tuple' | |
for n in node: | |
_ast += ' ' + ast_to_sexp(n, '') | |
_ast += ')' | |
else: | |
return '"' + str(node).encode('unicode-escape').decode().replace('"', '\\"') + '"' | |
return _ast | |
# print(_ast) | |
s = ast_to_sexp(t) | |
print(s) | |
# print(ast.dump(t)) | |
# Module(body=[FunctionDef(name='my_func', args=arguments(args=[arg(arg='n', annotation=None)], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), body=[Return(value=Name(id='n', ctx=Load()))], decorator_list=[], returns=None), Assign(targets=[Name(id='x', ctx=Store())], value=Num(n=0)), AugAssign(target=Name(id='x', ctx=Store()), op=Add(), value=Num(n=1)), Expr(value=Call(func=Name(id='print', ctx=Load()), args=[Str(s='Hello, world!')], keywords=[])), Assign(targets=[Name(id='y', ctx=Store())], value=List(elts=[Num(n=1), Num(n=2), Str(s='abc'), Name(id='x', ctx=Load())], ctx=Load())), Assign(targets=[Name(id='z', ctx=Store())], value=Bytes(s=b'\x11')), For(target=Name(id='i', ctx=Store()), iter=Call(func=Name(id='range', ctx=Load()), args=[Call(func=Name(id='len', ctx=Load()), args=[Name(id='y', ctx=Load())], keywords=[])], keywords=[]), body=[Expr(value=Call(func=Name(id='print', ctx=Load()), args=[Name(id='i', ctx=Load())], keywords=[]))], orelse=[]), For(target=Name(id='i', ctx=Store()), iter=Call(func=Name(id='range', ctx=Load()), args=[Num(n=100)], keywords=[]), body=[Expr(value=Call(func=Name(id='do_something', ctx=Load()), args=[], keywords=[]))], orelse=[])]) |
This file contains 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
(ast::Module | |
(body (py::list | |
(ast::FunctionDef | |
(name "my_func") | |
(args (ast::arguments | |
(args (py::list (ast::arg (arg "n") (annotation "None")))) | |
(vararg "None") | |
(kwonlyargs (py::list)) | |
(kw_defaults (py::list)) | |
(kwarg "None") | |
(defaults (py::list)))) | |
(body (py::list | |
(ast::Return (value (ast::Name (id "n") (ctx (ast::Load))))))) | |
(decorator_list (py::list)) | |
(returns "None")) | |
(ast::Assign | |
(targets (py::list (ast::Name (id "x") (ctx (ast::Store))))) | |
(value (ast::Num (n "0")))) | |
(ast::AugAssign | |
(target (ast::Name (id "x") (ctx (ast::Store)))) | |
(op (ast::Add)) | |
(value (ast::Num (n "1")))) | |
(ast::Expr | |
(value (ast::Call | |
(func (ast::Name (id "print") (ctx (ast::Load)))) | |
(args (py::list (ast::Str (s "Hello, world!")))) | |
(keywords (py::list))))) | |
(ast::Assign | |
(targets (py::list (ast::Name (id "y") (ctx (ast::Store))))) | |
(value (ast::List | |
(elts (py::list | |
(ast::Num (n "1")) | |
(ast::Num (n "2")) | |
(ast::Str (s "abc")) | |
(ast::Name (id "x") (ctx (ast::Load))))) | |
(ctx (ast::Load))))) | |
(ast::Assign | |
(targets (py::list (ast::Name (id "z") (ctx (ast::Store))))) | |
(value (ast::Bytes (s "b'\\x11'")))) | |
(ast::For | |
(target (ast::Name (id "i") (ctx (ast::Store)))) | |
(iter (ast::Call | |
(func (ast::Name (id "range") (ctx (ast::Load)))) | |
(args (py::list | |
(ast::Call | |
(func (ast::Name (id "len") (ctx (ast::Load)))) | |
(args (py::list | |
(ast::Name (id "y") (ctx (ast::Load))))) | |
(keywords (py::list))))) | |
(keywords (py::list)))) | |
(body (py::list | |
(ast::Expr | |
(value (ast::Call | |
(func (ast::Name (id "print") (ctx (ast::Load)))) | |
(args (py::list | |
(ast::Name (id "i") (ctx (ast::Load))))) | |
(keywords (py::list))))))) | |
(orelse (py::list))) | |
(ast::For | |
(target (ast::Name (id "i") (ctx (ast::Store)))) | |
(iter (ast::Call | |
(func (ast::Name (id "range") (ctx (ast::Load)))) | |
(args (py::list (ast::Num (n "100")))) | |
(keywords (py::list)))) | |
(body (py::list | |
(ast::Expr | |
(value (ast::Call | |
(func (ast::Name | |
(id "do_something") | |
(ctx (ast::Load)))) | |
(args (py::list)) | |
(keywords (py::list))))))) | |
(orelse (py::list)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment