From: Johan Grönqvist on 6 Apr 2010 17:49 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. (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
From: Dave Angel on 6 Apr 2010 18:01 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
From: Chris Colbert on 6 Apr 2010 18:46 you may have a look at sage: http://www.sagemath.org/
From: James Stroud on 6 Apr 2010 20:14 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+8)) breadth = 2*length + height overlap = abs (1 + 1) nexus = sin (5e-8) plexus = sin (5e8) % ./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
From: Ethan Furman on 6 Apr 2010 21:28 Manuel Graune wrote: > 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.). How about a decorator that turns your function into an object with the results? Your code would then look like this: 8<---------------------------------------------- #! /usr/bin/python3 from sp import ScratchPad from math import pi as PI @ScratchPad def code1(): d1= 3.0 A1= d1**2 * PI / 4.0 print("Area of Circle 1:\t", code1.A1) @ScratchPad def code2(): d2= 5.0 A2= d2**2 * PI / 4.0 print("Area of Circle 2:\t", code2.A2) Sum_Of_Areas= code1.A1 + code2.A2 print("Sum of areas:\t", Sum_Of_Areas) 8<---------------------------------------------- and the printout like so: Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sp_test d1= 3.0 A1= d1**2 * PI / 4.0 Area of Circle 1: 7.06858347058 d2= 5.0 A2= d2**2 * PI / 4.0 Area of Circle 2: 19.6349540849 Sum of areas: 26.7035375555 Here's the code for the decorator: 8<-------------------------------------------------------------- import inspect class PropertyObj(): """lookup using . notation""" def ScratchPad(func): source = inspect.getsourcelines(func)[0][2:] lead_space = 0 for char in source[0]: if char == ' ': lead_space += 1 else: break source = ''.join([line[lead_space:] for line in source]) print('\n' + source) intermed_result = {} final_result = PropertyObj() exec(source, func.__globals__, intermed_result) for key, value in intermed_result.items(): setattr(final_result, key, value) return final_result 8<-------------------------------------------------------------- Hope this helps! ~Ethan~
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Performance of list vs. set equality operations Next: plotting in python 3 |