python as pen and paper substitute

Manuel Graune manuel.graune at koeln.de
Wed Apr 7 19:15:20 CEST 2010


Hello Johan,

thanks to you (and everyone else who answered) for your effort.

Johan Grönqvist <johan.gronqvist at gmail.com> writes:

> Manuel Graune skrev:
>> Manuel Graune <manuel.graune at koeln.de> writes:
>>
>> Just as an additional example, let's assume I'd want to add the area of
>> to circles.
>> [...]
>> which can be explained to anyone who knows
>> basic math and is not at all interested in
>> python.
>>
>
> Third attempt. The markup now includes tagging of different parts of
> the code, and printing parts of the source based on a tag.
>

after playing around for a while, this is what I finally ended up with:

8<--------8<-------- source ---8<--------
#! /usr/bin/python
## Show
# List of all imports:
from __future__ import with_statement, print_function
from math import pi as PI
import sys
##

class Source_Printer(object):
    def __init__(self):
        self.is_printing= False
        with open(sys.argv[0]) as file:
            self.lines=(line for line in file.readlines())
        for line in self.lines:
            if line.startswith("print_source"):
                break 
            elif line == "##\n":
                self.is_printing= False
            elif line.startswith("## Show"):
                print("\n")
                self.is_printing= True
            elif self.is_printing:
                print(line,end="")
    def __call__(self):
        for line in self.lines:
            if line == "##\n" or line.startswith("print_source"):
                if self.is_printing:
                    self.is_printing= False
                    break
                else:
                    self.is_printing= False
            elif line.startswith("## Show"):
                print("\n")
                self.is_printing= True
            elif self.is_printing:
                print(line, end="")


print_source= Source_Printer()
## Show
#Calculation of first Area:
d1= 3.0
A1= d1**2 * PI / 4.0
##
print_source()

print ("Area of Circle 1:\t", A1)

## Show
#Calculation of second area:
d2= 5.0
A2= d2**2 * PI / 4.0
##
# This is a comment that won't be printed

print_source()
print ("Area of Circle 2:\t", A2)

# This is another one
Sum_Of_Areas= A1 + A2
print ("Sum of areas:\t", Sum_Of_Areas) 

8<--------8<-------- result: ---8<--------

# List of all imports:
from __future__ import with_statement, print_function
from math import pi as PI
import sys


#Calculation of first Area:
d1= 3.0
A1= d1**2 * PI / 4.0
Area of Circle 1:        7.06858347058


#Calculation of second area:
d2= 5.0
A2= d2**2 * PI / 4.0
Area of Circle 2:        19.6349540849
Sum of areas:    26.7035375555

8<--------8<-------- result: ---8<--------

Regards,

Manuel



-- 
A hundred men did the rational thing. The sum of those rational choices was
called panic. Neal Stephenson -- System of the world
http://www.graune.org/GnuPG_pubkey.asc
Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A  5828 5476 7E92 2DB4 3C99



More information about the Python-list mailing list