Re: [Python-Dev] PEP 350: Codetags
On 9/27/05, Phillip J. Eby <[EMAIL PROTECTED]> wrote: > At 03:35 PM 9/26/2005 -0700, Micah Elliott wrote: > >Please read/comment/vote. This circulated as a pre-PEP proposal > >submitted to c.l.py on August 10, but has changed quite a bit since > >then. I'm reposting this since it is now "Open (under consideration)" > >at <http://www.python.org/peps/pep-0350.html>. > > My suggestion: implement some tools, use them for a while, and come back > with more focused use cases to show why only this format can work, and why > the Python core developers should therefore use it. I'm not saying that > you can't have an informational PEP unless it should be used in the stdlib, > mind you. Just pointing out that if you can't convince the core developers > it's useful, I'm thinking you'll have a hard time convincing the community > at large to actually use it. You need to actually have a better mousetrap > to present before you ask people to move their cheese. :) > +1 I agree with PJE almost entirely. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Will python never intend to support private, protected and public?
> ___ > class a: > i=0 > def setI(iii): > if self.i!=iii: > self.i=iii > #do some extra works here, e.g, notify the observers that > #this property is changed, or do some logging things. > ___ > In the class "a" above, when "i" is changed, I will do some extra works, > the extra works could be very import, so I want to keep i invisible > to some others, they can only change i by the method setI. But python > can't ensure i to be invisible, everyone can change it whenever they > want! This is dangerous. > >>> class test(object): ... __i = 0 ... def incr(self, n): self.__i += 1; print "incremented i" ... def geti(self): print "got i"; return self.__i ... i = property(geti, incr) ... >>> t = test() >>> t.i got i 0 >>> t.i += 5 got i incremented i >>> t.i got i 1 >>> dir(t) ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash_ _', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr_ _', '__setattr__', '__str__', '__weakref__', '_test__i', 'geti', 'i', 'incr'] >>> >>> #here's how the crazy hackers subclassing your code can break your super ... #special private variable! ... >>> t._test__i += 6 >>> t.i got i 7 But, if your users can't figure out that they shouldn't be changing the variable called t._test__i without expecting side effects, what do you think of the users of your class? Python is for consenting adults. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Will python never intend to support private, protected and public?
Error correction time! > >>> #here's how the crazy hackers subclassing your code can break your super > ... #special private variable! > ... That should be "using your code" not "subclassing your code". D'oh! Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: A Moronicity of Guido van Rossum
> But, this post of his shows [Guido's] haughtiness +1 IQOTW (Ironic Quote Of The Week. Thanks for the laughs, Xah) Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: grouping array
On 29 Sep 2005 10:01:40 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > hi if I have an array > > say x = [[2,2,0,0,1,1], > [1,1,0,0,1,1], > [1,1,0,0,1,1]] > I basically want to group regions that are non zero like I want to get > the coordinates of non zero regions..as (x1,y1,x2,y2) > [(0,0,2,1),(0,4,2,5)] which show the top left(x1,y1) and bottom > right(x2,y2) corners of each group.hope i am clear. > I don't understand. Could you give some inputs with expected outputs and some explanation? Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: python's performance
You've gotta framinate your capacitor to speed it up. (Translated: With no information about your camera, memory card, type of connection, platform, method of access, version of python, version of PIL, or code, how in the world could I help you to diagnose your loosely-specified problem? Ask something that's answerable, and we'll try to help you.) Peace Bill Mill bill.mill at gmail.com On 9/29/05, James Hu <[EMAIL PROTECTED]> wrote: > Hi, > > I used python and PIL to capture image from a digital camera, > It seems like it took more than 1 second to capture a 1280x1024 image, > however, the demo capturing application from the company (coded by VB) > took only .2s or less for one image with the same size. > Don't know why python and PIL is so slow, any idea to improve the > performance? Thanks a lot! > > James > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: python's performance
> On 9/29/05, James Hu wrote: > > Hi, > > > > I used python and PIL to capture image from a digital camera, > > It seems like it took more than 1 second to capture a 1280x1024 image, > > however, the demo capturing application from the company (coded by VB) > > took only .2s or less for one image with the same size. > > Don't know why python and PIL is so slow, any idea to improve the > > performance? Thanks a lot! > > > > James > > > > -- > > http://mail.python.org/mailman/listinfo/python-list > > > > Bill Mill wrote: > You've gotta framinate your capacitor to speed it up. > > (Translated: With no information about your camera, memory card, type > of connection, platform, method of access, version of python, version > of PIL, or code, how in the world could I help you to diagnose your > loosely-specified problem? Ask something that's answerable, and we'll > try to help you.) > > Peace > Bill Mill > bill.mill at gmail.com > On 9/29/05, James Hu <[EMAIL PROTECTED]> wrote: > Thanks for your fast reply. > > Camera, HAMAMATSU C4742-95-12G04, IEEE1394-based, > Can capture frame at 8.8/s at full resolution 1344X1024 > Memory: 512M > Platform: Win2K and DELL 2.0GHz P4 > Python 2.4 > PIL: 1.15 > > Have u been used such camera with PIL before? > > im_1= Image.fromstring("I", datasize, buf, 'raw', 'I;16') > > Your help would be greatly appreciated! > > James > Where do datasize and buf come from? Have you profiled your application to make sure that it is the Image.fromstring line which is slowing down your code? (If you don't know how to profile, just ask) Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: A Moronicity of Guido van Rossum
On 9/29/05, Tim Leslie <[EMAIL PROTECTED]> wrote: > On 29 Sep 2005 07:24:17 -0700, Xah Lee <[EMAIL PROTECTED]> wrote: > > > Of course, you begin to write things like Java, in three thousand words > > just to state you are a moron. > > > > > > +1 QOTW. > > Tim > -1 XLEGQOTW (Xah Lee Ever Getting QOTW'd) Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: python server
On 7 Nov 2005 10:22:18 -0800, linuxpld <[EMAIL PROTECTED]> wrote: > Hello > > I`m writing a program (server in future) in python. > I would like to write it in such a way that I will be able to write gui > in any language and connect to my python program and use functionality > included with it. > are there any libraries that I could use? Lots...Perhaps you should start off in understanding your problem by learning about sockets: http://www.amk.ca/python/howto/sockets/ Sockets are a generalized method for passing data between programs on the same or different computers. > > I dont know if i wrote it understandably but maybe picture will explain > it: > > || > | python | > || <- module in python -> <- connection ("???") -> <-gui in > any language (java, c++, python, etc). > | server | > |---| > what could I use as "???"? http? mqseries? webservices? what are the > possibilites? http, webservices, xmlrpc, corba, people to type in messages between programs, carrier pigeons, and sockets are all possibilities. I'm gonna recommend that you learn sockets, because they're a general solution to this problem, and many methods of inter-program communication are based on sockets, but I don't know the specifics of the program you're designing. How tightly will the client and server interact? In what environment (ie over the internet, over a corporate LAN, on the same computer, on Internet2)? Is there any framework in existance, or is this program being written from scratch? Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: PYTHON LOOSING FOR JAVA???????
On 11/7/05, "Martin v. Löwis" <[EMAIL PROTECTED]> wrote: > Fcamattti wrote: > > Hello for everybody > > > > So I have a doubt. I'd like to know what do you think about the joint > > of efforts of Sun Microsystems and the Google to create a office web > > based. I sincerely enjoy the idea althoug I'd like to know what will be > > the future of this wonderful language called Python?? > > I think I know???? Although, the future is difficult to predict??? +1 QOTW Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python obfuscation
On 10 Nov 2005 08:40:17 -0800, Ben Sizer <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > > This is (a minor) one of the many reasons that make webservices the way > > of the future (hey, even *MSFT* noticed that recently, it seems...). > > But they are not suitable for all applications, and probably never will > be. > Your only solution, then, is to write unpopular code. Because, as Alex said, it will otherwise be broken into. Let's look at two very popular pieces of code: Half-Life 2 and Windows XP. How are they secured? Previous version of these software products used sophisticated client-side programming to try and be secure, but the security was nonexistant. Users share keys and cracks with each other. Now, both of these programs require verification (phone and/or web) to be used. The only truly secure method of assuring that they're not used in ways you don't intend is to require the user to contact you to use it, and that's a deal with the devil. One you might need to make if security is that important to you, as Microsoft and Valve have decided it is, but it's a deal with the devil nonetheless. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Default method arguments
On 15 Nov 2005 08:03:26 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hello everybody! > I have little problem: > > class A: > def __init__(self, n): > self.data = n > def f(self, x = ) > print x > > All I want is to make self.data the default argument for self.f(). (I > want to use 'A' class as following : > > myA = A(5) > myA.f() > > and get printed '5' as a result.) > class A: def __init__(self, n): self.data = n def f(self, x=None): if not x: x = self.data print x >>> myA = A(5) >>> myA.f() 5 Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Default method arguments
On 11/15/05, Nicola Larosa <[EMAIL PROTECTED]> wrote: > > def f(self, x=None): > > if not x: > > Ha! You fell for it! ;-D > (Hint: what about x being passed with a value of zero? :-) ) I wasn't sure if you saw my post before you posted - good call. I just tossed off an answer without thinking much, and we see the result. It could have been a good debugging lesson for him if he'd tried to pass 0; I think I'll use that as my excuse. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Zen of Python
The example that occurs to me is that "import smtplib" is better than "import stdlib.inet.services.smtp". Peace Bill Mill bill.mill at gmail.com On Wed, 19 Jan 2005 14:13:47 -0500, Timothy Fitz <[EMAIL PROTECTED]> wrote: > While I agree that the Zen of Python is an amazingly concise list of > truisms, I do not see any meaning in: > > Flat is better than nested. > > I strive for balance between flat and nested. Does anyone have a good > example of where this is applied? (specifically to python, or in > general) > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: a question
Nader, You've got a couple problems. First, you need to end the string before putting a continuation in. Secondly, you have 6 variables to be substituted and only provide 4. Here's some code, edited to show how to use continutations to join strings: >>> mosbin, jaar, filetype = (1,1,1) >>> cmd = '%s/mos user wmarch, cd /fa/wm/%s/%s, mkdir %s, put %s'\ ... '%s' % (mosbin, jaar, filetype, filetype, filetype, filetype) >>> cmd '1/mos user wmarch, cd /fa/wm/1/1, mkdir 1, put 1, chmod 6441' Peace Bill Mill bill.mill at gmail.com On Wed, 19 Jan 2005 16:16:32 +, Nader Emami <[EMAIL PROTECTED]> wrote: > L.S., > > I have a long command in Unix and I have to use os.system(cmd) > statement. I do the following: > > cmd = '%s/mos user wmarch, cd /fa/wm/%s/%s, mkdir %s, put %s, chmod 644 > %s' % (mosbin, jaar, filetype, filetype) > status = os.system(cmd) > > This is not very clear, and I have to break this long line in two > segment by means of the next character '\' : > cmd = '%s/mos user wmarch, cd /fa/wm/%s/%s, mkdir %s, put %s, \ > chmod 644 %s' % (mosbin, jaar, filetype, filetype) > > But in this case I get a syntax error! I don't know how I can solve this > problem. Could somebody tell me about this? > > With regards, > Nader > > (this > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: a question
You are correct, sir. Didn't know you could do that. Neato. Peace Bill Mill bill.mill at gmail.com On Wed, 19 Jan 2005 22:10:05 +0100, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > Bill Mill wrote: > > > You've got a couple problems. First, you need to end the string before > > putting a continuation in. > > >>> "no\ > ... pe" > 'nope' > > >>> "however\ > File "", line 1 > "however\ > ^ > SyntaxError: EOL while scanning single-quoted string > > (in the second case, the ^ is trying to point out that I added > some whitespace after the backslash) > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: list item's position
2 solutions: In [98]: bars = ["str", "foobaz", "barbaz", "foobar"] In [99]: for bar in bars: : if 'bar' in bar and 'baz' in bar: : print bar : print bars.index(bar) : barbaz 2 In [100]: for i in range(len(bars)): .: if 'bar' in bars[i] and 'baz' in bars[i]: .: print bars[i] .: print i .: barbaz 2 The first one is slow and pretty, the second one is fast and (a bit) ugly. I believe that you should avoid range(len(x)) when you can, but use it when you need to know the index of something without an additional x.index() call. Peace Bill Mill bill.mill at gmail.com On Wed, 19 Jan 2005 22:04:44 -0500, Bob Smith <[EMAIL PROTECTED]> wrote: > Hi, > > I have a Python list. I can't figure out how to find an element's > numeric value (0,1,2,3...) in the list. Here's an example of what I'm doing: > > for bar in bars: > if 'str_1' in bar and 'str_2' in bar: >print bar > > This finds the right bar, but not its list position. The reason I need > to find its value is so I can remove every element in the list before it > so that the bar I found somewhere in the list becomes element 0... does > that make sense? > > Thanks, > > Bob > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Distutils: blurring the file==module borders
read this thread, it should help you: http://mail.python.org/pipermail/tutor/2005-January/035124.html Peace Bill Mill bill.mill at gmail.com On Tue, 25 Jan 2005 02:15:58 +, Frans Englich <[EMAIL PROTECTED]> wrote: > > Hello all, > > Due to the size of my source, I want to split it up into multiple > files(basically one class in each file), but then I have difficulties with > the directory layout when the modules are installed with distutils. > > This is my file layout: > > in ./ I have a setup.py which has 'packages="foo"' > > in ./foo/ I have an __init__.py and a handful of files named ClassA.py, > ClassB.py, ClassC.py and so forth. > > The problem is that when installed, in order to reach, say, classB, I need to > do: > > import foo.ClassA > > var = foo.ClassA.ClassA() > > while I want to do var = foo.ClassA() > > In other words, the result I want can be achieved by putting all code in > __init__.py. The problem is that I would find it horrible to have all code in > one file. > > Python have this one-to-one relationship between modules and files; can what I > want somehow be achieved? > > Cheers, > > Frans > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Dynamic class methods misunderstanding
Hello all, I have a misunderstanding about dynamic class methods. I don't expect this behavior: In [2]: class test: ...: def __init__(self, method): ...: self.method = method ...: self.method() ...: In [3]: def m(self): print self ...: In [4]: test(m) --- exceptions.TypeError Traceback (most recent call last) /cygdrive/c/Documents and Settings/Wmill/ /cygdrive/c/Documents and Settings/Wmill/ in __init__(self, method) TypeError: m() takes exactly 1 argument (0 given) - Why doesn't m get the implicit self parameter in the self.method() call? How would I make it a proper member of the class, so that a self.method() call would work with the above "m" function? Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic class methods misunderstanding
On 28 Jan 2005 15:41:49 GMT, F. Petitjean <[EMAIL PROTECTED]> wrote: > Le Fri, 28 Jan 2005 10:20:30 -0500, Bill Mill a écrit : > > Hello all, > > > > I have a misunderstanding about dynamic class methods. I don't expect > > this behavior: > > > > In [2]: class test: > >...: def __init__(self, method): > >...: self.method = method > >...: self.method() > >...: > > > > In [3]: def m(self): print self > >...: > > > > In [4]: test(m) > > --- > > exceptions.TypeError Traceback (most recent > > call > > last) > > > > /cygdrive/c/Documents and Settings/Wmill/ > > > > /cygdrive/c/Documents and Settings/Wmill/ in __init__(self, method) > > > > TypeError: m() takes exactly 1 argument (0 given) > > - > > > > Why doesn't m get the implicit self parameter in the self.method() > > call? How would I make it a proper member of the class, so that a > > self.method() call would work with the above "m" function? > The "def m(self):" was not properly indented. So here, "m" is a module level > function, not a method of your class. I know this; I should have been clearer. I want to define a function outside of a class, then pass it to a class and make it a method of that class. > > > > Peace > > Bill Mill > > bill.mill at gmail.com > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic class methods misunderstanding
Diez, On Fri, 28 Jan 2005 16:57:37 +0100, Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > > > > Why doesn't m get the implicit self parameter in the self.method() > > call? How would I make it a proper member of the class, so that a > > self.method() call would work with the above "m" function? > > Use new.instancemethod: > > import new > > class Test: > def __init__(self, method): > self.m = new.instancemethod(method, self, Test) > Beautiful! thank you very much. Looking into the "new" module in python 2.4, that's equivalent to: self.m = type(self.__init__)(method, self, Test) I didn't know that you could call types to create another type. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic class methods misunderstanding
Hans, On Fri, 28 Jan 2005 11:09:16 -0500, Hans Nowak <[EMAIL PROTECTED]> wrote: > > m is a function. When you assign it to self.method, it's still a > function. You don't create a new method that way; all you have is a new > attribute called 'method' containing the function. > I figured as much; I just didn't know how to add it as a method. > To add m as a new method to the *class*, do this: > > >>> class test: > ... def __init__(self, method): > ... self.__class__.method = method > ... self.method() > ... > >>> def m(self): print self > ... > >>> test(m) > <__main__.test instance at 0x0192ED78> > <__main__.test instance at 0x0192ED78> When I run it, I only get one call to m, which is how I would expect python to work; I assume the double printing here is a typo? > >>> > > To add m as a new method to the *instance*, use new.instancemethod, as > Diez B. Roggisch already pointed out. > Thanks, you helped me understand it a lot. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic class methods misunderstanding
Kamilche, On Fri, 28 Jan 2005 08:10:07 -0800 (PST), Kamilche <[EMAIL PROTECTED]> wrote: > I see what you're attempting to do. However, your code, if it DID run, > would result in a method being added to the object, not the object's > class! Modify the class itself, not the object, as follows: > > |class Test: > |def __init__(self): > |self.method() > | > |def m(self): > |print self > | > |setattr(Test, 'method', m) > |Test() > beautiful; so this appears to be equivalent to the __class__ method that Hans suggested. Thanks a lot. Peace Bill Mill bill.mill at gmail.com > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic class methods misunderstanding
On Fri, 28 Jan 2005 11:59:50 -0500, Hans Nowak <[EMAIL PROTECTED]> wrote: > Bill Mill wrote: > > > On Fri, 28 Jan 2005 11:09:16 -0500, Hans Nowak <[EMAIL PROTECTED]> wrote: > > > > > >>To add m as a new method to the *class*, do this: > >> > >> >>> class test: > >>... def __init__(self, method): > >>... self.__class__.method = method > >>... self.method() > >>... > >> >>> def m(self): print self > >>... > >> >>> test(m) > >><__main__.test instance at 0x0192ED78> > >><__main__.test instance at 0x0192ED78> > > > > > > When I run it, I only get one call to m, which is how I would expect > > python to work; I assume the double printing here is a typo? > > Actually, no. I'm using the interactive interpreter, so doing test(m) > results in two lines: the first one is printed by m, the second one is > the __repr__ of the test instance that was created, displayed by the > interpreter. Compare: > > >>> x = test(m) > <__main__.test instance at 0x0192ED78> > >>> x > <__main__.test instance at 0x0192ED78> > d'oh; that's how I ran it. Thanks a lot. > -- > Hans Nowak > http://zephyrfalcon.org/ > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic class methods misunderstanding
On Fri, 28 Jan 2005 14:41:16 -0500, Terry Reedy <[EMAIL PROTECTED]> wrote: > > "Kamilche" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >I see what you're attempting to do. However, your code, if it DID run, > > would result in a method being added to the object, not the object's > > class! Modify the class itself, not the object, as follows: > > > > |class Test: > > |def __init__(self): > > |self.method() > > | > > |def m(self): > > |print self > > | > > |setattr(Test, 'method', m) > > # this is a longwinded way to say > Test.method = m That is the blindingly simple method that I wanted. I didn't know before that I wanted it, but I'm sure of it now. Thank you very much, terry. > > setattr is for when you do *not* know the attribute name at coding time but > will have it in a string at run time, as in > > methodname = 'method' > ..# some time later > setattr(Test, methodname, m) > > Sometime Python makes things easier than people are initially willing to > believe ;-) I felt like there had to be a simpler solution. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Java Integer.ParseInt translation to python
Jose, On Mon, 31 Jan 2005 19:23:35 -0500, jose isaias cabrera <[EMAIL PROTECTED]> wrote: > > Greetings! > > I've looked through the internet (not long, though) but I have not been able > to find a python translation to > > buffer[0] = (byte)Integer.parseInt(string,16); Tell me what this does, give me an example or two, and I'll translate it for you. I don't feel like going to read the java docs to figure it out. You probably want to look at the built-in int() function though. Type help(int) at the python prompt. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: Syntax error
Chad, try "elif tries == 2" or just "else:". You are not allowed to put an expression after an else statement. I recommend you read the python tutorial at http://docs.python.org/tut/tut.html . Peace Bill Mill bill.mill at gmail.com On Fri, 4 Feb 2005 12:49:51 -0600, Chad Everett <[EMAIL PROTECTED]> wrote: > Hi Everyone, > > I am new to Python and programming in general. I bought the book "Python > Programming for the Absolute Beginner" by michael Dawson. > > I have been working through it but am having trouble. > I am trying to make a coin flip program and keep geting a Synax Error > "invalid syntax". > > If anyone has a moment could you please look at it and tell me what I am > doing wrong. > > thanks for your time and patience. > > Chad > > # Coin Flip Program > # This program flips a coin 100 times and tells the number of heads and > tails. > # Chad Everett 2/3/2005 > > print "\t\tCoin Flip Game*\n" > import random > > # heads = 1 > # tails = 2 > > tries = random.randrange(2) + 1 > count = 1 > > while count != 100: > if tries == 1: > heads = 1 > count += 1 > > else tries == 2: # I AM GETTING THE SYNTAX ERROR HERE > tails = 1 > count += 1 > > print "heads: " + heads > print "tails: " + tails > > raw_input("Press enter to quit") > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: string issue
On Fri, 04 Feb 2005 14:23:36 -0500, rbt <[EMAIL PROTECTED]> wrote: > Either I'm crazy and I'm missing the obvious here or there is something > wrong with this code. Element 5 of this list says it doesn't contain the > string 255, when that's *ALL* it contains... why would it think that??? > > import time > > ips = ['255.255.255.255', '128.173.120.79', '198.82.247.98', > '127.0.0.1', '255.0.0.0', '255', '128.173.255.34'] > > for ip in ips: > if '255' in ip: > try: > print "Removing", ip > ips.remove(ip) > except Exception, e: > print e > > print ips > time.sleep(5) > You're gong crazy: >>> ips = ['255.255.255.255', '128.173.120.79', '198.82.247.98', ... '127.0.0.1', '255.0.0.0', '255', '128.173.255.34'] >>> for ip in ips: ... if '255' in ip: print ip ... 255.255.255.255 255.0.0.0 255 128.173.255.34 The problem is that you're operating in-place on an array while it's being iterated over. Since the iterator is only created once, you're can't change the array while you're iterating over it. Instead, try a list comprehension: >>> ips = [ip for ip in ips if '255' not in ip] >>> ips ['128.173.120.79', '198.82.247.98', '127.0.0.1'] Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: string issue
On Fri, 04 Feb 2005 14:43:30 -0500, rbt <[EMAIL PROTECTED]> wrote: > Steve Holden wrote: > > rbt wrote: > > > >> Either I'm crazy and I'm missing the obvious here or there is > >> something wrong with this code. Element 5 of this list says it doesn't > >> contain the string 255, when that's *ALL* it contains... why would it > >> think that??? > >> > >> import time > >> > >> ips = ['255.255.255.255', '128.173.120.79', '198.82.247.98', > >> '127.0.0.1', '255.0.0.0', '255', '128.173.255.34'] > >> > >> for ip in ips: > >> if '255' in ip: > >> try: > >> print "Removing", ip > >> ips.remove(ip) > >> except Exception, e: > >> print e > >> > >> print ips > >> time.sleep(5) > >> > >> Someone tell me I'm going crazy ;) > > > > > > You are modifying the list as you iterate over it. Instead, iterate over > > a copy by using: > > > > for ip in ips[:]: > > ... > > > > regards > > Steve > > Very neat. That's a trick that everyone should know about. I vote it > goes in Dr. Dobbs newsletter. Once you know it, it's neat, and I use it sometimes. However, it's a little too "magical" for my tastes; I'd rather be more explicit about what's going on. Peace Bill Mill bill.mill at gmail.com > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: string issue
On Fri, 04 Feb 2005 15:25:04 -0500, rbt <[EMAIL PROTECTED]> wrote: > John J. Lee wrote: > > Steve Holden <[EMAIL PROTECTED]> writes: > > [...] > > > >>You are modifying the list as you iterate over it. Instead, iterate > >>over a copy by using: > >> > >>for ip in ips[:]: > >> ... > > > > > > Just to help popularise the alternative idiom, which IMO is > > significantly less cryptic (sane constructors of mutable objects > > almost always make a copy, and list is no exception: it's guaranteed > > to do so): > > > > for ip in list(ips): > >... > > > > > > Works back to at least Python 1.5.2. > > > > > > John > > I don't know that that approach is less cryptic. ips is already a > list... it looks cryptic to make it a list again, doesn't it? IMO, the > two are equally cryptic. The epitome of clarity would be copy(ips)... > now *that* makes sense, of course, ips[:] or list(ips) work equally well > to the programmer who has learned them. Howsabout: >>> from copy import copy >>> ips = ['255.255.255.255', '128.173.120.79', '198.82.247.98', ... '127.0.0.1', '255.0.0.0', '255', '128.173.255.34'] >>> for ip in copy(ips): ... if '255' in ip: ... ips.remove(ip) ... >>> ips ['128.173.120.79', '198.82.247.98', '127.0.0.1'] But I still think that the list comprehension is the best. Peace Bill Mill bill.mill at gmail.com > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Possibly OT: Controlling winamp with Python
Brent, You could write the Python program as a proxy of the internet stream. Basically, you would point your proxy at the web stream and receive the data it sends. At the same time, you would be listening for connections on some socket on the local machine. You would then point winamp towards the local socket instead of the internet station. If you put a "mute" button on your program, pressing it could trigger your program to begin sending empty data to winamp, and resume sending it at some arbitrary point. Peace Bil Mill bill.mill at gmail.com On Fri, 4 Feb 2005 13:35:04 -0700, Brent W. Hughes <[EMAIL PROTECTED]> wrote: > The Python program won't decide whether a commercial is playing, I will. At > that point, I will run my program which will press mute, wait 20 seconds, > and then press mute again. > > Actually, I could leave the program running but minimized to the task bar. > When I hear the advertisement, I just click on the program in the task bar. > It knows what to do from there. > > Brent > > "Kartic" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > Brent, > > > > Question : how will your python script distinguish between a commercial > > and a song? > > > > I can understand if you are writing a streaming client in Python; in > > that case you can analyze the audio stream and decide if it is a > > commercial or a song/music. > > > > Did you check to see if there is already a Winamp plugin that would > > achieve this for you? > > > > Thanks, > > -Kartic > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Folding in vim
On 7/6/05, Terry Hancock <[EMAIL PROTECTED]> wrote: > On Tuesday 05 July 2005 03:53 pm, Renato Ramonda wrote: > > Why not use just spaces? Vim simplifies this immensely: > > > > set tabstop=4 > > set shiftwidth=4 > > set expandtab > > set smarttab > > set autoindent > > > > AFAICT this gives me all spaces, 4 spaces indent, tab inserts spaces and > > backspace over a block of 4 spaces deletes all of them (just like > > deleting a tab). > > Yep, this is what I just set up in my .vimrc. Works beautifully. > I don't use any of the fancy indenters; instead, I just add set foldmethod=indent to my .vimrc (_vimrc on windows), along with most of the aforementioned options (I don't like smarttab); it works nearly perfectly. Then zo opens the fold under the cursor one level, zO opens it recursively, zc and zC close it non- and recursively. zr opens all folds one level, zR opens them all recursively, zm closes them all one level, and zM closes them all recursively. It's pretty sweet. Maybe we should have a big Vim-python tip-a-thon thread? Peace Bill Mill [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Scipy - Latex Annotations in plots
> Robert Kern wrote: > > > fortuneteller wrote: > >> Hello, > >> > >> I'm quite new to python and Scipy. > >> Anyway I want to use it to plot graphs. > >> Does anybody know if there is the possibility to use Latex in SciPy's > >> plotting functions like gplt? > > > > I don't believe so. matplotlib, however, does have this functionality in > > recent releases. On 7/6/05, Matthias R. <[EMAIL PROTECTED]> wrote: > Unfortunately matplotlib is only a 2D-plotting library. > > Do you know another one with 3D-capabilities as well? > That would be very nice, > Perhaps gnuplot.py (http://gnuplot-py.sourceforge.net/) will work for you? It is a thin wrapper around Gnuplot, which is very good at producing ps format images, and is capable of producing 3 dimensional graphs. Peace Bill Mill bill.mill at gmail.com PS please try to not top-post, you can lose the rest of the thread easily -- http://mail.python.org/mailman/listinfo/python-list
Re: Yet Another Python Web Programming Question
> Python using CGI, for example, was enough for him until he started > getting 500 errors that he wasn't sure how to fix. A common error is that python cgi files need line endings to be in unix text file format, not windows text file format (\n instead of \r\n) [1]. Why this is, I don't know, but it causes a lot of errors for windows folks. I'm a frequent linux/windows switcher, and it's caused me no end of troubles - if you're getting "premature end of script headers" in your apache error logs, this may be your problem. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: question on "input"
On 12 Jul 2005 07:31:47 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi, > > I want to accept the user's answer yes or no. > If I do this: > > answer = input('y or n?') Use raw_input instead: >>> answer = raw_input("y or n?") y or n?y >>> answer 'y' Check out the documentation of both functions at http://docs.python.org/lib/built-in-funcs.html for more details. Peace Bill Mill -- http://mail.python.org/mailman/listinfo/python-list
Re: String Manipulation
On 13 Jul 2005 07:49:02 -0700, Michael Jordan <[EMAIL PROTECTED]> wrote: > hey, i have this huge text file and i need to go through and remove all > punctuation and every instance of the phrase "fruitloops=$" where $ is > any number 0-100" um, and yeah this is homework but i've tried to no > avail. thanks guys. cheerio :). jen Jen, This program iterates through one file and outputs all lines to another file which have the word "homework" in them. #-- Begin program 1 file_in = file('data.in') file_out = file('data.out') for line in file_in: #line is a string containing one line of the file if "homework" in line: file_out.write("homework") #--- End program 1 Here is a program which turns a string containing the phrase "number=42" into a variable containing the integer 42: #-- Begin program 2 #create a string variable called x x = "number=42" #split the string at the '=', resulting in ['number', '42'] n = x.split('=')[1] #turn n from a string into a number, so we could test its value n = int(n) if 0 < n < 100: print "n is between 0 and 100" else: print "n is not between 0 and 100" #-- End program 2 And, finally, a program to remove punctuation from a string: # Begin program 3 import string #create a sentence with punctuation punct = "This. is a, sentence with - punctuation" #remove the punctuation; make sure the first argument #to maketrans is the same length as the second, which #should be all blanks punct = punct.translate(string.maketrans('.,-', ' ')) #read the docs at # http://docs.python.org/lib/node109.html # for more details #End program 3 Hope this helps; you should be able to put the pieces together to do what you want to do. If you can't, feel free to ask more questions. Also, just so you know, there is a list at tutor@python.org set up just to answer questions like these. Peace Bill Mill [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Programming Contest
On 7/15/05, Brian Quinlan <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > Brian> I've decided that it would be be fun to host a weekly Python > > Brian> programming contest. The focus will be on algorithms that require > > Brian> a bit of thought to design but not much code to implement. > > > > For some of us that's what we do day-in, day-out at work. It's just not > > called a contest. To make it more challenging, we sometimes leave out the > > "bit of thought" part. ;-) > > Hmmm...I find that I am rarely faced with challenging algorithmic > problems in my day-to-day work. I continuously face difficult design > decisions but that is a difficult sort of beast all together. > > This contest is for people who like thinking about algorithms. > > Cheers, > Brian > -- > http://mail.python.org/mailman/listinfo/python-list > Questions: Will that random test generator (included in the download) be used to perform the actual testing? How many tests will be run on each program? What is the penalty for a wrong answer? Peace Bill Mill PS - check out http://www.sleepinginairports.net/ before you say you can't sleep in the airport :) -- http://mail.python.org/mailman/listinfo/python-list
odd python/linux/cherrypy behavior
On my laptop, I have an NTFS partition for NT, a FAT partition for data as a dmz which both linux and NT can access, and an ext3 partition for linux. However, I've experienced some weirdness on the FAT partition, and I'm wondering if anybody can tell me why it's happening. Yesterday, I downloaded the new release of cherrypy, and stuck it on the dmz drive. I ran tutorial01, which opens up a server on port 8080 and waits for connections. All seemed well, initialization info printed out, and it said it was waiting for connections on port 8080. However, when I tried to connect to it (via firefox or telnet) it just didn't respond. Not immediately - the connection attempts timed out. I tried different ports, but that didn't change anything. A reboot into NT, run the same file, it works perfectly. Eventually, after thinking it's a hosts file problem, or a firewall problem, I figure out that if I move it to my ext3 drive, it again works perfectly. Prints out the same information, says it's waiting on 8080, but this time, I can access it. Can anybody posit a guess as to why it would behave this way? Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: odd python/linux/cherrypy behavior
On 7/16/05, Neil Hodgson <[EMAIL PROTECTED]> wrote: > Bill Mill: > > > ... a FAT partition for data as a dmz which both linux and NT can > > access ... > > Yesterday, I downloaded the new release of cherrypy, and stuck it on > > the dmz drive. ... > > Eventually, after thinking it's a hosts file problem, or a firewall > > problem, I figure out that if I move it to my ext3 drive, it again > > works perfectly. > > Have you looked at your mount options to make sure they are sane? > Possibly you have mounted with only short (truncated) file names or all > the files have their execute bit on and that is unexpected or there are > non-ASCII characters in file names or ... > Definitely not mounted with short file names, and there aren't any non-ASCIIs in the file names; in both cases I imagine that the file wouldn't run at all. In this case, however, the file does run, and open a socket, it just can't seem to receive connections on it. I have tried running the file as su, with no success. The FAT dirs are mounted with the following options: defaults,user,umask=000 . I'm not sure what you mean by the execute bit, but all files do have execute permission. Here's the output of an ls -l on the file I'm talking about: -rwxrwxrwx 1 root root 1073 2005-07-15 21:40 /d/download/cherrypy/tutorial/tut01_helloworld.py Any other ideas? Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: odd python/linux/cherrypy behavior
On 7/16/05, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On Sat, 16 Jul 2005 19:54:31 -0400, Bill Mill <[EMAIL PROTECTED]> > declaimed the following in comp.lang.python: > > > > The FAT dirs are mounted with the following options: > > defaults,user,umask=000 . I'm not sure what you mean by the execute > > bit, but all files do have execute permission. Here's the output of an > > ls -l on the file I'm talking about: > > > > -rwxrwxrwx 1 root root 1073 2005-07-15 21:40 > > /d/download/cherrypy/tutorial/tut01_helloworld.py > > > Out of curiosity, is it possible to change ownership to your > "user" account? > Thanks a lot, that worked. Any guess as to why? Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: smtplib
On 7/18/05, Alberto Vera <[EMAIL PROTECTED]> wrote: > > Hello: > > Do you know If the smtplib routine have been changed in last releases? > > I used this script: > > http://docs.python.org/lib/SMTP-example.html > > but it didn't work with the last release. > > Do you know any idea about this change? check out the CVS changelog to see what's changed with it: http://cvs.sourceforge.net/viewcvs.py/python/python/dist/src/Lib/smtplib.py?rev=1.70&view=log Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: is this pythonic?
On 7/20/05, Simon Brunning <[EMAIL PROTECTED]> wrote: > On 7/20/05, Mage <[EMAIL PROTECTED]> wrote: > > Or is there better way? > > > > for (i, url) in [(i,links[i]) for i in range(len(links))]: > > for i, url in enumerate(links): > +2 for creating seeing a need and crafting a reasonable solution, but -1 for not reading the section on builtins to see if it existed already. (As for its pythonicity, I would have recommended isolating it into a function and making it a generator: def my_enumerate(enumerable): i = 0 for elt in enumerable: yield (i, elt) i += 1 for i, url in my_enumerate(links): but it's not too bad as it is. Also, my function is completely untested - it's close to right though.) Peace Bill Mill -- http://mail.python.org/mailman/listinfo/python-list
Re: is this pythonic?
On 7/20/05, Bill Mill <[EMAIL PROTECTED]> wrote: > On 7/20/05, Simon Brunning <[EMAIL PROTECTED]> wrote: > > On 7/20/05, Mage <[EMAIL PROTECTED]> wrote: > > > Or is there better way? > > > > > > for (i, url) in [(i,links[i]) for i in range(len(links))]: > > > > for i, url in enumerate(links): > > > > +2 for creating seeing a need and crafting a reasonable solution, but > -1 for not reading the section on builtins to see if it existed > already. > -1 for me for not reading over my email before sending. "creating seeing" should be "seeing". Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: is this pythonic?
On 7/21/05, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Wed, 20 Jul 2005 16:30:10 -0400, Bill Mill wrote: > > > On 7/20/05, Simon Brunning <[EMAIL PROTECTED]> wrote: > >> On 7/20/05, Mage <[EMAIL PROTECTED]> wrote: > >> > Or is there better way? > >> > > >> > for (i, url) in [(i,links[i]) for i in range(len(links))]: > >> > >> for i, url in enumerate(links): > >> > > > > +2 for creating seeing a need and crafting a reasonable solution, but > > -1 for not reading the section on builtins to see if it existed > > already. > > To see if *what* existed already? > > It is well and good to say RTFM, but there are 697 subsections to the > Python Library reference, and if you don't know what you are looking for, > and beginners rarely are, it isn't obvious which is the right section to > read. And the Library Reference isn't even "the" manual: there is also the > global module reference and language reference. > > If you already know what you are looking for, reading the manual is great > advice. Browsing the manual looking for interesting tidbits can even be > fun for a certain mindset. But if you don't know enough to know what to > look for, where in the 2000-odd sections of the Python references will > you find it? > I said the *builtins* section. I think you learn pretty quick that figuring out what functions are builtins is pretty important in every language. There's a fair number of people out there giving the advice to read chapter 2 of the library reference cover-to-cover for a good starter on python. Furthermore, I wasn't being hard on the guy, he still added up to +1. Lighten up, I was joking. > > > > (As for its pythonicity, I would have recommended isolating it into a > > function and making it a generator: > > It is easy to take this to extremes. It isn't necessary to isolate > everything into its own object, or class, or module. Too much > encapsulation is just as bad as too little. > agreed; his listcomp just looks awkward inside the for loop statement; if it were my code, I would put it into a function. He asked if his code was pythonic, and I think the (non-extreme) pythonic thing to do would be to put his listcomp into a function. > > > def my_enumerate(enumerable): > > i = 0 > > for elt in enumerable: > > yield (i, elt) > > i += 1 > > > > for i, url in my_enumerate(links): > > > > but it's not too bad as it is. Also, my function is completely > > untested - it's close to right though.) > > What is the advantage of your function my_enumerate over the Python > built-in enumerate? > > absolutely none; I just was saying how I would encapsulate it into a function. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Stupid question: Making scripts python-scripts
On 7/21/05, Jan Danielsson <[EMAIL PROTECTED]> wrote: > Hello all, > >How do I make a python script actually a _python_ in unix:ish > environments? > > I know about adding: > #!/bin/sh > >..as the first row in a shell script, but when I installed python on > a NetBSD system, I didn't get a "python" executable; only a "python2.4" > executable. > >Adding "#!/usr/pkg/bin/python2.4" as the first row in the script > would probably work, but that would be too specific for the system I'm > using, imho. > >I saw someone using "#!/usr/bin/env python", but that failed on the > system I'm using, so I assume that's something specific too (or is the > installation broken?). The env program [1], which usually exists at least on a linux system, executes the program given as its argument. Thus, "/usr/bin/env python" tries to executes python, which bash will then use to run the python script. As long as env exists, and python is somewhere in the PATH, this is a fairly portable way to run python scripts. Does BSD really not come with the env program? I bet there's an equivalent you could symlink to it. Unfortunately, I've never BSDed, so I can't help you find it. To get a workable subset of the normal env functionality, you could try (assuming you use bash): /home/llimllib $ echo "$@" > /usr/bin/env /home/llimllib $ chmod a+x /usr/bin/env Peace Bill Mill bill.mill at gmail.com [1]: http://rootr.net/man/man/env/1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Stupid question: Making scripts python-scripts
On 7/21/05, Bill Mill <[EMAIL PROTECTED]> wrote: > On 7/21/05, Jan Danielsson <[EMAIL PROTECTED]> wrote: > > Hello all, > > > >How do I make a python script actually a _python_ in unix:ish > > environments? > > > > I know about adding: > > #!/bin/sh > > > >..as the first row in a shell script, but when I installed python on > > a NetBSD system, I didn't get a "python" executable; only a "python2.4" > > executable. > > > >Adding "#!/usr/pkg/bin/python2.4" as the first row in the script > > would probably work, but that would be too specific for the system I'm > > using, imho. > > > >I saw someone using "#!/usr/bin/env python", but that failed on the > > system I'm using, so I assume that's something specific too (or is the > > installation broken?). > > The env program [1], which usually exists at least on a linux system, > executes the program given as its argument. Thus, "/usr/bin/env > python" tries to executes python, which bash will then use to run the > python script. As long as env exists, and python is somewhere in the > PATH, this is a fairly portable way to run python scripts. > > Does BSD really not come with the env program? I bet there's an > equivalent you could symlink to it. Unfortunately, I've never BSDed, > so I can't help you find it. To get a workable subset of the normal > env functionality, you could try (assuming you use bash): > > /home/llimllib $ echo "$@" > /usr/bin/env > /home/llimllib $ chmod a+x /usr/bin/env > ahhh, that should be: /home/llimllib $ echo "\$@" > /usr/bin/env otherwise bash tries to substitute into the string. Sorry bout that. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: [Beginner] Calling a function by its name in a string
On 7/27/05, Tito <[EMAIL PROTECTED]> wrote: > Hi all: > > Is there a metalanguage capability in Python (I know there are many) to > call a function having its name in a string? > > Something like: > __call__("foo") > > instead of: > foo() > >>> def foo(): print "foobarred" ... >>> foo() foobarred >>> eval("foo()") foobarred >>> Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: [Beginner] Calling a function by its name in a string
On 7/27/05, Tito <[EMAIL PROTECTED]> wrote: > Thank you both for your quick answers. > > What I wanted is to parameterize a function with another member > function, like this: > > def printFunctionForEach(collection, functionName): >for elem in collection: > print eval("elem." + functionName + "()") > > Moreover, I wanted to do it with a property: > > def printPropertyForEach(collection, propertyName): >for elem in collection: > print eval("elem." + propertyName) > > Is there another approach to do it? > Sure, piece of cake: >>> class test: ... def func1(self): print 'func1 called' ... >>> class test2: ... def func1(self): print 'other func1' ... >>> x = [test(), test2(), test()] >>> def call_this_func(lst, func_name): ... for e in lst: ... getattr(e, func_name)() ... >>> call_this_func(x, 'func1') func1 called other func1 func1 called >>> Note that the getattr raises an AttributeError if func_name doesn't exist in the object; you should probably wrap it in a try/except. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Ten Essential Development Practices
> although, as some argue, it's > possible [GvR] thinks in base 9.5, that just doesn't seem Pythonic to me. +1 QOTW Peace Bill Mill [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: sample code for parsing html file to get contents of td fields
On 4 Aug 2005 11:54:38 -0700, yaffa <[EMAIL PROTECTED]> wrote: > does anyone have sample code for parsting an html file to get contents > of a td field to write to a mysql db? even if you have everything but > the mysql db part ill take it. > Do you want something like this? In [1]: x = "something something else and\nanother thing in a td and again else" In [2]: import re In [3]: r = re.compile('(.*?)', re.S) In [4]: r.findall(x) Out[4]: ['something else', 'in a td'] If not, you'll have to explain more clearly what you want. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: The ONLY thing that prevents me from using Python
> I really wish Python could be more widely available on web server > machines. This is just my own experience and I would like to hear your > comments. > I would like a pony... no, wait, even better, a unicorn! Peace Bill Mill bill.mill at gmail.com PS (the gist is, why don't you offer some constructive comments, instead of ones we can do nothing about?) -- http://mail.python.org/mailman/listinfo/python-list
Re: help in algorithm
On 10 Aug 2005 12:46:08 -0700, gene tani <[EMAIL PROTECTED]> wrote: > this sounds like LSI / singular value decomposition (?) Why do you think so? I don't see it, but you might see something I don't. LSI can be used to cluster things, but I see no reason to believe that he's using LSI for his clustering. I ask because I've done some LSI [1], and could help him out with that if he is doing it. While I'm on the subject, is there any general interest in my python LSI code? [1] http://llimllib.f2o.org/files/lsi_paper.pdf Peace Bill Mill -- http://mail.python.org/mailman/listinfo/python-list
Re: loop in python
They come out even in the computer language shootout: http://shootout.alioth.debian.org/benchmark.php?test=all&lang=python&sort=fullcpu (tied 8-8 in execution time, although perl wins 4-12 on memory consumption) Peace Bill Mill On 8/23/05, km <[EMAIL PROTECTED]> wrote: > Hi all, > > > thing. If *all* your loops are going to do is print stuff, then you're > > doing the right thing with the version that "emits values". > > ya most of the loops print values. > > > know this). Since you haven't got any working code, it's not possible > > that you *need* whatever negligible speed difference there might be > > between Python and Perl. > > > > Python, don't let your first attempts at benchmarking dissuade you. > > Really, trust us. > > ya i do. > > > Python's strengths lie in four things: the readability of the code, the > > huge range of library modules available, the elegance of its object > > oriented constructs, and the helpfulness of its community. Raw speed is > > not one of its strengths, but there are tens of thousands of people > > using it quite effectively and without daily concern for its speed (same > > as Perl, by the way since, again, they are _not_ significantly different > > in speed no matter what an empty loop test shows). > > I agree that python emphasizes on readability which i didnt see in many of > the languages, but when the application concern is speed, does it mean that > python is not yet ready? even most of the googling abt python vs perl > convince me that perl is faster than python in most of the aspects. Also the > first thing any newbie to python asks me is abt "raw speed in comparison with > similar languages like perl" when i advocate python to perl. > > > regards, > KM > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: loop in python
> If you want a fast language, try Holden. I've just invented it. > Unfortunately it gets the answer to every problem wrong unless the > answer is 42, but boy it runs quickly. +1 QOTW (sometimes the zen master has to whack the student on the head) Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed quirk: redundant line gives six-fold speedup
On 8/25/05, Mark Dickinson <[EMAIL PROTECTED]> wrote: > I have a simple 192-line Python script that begins with the line: > > dummy0 = 47 > > The script runs in less than 2.5 seconds. The variable dummy0 is never > referenced again, directly or indirectly, by the rest of the script. > > Here's the surprise: if I remove or comment out this first line, the > script takes more than 15 seconds to run. So it appears that adding a > redundant line produces a spectacular six-fold increase in speed! > > (Actually, I had to add 29 dummy lines at the beginning of the code to > get the speed increase; if any one of these lines is removed the > running time reverts to around 15 seconds again.) > > Questions: One of my own: what in the world made you think "maybe I'll add 29 dummy global variables to speed things up?" It seems to work (>19x speedup on my machine), I'm just curious what path you followed to get there. And, finally, you should forward this to the python-dev list, if somebody hasn't already. There are more people who know a ton about python internals there. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed quirk: redundant line gives six-fold speedup
On 8/25/05, Erik Max Francis <[EMAIL PROTECTED]> wrote: > Mark Dickinson wrote: > > > Questions: > > > > (1) Can anyone else reproduce this behaviour, or is it just some quirk > > of my setup? > > (2) Any possible explanations? Is there some optimization that kicks > > in at a certain number of lines, or at a certain length of > > bytecode? > > (3) If (2), is there some way to force the optimization, so that I can > > get the speed increase without having to add the extra lines? > > I see no difference in execution times, as expected. The most likely > explanation is simply that other things were going on on your system > when you ran the first test, but not the second test, resulting in the > discrepancy. In other words, the speed change had nothing to do with > your dummy lines. > Unlikely; 2 people have confirmed these results already. I did find, though, that if I remove all print statements from the program, the dummy and non-dummy variable versions take indentical time. Can others reproduce this? I'm Investigating further... Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed quirk: redundant line gives six-fold speedup
Bill Mill wrote: > > Pentium M 1.8 GHz Windows 2k. Here's the top of the profile results > for fast and slow on my machine (these won't look decent except in a > fixed-width font): > > > Interestingly, the test.py:36 line, which takes 45 seconds (!!) in the > slow version, does not appear at all in the fast profile. I can't > figure out why - both printed out their data, so template must have > been called somewhere. > OK, I'm getting somewhere now. When I replace: template = (" | %s %s %s | %s %s %s | %s %s %s |\n" * 3).join([" +---+---+---+\n"] * 4) wtih: template = """ | %s %s %s | %s %s %s | %s %s %s |\n | %s %s %s | %s %s %s | %s %s %s |\n | %s %s %s | %s %s %s | %s %s %s |\n +---+---+---+\n | %s %s %s | %s %s %s | %s %s %s |\n | %s %s %s | %s %s %s | %s %s %s |\n | %s %s %s | %s %s %s | %s %s %s |\n +---+---+---+\n | %s %s %s | %s %s %s | %s %s %s |\n | %s %s %s | %s %s %s | %s %s %s |\n | %s %s %s | %s %s %s | %s %s %s |\n +---+---+---+\n""" Then the non-dummy version is faster than the dummy version (very slightly, presumably because it doesn't need to allocate 28 dummy variables). Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed quirk: redundant line gives six-fold speedup
On 8/25/05, Jack Diederich <[EMAIL PROTECTED]> wrote: > On Thu, Aug 25, 2005 at 01:35:04PM -0400, Bill Mill wrote: > > On 8/25/05, Erik Max Francis <[EMAIL PROTECTED]> wrote: > > > Mark Dickinson wrote: > > > > > > > Questions: > > > > > > > > (1) Can anyone else reproduce this behaviour, or is it just some quirk > > > > of my setup? > > > > (2) Any possible explanations? Is there some optimization that kicks > > > > in at a certain number of lines, or at a certain length of > > > > bytecode? > > > > (3) If (2), is there some way to force the optimization, so that I can > > > > get the speed increase without having to add the extra lines? > > > > > > > I did find, though, that if I remove all print statements from the > > program, the dummy and non-dummy variable versions take indentical > > time. Can others reproduce this? > > > > I'm Investigating further... > > I'm getting similarly freakish results. I tried a little ghetto debugging > by putting a printf in dictobject.c's resize method and recompiling python. > Sadly I can't get the problem to reproduce itself with the new binary > (with or without the printf). The Ubuntu default 2.4.1 is sometimes fast, > my hand compiled one (./configure && make) is always slow. > > There are some very arcane low level things going on here. > agreed. Also, either I was temporarily insane, or the version with the explicit template no longer runs faster for me, so I hope nobody spends a lot of time on that. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed quirk: redundant line gives six-fold speedup
On 8/25/05, Erik Max Francis <[EMAIL PROTECTED]> wrote: > Bill Mill wrote: > > > Unlikely; 2 people have confirmed these results already. > > > > I did find, though, that if I remove all print statements from the > > program, the dummy and non-dummy variable versions take indentical > > time. Can others reproduce this? > > Yes, it's obviously a real effect given the other sightings. I don't > see any speed difference, myself (Pentium IV 3.0 GHz running Slackware > Linux). > Pentium M 1.8 GHz Windows 2k. Here's the top of the profile results for fast and slow on my machine (these won't look decent except in a fixed-width font): Slow: 6766494 function calls (6737594 primitive calls) in 45.740 CPU seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function) 3322320 20.5390.000 31.1520.000 test.py:135( ) 27520 10.6410.000 41.7920.002 :0(min) 3322320 10.6130.000 10.6130.000 test.py:81(rowitems) 28100/203.6200.000 45.6332.282 test.py:130(search) 275450.1130.0000.1130.000 :0(append) 275200.0980.0000.0980.000 :0(pop) 10.0410.041 45.736 45.736 test.py:36(?) Fast: 540174 function calls (536514 primitive calls) in 3.506 CPU seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function) 2596401.5160.0002.3030.000 test.py:135( ) 22800.7910.0003.0940.001 :0(min) 2596400.7880.0000.7880.000 test.py:81(rowitems) 2860/200.2690.0003.3910.170 test.py:130(search) 10.0450.0453.4993.499 test.py:2(?) 36450.0210.0000.0210.000 test.py:71(colinsert) 32400.0190.0000.0190.000 test.py:62(rowinsert) 23050.0100.0000.0100.000 :0(append) Interestingly, the test.py:36 line, which takes 45 seconds (!!) in the slow version, does not appear at all in the fast profile. I can't figure out why - both printed out their data, so template must have been called somewhere. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed quirk: redundant line gives six-fold speedup
On 8/25/05, Jack Diederich <[EMAIL PROTECTED]> wrote: > On Thu, Aug 25, 2005 at 09:23:09PM +0300, Stelios Xanthakis wrote: > > The explanation is this: hash > > and comparison of objects depends on the state of the memory > > allocator. A sample case is this: > > > > class A: pass > > dummy0=47 # comment this to get a different result for min > > a=A() > > b=A() > > print min (a, b) > > > > the result of 'min' is not only non-deterministic but also depends > > on whether other things have been allocated before. The same > > thing can happen for 'dictionary.keys()' if the keys are objects > > and 'iterate-over-set' when the set contains objects. > > I'm also pretty sure I've caught a bug in his code, though I'm not sure how it works exactly. I replaced the 'min' built-in with my own min, and he's going to get nondeterministic results from this line: mm = min((c.S, c) for c in rowitems(h))[1].D because 'c' is often the exact same object. A snippet from my debugging version of 'min', which prints out the tuple its handed: (1, <__main__.LLentry object at 0x00969710>) (1, <__main__.LLentry object at 0x00969710>) (4, <__main__.LLentry object at 0x00969710>) (4, <__main__.LLentry object at 0x00969710>) (3, <__main__.LLentry object at 0x00969710>) (3, <__main__.LLentry object at 0x00969710>) (3, <__main__.LLentry object at 0x00969710>) (2, <__main__.LLentry object at 0x00969710>) Although they appear in order here, they don't always. Often, multiple objects have a value of 1, and he's going to get one of them at random as the 'min' object. I'm pretty sure. Mark, can you confirm that this is/isn't a bug? (btw, it still runs fast with and slow without the dummies with my custom min() func) Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: C#3.0 and lambdas
On 9/19/05, Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > > meanwhile, over in python-dev land: > > > > "Is anyone truly attached to nested tuple function parameters; 'def > > fxn((a,b)): print a,b'? /.../ > > > > Would anyone really throw a huge fit if they went away? I am willing > > to write a PEP for their removal in 2.6 with a deprecation in 2.5 if > > people are up for it." > > I am - I think that feature is sort of an orthogonality which should be > preserved. No doubt its not one of the most important ones - but if I > can write > > a, (b ,c) = 1, (2,3) > > I'd like to write > > def foo(a, (b,c)): > ... > > foo(1, (2,3)) > Agreed. I discovered them when I wondered "wouldn't it be neat if functions unpacked tuples just like regular code does?" And was pleasantly surprised to find that they did. +1 on keeping them. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: C#3.0 and lambdas
On 9/21/05, Scott David Daniels <[EMAIL PROTECTED]> wrote: > Roel Schroeven wrote: > > ... > > Christophe schreef: > >> ... > >>And what about a function which computes the line length ? > > > > That would have been a better example indeed, since the *p1 trick > > doesn't work there. > > > > def euclidian_distance((x1, y1), (x2, y2)): > > return math.sqrt((x2 - x1)**2 + (y2 - y1)**2) > > > > That's a lot nicer, I think, than this: > > > > def euclidian_distance(p1, p2): > > return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2) > > But not massively nicer than: > > def euclidian_distance(p1, p2): > (x1, y1), (x2, y2) = p1, p2 > return math.sqrt((x2 - x1)**2 + (y2 - y1)**2) > But the question is - why go to the effort to remove the (by your admission) slightly nicer version? Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to show percentage
You need to convert 1 or 3 to a float. How about: >>> def pct(num, den): return (float(num)/den) * 100 ... >>> pct(1, 3) 33.3333329 Peace Bill Mill bill.mill at gmail.com On 22 Sep 2005 10:51:43 -0700, Sen-Lung Chen <[EMAIL PROTECTED]> wrote: > Dear All: > I have a question of show percentage. > For example ,I want to show the percentage of 1/3 = 33.33% > > I use the 1*100/3 = 33 > it is 33 not 33.33 , how to show the 33.33 % > Thanks > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop in list.
Jim, That is called a "list comprehension", and it is a feature which appeared in python 2.3 (iirc). Thus if your books are about earlier versions of python, list comprehensions will not be covered. Check out the section of the tutorial about them at http://docs.python.org/tut/node7.html#SECTION00714 . Peace Bill Mill bill.mill at gmail.com On Tue, 08 Feb 2005 06:50:08 -0800 (PST), Jim <[EMAIL PROTECTED]> wrote: > Where did this type of structure come from: > > mat = ['a' for i in range(3)]? > > This will produce a list of three elements but > I don't see reference for it in any of the books. > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: check if object is number
On Fri, 11 Feb 2005 12:11:44 -0700, Steven Bethard <[EMAIL PROTECTED]> wrote: > Is there a good way to determine if an object is a numeric type? > Generally, I avoid type-checks in favor of try/except blocks, but I'm > not sure what to do in this case: > > def f(i): > ... > if x < i: > ... > > The problem is, no error will be thrown if 'i' is, say, a string: > > py> 1 < 'a' > True > py> 100 < 'a' > True > > But for my code, passing a string is bad, so I'd like to provide an > appropriate error. How about: if type(variable) == type(1): print "is an integer" else: print "please input an integer" > > I thought about calling int() on the value, but this will also allow > some strings (e.g. '1'). I guess this isn't horrible, but it seems > somewhat suboptimal... > > Ideas? > > Steve > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Regular Expressions: large amount of or's
On Tue, 01 Mar 2005 22:04:15 +0100, André Søreng <[EMAIL PROTECTED]> wrote: > Kent Johnson wrote: > > André Søreng wrote: > > > >> > >> Hi! > >> > >> Given a string, I want to find all ocurrences of > >> certain predefined words in that string. Problem is, the list of > >> words that should be detected can be in the order of thousands. > >> > >> With the re module, this can be solved something like this: > >> > >> import re > >> > >> r = re.compile("word1|word2|word3|...|wordN") > >> r.findall(some_string) > >> > >> Unfortunately, when having more than about 10 000 words in > >> the regexp, I get a regular expression runtime error when > >> trying to execute the findall function (compile works fine, but slow). > >> > >> I don't know if using the re module is the right solution here, any > >> suggestions on alternative solutions or data structures which could > >> be used to solve the problem? > > > > > > If you can split some_string into individual words, you could look them > > up in a set of known words: > > > > known_words = set("word1 word2 word3 ... wordN".split()) > > found_words = [ word for word in some_string.split() if word in > > known_words ] > > > > Kent > > > >> > >> André > >> > > That is not exactly what I want. It should discover if some of > the predefined words appear as substrings, not only as equal > words. For instance, after matching "word2sgjoisejfisaword1yguyg", word2 > and word1 should be detected. Show some initiative, man! >>> known_words = set(["word1", "word2"]) >>> found_words = [word for word in known_words if word in "word2sgjoisejfisawo rd1yguyg"] >>> found_words ['word1', 'word2'] Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How would you program this?
On Wed, 02 Mar 2005 10:23:33 -0800, engsol <[EMAIL PROTECTED]> wrote: > There is a number puzzle which appears in the daily paper. > Because I'm between Python projects, I thought it might be > fun to write a program to solve it20 minute job, max. > > On closer inspection, it became apparent that it's not a > simple thing to program. How would you approach it? > > The puzzle: a 4 x 4 grid. The rows are summed (and given), the > cols are summed (and given), and the two diagonals are summed, > and given. In addition, 4 clues are given, but none of the 4 are in > the same row or col. > > Example from today's paper:...solution time is 8 minutes, 1 second, > so they say. > > The set of allowable numbers is 1 thru 9 > > Rows: > 3 + B + C + D = 22 > E + F + 8 + H = 26 > I + J + K + 8 = 31 > M + 7 + O + P = 25 > > Col sums: > 24, 18, 31, 31 > > Diag sums: > 3 + F + K + P = 24 > M + J + 8 + D = 24 > > The first impulse is to just brute force it with nested for loops, > but the calculator shows the possible combinations are > 9^12 = 5,159,780,352, which would take much too long. > Are you sure about that? You can eliminate a whole lot of options just based on the row (or column, if you're so inclined) totals. Here's what I came up with in 10 minutes: #linalg_brute.py-- ns = range(1,10) def mkrow(limit): return [(a,b,c) for a in ns for b in ns for c in ns if a + b + c == limit] row1 = mkrow(19) row2 = mkrow(18) row3 = mkrow(23) row4 = mkrow(18) for b,c,d in row1: for e,f,h in row2: for i,j,k in row3: for m,o,p in row4: if 3 + e + i + m == 24 and 7 + b + f + j == 18 \ and 8 + c + k + o == 31 and 8 + d + h + p == 31 \ and 3 + f + k + p == 24 and m + j + 8 + d == 24: print 3,b,c,d print e,f,8,h print i,j,k,8 print m,7,o,p print '-' #--end linalg_brute.py- Of course, it could use a whole bunch of generalization, but that wasn't the point. It runs quite nicely: 02:42 PM /d/code/Python$ time python linalg.py 3 3 8 8 9 3 8 6 9 5 9 8 3 7 6 9 - 3 3 9 7 8 3 8 7 9 5 9 8 4 7 5 9 - real0m1.255s user0m1.221s sys 0m0.030s Both solutions look correct to me; how about you? With clever enough heuristics, problems that you can expect people to solve will almost always fall to brute force algorithms, I feel. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How would you program this?
This should have said "time python linalg_brute.py" . I changed the name, but didn't rerun the test. linalg.py is the same as linalg_brute.py. > > 02:42 PM /d/code/Python$ time python linalg.py > 3 3 8 8 > 9 3 8 6 > 9 5 9 8 > 3 7 6 9 > - > 3 3 9 7 > 8 3 8 7 > 9 5 9 8 > 4 7 5 9 > - > > real0m1.255s > user0m1.221s > sys 0m0.030s > Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: passing lists
Earl, Please post the smallest snippet of code you can which illustrates your problem. No list is an unsubscriptable object, so you seem to be passing something that is not a list to your function. As it stands, you don't give enough information to give an actual answer. Peace Bill Mill bill.mill at gmail.com On Wed, 02 Mar 2005 14:05:18 -0700, Earl Eiland <[EMAIL PROTECTED]> wrote: > I have a program in which I'm passing a list to functions. When I > reference an element in a function to which it is passed, I get the > error message "unsubscriptable object". When printing the list contents > in that same function, I get "xxx is ". How do > I pass a list? > > Earl Eiland > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: substring matching
On Wed, 02 Mar 2005 16:43:29 -0500, M.N.A.Smadi <[EMAIL PROTECTED]> wrote: > hi; > > say i have the following variable > > data="""XYZ dflsjdfkl sdfsdhfl > jdsflkdsjf > sldjfsldjf > """ > > i need to make sure that the first part is actually XYZ, is there an > easy way of doing that? > data.startswith('XYZ') Please read the python tutorial at http://docs.python.org/tut/tut.html . Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How would you program this?
On Thu, 03 Mar 2005 10:52:23 -0800, engsol <[EMAIL PROTECTED]> wrote: > > The diagonal constraint is interestingit seems to affect the number of > solutions. One surprise, (not being a math major), was that when I let the > brute force run (forever, it seemed), but without the diagonal > qualification(s), > there was maybe 100 solutions. The reson it was a surprise it that years > ago a programmer used the row-sum, col-sum method to detect and correct > data errors. He swore it was robust, and 100% reliable. Seems that that > isn't the case. I think that it probably is a decent gut-check for data errors, for the simple reason that changing one number requires, at a minimum, three other changes in order to maintain both the row and column sums. If you have at least a decent data fidelity rate, that is unlikely to happen, and even if it does, very very unlikely to happen in such a way that the row and column sums are maintained; especially as the number of rows and columns grows. Better to just do a crc or a hash of the data though. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Making things more functional in Python
On Fri, 4 Mar 2005 08:36:49 -0800 (PST), gf gf <[EMAIL PROTECTED]> wrote: > Is there a better, more FP style, more Pythonic way to > write this: > > def compute_vectors(samples, dset): > vectors = {} > for d in dset: > vectors[d] = [sample.get_val(d) for sample in > samples] > return vectors > > Namely, I'd like to get rid of the initilization > (vectors = {}) and also the loop Yet, I'd hate to put > an assignment into Python's FP list comprehensions. Well, I guess it's a little more FP, but whether it's (IMHO) way *less* pythonic: return dict([(d, [sample.get_val(d) for sample in samples]) for d in dset]) it should work, but is untested and really ugly. I don't see what's wrong with your code that you need to compress it. It's blindingly obvious what your code does, which is a good thing. My list comp, on the other hand, is seven kinds of confusing for a reader. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Integer From A Float List?!?
On Fri, 4 Mar 2005 22:35:48 +0100, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hello NG, > > I was wondering if there is a way to obtain, from a list of floats, > a list of integers without loops. Probably is a basic question, but I can't > find an answer... I have had my eyes blinded by Matlab for years, but now > that I discovered Python+wxPython there seems to be no limit on what one > can do with these 2 tools. Anyway, following the Matlab style, I would like > to do something like this: > > matrix = [1.5, 4.3, 5.5] > integer_matrix = int(matrix) (float for Matlab) You're going to have to use loops. I don't know how Matlab can do it without them, unless it maintains the matrix as a list of floats and simply *views* it as a list of ints. More likely, it simply hides the loop away from you. Anyway, here's some ways to do it: preferable: int_matrix = [int(x) for x in matrix] old way: int_matrix = map(int, matrix) explicit: int_matrix = [] for x in matrix: int_matrix.append(int(x)) Any of these methods should be neither really slow nor really fast, but the list comprehension should be the fastest (I think). Anyway, if you're going to be doing lots of large matrices, and want some of your old matlab stuff, check out numpy and numarray at http://numeric.scipy.org/ . Also, somebody was recently posting on here about a python <-> matlab bridge that they developed; you should search the archives for that (it was in february, I think). And, finally, when doing scientific stuff, I found IPython (http://ipython.scipy.org/) to be an invaluable tool. It's a much improved Python interpreter. Peace Bill Mill bill.mill at gmail.com > > (In Matlab, "integer_matrix" is always a double anyway, here I would like > only to show the vector-matrix operation). > > Obviously, Python complains about: > > Traceback (most recent call last): > File "", line 1, in ? > TypeError: int() argument must be a string or a number > > I would like to avoid loops because, having been blinded by Matlab > vector-matrix > abilities (and corresponding SLOW for-while loops operations), I tend to > think that also Python will be slow if I use loops. > > Does anyone have a suggestion (or maybe could anyone show me that I'm wrong > about loops?) > > Thanks you a lot. > > Andrea. > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a short-circuiting dictionary "get" method?
Dave, On Wed, 09 Mar 2005 09:45:41 -0800, Dave Opstad <[EMAIL PROTECTED]> wrote: > In this snippet: > > d = {'x': 1} > value = d.get('x', bigscaryfunction()) > > the bigscaryfunction is always called, even though 'x' is a valid key. > Is there a "short-circuit" version of get that doesn't evaluate the > second argument if the first is a valid key? For now I'll code around > it, but this behavior surprised me a bit... There is no short-circuit function like you're asking for, because it's impossible in python. To pass an argument to the 'get' function, python evaluates the bigscaryfunction before calling 'get'. (I believe this means that python doesn't have "lazy evaluation", but the language lawyers may shoot me down on that. Wikipedia seems to say that it means python doesn't have "delayed evaluation"). Here are two ways to do what you want: if 'x' in d: value = d['x'] else: value = bigscaryfunction() or: def sget(dict, key, func, *args): if key in dict: return key else: return func(*args) sget(d, 'x', bigscaryfunction) Both methods are untested, but should work with minor modifications. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a short-circuiting dictionary "get" method?
On 9 Mar 2005 10:05:21 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Maybe this can help: > > value = d.get('x', lambda: bigscaryfunction()) >>> def test(): print 'gbye' ... >>> d = {} >>> z = d.get('x', lambda: test()) >>> z at 0x008D6870> So this seems to be merely an obfuscation of: >>> z = d.get('x', test) >>> z I just wanted to ask, am I missing something? Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a short-circuiting dictionary "get" method?
On 09 Mar 2005 18:13:01 GMT, F. Petitjean <[EMAIL PROTECTED]> wrote: > Le Wed, 09 Mar 2005 09:45:41 -0800, Dave Opstad a écrit : > > In this snippet: > > > > d = {'x': 1} > > value = d.get('x', bigscaryfunction()) > > > > the bigscaryfunction is always called, even though 'x' is a valid key. > > Is there a "short-circuit" version of get that doesn't evaluate the > > second argument if the first is a valid key? For now I'll code around > > it, but this behavior surprised me a bit... > def scary(): > print "scary called" > return 22 > > d = dict(x=1) > d.get('x', lambda *a : scary()) > > # print 1 > d.get('z', (lambda *a : scary())()) > scary called > 22 but: >>> d.get('x', (lambda *a: test())()) test called 1 So how is this different than d.get('x', test()) ? Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: pre-PEP: Print Without Intervening Space
On Fri, 11 Mar 2005 10:00:03 -0600, Larry Bates <[EMAIL PROTECTED]> wrote: > > I also don't miss a no-space option on print. I've always believed > that print statements with commas in them were for simple output with > little or no regard for formatting (like for debugging statements). > If I want precisely formatted output I use '%' formats or I build > the output line manually with joins or some other mechanism. The > ''.join(seq) or ''.join([fn(x) for x in seq]) says exactly what > is being done to create the output string. I agree with everything said here. Furthermore, I don't like this PEP because it introduces magic. When reading code, you just need to magically know what the double-comma does in a print statement. Yes, there are bits of magic involved in the solutions that already exist, but I am opposed to the introduction of more. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Why tuple with one item is no tuple
On Tue, 15 Mar 2005 16:16:34 GMT, Gregor Horvath <[EMAIL PROTECTED]> wrote: > Hi, > > >>>type(['1']) > > > >>>type(('1')) > > > I wonder why ('1') is no tuple because, syntactically, those parens are for grouping, and do not unambiguously define a tuple. It's a python gotcha. To define a one-tuple, put a comma after the '1': >>>type(('1',)) > > Because I have to treat this "special" case differently in my code. you shouldn't have to; post your code if you still think you do. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Why tuple with one item is no tuple
On Tue, 15 Mar 2005 10:47:28 -0800, James Stroud <[EMAIL PROTECTED]> wrote: > On Tuesday 15 March 2005 08:25 am, Roy Smith wrote: > > a = () # tuple of zero elements > > a = (1,) # tuple of one element > > a = 1, # tuple of one element > > a = (1) # scalar > > a = (1, 2) # tuple of two elements > > a = 1, 2 # tuple of two elements > > a = ,# syntax error > > > > The big question is, is it the parens that make it a tuple, or is it > > the comma? If you go along with the parens school of thought, then > > (1,) is the special case. If you believe in commas, then the () is > > the special case. In either case, it's a bit ugly, but we learn to > > overlook the occasional cosmetic blemishes of those we love :-) > > The answer is obvious, the naked comma should be an empty tuple. > The other answer, that parens should be required to surround all tuples, is obvious too. Neither is particularly appealing; a lone comma creating a data structure seems counter-intuitive, but it's nice to do a, b = b, a instead of (a, b) = (b, a) . In this case, since the need to create empty tuples is vanishingly rare, I'm okay with a little inconsistency. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: c.l.p.announce question
On Tue, 15 Mar 2005 22:24:52 +0100, Fraca7 <[EMAIL PROTECTED]> wrote: > Hello. > > It's not quite clear from the chart; I'd like to know if it's kosher to > announce the creation of a Python-oriented blog on > comp.lang.python.announce ? > Well, it's a little late to back out now, isn't it? So check out http://fraca7.free.fr/blog/ for "from fraca7 import *", y'all. More to the point, add yourself at http://lowlife.jp/cgi-bin/moin.cgi/PythonProgrammersWeblog and http://www.planetpython.org/ (see email info on that site), and people will find your blog. I know for a fact that the daily python-url editors (when not away on vacation) read planet python, and they'll send thousands of visitors to you if they link you. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Text-to-speech
On 21 Mar 2005 12:47:07 -0800, Paul McGuire <[EMAIL PROTECTED]> wrote: > Brian, > > Having reviewed your Cease and Desist petition, I'm afraid I must > dispute some or all of your claims: > > 1. Your citation of prior art has one or more significant defects: > a. In your citation, "brace" is clearly rhymed with "whitespace", not > "space". The broad concept of "whitespace" is substantially different > from the specific term "spaces": "whitespace" encompasses all > white-printing characters, including tabs, formfeeds, and carriage > returns, as well as space characters. In the more general field of > publishing, "whitespace" also includes page margins, paragraph breaks, > and block indentations for embedded quotes or subsections. In my > submission, "spaces" is specifically intended to narrowly refer to the > character defined in ISO 8879 as ASCII code 32. Especially, I did > *not* intend to include reference to the ISO 8879 ASCII code 9 > character, or "tab". > b. Prior art predates your citation, see Guido van Rossum's post > "[marketing-python] How About a Slogan or Tagline?", at > http://wingware.com/pipermail/marketing-python/2002-March/003851.html, > which includes several notable references to derivative forms of > "brace" and "space". > 2. As the Python language's most salient feature is its usage of spaces > for program structuring, as opposed to use of enclosing brace > characters in related scripting languages (Tcl, Perl) and compiled > languages (C, C++, Java, C#), the juxtaposition of "brace" and "space" > in any poetic construct is obvious, and this obviousness further erodes > your IP claim. > 3. I think my poem was funnier - "lost track of his braces" (humorous > allusion to suspenders) is a knee-slapper! ("Perl before swine" was > cute, but it's not new.) > > Still, I am open to negotiation - would you be interested in > cross-licensing my patent pending rhyming of "van Rossum" and > "awesome"? > T'were two coders in c.l.p Who liked to argue legally About copyright All day and night, Just to prove their inanity -- http://mail.python.org/mailman/listinfo/python-list
Re: Data types
On 24 Mar 2005 10:29:40 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I am new to python and learning it. Can you please give me a simple > example of user defined type through class mechanism. GIYF: http://www.python.org/2.2/descrintro.html#subclassing Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Version Number Comparison Function
On 25 Mar 2005 07:34:38 -0800, Keith <[EMAIL PROTECTED]> wrote: > Is there a function for comparing version numbers? > > E.g. > > 0.1.0 < 0.1.2 > 1.876b < 1.876c > 3.2.2 < 3.4 > Not by default AFAIK. How about something like (untested): def test_version(v1, v2): v1, v2 = v1.split('.'), v2.split('.') for x, y in zip(v1, v2): if x < y: return v1 if y > x: return v2 It assumes that v1 and v2 have the same amount of '.'s and that all of the version numbers are of the same length (i.e. 1.1000 would be < 1.999). How general do you need to be? Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Grouping code by indentation - feature or ******?
On Fri, 25 Mar 2005 11:38:37 -0800, Robert Kern <[EMAIL PROTECTED]> wrote: > James Stroud wrote: > > On Friday 25 March 2005 08:39 am, Ivan Van Laningham wrote: > > > >>As far as grouping by indentation goes, it's why I fell in love with > >>Python in the first place. Braces and so on are just extraneous cruft > >>as far as I'm concerned. It's the difference between Vietnamese verbs > >>and Latin verbs;-) > > > > > > Say I buy into the indentation ideology. Python then has this > > inconsistency: : > > > > Why do we need : at the end of our if and for loops? I spend approximately 6 > > minutes/100 lines of code going back and finding all of the times I missed > > :. > > Is it for cheating? > > > > if False: print ":" > > > > Now, what happened to the whitespace idea here? This code seems very > > unpythonic. I think : is great for slices and lamda where things go on one > > line, but to require it to specify the start of a block of code seems a > > little perlish. > > During the usability studies for the language ABC, which Guido worked on > before developing Python and also used indentation for grouping, it was > found that the colon improved readability. > > I don't know what those studies said about the frequency of people > forgetting to put in the colon. Anecdotally, I can say that I do it very > rarely. > I can't remember having ever done it, although I am sure I have. The real question is, though, 6 minutes per 100 lines of code? There probably aren't more than 30 lines out of those 100 that should end in a colon. Assuming you forget half your colons, you're spending upwards of 20 seconds per colon? If you want, I'll write a script that checks for colons at the end of lines before increased indentation, and asks you if you want to put one there - I could save you 5.8 minutes per 100 lines of code. How's that for a productivity boost? Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: breaking up is hard to do
On 25 Mar 2005 12:37:29 -0800, bbands <[EMAIL PROTECTED]> wrote: > I've a 2,000 line and growing Python script that I'd like to break up > into a modules--one class alone is currently over 500 lines. There is a > large config.ini file involved (via ConfigParser), a fair number of > passed and global variables as well as interaction with external > programs such as MySQL (via MySQLdb), R (via Rpy) and gnuplot (via > Gnuplot). Every time I have tried to break it up I end up with thorny > name-space problems and have had to stitch it back together gain. What sort of namespace problems? I think you need to tell us what your specific problems were, so that we can help you more. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: String Splitter Brain Teaser
On 28 Mar 2005 04:12:15 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > This is shorter: > map(list,' '.join(s).replace(' / ','').split()) > > but for very long genomes Michael Spencer's nice version can be faster. > for very long genomes he might want a generator: def xgen(s): l = len(s) - 1 e = enumerate(s) for i,c in e: if i < l and s[i+1] == '/': e.next() i2, c2 = e.next() yield [c, c2] else: yield [c] >>> for g in xgen('ATT/GATA/G'): print g ... ['A'] ['T'] ['T', 'G'] ['A'] ['T'] ['A', 'G'] Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: String Splitter Brain Teaser
On Mon, 28 Mar 2005 09:18:38 -0800, Michael Spencer <[EMAIL PROTECTED]> wrote: > Bill Mill wrote: > > > for very long genomes he might want a generator: > > > > def xgen(s): > > l = len(s) - 1 > > e = enumerate(s) > > for i,c in e: > > if i < l and s[i+1] == '/': > > e.next() > > i2, c2 = e.next() > > yield [c, c2] > > else: > > yield [c] > > > > > >>>>for g in xgen('ATT/GATA/G'): print g > > > > ... > > ['A'] > > ['T'] > > ['T', 'G'] > > ['A'] > > ['T'] > > ['A', 'G'] > > > > Peace > > Bill Mill > > bill.mill at gmail.com > > works according to the original spec, but there are a couple of issues: > > 1. the output is specified to be a list, so delaying the creation of the list > isn't a win True. However, if it is a really long genome, he's not going to want to have both a string of the genome and a list of the genome in memory. Instead, I thought it might be useful to iterate through the genome so that it doesn't have to be stored in memory. Since he didn't specify what he wants the list for, it's possible that he just needs to iterate through the genome, grouping degeneracies as he goes. > > 2. this version fails down in the presence of "double degeneracies" (if that's > what they should be called) - which were not in the OP spec, but which cropped > up in a later post : > >>> list(xgen("AGC/C/TGA/T")) > [['A'], ['G'], ['C', 'C'], ['/'], ['T'], ['G'], ['A', 'T']] This is simple enough to fix, in basically the same way your function works. I think it actually makes the function simpler: def xgen(s): e = enumerate(s) stack = [e.next()[1]] #push the first char into the stack for i,c in e: if c != '/': yield stack stack = [c] else: stack.append(e.next()[1]) yield stack >>> gn 'ATT/GATA/G/AT' >>> for g in xgen(gn): print g ... ['A'] ['T'] ['T', 'G'] ['A'] ['T'] ['A', 'G', 'A'] ['T'] Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: String Splitter Brain Teaser
> That is clearer. At this point, though, you don't need the enumerator any > more > (so you can avoid indexing each item): Good point. > > def xgen(s): > srciter = iter(s) > item = [srciter.next()] > for i in srciter: > if i == '/': > item.append(srciter.next()) > else: > yield item > item = [i] > yield item > For some reason, keeping the != first feels a lot more logical to me, but I think that's just a reflection of my particular mental model of the problem. Also, item is a much clearer name than stack; I chose stack just to point out how similar the solution to objection 2 was to yours. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: PyParsing module or HTMLParser
On 28 Mar 2005 12:01:34 -0800, Lad <[EMAIL PROTECTED]> wrote: > I came across pyparsing module by Paul McGuire. It seems to be nice but > I am not sure if it is the best for my need. > I need to extract some text from html page. The text is in tables and a > table can be inside another table. > Is it better and easier to use the pyparsing module or HTMLparser? > You might want to check out BeautifulSoup at: http://www.crummy.com/software/BeautifulSoup/ . Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Little Q: how to print a variable's name, not its value?
On Tue, 29 Mar 2005 14:34:39 GMT, Ron_Adam <[EMAIL PROTECTED]> wrote: > On 28 Mar 2005 23:01:34 -0800, "Dan Bishop" <[EMAIL PROTECTED]> wrote: > > >>>> def print_vars(vars_dict=None): > >...if vars_dict is None: > >... vars_dict = globals() > >...for var, value in vars_dict.items(): > >... print '%s = %r' % (var, value) > >... > >>>> myPlace = 'right here' > >>>> myTime = 'right now' > >>>> print_vars() > >print_vars = > >__builtins__ = > >myTime = 'right now' > >myPlace = 'right here' > >__name__ = '__main__' > >__doc__ = None > > Fred = 5 > John = 8 > Winner = John > > Both John and Winner are pointing to the literal '8'. ummm, yes, of course they are. What's your point? > > Mixing data and program code, ie.. variable names as data, is not a > good idea. Down with eval! Exile exec! A pox on both their houses! (i.e. I respectfully disagree that mixing data with program code is a bad idea) Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Little Q: how to print a variable's name, not its value?
On Tue, 29 Mar 2005 18:08:04 GMT, Cameron Laird <[EMAIL PROTECTED]> wrote: > In article <[EMAIL PROTECTED]>, > Bill Mill <[EMAIL PROTECTED]> wrote: > . > . > . > >(i.e. I respectfully disagree that mixing data with program code is a bad > >idea) > . > . > . > Most applications (as opposed to "system") developers should be doing > it rarely. VERY often when they seem inclined to do so, it's just a > symptom of a confused design. On that point I can agree. I just wanted to say that there are valuable uses for mixing code and data. As soon as I sent my email out, I realized that I meant to say that mixing data with program code is *not always* a bad idea. Particularly, yelling at this guy for wanting to figure out a variable's name is, I think, overzealous. If he just wants to use it for debugging, then I don't know why he needs to be shouted down. > > In no way do I mean to restrict knowledgeable metaclass authors, LISPers, > debugger inventors, or other legitimate exceptions to my claim. I'm confused. Are you ron_adam, cameron laird, or both? Either way, your point is well taken, and I should have been clearer in my overly glib email. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is easier? Translating from C++ or from Java...
On 29 Mar 2005 11:02:38 -0800, cjl <[EMAIL PROTECTED]> wrote: > Hey all: > > Thanks for the responses... > > I've found a third open source implementation in pascal (delphi), and > was wondering how well that would translate to python? cjl, I think that the responses on the list so far collectively point the the correct answer: mu. pick your favorite, or the most well written, or the clearest code, or the one that contains the most 'e's, and try to translate it. Whichever you choose, dig deep enough to get to the underlying meaning of what's going on so that you don't end up writing another language in python. If you do this, it doesn't matter which of the three you pick. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Little Q: how to print a variable's name, not its value?
> >> > >> Fred = 5 > >> John = 8 > >> Winner = John > >> > >> Both John and Winner are pointing to the literal '8'. > > > >ummm, yes, of course they are. What's your point? > > Hi Bill, > > My point is if you look up the name and print it, you may get. > > Instead of: > > Fred has 5 points > John has 8 points > > You could get: > > Fred has 5 points > Winner has 8 points > > Or something else depending on how many references you made to the > value 8. Yes, this is true, assuming that he looks for keys with the value 8 in locals(). It's not necessarily true if there's a way to ask python what the name of John is, which is what the OP was asking. I just wanted you to explicitly say what you were implying so that we could discuss it. > > >> Mixing data and program code, ie.. variable names as data, is not a > >> good idea. > > > >Down with eval! Exile exec! A pox on both their houses! > > > >(i.e. I respectfully disagree that mixing data with program code is a bad > >idea) > > (I respectfully acknowledged your opinion.) > > To be fair, it's not always bad. But in most cases it is better not > too. > Yup, I meant to say that I disagree that mixing data with program code is *always* a bad idea. I had a "d'oh!" moment when I hit send. Naturally, I wasn't suggesting that anyone (shudder) do things like your examples of poor code. I had a much smaller point, about which I was not clear: Sometimes, it is handy to mix code and data. There *are* legitimate uses of reflection, eval, and exec. I was too glib in my response to you. I intended no disrespect, only silliness. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Little Q: how to print a variable's name, not its value?
On 31 Mar 2005 08:13:30 GMT, Duncan Booth <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > > But surely if you create an integer object and assign it a value, e.g. > > a = 3, > > why shouldn't Python be able to tell you something like the following: > > name(a) >>> 'a' > > ? > > But why should it return 'a' and not one of these? Because the python interpreter certainly knows the name of all the variables used in a python program. So, a name() function as discussed here should return the name of the exact variable passed in as input, not any variable which refers to the value 3 (which is of course constant). We're imagining a new function, not discussing yours. . > > The key here is to understand the difference between languages like C where > a variable is a container used to store a value and Python where variables > are simply the binding of names to objects. I don't see any technical barrier to implementing a name() function, except that either the current CPython doesn't allow this particular reflection from python, or it's difficult to do. Just because a variable is simply a name binding doesn't mean Python couldn't return the name of the binding. I'm not definitely saying that I *want* an easy name() function - there's a lot of potential for abuse - but I would at least like to see it accomplished before I decide whether I like it or not. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Little Q: how to print a variable's name, not its value?
On Thu, 31 Mar 2005 03:33:10 -0500, Bill Mill <[EMAIL PROTECTED]> wrote: > On 31 Mar 2005 08:13:30 GMT, Duncan Booth <[EMAIL PROTECTED]> wrote: > > [EMAIL PROTECTED] wrote: > > > > > But surely if you create an integer object and assign it a value, e.g. > > > a = 3, > > > why shouldn't Python be able to tell you something like the following: > > > name(a) >>> 'a' > > > ? > > > > But why should it return 'a' and not one of these? > > Because the python interpreter certainly knows the name of all the > variables used in a python program. So, a name() function as discussed > here should return the name of the exact variable passed in as input, > not any variable which refers to the value 3 (which is of course > constant). We're imagining a new function, not discussing yours. Reason #312 not to post at 3:30 AM: Ok, the variable name is evaluated before being passed to the function. Thus, it would require some pretty heavy interpreter trickery to implement. I take back what I said. Humbly, Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Pseudocode in the wikipedia
On Apr 1, 2005 3:15 PM, James Stroud <[EMAIL PROTECTED]> wrote: > Is anybody else bothered by those stupid pascal-like ":=" assignment > operators? > I actually like them. I think that the = should be a comparison operator, not a silly ==. I think that comparisons using = are much clearer, especially since you often write many of them in a row, whereas you almost always make one assignment per line. I use := every day in PL/SQL, and it's one of the few positive syntactical features of the language. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to reload local namespace definitions in the python interpreter?
On Apr 4, 2005 11:10 AM, Steve Holden <[EMAIL PROTECTED]> wrote: > Tim Jarman wrote: > > [EMAIL PROTECTED] wrote: > > > > > >>Hi, > >> > >>I am a beginner using the python interpreter. To reduce typing effort, > >>I created a module called "aliases.py" containing some aliases for > >>objects I commonly use like - > >> > >>aliases.py : > >> > >> > >>import filecmp, os, commands > >> > >>op = os.path > > By the way, are you aware of the import ... as ... idiom? > > e.g. import os.path as op > > > > This would, of course, require the user to qualify the names by > prefixing them with "op.". > What the OP listed above requires that too. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: change extensions
On Apr 5, 2005 10:43 AM, Jeffrey Maitland <[EMAIL PROTECTED]> wrote: > That approach works, but so does this one. > > import os, glob > > input_file_list = glob.glob('*.txt') > for in_file in input_file_list: > name = in_file.split('.') > os.rename(in_file, str(name[0]) + '.' + 'text')) > you should really use in_file.splitext - your script renames myfile.with.lots.of.dots.txt to myfile.text instead of myfile.with.lots.of.dots.text . If you *really* wanted to use split(), it oughta be ''.join(in_file.split('.')[:-1]) , but why not use the built-in? Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: shebang in cross platform scripts
On Apr 6, 2005 9:37 AM, rbt <[EMAIL PROTECTED]> wrote: > > Haven't tested this on Windows yet... thought I'd ask here: > > Does the line below have any negative impact on Windows machines? I > develop and test mostly on Unix, but my scripts are often used on Win > systems too. > > #!/usr/bin/env python What the others have said already is true, that it will be ignored on windows, with one caveat. The shebang is interpreted by Apache if your script is a CGI script. So, if your script is a CGI, you will need to have a windows version and a nix version. Peace Bill Mill bill.mill at gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: shebang in cross platform scripts
On Apr 6, 2005 11:06 AM, Ivan Van Laningham <[EMAIL PROTECTED]> wrote: > Hi All-- > > Simon Brunning wrote: > > > > On Apr 6, 2005 2:37 PM, rbt <[EMAIL PROTECTED]> wrote: > > > > > Does the line below have any negative impact on Windows machines? I > > > develop and test mostly on Unix, but my scripts are often used on Win > > > systems too. > > > > > > #!/usr/bin/env python > > > > Nope. On Windows it's just a comment. > > > > It works fine using cygwin, uwin, mks and pdksh, all available for > Windows. Google is your friend. > > Symbolic links also work under uwin (don't know for sure about the > others). That means you can install a link in /usr/bin to whereever > python lives, and expect #!/usr/bin/python to work just fine. This works in cygwin as well; I didn't mention cygwin since the OP seemed to be asking about windows distribution, but it's a good point. Peace Bill Mill -- http://mail.python.org/mailman/listinfo/python-list