Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > python as pen and paper substitute

Reply
Thread Tools

python as pen and paper substitute

 
 
Manuel Graune
Guest
Posts: n/a
 
      04-06-2010
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
 
Reply With Quote
 
 
 
 
Johan Grönqvist
Guest
Posts: n/a
 
      04-06-2010
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

 
Reply With Quote
 
 
 
 
Manuel Graune
Guest
Posts: n/a
 
      04-06-2010
Thanks for your reply.

Johan Grönqvist <(E-Mail Removed)> 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
 
Reply With Quote
 
Manuel Graune
Guest
Posts: n/a
 
      04-06-2010
Manuel Graune <(E-Mail Removed)> 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= d1**2 * PI / 4.0
"""
exec(code2)
print(code2)
print("Area of Circle 2:\t", A2)

code3="""
Sum_Of_Areas= A1 + A2
"""
exec(code3)
print(code3)
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: 7.06858347058

Sum_Of_Areas= A1 + A2

Sum of areas: 14.1371669412

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
 
Reply With Quote
 
Manuel Graune
Guest
Posts: n/a
 
      04-06-2010
Manuel Graune <(E-Mail Removed)> 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
 
Reply With Quote
 
Johan Grönqvist
Guest
Posts: n/a
 
      04-06-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

 
Reply With Quote
 
Johan Grönqvist
Guest
Posts: n/a
 
      04-06-2010
Manuel Graune skrev:
> Manuel Graune <(E-Mail Removed)> 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.

(Sorry about the mixture of python 2.X and python 3.X print statements,
I use 2.5)

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

def print_selected_source(tag = ""):
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") and tag in line:
is_printing = True
elif is_printing:
print line,



from math import pi as PI

## Show Code1
d1= 3.0
A1= d1**2 * PI / 4.0

## Ignore
print_selected_source(tag = "Code1")
print ("Area of Circle 1:\t", A1)

## Show Code2
d2= 5.0
A2= d2**2 * PI / 4.0


## Ignore
print_selected_source(tag = "Code2")
print ("Area of Circle 2:\t", A2)

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


/ johan

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      04-06-2010


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
 
Reply With Quote
 
Chris Colbert
Guest
Posts: n/a
 
      04-06-2010
you may have a look at sage:

http://www.sagemath.org/
 
Reply With Quote
 
James Stroud
Guest
Posts: n/a
 
      04-07-2010
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). To clarify, I just start an editor, write a file that
> might look something like this:


I wrote a module called pylave, which is meant to be a
parser-evaluator-solver for scratch paper-like calculations. It has
strict operator precedence, knows a variety of common operations, and
respects parenthetic grouping. It is designed to evaluate ini files,
which can have syntax highlighting depending on your text editor.
Pyparsing is required.


Here is a pylave example:


% cat test.ini
factor = 2
wwww = minor_axis / 2
www = 69.69 + wwww
height = 10
apex = epicenter // height
major_axis = epicenter + 2*radius / sin big
minor_axis = epicenter + (total / 14) % (length-1)
epicenter = 44
radius = ((14 + length)/2 + height)*breadth
length = width + 5
width = 2**2 - factor
total = big*(radius + 14)
big = abs (sin (5e+)
breadth = 2*length + height
overlap = abs (1 + 1)
nexus = sin (5e-
plexus = sin (5e


% ./pylave.py test.ini
breadth : 24
www : 93.8350093235
nexus : 5e-08
big : 0.284704073238
major_axis : 3547.35712408
height : 10
radius : 492.0
plexus : -0.284704073238
total : 144.060261058
epicenter : 44
apex : 4
overlap : 2
wwww : 24.1450093235
width : 2
length : 7
factor : 2
minor_axis : 48.290018647


It is not perfect but if it will help, I'll cheeseshop it.

James
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shift left operator using pen and paper Ian Collins C++ 17 08-03-2010 08:30 AM
[Solution] [QUIZ] Pen and Paper (#90) David Tran Ruby 6 08-17-2006 01:46 PM
[QUIZ] Some observations on Pen and Paper (#90) or to make it fast, choose your algorithm wisely Rick DeNatale Ruby 2 08-16-2006 05:39 PM
[QUIZ] Pen and Paper (#90) Ruby Quiz Ruby 44 08-15-2006 10:22 PM
pen and paper in the exam - 70-315 SpeedyGonzalez MCAD 3 11-17-2004 04:51 PM



Advertisments