title: Parse a .py file, read the AST, modify it, then write back the modified source code
body: <p>I want to programmatically edit python source code. Basically I want to read a <code>.py</code> file, generate the <a href="http://en.wikipedia.org/wiki/Abstract_syntax_tree" rel="noreferrer">AST</a>, and then write back the modified python source code (i.e. another <code>.py</code> file).</p>
  
  <p>There are ways to parse/compile python source code using standard python modules, such as <a href="http://docs.python.org/library/ast.html" rel="noreferrer"><code>ast</code></a> or <a href="http://docs.python.org/library/compiler.html" rel="noreferrer"><code>compiler</code></a>.  However, I don't think any of them support ways to modify the source code (e.g. delete this function declaration) and then write back the modifying python source code.</p>
  
  <p>UPDATE: The reason I want to do this is I'd like to write a <a href="http://en.wikipedia.org/wiki/Mutation_testing" rel="noreferrer">Mutation testing library</a> for python, mostly by deleting statements / expressions, rerunning tests and seeing what breaks.</p>