python as pen and paper substitute

Johan Grönqvist johan.gronqvist at gmail.com
Tue Apr 6 23:32:46 CEST 2010


Manuel Graune skrev:
> Thanks for your reply.
> 
> 
> The output should  1) show manually selected python code and comments
> (whatever I think is important), 2) show selected results (final and
> intermediate) and 3) *not* show python code that for someone only
> interested in the calculation and the results (and probably not
> knowing python) would just be "noise" (e. g. "import"-statements,
> actual "print()"-functions, etc.). 
> 

Here is my second attempt. This version introduces what I might 
optimistically call a very simple markup language in the code.
Printing of source can selectively be turned on and off by inserting 
lines beginning with "## Ignore" or "## Show" into the source file.


------------------------------
## Ignore
from __future__ import with_statement
import sys

def print_selected_source():
     is_printing = True
     with open(sys.argv[0]) as file:
         for line in file:
             if line.startswith("## Ignore"):
                 is_printing = False
             elif line.startswith("## Show"):
                 is_printing = True
             elif is_printing:
                 print line,


## Show
a = 1
b = 2
c = 3
result = a + b

## Ignore
print_selected_source()
print("result     =\t", result)
print("result + c =\t", result + c)
------------------------------

Is this getting closer?

/ johan




More information about the Python-list mailing list