From: Manuel Graune on
Hello Johan,

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

Johan Grönqvist <johan.gronqvist(a)gmail.com> writes:

> Manuel Graune skrev:
>> Manuel Graune <manuel.graune(a)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
From: Manuel Graune on
Hello Johan,

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

Johan Grönqvist <johan.gronqvist(a)gmail.com> writes:

> Manuel Graune skrev:
>> Manuel Graune <manuel.graune(a)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= iter(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
From: Michael Torrie on
On 04/06/2010 12:40 PM, Manuel Graune wrote:
> Hello everyone,
>
> I am looking for ways to use a python file as a substitute for simple
> pen and paper calculations. At the moment I mainly use a combination
> of triple-quoted strings, exec and print (Yes, I know it's not exactly
> elegant).

This isn't quite along the lines that this thread is going, but it seems
to me that a program like "reinteract" is about what I want to replace a
pen and paper with a python-based thing. Last time I used it, it was
buggy, but if this concept was developed, it would totally rock:

http://fishsoup.net/software/reinteract/

From: Giacomo Boffi on
Manuel Graune <manuel.graune(a)koeln.de> writes:

> Hello everyone,
>
> I am looking for ways to use a python file as a substitute for simple
> pen and paper calculations.

search("embedded calc mode") if manuel in emacs_fellows_set or sys.exit(1)
From: Manuel Graune on
Giacomo Boffi <giacomo.boffi(a)polimi.it> writes:

> Manuel Graune <manuel.graune(a)koeln.de> writes:
>
>> Hello everyone,
>>
>> I am looking for ways to use a python file as a substitute for simple
>> pen and paper calculations.
>
> search("embedded calc mode") if manuel in emacs_fellows_set or sys.exit(1)

Well, the subject does say python and not elisp, but I'm a vim-user
anyways.

*duckandrun*


--
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