Re: py2exe compression not working with Python 2.5
Thomas Heller wrote: > nikie schrieb: > > When I try to compress the output of py2exe like this: > > > > from distutils.core import setup > > import py2exe > > > > setup(console=['hello.py'], options={"py2exe": {"compressed": 1}}) > > > > I get strange error messages: > > > > Adding zlib.pyd to C:\tests\CanControllerTest\New Folder > > (2)\dist\library.zip > > Traceback (most recent call last): > > File "setup.py", line 4, in > > setup(console=['hello.py'], options={"py2exe": {"compressed": 1}}) > > File "C:\Python25\lib\distutils\core.py", line 151, in setup > > dist.run_commands() > > File "C:\Python25\lib\distutils\dist.py", line 974, in run_commands > > self.run_command(cmd) > > File "C:\Python25\lib\distutils\dist.py", line 994, in run_command > > cmd_obj.run() > > File "C:\Python25\lib\site-packages\py2exe\build_exe.py", line 218, > > in run > > self._run() > > File "C:\Python25\lib\site-packages\py2exe\build_exe.py", line 285, > > in _run > > self.create_binaries(py_files, extensions, dlls) > > File "C:\Python25\lib\site-packages\py2exe\build_exe.py", line 591, > > in create_ > > binaries > > bytes = zlib_file.read() > > AttributeError: 'NoneType' object has no attribute 'read' > > > > This is a standard Python 2.5 installation. Doing the same thing with > > 2.4 works like a charm. > > > > Did I miss anything? > > > Patches for this have been posted to the py2exe-users list. Doh, should have looked there first... Thanks a lot! -- http://mail.python.org/mailman/listinfo/python-list
py2exe compression not working with Python 2.5
When I try to compress the output of py2exe like this: from distutils.core import setup import py2exe setup(console=['hello.py'], options={"py2exe": {"compressed": 1}}) I get strange error messages: Adding zlib.pyd to C:\tests\CanControllerTest\New Folder (2)\dist\library.zip Traceback (most recent call last): File "setup.py", line 4, in setup(console=['hello.py'], options={"py2exe": {"compressed": 1}}) File "C:\Python25\lib\distutils\core.py", line 151, in setup dist.run_commands() File "C:\Python25\lib\distutils\dist.py", line 974, in run_commands self.run_command(cmd) File "C:\Python25\lib\distutils\dist.py", line 994, in run_command cmd_obj.run() File "C:\Python25\lib\site-packages\py2exe\build_exe.py", line 218, in run self._run() File "C:\Python25\lib\site-packages\py2exe\build_exe.py", line 285, in _run self.create_binaries(py_files, extensions, dlls) File "C:\Python25\lib\site-packages\py2exe\build_exe.py", line 591, in create_ binaries bytes = zlib_file.read() AttributeError: 'NoneType' object has no attribute 'read' This is a standard Python 2.5 installation. Doing the same thing with 2.4 works like a charm. Did I miss anything? -- http://mail.python.org/mailman/listinfo/python-list
Re: Fastest Way To Loop Through Every Pixel
Paul McGuire wrote: > "Chaos" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > > > Paul McGuire wrote: > > > "Paul McGuire" <[EMAIL PROTECTED]> wrote in message > > > news:[EMAIL PROTECTED] > > > > "Chaos" <[EMAIL PROTECTED]> wrote in message > > > > news:[EMAIL PROTECTED] > > > > > > > > > > > > > > > myCol = (0.3 * image.GetRed(thisX, thisY)) + (0.59 * > > > > > image.GetGreen(thisX, thisY)) + (0.11 * image.GetBlue(thisX, thisY)) > > > > > if myCol < darkestCol: > > > > >darkestCol = myCol > > > > >possX = thisX > > > > >possY = thisY > > > > > > > > > > > > > Psyco may be of some help to you, especially if you extract out your > myCol > > > > expression into its own function, something like: > > > > > > > > def darkness(img,x,y): > > > > return (0.3 * img.GetRed(x,y)) + (0.59 * img.GetGreen(x,y)) + > (0.11 * > > > > img.GetBlue(x,y)) > > > > > > > > > > > > > Even better than my other suggestions might be to write this function, > and > > > then wrap it in a memoizing decorator > > > > (http://wiki.python.org/moin/PythonDecoratorLibrary#head-11870a08b0fa59a8622 > > > 201abfac735ea47ffade5) - surely there must be some repeated colors in > your > > > image. > > > > > > -- Paul > > > > Its not only finding the darkest color, but also finding the X position > > and Y Position of the darkest color. > > > > Sorry, you are correct. To take advantage of memoizing, the darkness method > would have to work with the r, g, and b values, not the x's and y's. > > -- Paul > > > import psyco > psyco.full() > > IMAGE_WIDTH = 200 > IMAGE_HEIGHT = 200 > imgColRange = range(IMAGE_WIDTH) > imgRowRange = range(IMAGE_HEIGHT) > > @memoize# copy memoize from > http://wiki.python.org/moin/PythonDecoratorLibrary, or similar > def darkness(r,g,b): > return 0.3*r + 0.59*g + 0.11*b > > def getDarkestPixel(image): > getR = image.GetRed > getG = image.GetGreen > getB = image.GetBlue > > darkest = darkness( getR(0,0), getG(0,0), getB(0,0) ) > # or with PIL, could do > # darkest = darkness( *getpixel(0,0) ) > dkX = 0 > dkY = 0 > for r in imgRowRange: > for c in imgColRange: > dk = darkness( getRed(r,c), getGrn(r,c), getBlu(r,c) ) > if dk < darkest: > darkest = dk > dkX = c > dkY = r > return dkX, dkY >From my experiences with Psyco/PIL, it's probably faster to move the image data into lists first (using list(Image.getdata) or list(image.getband)) and access the raw data in your loop(s). Don't use Image.getpixel in a tight loop, they result in Python-to-C-Calls which can't be optimized by Psyco. Even when you're not using Psyco, getpixel is probably slower (at least the PIL docs say so: "Note that this method is rather slow; if you need to process larger parts of an image from Python, use the getdata method.") -- http://mail.python.org/mailman/listinfo/python-list
Re: Fastest Way To Loop Through Every Pixel
Chaos wrote: > nikie wrote: > > Chaos wrote: > > > > > As my first attempt to loop through every pixel of an image, I used > > > > > > for thisY in range(0, thisHeight): > > > for thisX in range(0, thisWidth): > > > #Actions here for Pixel thisX, thisY > > > > > > But it takes 450-1000 milliseconds > > > > > > I want speeds less than 10 milliseconds > > > > Milliseconds don't mean much unless we knew how big your images are and > > what hardware you're using. > > > > Have you considered using NumPy? Assuming you can get the image into a > > numpy array efficiently, the actual algorithm boils down to something > > like this: > > > > grey = r*0.3 + > > g*0.59 + b*0.11 > > index = grey.argmin() > > x,y = index%step, index/step > > v = grey[x,y] > > > > where r,g,b and grey are numpy.ndarray objects; The arithmetic > > operators and the argmin-function are implemented in C, so you can > > expect decent performance. (the 4 lines above take about 80 ms for a > > 1000x1000 image on my PC) > > > > If that's not enough, you might want to use some specially optimized C > > library for this purpose. (I'd suggest Intel's IPP, but there are > > others). > > I really do not understand the code. Where did you get the varibales r, > g, b and step and what does v produce? Sorry, I should have commented it better. The idea is that r,g,b are numpy-arrays containig the r, g, b-pixel-values in the image: import numpy, Image img = Image.open("Image1.jpg") r = numpy.array(img.getdata(0)) g = numpy.array(img.getdata(1)) b = numpy.array(img.getdata(2)) w,h = img.size The "step" is the length of one line of pixels, that is, the offset to a pixel (x,y) is x+y*step. (This is usually called "step" or "stride" in literature) step = w Now, I can use numpy's overridden arithmetic operators to do the per-pixel calculations (Note that numpy also overrides sin, cos, max, ..., but you'll have to use from numpy import * to get these overrides in your namespace): grey = r*0.3 + g*0.59 + b*0.11 The multiplication-operator is overridden, so that "r*0.3" multiplies each value in the array "r" with "0.3" and returns the multiplied array, same for "g*0.59", "b*0.11". Adding the arrays adds them up value by value, and returns the sum array. This generally works quite well and intuitively if you perform only per-pixel operations. If you need a neighborhood, use slicing. The function "argmin" searches for the index of the minimum value in the array: index = grey.argmin() But since we want coordinates instead of indices, we'll have to transform these back using the step value: x,y = index % step, index / step Result: print x,y,r[index],g[index],b[index] Works for my image. Notes: - I'm _not_ sure if this is the fastest way to get an image into a numpy array. It's convenient, but if you need speed, digging into the numpy docs/newsgroup archive, and doing a few benchmarks for yourself would be a good idea - numpy does have 2d-Arrays, but I'm not that much of a numpy expert to tell you how to use that to get rid of the index/step-calculations. Again, numpy-docs/newsgroup might help - a general advice: using integer multiplications instead for floating-point may help with performance, too. - you said something about 10 ms, so I'm guessing your image doesn't come from a harddrive at all (because reading it alone will usually take longer than 10 ms). Numpy arrays have a C-Interface you might want to use to get data from a framegrabber/digital camera into a numpy array. -- http://mail.python.org/mailman/listinfo/python-list
Re: Fastest Way To Loop Through Every Pixel
Chaos wrote: > As my first attempt to loop through every pixel of an image, I used > > for thisY in range(0, thisHeight): > for thisX in range(0, thisWidth): > #Actions here for Pixel thisX, thisY > > But it takes 450-1000 milliseconds > > I want speeds less than 10 milliseconds Milliseconds don't mean much unless we knew how big your images are and what hardware you're using. Have you considered using NumPy? Assuming you can get the image into a numpy array efficiently, the actual algorithm boils down to something like this: grey = r*0.3 + g*0.59 + b*0.11 index = grey.argmin() x,y = index%step, index/step v = grey[x,y] where r,g,b and grey are numpy.ndarray objects; The arithmetic operators and the argmin-function are implemented in C, so you can expect decent performance. (the 4 lines above take about 80 ms for a 1000x1000 image on my PC) If that's not enough, you might want to use some specially optimized C library for this purpose. (I'd suggest Intel's IPP, but there are others). -- http://mail.python.org/mailman/listinfo/python-list
Re: Wxpython, using more than 1 timer?
janama wrote: > Hi all, > > Using wx > When adding a second timer as i have the first, the second timer > adding stops the first timer (updating or stops?) . In this example im > updating a uptime and localtime label. It works fine for displaying the > last "self.startTimer2()" called. But prevents the previous > self.startTimer1() from running . Im doing something fundamentally > wrong i guess? > > def __init__(self, parent): > self._init_ctrls(parent) > > #Start timers > self.startTimer1() > self.startTimer2() > > def startTimer1(self): > self.t1 = wx.Timer(self) > self.t1.Start(360) # 36 ms = 1/10 hour > self.Bind(wx.EVT_TIMER, self.OnUpTime) > > def startTimer2(self): > self.t2 = wx.Timer(self) > self.t2.Start(1000) # run every second > self.Bind(wx.EVT_TIMER, self.OnTime) > > def OnTime(self,evt): > self.lblTime.SetLabel(str(time.localtime())) > > def OnUpTime(self, evt): > self.lblUptime.SetLabel('Running ' + (str(myTimerText[0])) + ' > hours') # 1/10 hour count > myTimerText[0] = myTimerText[0] + .1 > > Any help appreciated, ta The problem is not that the first timer ist stopped, the problem is that both timers happen to call the same method in the end. Think of the "Bind" method as an assignment: it assigns a handler function to an event source. If you call it twice for the same event source, the second call will overwrite the first event handler. That's what happens in your code. The easiest way to change this is by using different ids for the timers: def startTimer1(self): self.t1 = wx.Timer(self, id=1) self.t1.Start(2000) self.Bind(wx.EVT_TIMER, self.OnUpTime, id=1) def startTimer2(self): self.t2 = wx.Timer(self, id=2) self.t2.Start(1000) self.Bind(wx.EVT_TIMER, self.OnTime, id=2) This way, the timers launch two different events, which are bound to two different methods. -- http://mail.python.org/mailman/listinfo/python-list
Re: Which compiler will Python 2.5 / Windows (Intel) be built with?
[EMAIL PROTECTED] wrote: > Hi everyone, > > which compiler will Python 2.5 on Windows (Intel) be built with? I > notice that Python 2.4 apparently has been built with the VS2003 > toolkit compiler, and I read a post from Scott David Daniels [1] where > he said that probably the VS2003 toolkit will be used for Python 2.5 > again. However, even before the release of Python 2.5, I cannot seem to > find many retailers around here that still carry Visual Studio 2003, > and some were a bit surprised about my request since Visual Studio 2005 > has been available for some months now. ... If you want to *buy* VS 2003, you could still purchase a 1-year MSDN Pro Subscription. The price difference isn't *that* big compared to a single-user license of VS, and it automatically includes past VS versions (everything from VC++ 6.0 and upwards, IIRC). -- http://mail.python.org/mailman/listinfo/python-list
Re: Python less error-prone than Java
Christoph Zwerschke wrote: > nikie wrote: > > Let's look at two different examples: Consider the following C# code: > > > > static decimal test() { > >decimal x = 10001; > >x /= 100; > >x -= 100; > >return x; > > > > It returns "0.01", as you would expect it. > > Yes, I would expect that because I have defined x as decimal, not int. > > > Now, consider the python equivalent: > > > > def test(): > > x = 10001 > > x /= 100 > > x -= 100 > > return x > > No, that's not the Python equivalent. The equivalent of the line > > decimal x = 10001 > > in Python would be > > x = 10001.0 > > or even: > > from decimal import Decimal > x = Decimal(10001) Hm, then I probably didn't get your original point: I thought your argument was that a dynamically typed language was "safer" because it would choose the "right" type (in your example, an arbitrary-pecision integer) automatically. As you can see from the above sample, it sometimes picks the "wrong" type, too. Now you tell me that this doesn't count, because I should have told Python what type to use. But shouldn't that apply to the Java binary-search example, too? I mean, you could have told Java to used a 64-bit or arbitrary-length integer type instead of a 32-bit integer (which would actually be equivalent to the Python code), so it would do the same thing as the Python binary search implementation. > ... > By the way, the equivalent Python code to your C# program gives on my > machine the very same result: > >>> x = 10001.0; x /= 100; x -= 100; print x > 0.01 Try entering "x" in the interpreter, and read up about the difference between str() and repr(). > > > Even if you used "from __future__ import division", it would actually > > return "0.015116", which, depending on the context, may > > still be an intolerable error. > > With from __future__ import division, I also get 0.01 printed. Anyway, > if there are small discrepancies then these have nothing to do with > Python but rather with the underlying floating-point hardware and C > library, the way how you print the value and the fact that 0.01 can > principally not be stored exactly as a float (nor as a C# decimal), only > as a Python Decimal. The is OT, but what makes you think a C# decimal can't store 0.01? > > I can even think of an example where C's (and Java's) bounded ints are > > the right choice, while Python's arbitraty-precision math isn't: > > Assume you get two 32-bit integers containing two time values (or > > values from an incremental encoder, or counter values). How do you > > find out how many timer ticks (or increments, or counts) have occured > > between those two values, and which one was earlier? In C, you can > > just write: > > > >long Distance(long t1, long t0) { return t1-t0; } > > > > And all the wraparound cases will be handled correctly (assuming there > > have been less than 2^31 timer ticks between these two time values). > > "Distance" will return a positive value if t1 was measured after t0, a > > negative value otherwise, even if there's been a wraparound in > > between. Try the same in Python and tell me which version is simpler! > > First of all, the whole problem only arises because you are using a > statically typed counter ;-) And it only is easy in C when your counter > has 32 bits. But what about a 24 bit counter? Easy, multiply it with 256 and it's a 32-bit counter ;-) Fortunately, 24-bit-counters are quite rare. 16-bit or 32-bit counters on the other hand are quite common, especially when you're working close to the hardware (where C is at home). All I wanted to point out is that bounded integers do have their advantages, because some people in this thread apparently have never stumbled over them. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python less error-prone than Java
Let's look at two different examples: Consider the following C# code: static decimal test() { decimal x = 10001; x /= 100; x -= 100; return x; } It returns "0.01", as you would expect it. Now, consider the python equivalent: def test(): x = 10001 x /= 100 x -= 100 return x It returns "0". Clearly an error! Even if you used "from __future__ import division", it would actually return "0.015116", which, depending on the context, may still be an intolerable error. Morale: the problem isn't whether the the types are chosen at compile-time or at runtime, it's simply _what_ type is chosen, and whether it's appropriate or not. I can even think of an example where C's (and Java's) bounded ints are the right choice, while Python's arbitraty-precision math isn't: Assume you get two 32-bit integers containing two time values (or values from an incremental encoder, or counter values). How do you find out how many timer ticks (or increments, or counts) have occured between those two values, and which one was earlier? In C, you can just write: long Distance(long t1, long t0) { return t1-t0; } And all the wraparound cases will be handled correctly (assuming there have been less than 2^31 timer ticks between these two time values). "Distance" will return a positive value if t1 was measured after t0, a negative value otherwise, even if there's been a wraparound in between. Try the same in Python and tell me which version is simpler! -- http://mail.python.org/mailman/listinfo/python-list
Re: Watching serial port activity.
xkenneth wrote: > Hi, > >I'm writing a couple python applications that use the serial port > (RS-232) quite extensively. Is there any way I can monitor all activity > on the serial port and have it printed as the transactions occur? I'm > trying to reverse engineer a microcontroller serial routine and I'd > like to see any response the chip sends back. I've done similar things in the past, and the best tools I found at that time were: - Serial Port sniffer from www.hhdsoftware.com Similar to portmon, but (in my experience) more stable. Windows only, though. - VMWare You can run your serial port app in a VMWare and connect the virtual serial port to a file or named pipe on the host system. But this won't help you if the app uses serial commands not available for files/pipes. - Hardware cable If you know how to use a soldering iron, this might be the best way: a serial cable has an RX and a TX wire, connect each of them to the RX wires of two separate serial cables, that way you can "wiretap" the whole communication to two different serial ports (e.g. on your laptop) in a running system. Dead useful for debugging! -- http://mail.python.org/mailman/listinfo/python-list
Re: Watching serial port activity.
xkenneth wrote: > Hi, > >I'm writing a couple python applications that use the serial port > (RS-232) quite extensively. Is there any way I can monitor all activity > on the serial port and have it printed as the transactions occur? I'm > trying to reverse engineer a microcontroller serial routine and I'd > like to see any response the chip sends back. I've done similar things in the past, and the best tools I found at that time were: - Serial Port sniffer from www.hhdsoftware.com Similar to portmon, but (in my experience) more stable. Windows only, though. - VMWare You can run your serial port app in a VMWare and connect the virtual serial port to a file or named pipe on the host system. But this won't help you if the app uses serial commands not available for files/pipes. - Hardware cable If you know how to use a soldering iron, this might be the best way: a serial cable has an RX and a TX wire, connect each of them to the RX wires of two separate serial cables, that way you can "wiretap" the whole communication to two different serial ports (e.g. on your laptop) in a running system. Dead useful for debugging! -- http://mail.python.org/mailman/listinfo/python-list
Re: Software Needs Philosophers
Xah Lee wrote: > Software Needs Philosophers > > by Steve Yegge, 2006-04-15. > > Software needs philosophers. > > This thought has been nagging at me for a year now, and recently it's > been growing like a tumor. One that plenty of folks on the 'net would > love to see kill me. No, we all wish you a long and quiet life! Although some of us are a little annoyed that you keep cross-posting articles wildly to completely unrelated newsgroups... > People don't put much stock in philosophers these days. The popular > impression of philosophy is that it's just rhetoric, just frivolous > debating about stuff that can never properly be answered. "Spare me > the philosophy; let's stick to the facts!" > > The funny thing is, it's philosophers who gave us the ability to think > rationally, to stick to the facts. If it weren't for the work of > countless philosophers, facts would still be getting people tortured > and killed for discovering and sharing them. > > Does it ever strike you as just a teeny bit odd that after a brief > period where philosophy flourished, from maybe 400 B.C.E. to ~100 C.E., > we went through a follow-on period of well over one thousand five > hundred years during which the Roman Catholic Church enslaved > everyone's minds and killed anyone who dared think differently? I wonder where you get your historical "facts" form? (Monty Python movies?) Let's just add a few fun facts: Yes, philosophy did flourish in ancient greece, but liberty certainly didn't. Yes, Athens was (at least most of the time) a democracy - which by the way, most philosophers thought was a very bad thing. But still, about 90% of the population of Athens were slaves at that time. Not just "mentally enslaved", no, real, physical slaves. Also, it was dangerous to have oppinions that authorities didn't like (Socrates for example was sentenced to death because of impiety, Anaxagoras and Aristoteles had to flee because of similar charges, Hipposus, who _proved_ a flaw in Pythagoras' number theory was drowned). And, sad to say, if philosophers would have been in charge, things would probably have been even worse (Ever read Plato's "The State"?) Also, has the roman catholic church really "killed anyone who dared think differently"? The Spanish Inquisition for example killed about 1000-2000 people in two centuries. That's bad enough, no question, but "anyone who dared think differently"? Hardly. > What's weirder is that we tend to pretend it didn't really happen. We > like to just skip right over the dominance of religion over our minds > for a hundred generations, and think of religion today as a kindly old > grandpa who's just looking out for us kids. No harm, no foul. Let > bygones be bygones. Sure, there were massacres and crusades and > genocides and torture chambers with teeth grinding and eyes bleeding > and intestines torn out in the name of God. But we were all just kids > then, right? Nobody does that kind of thing today, at least not in > civilized countries. Hmmm. There were massacres in the name of liberty to, e.g. in the French Revolution. Does that make liberty (and those who value it) equally evil? (The same is of course true for money, love, or probably anything else people like) > We try not to think about the uncivilized ones. We do! Let's think about some of them: The Khmers rouges come to my mind, also China, and a few years back the Soviet Union. Notice something? Right, no religion. In fact, they were more or less following the works of the philosopher Karl Marx. > It was philosophers that got us out of that Dark Ages mess, and no > small number of them lost their lives in doing so. In the "Dark Ages" pretty much the only chance to get a decent education was to become a monk or at least be taught by monks. So, it isn't surprising that almost all of the philosophers at the time (like William of Occam or Roger Bacon) were monks. Therefore, philosophy was never clearly separated from theology during that time. The end of the middle ages is probably marked by the renaissance and the reformation, the latter of course started by a priest. What have we learned? Yes, Religion was an important power in the development of europe over the last 3000 years (yes, I'm including the Antiquity in this, it didn't just take a break to watch the philosophy channel). So were money, and military power, technology, social factors, and of course philosophy. Yes, it did have bad consequences, and it did have good ones. The same is true for all the other powers as well. (BTW: Have you ever considered the possibility that philosophers might not be interested in tab-versus-spaces-debates in the first place? Maybe they have more interesting matters to discuss. Just like the rest of us.) -- http://mail.python.org/mailman/listinfo/python-list
Re: which windows python to use?
Brian Blais wrote: > Hello, > > Are there any recommendations for which Windows python version to use? I'd > like to > see a pros-and-cons description of each, given that different uses might > dictate > different versions. The versions I know (and there are surely more) are: > > 1) download from www.python.org > 2) enthought > 3) activepython > > Are there advantages/disadvantages? I have used enthought before, but it > seems as if > they are not at 2.4, and may lag behind in versions (which may not be a bad > thing). It depends on what you want to do. If you're going to use SciPy (a library for scientific computing), I'd suggest using enthought, as installing SciPy on windows can be a real nightmare for a newbie... Anyway, the enthought edition comes with lots of interesting libraries installed (listed on their homepage), you might want to look at them even if you don't use their distribution. Note that you can have more than one Python installation on the same PC, so you can also work with Python 2.4 (from www.python.org) and switch to enthought for certain projectes if you need it. I haven't looked into ActivePython too much, from what I read it seemed more or less standard python + mark hammonds Win32 utils, but you can install those manually on a plain www.python.org-distribution. -- http://mail.python.org/mailman/listinfo/python-list
Re: Multi-line lambda proposal.
Kaz Kylheku wrote: > Kaz Kylheku wrote: > > But suppose that the expression and the multi-line lambda body are > > reordered? That is to say, the expression is written normally, and the > > mlambda expressions in it serve as /markers/ indicating that body > > material follows. This results in the most Python-like solution. > > Unfortunately, while this idea has intuitive appeal, It does? Didn't seem so in the responses you got so far, did it... ;-) SCNR > it leaves some > problems to solve; namely, lambdas that occur in expressions embedded > within statement syntax which has body material of its own. For > instance > > # lambda defined and immediately called here > if lambda(x)(4) < 0: > print "a" > elif y = 4: > print "b" > else: > print "foo" > > Where to stick the lambda body? It's not hard to come up with > straightforward answers, except that they are not particularly > appealing. One rule might be that the lambda bodies have to be placed > immediately after the statement body material, set off by the lambda: > thing. In the case of if/elif/else, they have to be placed behind the > closest suite that follows the expression in the syntax of the > statement: > > if lambda(x)(4) < 0: > print "a" > lambda: > return x + 1 > elif y = 4: > print "b" > else: > print "foo" > > The overall rule is simple and uniform: each suite can have lambda: > clauses. These have to match lambda() occurences in the expression (or > expression list) immediately to the left in the same grammar > production. Sorry to tell you this, but I think that kind of thing is exactly what the BDFL had in mind when he talked about "Rube-Goldberg-Devices" in Python... -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get a part of string which follows a particular pattern using shell script
Hari wrote: > Hi all, > > I need to get a part of string which follows a pattern 'addr=' > > > For example: > > > a)test="192.168.1.17:/home/ankur/nios_fson/mnt/tmptype > nfs(rw,addr=192.168.1.17)" > b)test="/dev/root on / typr nfs > (rw,v2,rsize=1024,wsize=1024,hard,udp,nolock,addr=192.168.1.93)" > > > I need to get the ipaddress from the above two strings a and b which > follows 'addr='. I tried to use cut, but it accepts only single charter > > as delimiter. If I give delimiter as 'addr=' for cut command it gives > me a error. Regular Expressions are probably the easiest way to do this. Example: import re str = """a)test="192.168.1.17:/home/ankur/nios_fson/mnt/tmptype nfs(rw,addr=192.168.1.17)" b)test="/dev/root on / typr nfs (rw,v2,rsize=1024,wsize=1024,hard,udp,nolock,addr=192.168.1.93)""" m = re.search("addr=(\d+\.\d+\.\d+\.\d+)", str) print m.group(1) -> Prints: 192.168.1.17 Read the Python manual on Regular Expressions (module re) or google for Python regular expression tutorials if your search is more complex than this (or if you want to know what's going on). If you want a good in-depth text on the subject, I'd recommend J. Friedls "Mastering Regular Expressions." Have fun. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can I use python for this .. ??
san wrote: > Hi > > I am using windows xp and have installed python and win32. I am > familiar with basic Python. I wanted to control some of the > applications via python script. > > I would like to write a python script to say: > 1. Open firefox and log on to gmail > 2. Another firefox window to visit slickdeals > 3. Open winamp and tune in to a shoutcast station > 4. Open groupwise ... > etc .. > > These are the task I do every day whenever I log on my computer .. it > just seems repetitive and hence probably can be coded in python. > > Can I do this in python? Where do I find the COM objects/interfaces for > all these different programs? Are there any sample codes ? I just googled for "iTunes com python", there are lots of samples. If you can't find any, or want to find out about an applications interfaces yourself, you can sill use MakePy to find out what com interfaces are installed on your computer, and simply use the "dir" and "help" commands in the interactive console on those interfaces to find out what they do. (There are other tools to explore COM interfaces, like OleView, but if you're familiar with Python already, using dir/help is probably going to be easier) Reading Mark Hammond's excellent book (or at least the sample chapter on automation) wouldn't hurt either ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to determine sequence ordering?
Steven Bethard wrote: > nikie wrote: > > Steven Bethard wrote: > > > >> nikie wrote: > >>> Steven Bethard wrote: > >>> > >>>> John Salerno wrote: > >>>>> If I want to make a list of four items, e.g. L = ['C', 'A', 'D', 'B'], > >>>>> and then figure out if a certain element precedes another element, what > >>>>> would be the best way to do that? > >>>>> > >>>>> Looking at the built-in list functions, I thought I could do something > >>>>> like: > >>>>> > >>>>> if L.index('A') < L.index('D'): > >>>>> # do some stuff > >>>> This is probably a pretty reasonable approach as long as you're not too > >>>> worried about efficiency. It scans the list twice though, so if you > >>>> start doing this with really big lists, it might be better to do > >>>> something like: > >>> On average, it'll only have go through half the list twice, as it can > >>> break as soon as it finds the item it searches for. Don't think you can > >>> get any more efficient than that. > >> Sure you can: > >> > >> a_index = None > >> d_index = None > >> for i, item in enumerate(L): > >> if item == 'A': > >> a_index = i > >> elif item == 'D': > >> d_index = i > >> if a_index is not None and d_index is not None: > >> break > >> > >> if a_index < d_index: > >> # do some stuff > >> > >> That goes through exactly the minimum number of elements. But it's > >> probably slower than two .index() calls since .index() is coded in C. > >> But timeit and see if you're interested. > > > > On my PC, calling index twice is 5 times faster. > > > > But let's just pretend for a moment Python code was compiled and > > optimized as well as C code. > > Ok, lets get comparable functions by writing them both in Python: That's changing the subject, and you know that. The OP asked whether using "L.index('A') < L.index('D')" was a good (pythonic, efficient) idea. It is. Maybe it wouldn't be efficient if "List.index" was implemented in python (because, as I have already said in my previous post, looping through an enumerate-object in python is more complex than a simple C-loop), but it is actually implemented in C. Even if you wrote a C function that tried to do all the comparisons in one sweep through the list, I'm willing to bet it won't be faster than the OP's suggestion on the average (at least for moderate-sized lists, otherwise, processing the list in blocks, using the cache more efficiently might be a good idea). That's what this thread was all about. Now, I don't really see what you are trying to say: Are you still trying to convince the OP that he should write a Python function like one of those you suggested, for performance reasons? If not, I must have misunderstood your last posts, and apologize for repeating the obvious ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: self modifying code
Robin Becker schrieb: > When young I was warned repeatedly by more knowledgeable folk that self > modifying code was dangerous. > > Is the following idiom dangerous or unpythonic? > > def func(a): > global func, data > data = somethingcomplexandcostly() > def func(a): > return simple(data,a) > return func(a) It took me quite a while to figure out how it works, so, yes, I'd say it's unpythonic ;-). It's not really dangerous, but it can get nasty if someone tries to rename the function, or put it into a class. But that's probably not the kind of "self-modifying code" you've been warned against anyway: I've only ever seen self-modifying code in assembly language or in lisp, the idea is that you really change the code (i.e. single opcodes in the function that's currently running), so you can e.g. make an infinite loop, and eventually overwrite the loop statement to do something else so the loop ends. I'm not sure if you can do the same thing in Python, maybe by changing the bytecode of a running function. > It could be replaced by > > data = somethingcomplexandcostly() > def func(a): > return simple(data,a) > > but this always calculates data. You could of course initialize data with None and calculate it only on demand. Or you could use: http://www.phyast.pitt.edu/~micheles/python/documentation.html#memoize This has the advantage of encapsulating the memoization logic so it can be tested (and understood) separately from your code. -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to determine sequence ordering?
Steven Bethard wrote: > nikie wrote: > > Steven Bethard wrote: > > > >> John Salerno wrote: > >>> If I want to make a list of four items, e.g. L = ['C', 'A', 'D', 'B'], > >>> and then figure out if a certain element precedes another element, what > >>> would be the best way to do that? > >>> > >>> Looking at the built-in list functions, I thought I could do something > >>> like: > >>> > >>> if L.index('A') < L.index('D'): > >>> # do some stuff > >> This is probably a pretty reasonable approach as long as you're not too > >> worried about efficiency. It scans the list twice though, so if you > >> start doing this with really big lists, it might be better to do > >> something like: > > > > On average, it'll only have go through half the list twice, as it can > > break as soon as it finds the item it searches for. Don't think you can > > get any more efficient than that. > > Sure you can: > > a_index = None > d_index = None > for i, item in enumerate(L): > if item == 'A': > a_index = i > elif item == 'D': > d_index = i > if a_index is not None and d_index is not None: > break > > if a_index < d_index: > # do some stuff > > That goes through exactly the minimum number of elements. But it's > probably slower than two .index() calls since .index() is coded in C. > But timeit and see if you're interested. On my PC, calling index twice is 5 times faster. But let's just pretend for a moment Python code was compiled and optimized as well as C code. Then, this function would still not be faster than calling index twice. You seem to be under the impression that "looping through a list" takes a lot of time and "comparing items" takes none. The reverse is the case: Looping through a list means the processor has to do something like "increment an index" once for every item in the list (for optimized C code! Looping through a generator like the one returned by enumerate in interpreted code is a lot more complex). Comparing two items on the other hand involves (as lists aren't statically typed) looking up a cmp-method dynamically, calling it dynamically, and, of course, a string comparison, which again involves two pointer lookups for every character that has to be compared. So, if you want to know how fast your function will be, you'll have to count the number of comparisons. (Of course, we're ignoring the fact that smaller functions tend to run quicker due to cache-, branch-prediction and optimization-effects) If you call your function with the list ['C', 'A', 'D', 'B'], it will compare the first item 'C' to 'A' and 'D', than the second, 'A' to 'A' and the third 'D' to 'A' and 'D', so it'll have to do 5 comparisons, correct? If you call index to find the first occurence of the item 'A' in the same list, it will have to do 2 comparisons (it can break as soon as it finds the iten), and 3 comparisons are needed for finding the item 'D', so it'll do 5 comparisons, too. Plus, you have a small overhead for comparing "a_index" and "d_index" to none (which is faster than a sting-comparison, but still takes time, probably more time than incrementing an index for looping through a list). Things get even worse if 'A' and 'D' aren't "neighbors" in the list, because than all the items bewteen 'A' and 'D' will have to be compared to 'A' and 'D' in the version above, but only to 'D' if you call index twice. So, the function above isn't only less readable, it's also slower on the average case. You might however be able to tweak the performance of the index-version a little bit if you call it only on small chunks of the array at a time, using the index()-versions that take a start- and stop index, so the whole list only has to be moved from the memory to the cache once. But I'm not sure the performance is memory-bound at all (always hard to tell in an interpreted language). > > > >> >>> L = ['C', 'A', 'D', 'B'] > >> >>> positions = dict((item, i) for i, item in enumerate(L)) > >> >>> positions > >> {'A': 1, 'C': 0, 'B': 3, 'D': 2} > >> >>> positions['A'] < positions['D'
Re: best way to determine sequence ordering?
I V wrote: > On Fri, 28 Apr 2006 14:27:00 -0700, nikie wrote: > > Steven Bethard wrote: > > > >> >>> L = ['C', 'A', 'D', 'B'] > >> >>> positions = dict((item, i) for i, item in enumerate(L)) > > Do you need the generator expression here? dict(enumerate(L)) should be > equivalent, no? I think the generator is needed to swap the item and the index. dict(enumerate(L)) would yield a dict like {0:'C', 1:'A'...} > > Isn't this bound to be less efficient? I mean, inserting the items into > > a dict is probably O(n log n), which is definitely worse than O(n) for > > searching the list twice. And, of course, it would yield different > > results if 'A' or 'D' are in the list more than once. > > Although presumably the dict method might be quicker if you were comparing > the positions a large number of times. Only if you build the dict once, but called index each and every time, which is comparing apples with oranges... > Incidentally, does python have a built-in to do a binary search on a > sorted list? Obviously it's not too tricky to write one, but it would be > nice if there was one implemented in C. I once read in an algorithm book that it took 10 years from the first binary search publication until a correct version was published, so, it actually is a bit tricky... Better stick to the bisect module. Don't know if it's written in C, though. -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to determine sequence ordering?
Steven Bethard wrote: > John Salerno wrote: > > If I want to make a list of four items, e.g. L = ['C', 'A', 'D', 'B'], > > and then figure out if a certain element precedes another element, what > > would be the best way to do that? > > > > Looking at the built-in list functions, I thought I could do something > > like: > > > > if L.index('A') < L.index('D'): > > # do some stuff > > This is probably a pretty reasonable approach as long as you're not too > worried about efficiency. It scans the list twice though, so if you > start doing this with really big lists, it might be better to do > something like: On average, it'll only have go through half the list twice, as it can break as soon as it finds the item it searches for. Don't think you can get any more efficient than that. > >>> L = ['C', 'A', 'D', 'B'] > >>> positions = dict((item, i) for i, item in enumerate(L)) > >>> positions > {'A': 1, 'C': 0, 'B': 3, 'D': 2} > >>> positions['A'] < positions['D'] > True Isn't this bound to be less efficient? I mean, inserting the items into a dict is probably O(n log n), which is definitely worse than O(n) for searching the list twice. And, of course, it would yield different results if 'A' or 'D' are in the list more than once. > If you care about memory in addition to speed, you could do something like: > > >>> L = ['C', 'A', 'D', 'B'] > >>> items_to_compare = ['A', 'D'] > >>> positions = dict( > ... (item, i) > ... for i, item in enumerate(L) > ... if item in items_to_compare > ... ) > >>> positions > {'A': 1, 'D': 2} > >>> positions['A'] < positions['D'] > True Did you measure that? Is this really faster than using index twice, for big lists? The number of comparisons (when dealing with strings, that's probably what'll take the time) should be about twice as high as for the index-version of the OP (assuming the items only exactly once in the list). -- http://mail.python.org/mailman/listinfo/python-list
Re: Events in Python?
[EMAIL PROTECTED] wrote: > Here is another non-pythonic question from the Java Developer. (I beg > for forgiveness...) > > Does Python have a mechanism for events/event-driven programming? > > I'm not necessarily talking about just GUIs either, I'm interested in > using events for other parts of an application as well. > > If there isn't some sort of event mechanism built into Python, where > might I find some information about implementing one? Maybe I got your question wrong, but why not simply use something like: class Sender: def __init__(self, event): self.event = event def raiseEvent(self): self.event("Event") class Receiver: def receiveEvent(self, msg): print "Received %r" % msg r = Receiver() s = Sender(r.receiveEvent) s.raiseEvent() You can pass around functions and bound methods, I always found that's often a really good substitute for full-blown event mechanisms. -- http://mail.python.org/mailman/listinfo/python-list
Re: Best way to have a for-loop index?
Roy Smith wrote: > In article <[EMAIL PROTECTED]>, > [EMAIL PROTECTED] wrote: > > > I write a lot of code that looks like this: > > > > for myElement, elementIndex in zip( elementList, > > range(len(elementList))): > > print "myElement ", myElement, " at index: ",elementIndex > > > > > > My question is, is there a better, cleaner, or easier way to get at the > > element in a list AND the index of a loop than this? > > > > TIA, > > Andrew > > The real question is *why* do you want the index? > > If you're trying to iterate through list indicies, you're probably trying > to write C, C++, Fortran, Java, etc in Python. Interesting. I just wrote some tools today that parse through a bunch of logfiles and print out something like: "unmatched memory allocation in line XXX", or something like that. All of them have a main loop like this: for lineNumber, line in enumerate(file("some.log")): ... I don't think there's anything wrong with that, is there a better way to do it? Personally, I don't think "enumerate" would be there if it always encouraged an "unpythonic" programming style. But then again, I'm not dutch, so I couldn't tell... ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Identifying filled circles in a scanned image
Douglas Douglas schrieb: > Hi everybody. > > I have a paper form that I scan into an image. My user fills some circles in > this paper form using black ink. Every form has ten rows with five circles > each > and the user fills only one circle for each row. > > I was wondering if I could use the Python Imaging Library to process these > forms. I know the Image class is really powerful, but I can't think of a way > of > how to locate wich circle was filled. > > Could anybody please give me an advice on this? Depends on how much time you have... Google for "Hough Transformation", or look it up in a text book on image processing. That's a very general method for finding lines and circles. If you know where the circles are, comparing the average grey value inside the circle with the average of the environment should tell you if it's filled or not. -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL ImageDraw line not long enough
Bob Greschke wrote > I've resorted to actually drawing all of the characters of the alphabet on a > graph to avoid having to drag around font files. It's mostly just uppercase > characters, so it's not too bad. > > But I noticed that some of the line segments have to be extended one pixel > longer than they should be in order for the last pixel to show up. > > The character cells are 6 pixels wide by 8 pixels high. An "L" is drawn > with > >Graph.line((x,y, x,y+7, x+5,y+7), Color) > > where the starting x and y are supplied to the function. An L works OK, but > to get a "T" to look right I have to do > >Graph.line((x+1,y, x+5,y), Color) >Graph.line((x+3,y, x+3,y+8), Color) > > I have to extend the vertical line to y+8, instead of y+7 to get the line > segment to be drawn long enough. This is on Linux, Solaris, 2.x versions of > Python, 1.1.5 version of PIL, and on Windows with the latest of everything. > Am I missing a setting somewhere? Some drawing APIs (e.g. Windows GDI) draw lines including the starting point but excluding the end point: I think the reason is that this way, if you draw a series of connected lines (like in your "L" case), no pixel gets drawn twice (this is important if you use colors with alpha channels, or line patterns). -- http://mail.python.org/mailman/listinfo/python-list
Re: Find similar images using python
> How can I use python to find images that looks quite similar? Thought > I'd scale the images down to 32x32 and convert it to use a standard > palette of 256 colors then compare the result pixel for pixel etc, but > it seems as if this would take a very long time to do when processing > lots of images. > > Any hint/clue on this subject would be appreciated. A company I used to work for has been doing research in this area (finding differences between images) for years, and the results are still hardy generalizable, so don't expect to get perfect results after a weekend ;-) I'm not sure what you mean by "similar": I assume for the moment that you want to detect if you really have the same photo, but scanned with a different resolution, or with a different scanner or with a digital camera that's slightly out of focus. This is still hard enough! There are many approaches to this problem, downsampling the image might work (use supersampling!), but it doesn't cover rotations, or different borders or blur..., so you'll have to put some additional efforts into the comparison algorithm. Also, converting the images to a paletted format is almost definitly the wrong way - convert them to greyscale, or work on 24 bit (RGB or HSV). Another approach that you might try is comparing the image histograms: they aren't affected by geometric transformations, and should still contain some information about the original image. Even if they aren't sufficient, they might help you to narrow down your search, so you have more processing time for advanced algorithms. If you have performance problems, NumPy and Psyco might both be worth a look. -- http://mail.python.org/mailman/listinfo/python-list
Re: encode short string as filename (unix/windows)
> want to encode/decode an arbitrary short 8-bit string as save filename. > is there a good already builtin encoding to do this (without too much > inflation) ? or re.sub expression? > > or which characters are not allowed in filenames on typical OS? On Windows, / \ : * ? " < > | are forbidden, and the name can't be empty. Using urlsafe_b64encode/...decode should work on any platform. -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL & image size reduction script
Philippe Martin wrote: > PS: where can I find those extra parameters in the doc (ex: quality) ... I > must be blind. In the http://www.pythonware.com/library/pil/handbook/formats.htm page. Look in the "JPEG" section. Different file formats support different options. -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL & image size reduction script
Philippe Martin wrote: > Hi, > > Thanks to the NG, I got the script hereunder working. > > 1) I am not certain that the call to convert does much (checking the doc) I think you only need it if your source image comes in a format that can't be stored into a jpeg file (e.g. 8-bit paletted). You'll need that if you're converting from GIF files, for example. It shouldn't hurt otherwise. > 2) Can this be improved as far as the final image size in (X,Y) ? I'm not sure if I get you: You tell the image object it's new (X,Y)-size in the resize method, don't you? > For instance, passing a large .jpg with a target byte size of 7000, I get > final (X,Y) results around (213, 174) ... but might want to strech it a bit > while keeping the byte size. If I got you right, you want to compress the image to a certain file size. Maybe you should try optimizing the additional save parameters for the jpeg encoder. (http://www.pythonware.com/library/pil/handbook/formats.htm). Try reducing the "quality" parameter. l_image.save(l_tmp_file_name, quality=25) Hope this helps. Niki -- http://mail.python.org/mailman/listinfo/python-list
Re: Python float representation error?
> try running this in python: > > print [39.95] > > the output i get is: > [39.953] > > what's up with that? That's perfectly normal - some numbers aren't exactly expressible as binary floating points, just like 1/3 isn't expressible as a decimal floating point number. Here's a good link on floating point arithmetic: http://docs.sun.com/source/806-3568/ncg_goldberg.html -- http://mail.python.org/mailman/listinfo/python-list
Re: using "else: interactive programme
I guess the indention got mixed up in your post, if I try to run it, I get error messages at each of the "else:" lines, because they're mis-indented. "else" should be indented like the "if" it belongs to, e.g (using underscores for spaces): if s=='10': print "well done" else: print "something else" Make sure you're using tabs/spaces consistently across the whole file, too. If I indent the else clauses correctly, I can run your script and it behaves the way you expeced. -- http://mail.python.org/mailman/listinfo/python-list
Re: Linear regression in NumPy
> The version I have in front of me is a bit shorter, 252 pages, but describes > polyfit in section 5.3 on page 91 along with the other polynomial functions. > lstsq (linear_least_squares is a backwards-compatibility alias that was > recently > moved to numpy.linalg.old) is described in section 10.1 on page 149. Oops, sorry, shouldn't have posted before reading the whole document... You are right, of course, both functions are explained. I wonder why the acrobat's search function doesn't work, though. -- http://mail.python.org/mailman/listinfo/python-list
Re: Linear regression in NumPy
Robert Kern wrote: > Both functions are described in the full book. Were you just looking at the > sample chapter? No, I've got the full PDF by mail a few days ago, "numpybook.pdf", 261 pages (I hope we're talking about the same thing). I entered "linear_least_squares" and "polyfit" in acrobat's "find text" box, but neither one could be found. -- http://mail.python.org/mailman/listinfo/python-list
Re: Linear regression in NumPy
Although I think it's worth reading, it only covers the fundamental structure (what arrays are, what ufuncs are..) of NumPy. Neither of the functions dicussed in this thread (polyfit/linear_least_squares) is mentioned in the file. -- http://mail.python.org/mailman/listinfo/python-list
Re: Linear regression in NumPy
Thank you! THAT's what I've been looking for from the start! -- http://mail.python.org/mailman/listinfo/python-list
Re: Linear regression in NumPy
I still don't get it... My data looks like this: x = [0,1,2,3] y = [1,3,5,7] The expected output would be something like (2, 1), as y[i] = x[i]*2+1 (An image sometimes says more than 1000 words, so to make myself clear: this is what I want to do: http://www.statistics4u.info/fundstat_eng/cc_regression.html) So, how am I to fill these matrices? (As a matter of fact, I already wrote the whole thing in Python in about 9 lines of code, but I'm pretty sure this should have been possible using NumPy) -- http://mail.python.org/mailman/listinfo/python-list
Linear regression in NumPy
I'm a little bit stuck with NumPy here, and neither the docs nor trial&error seems to lead me anywhere: I've got a set of data points (x/y-coordinates) and want to fit a straight line through them, using LMSE linear regression. Simple enough. I thought instead of looking up the formulas I'd just see if there isn't a NumPy function that does exactly this. What I found was "linear_least_squares", but I can't figure out what kind of parameters it expects: I tried passing it my array of X-coordinates and the array of Y-coordinates, but it complains that the first parameter should be two-dimensional. But well, my data is 1d. I guess I could pack the X/Y coordinates into one 2d-array, but then, what do I do with the second parameter? Mor generally: Is there any kind of documentation that tells me what the functions in NumPy do, and what parameters they expect, how to call them, etc. All I found was: "This function returns the least-squares solution of an overdetermined system of linear equations. An optional third argument indicates the cutoff for the range of singular values (defaults to 10-10). There are four return values: the least-squares solution itself, the sum of the squared residuals (i.e. the quantity minimized by the solution), the rank of the matrix a, and the singular values of a in descending order." It doesn't even mention what the parameters "a" and "b" are for... -- http://mail.python.org/mailman/listinfo/python-list