Re: Turning String into Numerical Equation
Brian Kazian wrote: Here's my problem, and hopefully someone can help me figure out if there is a good way to do this. I am writing a program that allows the user to enter an equation in a text field using pre-existing variables. They then enter numerical values for these variables, or can tell the program to randomize the values used within a certain bounds. My problem is taking in this equation they have written in the text field and converting it into an equation Python can calculate. The only way I know of to do this is by parsing it, which could get pretty nasty with all of the different mathematical rules. Does anyone have a better idea than parsing to compute an equation from a string representation? Thanks so much! Brian Kazian eval() See: http://docs.python.org/lib/built-in-funcs.html#l2h-23 HTH, --ag -- Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays -- http://mail.python.org/mailman/listinfo/python-list
Re: Turning String into Numerical Equation
Brian Kazian wrote: Thanks for the help, I didn't even think of that. I'm guessing there's no easy way to handle exponents or logarithmic functions? I will be running into these two types as well. Well, consider: import math eval("log(pow(x,2)*pow(y,3),2)",{'pow':math.pow,'log':math.log},{'x':1,'y':2}) [No, you wouldn't want to write it that way; it's merely illustrating what you can do without doing much.] HTH, --ag [BTW -- cultural question: Do we top-post here?] "Artie Gold" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Brian Kazian wrote: Here's my problem, and hopefully someone can help me figure out if there is a good way to do this. I am writing a program that allows the user to enter an equation in a text field using pre-existing variables. They then enter numerical values for these variables, or can tell the program to randomize the values used within a certain bounds. My problem is taking in this equation they have written in the text field and converting it into an equation Python can calculate. The only way I know of to do this is by parsing it, which could get pretty nasty with all of the different mathematical rules. Does anyone have a better idea than parsing to compute an equation from a string representation? Thanks so much! Brian Kazian eval() See: http://docs.python.org/lib/built-in-funcs.html#l2h-23 HTH, --ag -- Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays -- Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays -- http://mail.python.org/mailman/listinfo/python-list
Re: Bug?
[EMAIL PROTECTED] wrote: Hello Ive recently found that you cannot type anything over 7 into a number that is preceded with a 0. ex: >>> 01 1 >>> 07 7 >>> 08 SyntaxError: invalid token >>> 011 9 >>> 017 15 >>> 077 63 >>> 078 SyntaxError: invalid token I know this isnt that big of a problem, but i cannot think of one reason why they would not allow numbers preceded with a 0 to have a number higher then a 7 in them... And it seems very inconsistant to me... Is there a reason for this? Yup. ;-) Numbers beginning with a `0' are in octal (base 8), so only the digits `0' through `7' are valid. HTH, --ag -- Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple thread-safe counter?
Skip Montanaro wrote: Paul> I'd like to have a function (or other callable object) that Paul> returns 0, 1, 2, etc. on repeated calls. ... Paul> There should never be any possibility of any number getting Paul> returned twice, or getting skipped over, even if f is being called Paul> from multiple threads. How about (untested): import Queue counter = Queue.Queue() counter.put(0) def f(): i = counter.get() I think you need: i = counter.get(True) for this to work; otherwise a race condition would raise an exception. counter.put(i+1) return i [snip] This is, of course dependent upon counter.get() being guaranteed to be thread safe. (I haven't found anything in the docs specifically relating to that. Perhaps it's implicit?) Thanks, --ag -- Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple thread-safe counter?
Leif K-Brooks wrote: Artie Gold wrote: Skip Montanaro wrote: counter = Queue.Queue() def f(): i = counter.get() I think you need: i = counter.get(True) The default value for the "block" argument to Queue.get is True. Right. I misparsed the entry in the documentation: "If optional args block is true and timeout is None (the default), block if necessary..." Thanks, --ag -- Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays -- http://mail.python.org/mailman/listinfo/python-list
Re: Lambda: the Ultimate Design Flaw
Torsten Bronger wrote: HallÃchen! Daniel Silva <[EMAIL PROTECTED]> writes: Shriram Krishnamurthi has just announced the following elsewhere; it might be of interest to c.l.s, c.l.f, and c.l.p: http://list.cs.brown.edu/pipermail/plt-scheme/2005-April/008382.html The Fate Of LAMBDA in PLT Scheme v300 or Lambda the Ultimate Design Flaw About 30 years ago, Scheme had FILTER and MAP courtesy of Lisp hackers who missed them from their past experience. To this collection, Scheme added a lexically-scoped, properly-functioning LAMBDA. But, despite of the PR value of anything with Guy Steele's name associated with it, we think these features should be cut from PLT Scheme v300. [...] The whole text seems to be a variant of <http://www.artima.com/weblogs/viewpost.jsp?thread=98196>. TschÃ, Torsten. Ya think? ;-) --ag -- Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with unpack hex to decimal
Jonathan Brady wrote: <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Hello, I was looking at this: http://docs.python.org/lib/module-struct.html and tried the following import struct struct.calcsize('h') 2 struct.calcsize('b') 1 struct.calcsize('bh') 4 I would have expected struct.calcsize('bh') 3 what am I missing ? Not sure, however I also find the following confusing: struct.calcsize('hb') 3 struct.calcsize('hb') == struct.calcsize('bh') False I could understand aligning to multiples of 4, but why is 'hb' different from 'bh'? Evidently, shorts need to be aligned at an even address on your platform. Consider the following layout, where `b' represents the signed char, `h' represents the bytes occupied by the short and `X' represents unused bytes (due to alignment. 'bh', a signed char followed by a short would look like: bXhh -- or four bytes, but 'hb', a short followed by a signed char would be: hhb (as `char' and its siblings have no alignment requirements) HTH, --ag -- Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays -- http://mail.python.org/mailman/listinfo/python-list
Re: libpython2.3.a Linux
David wrote: > Hello, > > I'm trying to build a python module in the form of an .so file but I keep > getting the following message : > > /usr/bin/ld cannot find -llibpython2.3 > > I have all the necessary files in /usr/local/src/Python-2.3.5 > > with libpython2.3.a and libpython2.3.so files > > g++ -I /usr/local/src/Python-2.3.5/Include -L /usr/local/src/Python-2.3.5 - > shared -fPIC -l libpython2.3 -o file.so Close: g++ -I/usr/local/src/Python-2.3.5/Include -L/usr/local/src/Python-2.3.5 -shared -fPIC -lpython2.3 -o file.so Please read the man page (or equivalent documentation) for the correct way to use options in g++. > > Running on Fedora Core 3 - 1.7.0 > > made the following steps in compiling and installing Python : > > ./configure > make > make install > make libpython2.3.so > > Compiling on windows 2k is fine. > > Thanks for any input. > > David > > > HTH, --ag -- Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays -- http://mail.python.org/mailman/listinfo/python-list