From: Manuel Graune on
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). To clarify, I just start an editor, write a file that
might look something like this:

---------snip-----
code="""
a = 1
b = 2
c = 3
result = a + b
"""
exec(code)
print(code)
print("result =\t", result)
print("result + c =\t", result + c)
---------snip------

and feed this to python.

For what it's worth, this approach achieves what it's supposed to,
which is to get some basic control over the output and
to avoid to much redundant typing.

Now I'm wondering if there is a more elegant method to achieve this which
e. g. does not mess up the syntax-hightlighting, does not use exec()
and avoids the redundant mentioning of the variable that holds the
acutal code. Since I have complete control over the input and the files
are not supposed to be shared, security should not a problem and
simplicity is criterion #1.


So, does anyone have tips?

Regards,

Manuel

P.S.: I know Ipython. In the cases where I use the hack shown above
it just does not fit my workflow




--
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: Johan Grönqvist on
Manuel Graune skrev:
> To clarify, I just start an editor, write a file that
> might look something like this:
>
> ---------snip-----
> code="""
> a = 1
> b = 2
> c = 3
> result = a + b
> """
> exec(code)
> print(code)
> print("result =\t", result)
> print("result + c =\t", result + c)
> ---------snip------
>
> and feed this to python.
>

I do not understand your use-case, but as one way of performing the same
task as the above code, without sacrificing syntax-highlighting, I would
suggest:

-------------------------
from __future__ import with_statement
import sys

def print_source():
print sys.argv
with open(sys.argv[0]) as file:
for line in file:
print line,

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

print_source()
print("result =\t", result)
print("result + c =\t", result + c)

------------------------


Does that help towards a solution of your problem?

/ johan

From: Manuel Graune on
Thanks for your reply.

Johan Grönqvist <johan.gronqvist(a)gmail.com> writes:
> Manuel Graune skrev:
>> To clarify, I just start an editor, write a file that
>> might look something like this:
>>
>> ---------snip-----
>> code="""
>> a = 1
>> b = 2
>> c = 3
>> result = a + b
>> """
>> exec(code)
>> print(code)
>> print("result =\t", result)
>> print("result + c =\t", result + c)
>> ---------snip------
>>
>> and feed this to python.
>>
>
> I do not understand your use-case, but as one way of performing the
> same task as the above code, without sacrificing syntax-highlighting,

The use-case is acually fairly simple. The point is to use a python
source-file as subsitute for scrap-paper (with the opportunity to
edit what is already written and without illegible handwriting).
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.).

> from __future__ import with_statement
> import sys
>
> def print_source():
> print sys.argv
> with open(sys.argv[0]) as file:
> for line in file:
> print line,
>
> [...]
>
> print_source()
> print("result =\t", result)
> print("result + c =\t", result + c)


As far as I understand this code, all of this would be printed as well,
which is exactly what I do not want.

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
Manuel Graune <manuel.graune(a)koeln.de> writes:
>
> The use-case is acually fairly simple. The point is to use a python
> source-file as subsitute for scrap-paper (with the opportunity to
> edit what is already written and without illegible handwriting).
> 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.).
>

Just as an additional example, let's assume I'd want to add the area of
to circles.

The source-file would look something like this:
------>snip source.py snip<------
#! /usr/bin/python3
from math import pi as PI

code1="""
d1= 3.0
A1= d1**2 * PI / 4.0
"""
exec(code1)
print(code1)
print("Area of Circle 1:\t", A1)

code2="""
d2= 5.0
A2= d2**2 * PI / 4.0
"""
exec(code2)
print(code2)
print("Area of Circle 2:\t", A2)

Sum_Of_Areas= A1 + A2
print("Sum of areas:\t", Sum_Of_Areas)
------->snip<------------------

And the output is:

d1= 3.0
A1= d1**2 * PI / 4.0

Area of Circle 1: 7.06858347058

d2= 5.0
A2= d1**2 * PI / 4.0

Area of Circle 2: 19.6349540849

Sum of areas: 26.7035375555

which can be explained to anyone who knows
basic math and is not at all interested in
python.


> 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: Johan Grönqvist on
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