In [1]:
import showast

In [2]:
%%showast
print 1 + 2


%3 0 Print 1 BinOp 0--1 7 True 0--7 2 Num 1--2 4 Add 1--4 5 Num 1--5 3 1 2--3 6 2 5--6

In [3]:
%%showast
a = 1
b = a + 2
b /= 2.


%3 0 Module 1 Assign 0--1 7 Assign 0--7 18 AugAssign 0--18 2 Name 1--2 5 Num 1--5 3 "a" 2--3 4 Store 2--4 6 1 5--6 8 Name 7--8 11 BinOp 7--11 9 "b" 8--9 10 Store 8--10 12 Name 11--12 15 Add 11--15 16 Num 11--16 13 "a" 12--13 14 Load 12--14 17 2 16--17 19 Name 18--19 22 Div 18--22 23 Num 18--23 20 "b" 19--20 21 Store 19--21 24 2.0 23--24

In [4]:
%%showast
x.y.z().t


%3 0 Expr 1 Attribute 0--1 2 Call 1--2 12 "t" 1--12 13 Load 1--13 3 Attribute 2--3 4 Attribute 3--4 10 "z" 3--10 11 Load 3--11 5 Name 4--5 8 "y" 4--8 9 Load 4--9 6 "x" 5--6 7 Load 5--7

In [5]:
%%showast
def abc():
    for x in 'abc':
        print x
    return 0


%3 0 FunctionDef 1 "abc" 0--1 2 arguments 0--2 3 For 0--3 14 Return 0--14 4 Name 3--4 7 Str 3--7 9 Print 3--9 5 "x" 4--5 6 Store 4--6 8 "abc" 7--8 10 Name 9--10 13 True 9--13 11 "x" 10--11 12 Load 10--12 15 Num 14--15 16 0 15--16

In [6]:
%%showast
a, b = [x for x in y if x.z]


%3 0 Assign 1 Tuple 0--1 9 ListComp 0--9 2 Name 1--2 5 Name 1--5 8 Store 1--8 3 "a" 2--3 4 Store 2--4 6 "b" 5--6 7 Store 5--7 10 Name 9--10 13 comprehension 9--13 11 "x" 10--11 12 Load 10--12 14 Name 13--14 17 Name 13--17 20 Attribute 13--20 15 "x" 14--15 16 Store 14--16 18 "y" 17--18 19 Load 17--19 21 Name 20--21 24 "z" 20--24 25 Load 20--25 22 "x" 21--22 23 Load 21--23

In [7]:
%%showast
@decorator()
def decorated():
    import time
    yield time.time()


%3 0 FunctionDef 1 "decorated" 0--1 2 arguments 0--2 3 Import 0--3 6 Expr 0--6 15 Call 0--15 4 alias 3--4 5 "time" 4--5 7 Yield 6--7 8 Call 7--8 9 Attribute 8--9 10 Name 9--10 13 "time" 9--13 14 Load 9--14 11 "time" 10--11 12 Load 10--12 16 Name 15--16 17 "decorator" 16--17 18 Load 16--18

In [8]:
# showast can also be used to visualize inspectable objects

from showast import show_source

import antigravity
show_source(antigravity)


%3 0 Module 1 Import 0--1 4 Expr 0--4 2 alias 1--2 3 "webbrowser" 2--3 5 Call 4--5 6 Attribute 5--6 12 Str 5--12 7 Name 6--7 10 "open" 6--10 11 Load 6--11 8 "webbrowser" 7--8 9 Load 7--9 13 "http://xkcd.com/353/" 12--13

In [9]:
import random
show_source(random.choice)


%3 0 FunctionDef 1 "choice" 0--1 2 arguments 0--2 9 Return 0--9 3 Name 2--3 6 Name 2--6 4 "self" 3--4 5 Param 3--5 7 "seq" 6--7 8 Param 6--8 10 Subscript 9--10 11 Name 10--11 14 Index 10--14 35 Load 10--35 12 "seq" 11--12 13 Load 11--13 15 Call 14--15 16 Name 15--16 19 BinOp 15--19 17 "int" 16--17 18 Load 16--18 20 Call 19--20 27 Mult 19--27 28 Call 19--28 21 Attribute 20--21 22 Name 21--22 25 "random" 21--25 26 Load 21--26 23 "self" 22--23 24 Load 22--24 29 Name 28--29 32 Name 28--32 30 "len" 29--30 31 Load 29--31 33 "seq" 32--33 34 Load 32--34

In [10]:
import contextlib
show_source(contextlib.closing.__enter__)
show_source(contextlib.closing.__exit__)


%3 0 FunctionDef 1 "__enter__" 0--1 2 arguments 0--2 6 Return 0--6 3 Name 2--3 4 "self" 3--4 5 Param 3--5 7 Attribute 6--7 8 Name 7--8 11 "thing" 7--11 12 Load 7--12 9 "self" 8--9 10 Load 8--10
%3 0 FunctionDef 1 "__exit__" 0--1 2 arguments 0--2 7 Expr 0--7 3 Name 2--3 6 "exc_info" 2--6 4 "self" 3--4 5 Param 3--5 8 Call 7--8 9 Attribute 8--9 10 Attribute 9--10 16 "close" 9--16 17 Load 9--17 11 Name 10--11 14 "thing" 10--14 15 Load 10--15 12 "self" 11--12 13 Load 11--13

In [11]:
# styling options can also be set

from showast import Settings
Settings['font'] = 'Century'
Settings['shape'] = 'oval'
Settings['nonterminal_color'] = '#7733AA'

In [12]:
%%showast
def add(x, y):
    return x + y


%3 0 FunctionDef 1 "add" 0--1 2 arguments 0--2 9 Return 0--9 3 Name 2--3 6 Name 2--6 4 "x" 3--4 5 Param 3--5 7 "y" 6--7 8 Param 6--8 10 BinOp 9--10 11 Name 10--11 14 Add 10--14 15 Name 10--15 12 "x" 11--12 13 Load 11--13 16 "y" 15--16 17 Load 15--17

In [13]:
# nltk and pillow can be used as an alternate rendering engine

from showast import Renderers
Settings['renderer'] = Renderers.nltk

# reset styling:
Settings['font'] = 'courier'
Settings['shape'] = 'none'
Settings['nonterminal_color'] = '#004080'

In [14]:
%%showast
while b != 0:
    if a > b:
        a = a - b
    else:
        b = b - a
print a