Johan Gr wrote:
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

How about simply importing the file with the calculations? Printing the imported file is quite straightforward, and except for maybe skipping the import lines that may be at the top (eg. import math), the rest of the file could be meaningful for the end user.


That has the advantage that it's easy to have multiple "notepads", but only one set of code that runs them.

DaveA
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to