newbie: generate a function based on an expression
Hello, I would like write a function that I can pass an expression and a dictionary with values. The function would return a function that evaluates the expression on an input. For example: fun = genFun("A*x+off", {'A': 3.0, 'off': -0.5, 'Max': 2.0, 'Min': -2.0} ) >>> fun(0) -0.5 >>> fun(-10) -2 >>> fun(10) 2 so fun would act as if I did: def fun(x): A = 3 off = -0.5 Max = 2 Min = -2 y = min(Max,max(Min,A*x + off)) return(y) Any ideas? jr -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: generate a function based on an expression
Overall I am trying to learn OOP by porting CppSim (http://www-mtl.mit.edu/~perrott) to Python. In CppSim, classes are defined that allow various functions to be defined, like amplifiers. In some cases they are linear: y = A*x some have offsets: y = A*x + off some are non-linear y = A*x - C*x**3 The coefficients and the function will remain constant once the function is defined. I read about the security concerns involved in using eval(). I don't expect this project to grow to the point where I require a web interface. However, since I am learning, I might as well learn the right way. -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: generate a function based on an expression
Another example is a filter. From the CppSim doc: Filter filt("1+1/(2*pi*fz)s","C3*s + C3/(2*pi*fp)*s^2","C3,fz,fp,Ts",1/gain,fz,fp,Ts); jr -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: generate a function based on an expression
Thanks for all the suggestions and comments!! I will try all those suggestions just to I can figure out how they work. For phase 1 of this project, I will probably go with the eval. thanks again, happy hacking... jr -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing engineering symbols
Just a newbie, trolling. I like this solution. Simple, easy to understand. Did you put the SI_prefixes outside the def so you could wrap the whole thing in a module and reuse it in other blocks? jr -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing engineering symbols
Wow!! 261 v. 75 lines!! jr -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading GDSII layouts
Funny, I started writing one this past weekend as a learning exercise (handling large files and start to use classes). If ipkiss does not work out, let me know specifically what you need and maybe my hack will work. jr Vincent Arnoux wrote: > Hello, > I am looking for a library for reading GDSII layout files structures > (hierarchy, cells names, ...). > I found IPKISS > (http://www.photonics.intec.ugent.be/research/facilities/design/ipkiss/default.htm), > but it looks to be a generator and not a reader. > > Thank you, > Vincent -- http://mail.python.org/mailman/listinfo/python-list
Simple text parsing gets difficult when line continues to next line
Hello, I have a simple script to parse a text file (a visual basic program) and convert key parts to tcl. Since I am only working on specific sections and I need it quick, I decided not to learn/try a full blown parsing module. My simple script works well until it runs into functions that straddle multiple lines. For example: Call mass_write(&H0, &HF, &H4, &H0, &H5, &H0, &H6, &H0, &H7, &H0, &H8, &H0, _ &H9, &H0, &HA, &H0, &HB, &H0, &HC, &H0, &HD, &H0, &HE, &H0, &HF, &H0, -1) I read in each line with: for line in open(fileName).readlines(): I would line to identify if a line continues (if line.endswith('_')) and concate with the next line: line = line + nextLine How can I get the next line when I am in a for loop using readlines? jr -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple text parsing gets difficult when line continues to next line
Thanks all. I think I'll follow the "don't do that" advice. jr Jacob Rael wrote: > Hello, > > I have a simple script to parse a text file (a visual basic program) > and convert key parts to tcl. Since I am only working on specific > sections and I need it quick, I decided not to learn/try a full blown > parsing module. My simple script works well until it runs into > functions that straddle multiple lines. For example: > > Call mass_write(&H0, &HF, &H4, &H0, &H5, &H0, &H6, &H0, &H7, &H0, > &H8, &H0, _ > &H9, &H0, &HA, &H0, &HB, &H0, &HC, &H0, &HD, &H0, &HE, > &H0, &HF, &H0, -1) > > > I read in each line with: > > for line in open(fileName).readlines(): > > I would line to identify if a line continues (if line.endswith('_')) > and concate with the next line: > > line = line + nextLine > > How can I get the next line when I am in a for loop using readlines? > > jr -- http://mail.python.org/mailman/listinfo/python-list
Add readline capability to existing interactive program
Hello, I use an interactive simulator/data plotter called ocean. I get really frustrated using it because it does not have basic readline capabilities like ctrl-a for beginning of line and ctrl-k to kill the rest of the line. I was thinking this might be easy to add with Python. Do something like start python and then kick off the ocean job and then use something like pexpect to interact with it. I think this would work but there may be an even easier way to do it, maybe directly with popen2. A quick google search lead me to: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/108287 and shows code: import os f=os.popen('gnuplot', 'w') print >>f, "set yrange[-300:+300]" for n in range(300): print >>f, "plot %i*cos(x)+%i*log(x+10)" % (n,150-n) f.flush() this kind of works but not quite. Windows don't pop open and results of calculations (3+3) don't echo back. Before I started coding, I thought I would ask if a module or examples already exits that do what I want. jr -- http://mail.python.org/mailman/listinfo/python-list
c_string missing from ctypes?
Hello, I was following along with this site: http://www.brunningonline.net/simon/blog/archives/000659.html and I got a error. It boils down to: In [9]: import ctypes In [10]: dir(ctypes.c_string) --- Traceback (most recent call last) P:\ in () : 'module' object has no attribute 'c_string' I google ctypes.c_string and many people use it. I am using python 2.5 with ctypes version: 1.0.1 on a windows machine. I have to admit I don't know where ctypes came from. I tried to re- install it but the window binaries only support 2.4. Also: http://starship.python.net/crew/theller/ctypes/tutorial.html seems dead. An info is greatly appreciated. jr -- http://mail.python.org/mailman/listinfo/python-list
Re: Using a browser as a GUI: which Python package
I am looking for the samething. I was thinking of Karrigell. http://karrigell.sourceforge.net/ -- http://mail.python.org/mailman/listinfo/python-list
skip item in list "for loop"
I am new to python and I love it. I am hacking a file. I want to not print a line if it contains the word 'pmos4_highv'. I also don't want to print the next line. The following code works but it "smells bad" and just doesn't look right. I was reading about generators. If I was using one, I could do a .next() after the match and get rid of the flags. Any suggestions how I can improve this? inPmos= False inFile = sys.stdin fileList = inFile.readlines() for line in fileList: line = line.rstrip() # Remove PMOS definitions - typically two lines. Screwed if only one and last inst. if inPmos: inPmos = False continue if 'pmos4_highv' in line: inPmos = True continue jr -- http://mail.python.org/mailman/listinfo/python-list
Re: skip item in list "for loop"
Thanks for the suggestions. The solution I liked most was to prevent the lines from appearing in the first place!! -- http://mail.python.org/mailman/listinfo/python-list