title: Example for ast.NodeTransformer that mutates an equation
body: <p>This is a continuation of my last question. I want to parse an equation and work on the ast I get. What I want to do is basically randomly scramble it so I get a new equation, that has to be also a valid function. This is to be used in a genetic algorithm. </p>
  
  <p>Here is where I start:</p>
  
  <pre><code>class Py2do(ast.NodeTransformer):
  def __init__(self):
    self.tree=[]
  def generic_visit(self, node):
      print type(node).__name__
      self.tree.append(type(node).__name__)
      ast.NodeVisitor.generic_visit(self, node)
      depth=3
      s = node.__dict__.items()
      s = "    ".join("%s %r" % x for x in sorted(node.__dict__.items()))
      print( "%s%s\t%s" % (depth, str(type(node)), s) )
      for x in ast.iter_child_nodes(node):
        print (x, depth)
  
  def visit_Name(self, node):
      # print 'Name :', node.id
      pass
  
  def visit_Num(self, node):
      print 'Num :', node.__dict__['n']
  
  def visit_Str(self, node):
      print "Str :", node.s
  
  def visit_Print(self, node):
      print "Print :"
      ast.NodeVisitor.generic_visit(self, node)
  
  def visit_Assign(self, node):
      print "Assign :"
      ast.NodeVisitor.generic_visit(self, node)
  
  def visit_Expr(self, node):
      print "Expr :"
      ast.NodeVisitor.generic_visit(self, node)
  
  
  
  
  
  if __name__ == '__main__':
      node = ast.parse("res= e**(((-0.5*one)*((delta_w*one/delta*one)**2)))")
      import ast_pretty
      print ast.dump(node)
      pprintAst(node)
      v = Py2do()
      v.visit(node)
      print v.tree
  </code></pre>
  
  <p>What I want to get out is something like this :</p>
  
  <pre><code>res= e**(delta*((one/delta_w*one)**2)))
  </code></pre>
  
  <p>or another valid random equation of some sort. This will be used in a Fortran program, so it would be nice if the resulting equation can also be transferred into Fortran. 
  Please comment your code and provide a test sample/unit test.</p>