In [1]:
import tokenize
from StringIO import StringIO
In [2]:
a = "x = 3"
b = "y = x + 52.817"
hw = "print 'Hello, World!'"
In [3]:
def print_token(token_type, token_str, (srow, scol), (erow, ecol), line):
if srow == erow:
print "{}: {}, {}, {}".format(srow, tokenize.tok_name[token_type], repr(token_str), line)
else:
print "{}-{}: {}, {}, {}".format(srow, erow, tokenize.tok_name[token_type], repr(token_str), repr(line))
def print_token_verbose(token_type, token_str, (srow, scol), (erow, ecol), line):
print "({},{})--({},{}), {}, {}, {}".format(
srow, scol, erow, ecol, tokenize.tok_name[token_type], repr(token_str), repr(line))
def display_tokens(string, verbose=False):
s = StringIO(string).readline
f = print_token
if verbose:
f = print_token_verbose
tokenize.tokenize(s, f)
In [4]:
display_tokens(a, verbose=True)
In [5]:
display_tokens(b, verbose=True)
In [6]:
display_tokens(hw, verbose=True)
In [7]:
longer="""a = 3
b = a**4
b - a == 78"""
display_tokens(longer, verbose=True)
In [8]:
class_ex="""class Foo(object):
def __init__(self, x):
self.x = x
f = Foo(3)
print f.x"""
display_tokens(class_ex, verbose=True)
In [9]:
import ast
import copy
In [10]:
ast.dump(ast.parse(a))
Out[10]:
In [11]:
exec(compile(ast.parse(hw, mode='exec'), '', 'exec'))
In [12]:
ast.dump(ast.parse(a), include_attributes=True)
Out[12]:
In [13]:
print ast.dump(ast.parse(a))
In [14]:
# http://05d2db1380b6504cc981-8cbed8cf7e3a131cd8f1c3e383d10041.r93.cf2.rackcdn.com/pycon-us-2011/419_what-would-you-do-with-an-ast.mp4
use_x = ast.Name(id="x", ctx=ast.Load())
assign_x = ast.Name(id="x", ctx=ast.Store())
one = ast.Num(n=1)
two = ast.Num(n=2)
four = ast.Num(n=4)
In [15]:
assign_2 = ast.Assign(targets=[copy.copy(assign_x)], value=copy.copy(two))
assign_2.lineno = 1
assign_2.col_offset = 0
ast.fix_missing_locations(assign_2) # <-- Applies lineno and col_offset to nodes in subtree (wrong, but ok for now)
Out[15]:
In [16]:
if_ = ast.If()
if_.test = ast.Compare()
if_.test.left = copy.copy(use_x)
if_.test.ops = [ast.Eq()]
if_.test.comparators = [copy.copy(four)]
In [17]:
if_.body = [ast.Print(dest=None, values=[copy.copy(use_x)], nl=True)]
In [18]:
x_plus_1 = ast.BinOp(left=copy.copy(use_x), op=ast.Add(), right=copy.copy(one))
assign = ast.Assign(targets=[copy.copy(assign_x)], value=x_plus_1)
if_.orelse = [assign]
In [19]:
if_.lineno = 2
if_.col_offset = 0
ast.fix_missing_locations(if_)
Out[19]:
In [20]:
print_x = ast.Print(dest=None, values=[copy.copy(use_x)], nl=True)
In [21]:
print_x.lineno = 3
print_x.col_offset = 0
ast.fix_missing_locations(print_x)
Out[21]:
In [22]:
mod = ast.Module(body=[assign_2, if_, print_x])
In [23]:
code = compile(mod, '', 'exec')
In [24]:
exec code
print
print (ast.dump(mod, include_attributes=True))
print
In [25]:
fcn_str = """def f(a, b=3, c=None):
print a
print "b={}, c={}".format(b,c)"""
In [26]:
fcn_parsed = ast.parse(fcn_str)
print ast.dump(fcn_parsed)
In [27]:
for arg in fcn_parsed.body[0].args.args:
print arg.id
In [28]:
ast.dump(fcn_parsed.body[0].args)
Out[28]:
In [29]:
for x in fcn_parsed.body[0].args.defaults:
if isinstance(x, ast.Name):
print x.id
elif isinstance(x, ast.Num):
print x.n
else:
print x
In [30]:
call = "foo(a, b, x=3, y='meh')"
ast.dump(ast.parse(call))
Out[30]:
In [31]:
ast.dump(ast.parse("foo.bar(a,b,x=y)"))
Out[31]:
In [32]:
ast.dump(ast.parse("""
import foo
foo.bar(a,b,x=y)"""))
Out[32]:
In [36]:
ast.dump(ast.parse("""kw = {"b": 3}
foo(2, [3,4,"bah"], **kw)"""))
Out[36]:
In [69]:
x = ast.parse("foo(2, 3, a=6)")
print ast.dump(x.body[0].value)
x.body[0].value.func.id
Out[69]:
In [38]:
with open("../pip-create/setup.py") as f:
setup = f.read()
In [93]:
functions = {}
def grab_crap(x):
if isinstance(x, ast.Str):
return x.s
elif isinstance(x, ast.Name):
return x.id
elif isinstance(x, ast.List):
return [grab_crap(y) for y in x.elts]
elif isinstance(x, ast.Dict):
return {grab_crap(k): grab_crap(v) for k,v in zip(x.keys, x.values)}
else:
return x
class FunctionGrabber(ast.NodeVisitor):
def visit_Call(self, node):
if isinstance(node.func, ast.Name) and node.func.id == "setup":
#print ast.dump(node)
gah = {kw.arg: grab_crap(kw.value) for kw in node.keywords}
print gah
In [94]:
FunctionGrabber().visit(ast.parse(setup))
In [72]:
functions
Out[72]:
In [89]:
zip(["a", "b"], [1, 2])
Out[89]:
In [ ]: