title: How to accomplish this in python?
body: <p>Given the following input file:</p>
  
  <pre><code>a = 2
  b = 3
  c = a * b
  d = c + 4
  </code></pre>
  
  <p>I want to run the above input file through a python program that produces
  the following output:</p>
  
  <pre><code>a = 2
  b = 3
  c = a * b = 6
  d = c + 4 = 10
  </code></pre>
  
  <p>The input file is a legal python program, but the output is python with
  extra output that prints the value of each variable to the right of the
  declaration/assignment. Alternatively, the output could look like:</p>
  
  <pre><code>a = 2
  b = 3
  c = a * b
  c = 6
  d = c + 4
  d = 10
  </code></pre>
  
  <p>The motivation for this is to implement a simple "engineer's notebook"
  that allows chains of calculations without needing print statements
  in the source file.</p>
  
  <p>Here's what I have after modifying D.Shawley's (much appreciated) contribution:</p>
  
  <pre><code>#! /usr/bin/env python
  
  from math import *
  
  import sys
  locals = dict()
  
  for line in sys.stdin:
    line = line.strip()
    if line == '':
      continue
    saved = locals.copy()
    stmt = compile(line,'&lt;stdin&gt;','single')
    eval(stmt,None,locals)
    print line,
    for (k,v) in locals.iteritems():
      if k not in saved:
        print '=', v,
    print
  </code></pre>