Re: Please fix your clock [was Re: Multiple equates]
On Thu, 4 Dec 2008 06:40:02 +0200 "Hendrik van Rooyen" <[EMAIL PROTECTED]> wrote: > "Andreas Waldenburger" <[EMAIL PROTECTED]> > > > On 04 Dec 2008 15:53:21 GMT Steven D'Aprano > > <[EMAIL PROTECTED]> wrote: > > > > > Hendrik, I think your PC's clock is wrong. You seem to be posting > > > from the future. > > > > So? Maybe he is. What's your problem? > > It was probably playing hob with his threading reader... > Sorry, I should probably have enclosed that in a tag. ;) /W -- My real email address is constructed by swapping the domain with the recipient (local part). -- http://mail.python.org/mailman/listinfo/python-list
Re: Please fix your clock [was Re: Multiple equates]
Steven D'Aprano wrote: Hendrik, I think your PC's clock is wrong. You seem to be posting from the future. further evidence of the Guido's time-machine[1]. For the secret cabal of core developers who borrow the time-machine, I recommend not sending email from within it. :) -tkc [1] http://catb.org/esr/jargon/html/G/Guido.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Please fix your clock [was Re: Multiple equates]
"Andreas Waldenburger" <[EMAIL PROTECTED]> > On 04 Dec 2008 15:53:21 GMT Steven D'Aprano > <[EMAIL PROTECTED]> wrote: > > > Hendrik, I think your PC's clock is wrong. You seem to be posting > > from the future. > > So? Maybe he is. What's your problem? It was probably playing hob with his threading reader... - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: Please fix your clock [was Re: Multiple equates]
"Steven D'Aprano" wrote: >Hendrik, I think your PC's clock is wrong. You seem to be posting from >the future. I always knew I was more advanced than other people... :-) Well spotted! Thanks - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: Please fix your clock [was Re: Multiple equates]
On 04 Dec 2008 15:53:21 GMT Steven D'Aprano <[EMAIL PROTECTED]> wrote: > Hendrik, I think your PC's clock is wrong. You seem to be posting > from the future. So? Maybe he is. What's your problem? /W -- My real email address is constructed by swapping the domain with the recipient (local part). -- http://mail.python.org/mailman/listinfo/python-list
Please fix your clock [was Re: Multiple equates]
Hendrik, I think your PC's clock is wrong. You seem to be posting from the future. -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
On Thu, Dec 4, 2008 at 1:19 PM, Hendrik van Rooyen <[EMAIL PROTECTED]>wrote: > > "Cameron Laird" <[EMAIL PROTECTED]> wrote: > > > There's a realm within Pythonia that favors lambdalessness. > > And who, may I ask, Is the King of this realm? > > - Hendrik > > -- > http://mail.python.org/mailman/listinfo/python-list > Guido? -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
"Cameron Laird" <[EMAIL PROTECTED]> wrote: > There's a realm within Pythonia that favors lambdalessness. And who, may I ask, Is the King of this realm? - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
On Dec 4, 1:52 pm, Lawrence D'Oliveiro <[EMAIL PROTECTED] central.gen.new_zealand> wrote: > Why is that? Was Rossum frightened in his cradle by a lambda when he was a > baby? Are some people afraid of lambdas the way others are afraid of > spiders? "Language designers are a superstitious and cowardly lot..." -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
In message <[EMAIL PROTECTED]>, Cameron Laird wrote: > There's a realm within Pythonia that favors lambdalessness. Why is that? Was Rossum frightened in his cradle by a lambda when he was a baby? Are some people afraid of lambdas the way others are afraid of spiders? -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
In article <[EMAIL PROTECTED]>, Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: >In message <[EMAIL PROTECTED]>, Cameron Laird wrote: > >>def shell_escape(Arg) : >>"""returns Arg suitably escaped for use as a command-line argument >>to Bash.""" >> >>pattern = r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]" >>def f1(Match): >> return "\\" + Match.group(0) >>return re.sub(pattern, f1, Arg) >> # Need to catch anything that might be meaningful to shell >>#end shell_escape > >So why is that better? I'm very lost, now. As best I can remember and/or reconstruct this conversation, the form above is adequately readable and does not involve lambda (and fits in eighty-column lines, and has a few even more mundane features). There's a realm within Pythonia that favors lambdalessness. I think that's all I'm claiming at this point. -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
Hi, what about numpy? import numpy a = numpy.ones((10,),dtype=numpy.bool) I = [1,3,8] a[I]=False print a gives: [ True False True False True True True True False True] Almar -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
On Wed, Dec 3, 2008 at 6:23 AM, Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > > So why is that better? > -- > http://mail.python.org/mailman/listinfo/python-list > I personally think that it looks marginally cleaner (indentation issues aside). Do you think it's substantially worse? If so, why? I mean you asked how you would express the original code with the lambda - that's how you would, by pulling the lambda out into a function. How would you express it better is a different question. I think that the code in question is an example of a case where a lambda is appropriate in python, but it's definitely not any worse for having the code pulled out. If the function wasn't trival, you'd have to anyhow. The whole "lambdas good or bad" thing has been done to death as far as it relates to python. For python, it seems that anonymous functions are a no-no unless they're effectively one liners. -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
In message <[EMAIL PROTECTED]>, Cameron Laird wrote: >def shell_escape(Arg) : >"""returns Arg suitably escaped for use as a command-line argument >to Bash.""" > >pattern = r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]" >def f1(Match): > return "\\" + Match.group(0) >return re.sub(pattern, f1, Arg) > # Need to catch anything that might be meaningful to shell >#end shell_escape So why is that better? -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
In article <[EMAIL PROTECTED]>, Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: >Cameron Laird wrote: > >>def f1(Match): >>return > >Something missing here? Ugh; yes, sorry: def shell_escape(Arg) : """returns Arg suitably escaped for use as a command-line argument to Bash.""" pattern = r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]" def f1(Match): return "\\" + Match.group(0) return re.sub(pattern, f1, Arg) # Need to catch anything that might be meaningful to shell #end shell_escape -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
Cameron Laird wrote: >def f1(Match): >return Something missing here? -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
In article <[EMAIL PROTECTED]>, Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: >In message <[EMAIL PROTECTED]>, Cameron Laird wrote: > >> In article <[EMAIL PROTECTED]>, >> Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: >> >>>Cameron Laird wrote: >>> I've been trying to decide if there's any sober reason to advocate the one-liner map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024]) >>> >>>Are lambdas like the Dark Side of Python? >>> >>>:) >> >> Enough so, apparently, that I'm reluctant even to touch that question. > >So how else would you express something like > >def shell_escape(Arg) : >"""returns Arg suitably escaped for use as a command-line argument >to Bash.""" >return \ >re.sub \ > ( >r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]", >lambda Match : "\\" + Match.group(0), >Arg > ) > # Need to catch anything that might be meaningful to shell >#end shell_escape > >? I suspect we're confusing each other. I *like* lambdas--at least, more than Guido does, which I recognize is a low standard. When I take your question at face value, my response is def shell_escape(Arg) : """returns Arg suitably escaped for use as a command-line argument to Bash.""" pattern = r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]" def f1(Match): return return re.sub(pattern, f1, Arg) # Need to catch anything that might be meaningful to shell #end shell_escape 'cept that I'd hope to find a way to simplify pattern. Was that what you were asking? -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
In message <[EMAIL PROTECTED]>, Cameron Laird wrote: > In article <[EMAIL PROTECTED]>, > Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > >>Cameron Laird wrote: >> >>> I've been trying to decide if there's any sober reason to advocate >>> the one-liner >>> >>> map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024]) >> >>Are lambdas like the Dark Side of Python? >> >>:) > > Enough so, apparently, that I'm reluctant even to touch that question. So how else would you express something like def shell_escape(Arg) : """returns Arg suitably escaped for use as a command-line argument to Bash.""" return \ re.sub \ ( r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]", lambda Match : "\\" + Match.group(0), Arg ) # Need to catch anything that might be meaningful to shell #end shell_escape ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
In article <[EMAIL PROTECTED]>, Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: >Cameron Laird wrote: > >> I've been trying to decide if there's any sober reason to advocate >> the one-liner >> >> map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024]) > >Are lambdas like the Dark Side of Python? > >:) Enough so, apparently, that I'm reluctant even to touch that question. -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
On Nov 25, 11:29 am, Iain King <[EMAIL PROTECTED]> wrote: > On Nov 17, 7:41 pm, Tim Chase <[EMAIL PROTECTED]> wrote: > > > > > > It doesn't matter as none of this is valid Python. In Python you have to > > > write > > > > array[x1] = False > > > array[x2] = False > > > Uh...not so much... > > > >>> a = [1,2,3,4,5] > > >>> x1, x2 = 1, 3 > > >>> a[x1] = a[x2] = False > > >>> a > > [1, False, 3, False, 5] > > > Works for me. > > > To the OP, I think rather than cluttering my code, I'd just > > create a loop > > >for i in [x1,x2,x3,x4,...x1024]: > > a[i] = False > > > From Diez's disassembly of it (as an aside, nifty little intro > > to dis.dis()...thanks, Diez!), it looks like it boils down to "is > > DUP_TOP faster than LOAD_CONST" because the rest of the > > operations. At this point, it's pretty nitty-gritty. > > > Unless the code is in an inner loop somewhere, the simple loop > > should be more than fast enough. Without knowing the source of > > the [x1,...] index variables, it's hard to tell if there's a more > > optimal way to do this. > > > -tkc > > The loop is much nicer, especially as your array gets longer. The > generic: > > for i in xrange(array): > array[i] = False > > will set the entire array. > > Regarding your original question: > > array[x1] = array[x2] = False > > array[x1] = False > array[x2] = False > > These two blocks are functionally the same when you are setting to > True or False (or any immutable), but are not if setting to > immutables, which could give you some real head-scratching bugs if you > were unaware of the difference - the first version assigns the same > object to both names: > > >>> array[x1] = array[x2] = [] > >>> array[x1].append("Hi") > >>> array[x2] > > ['Hi'] > > Iain ...and of course, the second time I say 'immutable' I mean 'mutable'. Hopefully the example was clearer than the text. Iain -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
On Nov 17, 7:41 pm, Tim Chase <[EMAIL PROTECTED]> wrote: > > It doesn't matter as none of this is valid Python. In Python you have to > > write > > > array[x1] = False > > array[x2] = False > > Uh...not so much... > > >>> a = [1,2,3,4,5] > >>> x1, x2 = 1, 3 > >>> a[x1] = a[x2] = False > >>> a > [1, False, 3, False, 5] > > Works for me. > > To the OP, I think rather than cluttering my code, I'd just > create a loop > >for i in [x1,x2,x3,x4,...x1024]: > a[i] = False > > From Diez's disassembly of it (as an aside, nifty little intro > to dis.dis()...thanks, Diez!), it looks like it boils down to "is > DUP_TOP faster than LOAD_CONST" because the rest of the > operations. At this point, it's pretty nitty-gritty. > > Unless the code is in an inner loop somewhere, the simple loop > should be more than fast enough. Without knowing the source of > the [x1,...] index variables, it's hard to tell if there's a more > optimal way to do this. > > -tkc The loop is much nicer, especially as your array gets longer. The generic: for i in xrange(array): array[i] = False will set the entire array. Regarding your original question: array[x1] = array[x2] = False array[x1] = False array[x2] = False These two blocks are functionally the same when you are setting to True or False (or any immutable), but are not if setting to immutables, which could give you some real head-scratching bugs if you were unaware of the difference - the first version assigns the same object to both names: >>> array[x1] = array[x2] = [] >>> array[x1].append("Hi") >>> array[x2] ['Hi'] Iain -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
Cameron Laird wrote: > I've been trying to decide if there's any sober reason to advocate > the one-liner > > map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024]) Are lambdas like the Dark Side of Python? :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
jzakiya <[EMAIL PROTECTED]> wrote: > >I looked online and in books, but couldn't find a definitive answer to >this. > >I have an array and set multiple elements to either True or False at >one time. > >Question: Which way is faster (or does it matter)? Answer: it does not matter. This is premature optimization. First, make it work. Then, figure out whether it is fast enough. THEN, figure out what's taking the most time. I'd be very, very surprised if this was an important part of your run time. -- Tim Roberts, [EMAIL PROTECTED] Providenza & Boekelheide, Inc. -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
On Nov 17, 2:54 pm, [EMAIL PROTECTED] (Cameron Laird) wrote: > In article <[EMAIL PROTECTED]>, > Tim Chase <[EMAIL PROTECTED]> wrote: > . > . > .>To the OP, I think rather than cluttering my code, > I'd just > >create a loop > > > for i in [x1,x2,x3,x4,...x1024]: > > a[i] = False > > . > . > . > I much prefer this, too. > > I've been trying to decide if there's any sober reason to advocate > the one-liner > > map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024]) > > My answer: no. well, the OP was "...faster?" not "best, cleanest, pythonic..." so the reason would be speed. (looks faster, don't know for sure) (and yeah, looks ugly. but we need the speed...) Carl K -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
In article <[EMAIL PROTECTED]>, Tim Chase <[EMAIL PROTECTED]> wrote: . . . >To the OP, I think rather than cluttering my code, I'd just >create a loop > > for i in [x1,x2,x3,x4,...x1024]: > a[i] = False . . . I much prefer this, too. I've been trying to decide if there's any sober reason to advocate the one-liner map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024]) My answer: no. -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
Arnaud Delobelle <[EMAIL PROTECTED]> writes: > jzakiya <[EMAIL PROTECTED]> writes: > >> I looked online and in books, but couldn't find a definitive answer to >> this. >> >> I have an array and set multiple elements to either True or False at >> one time. >> >> Question: Which way is faster (or does it matter)? >> >> 1) >> >> array[x1]=array[x2]== array[x10] = \ >> array[x11]=array[x12]=... = array[x20] = \ >> .. >> .. >> array[x40]=array[x41]== array[x50] = False (or True) >> >> >> 2) >> >> array[x1]=array[x2]== array[x10] = False >> array[x11]=array[x12]=... = array[x20] = False >> .. >> .. >> array[x40]=array[x41]== array[x50] = False > > It doesn't matter as none of this is valid Python. In Python you have to > write > > array[x1] = False > array[x2] = False > > Etc... Sorry, I don't know what came over me then, some kind of brain short-circuit... No doubt plenty of people will correct me before this is posted! Of course it's valid Python, but it's not very stylish. Assuming your array is a list, you could put all your indices that are meant to be True in a list: indices = [x1, x2, x3, ..., x50] Then loop over them: for i in indices: array[i] = True It might not be as fast of course -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
It doesn't matter as none of this is valid Python. In Python you have to write array[x1] = False array[x2] = False Uh...not so much... >>> a = [1,2,3,4,5] >>> x1, x2 = 1, 3 >>> a[x1] = a[x2] = False >>> a [1, False, 3, False, 5] Works for me. To the OP, I think rather than cluttering my code, I'd just create a loop for i in [x1,x2,x3,x4,...x1024]: a[i] = False From Diez's disassembly of it (as an aside, nifty little intro to dis.dis()...thanks, Diez!), it looks like it boils down to "is DUP_TOP faster than LOAD_CONST" because the rest of the operations. At this point, it's pretty nitty-gritty. Unless the code is in an inner loop somewhere, the simple loop should be more than fast enough. Without knowing the source of the [x1,...] index variables, it's hard to tell if there's a more optimal way to do this. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
Arnaud Delobelle wrote: > jzakiya <[EMAIL PROTECTED]> writes: > >> I looked online and in books, but couldn't find a definitive answer to >> this. >> >> I have an array and set multiple elements to either True or False at >> one time. >> >> Question: Which way is faster (or does it matter)? >> >> 1) >> >> array[x1]=array[x2]== array[x10] = \ >> array[x11]=array[x12]=... = array[x20] = \ >> .. >> .. >> array[x40]=array[x41]== array[x50] = False (or True) >> >> >> 2) >> >> array[x1]=array[x2]== array[x10] = False >> array[x11]=array[x12]=... = array[x20] = False >> .. >> .. >> array[x40]=array[x41]== array[x50] = False > > It doesn't matter as none of this is valid Python. In Python you have to > write > > array[x1] = False > array[x2] = False > > Etc... > No. l = range(10) l[0] = l[1] = 100 And there seems to be a small difference between both versions, but I can't judge for sure if that favors one of them - yet I tend to think it's not worth the effort... import dis def a(): l = range(10) l[0] = l[1] = 100 def b(): l = range(10) l[0] = 100 l[1] = 100 print " a ---" dis.dis(a) print " b ---" dis.dis(b) a --- 4 0 LOAD_GLOBAL 0 (range) 3 LOAD_CONST 1 (10) 6 CALL_FUNCTION1 9 STORE_FAST 0 (l) 5 12 LOAD_CONST 2 (100) *15 DUP_TOP 16 LOAD_FAST0 (l) 19 LOAD_CONST 3 (0) 22 STORE_SUBSCR 23 LOAD_FAST0 (l) 26 LOAD_CONST 4 (1) 29 STORE_SUBSCR 30 LOAD_CONST 0 (None) 33 RETURN_VALUE b --- 8 0 LOAD_GLOBAL 0 (range) 3 LOAD_CONST 1 (10) 6 CALL_FUNCTION1 9 STORE_FAST 0 (l) 9 12 LOAD_CONST 2 (100) 15 LOAD_FAST0 (l) 18 LOAD_CONST 3 (0) 21 STORE_SUBSCR 10 22 LOAD_CONST 2 (100) 25 LOAD_FAST0 (l) 28 LOAD_CONST 4 (1) 31 STORE_SUBSCR 32 LOAD_CONST 0 (None) 35 RETURN_VALUE -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
On Nov 17, 2:10 pm, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > jzakiya <[EMAIL PROTECTED]> writes: > > I looked online and in books, but couldn't find a definitive answer to > > this. > > > I have an array and set multiple elements to either True or False at > > one time. > > > Question: Which way is faster (or does it matter)? > > > 1) > > > array[x1]=array[x2]== array[x10] = \ > > array[x11]=array[x12]=... = array[x20] = \ > > .. > > .. > > array[x40]=array[x41]== array[x50] = False (or True) > > > 2) > > > array[x1]=array[x2]== array[x10] = False > > array[x11]=array[x12]=... = array[x20] = False > > .. > > .. > > array[x40]=array[x41]== array[x50] = False > > It doesn't matter as none of this is valid Python. In Python you have to > write > > array[x1] = False > array[x2] = False > > Etc... > > -- > Arnaud Could you explain please. Python allows this, so is it just not considered good idiomatic python? jz -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple equates
jzakiya <[EMAIL PROTECTED]> writes: > I looked online and in books, but couldn't find a definitive answer to > this. > > I have an array and set multiple elements to either True or False at > one time. > > Question: Which way is faster (or does it matter)? > > 1) > > array[x1]=array[x2]== array[x10] = \ > array[x11]=array[x12]=... = array[x20] = \ > .. > .. > array[x40]=array[x41]== array[x50] = False (or True) > > > 2) > > array[x1]=array[x2]== array[x10] = False > array[x11]=array[x12]=... = array[x20] = False > .. > .. > array[x40]=array[x41]== array[x50] = False It doesn't matter as none of this is valid Python. In Python you have to write array[x1] = False array[x2] = False Etc... -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Multiple equates
I looked online and in books, but couldn't find a definitive answer to this. I have an array and set multiple elements to either True or False at one time. Question: Which way is faster (or does it matter)? 1) array[x1]=array[x2]== array[x10] = \ array[x11]=array[x12]=... = array[x20] = \ .. .. array[x40]=array[x41]== array[x50] = False (or True) 2) array[x1]=array[x2]== array[x10] = False array[x11]=array[x12]=... = array[x20] = False .. .. array[x40]=array[x41]== array[x50] = False -- http://mail.python.org/mailman/listinfo/python-list