Return to index
Generate Python source from AbstractSyntaxTrees generated by the compiler.
Walk with me to generate source:
>>> source = "1+2" >>> ast = parse(source) >>> print walk(ast, SourceWriter()) 1 + 2 <BLANKLINE>
Normally this class is not used directly, use the fileToString, nodeToString, or sourceToString methods instead.
Back to astwriter.SourceWriter
Called if a node handler is not found.
Back to astwriter.SourceWriter
I act like string.join(seq) except seq is a list of nodes, and the results are written to the internal buffer and not returned by this function.
Back to astwriter.SourceWriter
Binary addition, i.e. "+".
>>> print sourceToString("1+2") 1 + 2 <BLANKLINE>
Back to astwriter.SourceWriter
Called for logical and operations. Note that a series of and's will result in only one to this method.
>>> print sourceToString("1 and 2 and 3") 1 and 2 and 3 <BLANKLINE>
Back to astwriter.SourceWriter
Assign expression to an attribute.
>>> print sourceToString("test.a=5") test.a = 5 <BLANKLINE>
Back to astwriter.SourceWriter
Assign expression to a list.
>>> print sourceToString("[a,b,c] = [1,2,3]") [a, b, c] = [1, 2, 3] <BLANKLINE>
Back to astwriter.SourceWriter
Assign expression to a variable.
>>> print sourceToString("x,y,z = 1, str(2), False") (x, y, z) = (1, str(2), False) <BLANKLINE>
Back to astwriter.SourceWriter
Assign expression to a tuple.
>>> print sourceToString("x, (a,b) ,z = 1, (str(2),str(3)), False") (x, (a, b), z) = (1, (str(2), str(3)), False) <BLANKLINE>
Back to astwriter.SourceWriter
The assert statement.
>>> print sourceToString("assert True == False, 'Oops'") assert (True == False, 'Oops') <BLANKLINE>
Back to astwriter.SourceWriter
Called for each assignment. Note that a statement like a = b = None will result in one call to this method.
>>> print sourceToString("x,y,z = 1, str(2), False") (x, y, z) = (1, str(2), False) <BLANKLINE>
>>> print sourceToString("x,y,z = l = 1, str(2), False") (x, y, z) = l = (1, str(2), False) <BLANKLINE>
Back to astwriter.SourceWriter
Called for operations like x += 5 and x -= 1.
>>> print sourceToString("x += 5") x += 5 <BLANKLINE>
Back to astwriter.SourceWriter
Called when the backquote character is used.
>>> print sourceToString("`a`") :wiki:`a`
Back to astwriter.SourceWriter
Called for bitwise 'and' operations. Note that a series of "&"'s will result in only one call to this method.
>>> print sourceToString("1&2&3") 1 & 2 & 3 <BLANKLINE>
Back to astwriter.SourceWriter
Called for bitwise 'or' operations. Note that a series of "|"'s will result in only one call to this method.
>>> print sourceToString("1|2|3") 1 | 2 | 3 <BLANKLINE>
Back to astwriter.SourceWriter
Called for bitwise 'xor' operations. Note that a series of "^"'s will result in only one call to this method.
>>> print sourceToString("1^2^3") 1 ^ 2 ^ 3
Back to astwriter.SourceWriter
Called for a break statement.
>>> print sourceToString("if True: break") if True: break <BLANKLINE>
Back to astwriter.SourceWriter
Called for a function call.
>>> print sourceToString("x,y,z = 1, str(2), False") (x, y, z) = (1, str(2), False) <BLANKLINE>
Back to astwriter.SourceWriter
Called for a class definition.
>>> source = ''' ... class Test(Base1, Base2): ... """ Documentation string ... This is a test. """ ... pass ... ''' >>> >>> print sourceToString(source) class Test(Base1, Base2): """ Documentation string This is a test. """ <BLANKLINE> pass <BLANKLINE>
>>> source = ''' ... class Test: ... """ Documentation string ... This is a test. """ ... pass ... ''' >>> >>> print sourceToString(source) class Test: """ Documentation string This is a test. """ <BLANKLINE> pass <BLANKLINE>
Back to astwriter.SourceWriter
Called for each comparison operation.
>>> print sourceToString("a > b or c <= d") a > b or c <= d
Back to astwriter.SourceWriter
Called for constants.
>>> print sourceToString("1 + 2; 'A string constant'") 1 + 2 'A string constant' <BLANKLINE>
Back to astwriter.SourceWriter
Called for the continue statement.
>>> print sourceToString("if True: continue") if True: continue
Back to astwriter.SourceWriter
Called for function decorators. Note if multiple decorators are applied to a function, this method is called only once.
>>> source = ''' ... @deco ... @deco(1,2,3) ... def test(): pass ... ''' >>> print sourceToString(source) @deco @deco(1, 2, 3) def test(): pass <BLANKLINE>
Back to astwriter.SourceWriter
Called for a literal dictionary definition (i.e. { a : b, c : d })
>>> print sourceToString("{a:5,'t':test}") { a : 5, 't' : test }
Back to astwriter.SourceWriter
This is called when the results of an expression are discarded.
>>> print sourceToString("a") a
Back to astwriter.SourceWriter
Called for each division operation.
>>> print sourceToString("1/2") 1 / 2
Back to astwriter.SourceWriter
I don't know why I would want it but...
>>> print sourceToString("a[...]") a[...]
Back to astwriter.SourceWriter
Called for the exec statement.
>>> print sourceToString("exec 'a=1'in {}, {}") exec 'a=1' in { } , { } >>> print sourceToString("exec 'a=1'in {}") exec 'a=1' in { }
Back to astwriter.SourceWriter
Called for floor division.
>>> print sourceToString("1//2") 1 // 2
Back to astwriter.SourceWriter
Called for a for statement.
>>> print sourceToString(''' ... for x in :changeset:`1 [1]`: ... print x ... else: ... print "else" ... ''') for x in :changeset:`1 [1]`: print x else: print 'else' <BLANKLINE>
Back to astwriter.SourceWriter
Called for a from (import) statement.
>>> print sourceToString(''' ... from module import some as thing, a as b ... ''') from module import some as thing, a as b
Back to astwriter.SourceWriter
Called for each function definition.
>>> print sourceToString(''' ... def test(a,b=1, *a, **k): ... """ This is my docstring. ... Some more documentation.""" ... pass ... ''') def test(a, b=1, *a, **k): <BLANKLINE> """ This is my docstring. Some more documentation. """ pass <BLANKLINE>
Back to astwriter.SourceWriter
Called for attribute access.
>>> print sourceToString("a = test.a") a = test.a
Back to astwriter.SourceWriter
Called for the global statement.
>>> print sourceToString("global a,b,c") global a, b, c
Back to astwriter.SourceWriter
Called for the if statement. Also handles the elif and else clauses.
>>> source = ''' ... if True: ... print "true" ... elif False: ... print "false" ... else: ... print "I am confused" ... ''' >>> print sourceToString(source) if True: print 'true' <BLANKLINE> elif False: print 'false' <BLANKLINE> else: print 'I am confused' <BLANKLINE> <BLANKLINE>
Back to astwriter.SourceWriter
Called for the import statement.
>>> print sourceToString( ... "import a.b as b, some.thing as some") import a.b as b, some.thing as some
Back to astwriter.SourceWriter
Called for bitwise inversion.
>>> print sourceToString("~ 5") ~5
Back to astwriter.SourceWriter
Called for a keyword argument.
>>> print sourceToString("f(a=5)") f(a = 5) <BLANKLINE>
Back to astwriter.SourceWriter
Called for a lambda statement.
>>> print sourceToString("lambda a,b=1, *a, **k: a") lambda a, b=1, *a, **k: a
Back to astwriter.SourceWriter
Called for bitwise shift-left.
>>> print sourceToString("1<<2") 1 << 2
Back to astwriter.SourceWriter
Called for a literal list.
>>> print sourceToString("[a,b,c]") [a, b, c]
Back to astwriter.SourceWriter
Called for a list comprehension.
>>> source = '[x for x in range(10)]' >>> print sourceToString(source) [x for x in range(10)]
Back to astwriter.SourceWriter
Hmmm, if expr1 if expr2 is valid syntax (at least in 2.4.2.)
>>> source = '[x for x in range(10) if x if y]' >>> print sourceToString(source) [x for x in range(10) if x if y]
Back to astwriter.SourceWriter
Used for the if part of a list comprehension.
>>> source = '[x for x in range(10) if x if y]' >>> print sourceToString(source) [x for x in range(10) if x if y]
Back to astwriter.SourceWriter
Called for modulus division.
>>> print sourceToString("1%2") 1 % 2
Back to astwriter.SourceWriter
This allways gets visited first.
>>> print sourceToString("' test'") """ test """ <BLANKLINE>
Back to astwriter.SourceWriter
Called for the multiplication operation.
>>> print sourceToString("1*2") 1 * 2
Back to astwriter.SourceWriter
Called for variable access.
>>> print sourceToString("test") test
Back to astwriter.SourceWriter
Called for logical not.
>>> print sourceToString("not True") not True
Back to astwriter.SourceWriter
Called for logical or. Note that multiple or's result in one call to this method.
>>> print sourceToString("a or b or c") a or b or c
Back to astwriter.SourceWriter
Called for the pass statement.
>>> print sourceToString("def test(): pass") def test(): pass
Back to astwriter.SourceWriter
Called for exponation oerations.
>>> print sourceToString("1**2") 1 ** 2
Back to astwriter.SourceWriter
Called for a print statement without a trailing linefeed.
>>> print sourceToString("print >>f,a,b,c,") print >>f, a, b, c, >>> print sourceToString("print a,b,c,") print a, b, c,
Back to astwriter.SourceWriter
Called for a print statement with a trailing linefeed.
>>> print sourceToString("print >>f,a,b,c") print >>f, a, b, c >>> print sourceToString("print a,b,c") print a, b, c
Back to astwriter.SourceWriter
Called for the raise statement.
>>> print sourceToString( ... "raise ValueError, value, tb") raise ValueError, value, tb
Back to astwriter.SourceWriter
Called for the return statement.
>>> print sourceToString("return some,things") return (some, things)
Back to astwriter.SourceWriter
Called for bitwise shift right.
>>> print sourceToString("1>>2") 1 >> 2
Back to astwriter.SourceWriter
Called for sequence slice operations.
>>> print sourceToString("l[a: b: c ]") l[a:b:c] <BLANKLINE> >>> print sourceToString("l[:b:c ]") l[:b:c]
>>> print sourceToString("l[a::c ]") l[a::c]
>>> print sourceToString("l[a:b:]") l[a:b]
Back to astwriter.SourceWriter
Called for each statement.
>>> print sourceToString("print x") print x
Back to astwriter.SourceWriter
Called for each subtraction operation.
>>> print sourceToString("x-1") x - 1
Back to astwriter.SourceWriter
Called for a subscript (not a slice).
>>> print sourceToString("[1,2,3][0]") [1, 2, 3][0] >>> print sourceToString("del l[2]") del l[2]
Back to astwriter.SourceWriter
Called for a try/except block.
>>> source = ''' ... try: ... some(thing) ... except ValueError,e: ... done() ... ''' >>> print sourceToString(source) try: some(thing) <BLANKLINE> except ValueError, e: done() <BLANKLINE> <BLANKLINE>
>>> source = ''' ... try: ... some(thing) ... except (ValueError,CustomError),e: ... done() ... ''' >>> print sourceToString(source) try: some(thing) <BLANKLINE> except (ValueError, CustomError), e: done() <BLANKLINE> <BLANKLINE>
Back to astwriter.SourceWriter
Called for a try/finally block.
>>> source = ''' ... try: ... some(thing) ... finally: ... done() ... ''' >>> print sourceToString(source) try: some(thing) <BLANKLINE> finally: done() <BLANKLINE> <BLANKLINE>
Back to astwriter.SourceWriter
Called for each tuple.
>>> print sourceToString("(1,2,3)") (1, 2, 3) >>> print sourceToString("(1,)") (1,)
Back to astwriter.SourceWriter
Called for unary "+".
>>> print sourceToString("+100") +100
Back to astwriter.SourceWriter
Called for unary "-".
>>> print sourceToString("-100") -100
Back to astwriter.SourceWriter
Called for the while statement.
>>> source = ''' ... while True: ... some(thing) ... else: ... else_(thing) ... ''' >>> print sourceToString(source) while True: some(thing) <BLANKLINE> else: else_(thing) <BLANKLINE>
Back to astwriter.SourceWriter
Called for a yield statement.
>>> print sourceToString("yield y") yield y
I return the argument signature for node which must be a Function or Lambda node.