Re: A curious bit of code...

2014-02-14 Thread Dave Angel
Terry Reedy tjre...@udel.edu Wrote in message: On 2/13/2014 1:37 PM, forman.si...@gmail.com wrote: I ran across this and I thought there must be a better way of doing it, but then after further consideration I wasn't so sure. if key[:1] + key[-1:] == '': ... if key[:1] == '' and

Re: A curious bit of code...

2014-02-14 Thread Roy Smith
In article mailman.6914.1392380171.18130.python-l...@python.org, Dave Angel da...@davea.name wrote: Terry Reedy tjre...@udel.edu Wrote in message: On 2/13/2014 1:37 PM, forman.si...@gmail.com wrote: I ran across this and I thought there must be a better way of doing it, but then after

Re: A curious bit of code...

2014-02-14 Thread forman . simon
On Thursday, February 13, 2014 7:26:48 PM UTC-8, Ned Batchelder wrote: On 2/13/14 9:45 PM, forman.si...@gmail.com wrote: For the record I wasn't worried about the performance. ;-) It was for Tkinter event strings not markup tags. I'm glad this was the time winner! key

Re: A curious bit of code...

2014-02-14 Thread Mark Lawrence
On 14/02/2014 20:04, forman.si...@gmail.com wrote: On Thursday, February 13, 2014 7:26:48 PM UTC-8, Ned Batchelder wrote: On 2/13/14 9:45 PM, forman.si...@gmail.com wrote: For the record I wasn't worried about the performance. ;-) It was for Tkinter event strings not markup tags.

Re: A curious bit of code...

2014-02-14 Thread Simon Forman
On Friday, February 14, 2014 1:01:48 PM UTC-8, Mark Lawrence wrote: [snip] Pleased to have you on board, as I'm know that Terry Reedy et al can do with a helping hand. But please note you appear to be using google groups, hence the double line spacing above and trying to reply to

A curious bit of code...

2014-02-14 Thread Simon Forman
(Apologies if this results in a double-post.) On Friday, February 14, 2014 1:01:48 PM UTC-8, Mark Lawrence wrote: [snip] Pleased to have you on board, as I'm know that Terry Reedy et al can do with a helping hand. But please note you appear to be using google groups, hence the double

A curious bit of code...

2014-02-13 Thread forman . simon
I ran across this and I thought there must be a better way of doing it, but then after further consideration I wasn't so sure. if key[:1] + key[-1:] == '': ... Some possibilities that occurred to me: if key.startswith('') and key.endswith(''): ... and: if (key[:1], key[-1:]) == ('',

Re: A curious bit of code...

2014-02-13 Thread Roy Smith
In article 4cc09129-43ee-4205-a24c-03f92b594...@googlegroups.com, forman.si...@gmail.com wrote: I ran across this and I thought there must be a better way of doing it, but then after further consideration I wasn't so sure. if key[:1] + key[-1:] == '': ... Some possibilities that

Re: A curious bit of code...

2014-02-13 Thread Ethan Furman
On 02/13/2014 10:37 AM, forman.si...@gmail.com wrote: I ran across this and I thought there must be a better way of doing it, but then after further consideration I wasn't so sure. if key[:1] + key[-1:] == '': ... Some possibilities that occurred to me: if key.startswith('') and

Re: A curious bit of code...

2014-02-13 Thread Mark Lawrence
On 13/02/2014 18:37, forman.si...@gmail.com wrote: I ran across this and I thought there must be a better way of doing it, but then after further consideration I wasn't so sure. if key[:1] + key[-1:] == '': ... Some possibilities that occurred to me: if key.startswith('') and

Re: A curious bit of code...

2014-02-13 Thread Ethan Furman
On 02/13/2014 11:09 AM, Mark Lawrence wrote: All I can say is that if you're worried about the speed of a single line of code like the above then you've got problems. Having said that, I suspect that using an index to extract a single character has to be faster than using a slice, but I

Re: A curious bit of code...

2014-02-13 Thread Alain Ketterlin
forman.si...@gmail.com writes: I ran across this and I thought there must be a better way of doing it, but then after further consideration I wasn't so sure. if key[:1] + key[-1:] == '': ... Some possibilities that occurred to me: if key.startswith('') and key.endswith(''): ... and:

Re: A curious bit of code...

2014-02-13 Thread Ethan Furman
On 02/13/2014 11:20 AM, Ethan Furman wrote: On 02/13/2014 11:09 AM, Mark Lawrence wrote: All I can say is that if you're worried about the speed of a single line of code like the above then you've got problems. Having said that, I suspect that using an index to extract a single character has

Re: A curious bit of code...

2014-02-13 Thread Neil Cerutti
On 2014-02-13, Ethan Furman et...@stoneleaf.us wrote: On 02/13/2014 11:09 AM, Mark Lawrence wrote: All I can say is that if you're worried about the speed of a single line of code like the above then you've got problems. Having said that, I suspect that using an index to extract a single

Re: A curious bit of code...

2014-02-13 Thread Neil Cerutti
On 2014-02-13, forman.si...@gmail.com forman.si...@gmail.com wrote: I ran across this and I thought there must be a better way of doing it, but then after further consideration I wasn't so sure. if key[:1] + key[-1:] == '': ... Some possibilities that occurred to me: if

Re: A curious bit of code...

2014-02-13 Thread Mark Lawrence
On 13/02/2014 19:25, Neil Cerutti wrote: On 2014-02-13, Ethan Furman et...@stoneleaf.us wrote: On 02/13/2014 11:09 AM, Mark Lawrence wrote: All I can say is that if you're worried about the speed of a single line of code like the above then you've got problems. Having said that, I suspect that

Re: A curious bit of code...

2014-02-13 Thread Roy Smith
In article mailman.6859.1392319225.18130.python-l...@python.org, Ethan Furman et...@stoneleaf.us wrote: On 02/13/2014 11:09 AM, Mark Lawrence wrote: All I can say is that if you're worried about the speed of a single line of code like the above then you've got problems. Having said

Re: A curious bit of code...

2014-02-13 Thread Peter Otten
forman.si...@gmail.com wrote: I ran across this and I thought there must be a better way of doing it, but then after further consideration I wasn't so sure. if key[:1] + key[-1:] == '': ... Some possibilities that occurred to me: if key.startswith('') and key.endswith(''): ...

Re: A curious bit of code...

2014-02-13 Thread Ethan Furman
On 02/13/2014 11:17 AM, Neil Cerutti wrote: On 2014-02-13, forman.si...@gmail.com forman.si...@gmail.com wrote: I ran across this and I thought there must be a better way of doing it, but then after further consideration I wasn't so sure. if key[:1] + key[-1:] == '': ... Some possibilities

Re: A curious bit of code...

2014-02-13 Thread Neil Cerutti
On 2014-02-13, Peter Otten __pete...@web.de wrote: forman.si...@gmail.com wrote: The first is too clever for my taste. The second is fast and easy to understand. It might attract improvements replacing the slice with an index, but I trust you will catch that with your unit tests ;) It's

Re: A curious bit of code...

2014-02-13 Thread Ethan Furman
On 02/13/2014 11:43 AM, Peter Otten wrote: forman.si...@gmail.com wrote: I ran across this and I thought there must be a better way of doing it, but then after further consideration I wasn't so sure. if key[:1] + key[-1:] == '': ... Some possibilities that occurred to me: if

Re: A curious bit of code...

2014-02-13 Thread Marko Rauhamaa
Peter Otten __pete...@web.de: Personally, I'm willing to spend the few extra milliseconds and use the foolproof third. Speaking of foolproof, what is this key? Is it an XML start tag, maybe? Then, how does your test fare with, say, start comparison= which is equivalent to start

Re: A curious bit of code...

2014-02-13 Thread Zachary Ware
On Thu, Feb 13, 2014 at 12:37 PM, forman.si...@gmail.com wrote: I ran across this and I thought there must be a better way of doing it, but then after further consideration I wasn't so sure. if key[:1] + key[-1:] == '': ... Some possibilities that occurred to me: if

Re: A curious bit of code...

2014-02-13 Thread Chris Angelico
On Fri, Feb 14, 2014 at 6:32 AM, Mark Lawrence breamore...@yahoo.co.uk wrote: There will be an exception only if it is zero-length. But good point! That's a pretty sneaky way to avoid checking for a zero-length string. Is it a popular idiom? I hope not. The use of slicing rather than

Re: A curious bit of code...

2014-02-13 Thread Tim Chase
On 2014-02-13 10:37, forman.si...@gmail.com wrote: I ran across this and I thought there must be a better way of doing it, but then after further consideration I wasn't so sure. Some possibilities that occurred to me: if key.startswith('') and key.endswith(''): ... This is my favorite

Re: A curious bit of code...

2014-02-13 Thread Emile van Sebille
On 2/13/2014 11:59 AM, Zachary Ware wrote: In a fit of curiosity, I did some timings: Snip of lots of TMTOWTDT/TIMTOWTDI/whatever... timed examples :) But I didn't see this one: s[::len(s)-1] Emile -- https://mail.python.org/mailman/listinfo/python-list

Re: A curious bit of code...

2014-02-13 Thread Neil Cerutti
On 2014-02-13, Zachary Ware zachary.ware+pyl...@gmail.com wrote: In a fit of curiosity, I did some timings: 'and'ed indexing: C:\tmppy -m timeit -s key = 'test' key[0] == '' and key[-1] == '' 100 loops, best of 3: 0.35 usec per loop C:\tmppy -m timeit -s key = 'test' key[0] == '' and

Re: A curious bit of code...

2014-02-13 Thread Chris Angelico
On Fri, Feb 14, 2014 at 7:55 AM, Emile van Sebille em...@fenx.com wrote: On 2/13/2014 11:59 AM, Zachary Ware wrote: In a fit of curiosity, I did some timings: Snip of lots of TMTOWTDT/TIMTOWTDI/whatever... timed examples :) But I didn't see this one: s[::len(s)-1]

Re: A curious bit of code...

2014-02-13 Thread Peter Otten
Chris Angelico wrote: On Fri, Feb 14, 2014 at 7:55 AM, Emile van Sebille em...@fenx.com wrote: On 2/13/2014 11:59 AM, Zachary Ware wrote: In a fit of curiosity, I did some timings: Snip of lots of TMTOWTDT/TIMTOWTDI/whatever... timed examples :) But I didn't see this one: s[::len(s)-1]

Re: A curious bit of code...

2014-02-13 Thread Chris Angelico
On Fri, Feb 14, 2014 at 8:06 AM, Peter Otten __pete...@web.de wrote: For the record: s = x s[::len(s)-1] Traceback (most recent call last): File stdin, line 1, in module ValueError: slice step cannot be zero And that, my friends, is a classic example of a Python exception that ought to

Re: A curious bit of code...

2014-02-13 Thread Mark Lawrence
On 13/02/2014 21:01, Neil Cerutti wrote: On 2014-02-13, Zachary Ware zachary.ware+pyl...@gmail.com wrote: In a fit of curiosity, I did some timings: 'and'ed indexing: C:\tmppy -m timeit -s key = 'test' key[0] == '' and key[-1] == '' 100 loops, best of 3: 0.35 usec per loop C:\tmppy -m

Re: A curious bit of code...

2014-02-13 Thread Zachary Ware
On Thu, Feb 13, 2014 at 2:55 PM, Emile van Sebille em...@fenx.com wrote: On 2/13/2014 11:59 AM, Zachary Ware wrote: In a fit of curiosity, I did some timings: Snip of lots of TMTOWTDT/TIMTOWTDI/whatever... timed examples :) But I didn't see this one: s[::len(s)-1] It's not great, around

Re: A curious bit of code...

2014-02-13 Thread Zachary Ware
On Thu, Feb 13, 2014 at 3:01 PM, Neil Cerutti ne...@norwich.edu wrote: On 2014-02-13, Zachary Ware zachary.ware+pyl...@gmail.com wrote: C:\tmppy -m timeit -s key = '' key[0] == '' and key[-1] == '' Traceback (most recent call last): File P:\Python34\lib\timeit.py, line 292, in main x =

Re: A curious bit of code...

2014-02-13 Thread Roy Smith
In article mailman.6880.1392324956.18130.python-l...@python.org, Emile van Sebille em...@fenx.com wrote: On 2/13/2014 11:59 AM, Zachary Ware wrote: In a fit of curiosity, I did some timings: Snip of lots of TMTOWTDT/TIMTOWTDI/whatever... timed examples :) But I didn't see this one:

Re: A curious bit of code...

2014-02-13 Thread Emile van Sebille
On 2/13/2014 1:10 PM, Chris Angelico wrote: On Fri, Feb 14, 2014 at 8:06 AM, Peter Otten __pete...@web.de wrote: For the record: s = x s[::len(s)-1] Traceback (most recent call last): File stdin, line 1, in module ValueError: slice step cannot be zero And that, my friends, is a classic

Re: A curious bit of code...

2014-02-13 Thread Chris Angelico
On Fri, Feb 14, 2014 at 8:19 AM, Zachary Ware zachary.ware+pyl...@gmail.com wrote: Also, uglier than sin itself. Hey hey, no need to insult our lovely trigonometric functions! ChrisA -- https://mail.python.org/mailman/listinfo/python-list

Re: A curious bit of code...

2014-02-13 Thread Roy Smith
In article mailman.6891.1392327074.18130.python-l...@python.org, Chris Angelico ros...@gmail.com wrote: On Fri, Feb 14, 2014 at 8:19 AM, Zachary Ware zachary.ware+pyl...@gmail.com wrote: Also, uglier than sin itself. Hey hey, no need to insult our lovely trigonometric functions! This

Re: A curious bit of code...

2014-02-13 Thread Zachary Ware
On Thu, Feb 13, 2014 at 3:31 PM, Chris Angelico ros...@gmail.com wrote: On Fri, Feb 14, 2014 at 8:19 AM, Zachary Ware zachary.ware+pyl...@gmail.com wrote: Also, uglier than sin itself. Hey hey, no need to insult our lovely trigonometric functions! Here's your sine... -- Zach --

Re: A curious bit of code...

2014-02-13 Thread Serhiy Storchaka
13.02.14 21:59, Zachary Ware написав(ла): don't use re for simple stuff (because while it may be very fast, it's dominated by attribute lookup and function call overhead), And the time of re variant depends on the size of input. -- https://mail.python.org/mailman/listinfo/python-list

Re: A curious bit of code...

2014-02-13 Thread Roy Smith
In article mailman.6893.1392328170.18130.python-l...@python.org, Serhiy Storchaka storch...@gmail.com wrote: 13.02.14 21:59, Zachary Ware написав(ла): don't use re for simple stuff (because while it may be very fast, it's dominated by attribute lookup and function call overhead),

Re: A curious bit of code...

2014-02-13 Thread Ethan Furman
On 02/13/2014 01:01 PM, Chris Angelico wrote: On Fri, Feb 14, 2014 at 7:55 AM, Emile van Sebille em...@fenx.com wrote: On 2/13/2014 11:59 AM, Zachary Ware wrote: In a fit of curiosity, I did some timings: Snip of lots of TMTOWTDT/TIMTOWTDI/whatever... timed examples :) But I didn't see

Re: A curious bit of code...

2014-02-13 Thread Chris Angelico
On Fri, Feb 14, 2014 at 8:33 AM, Ethan Furman et...@stoneleaf.us wrote: Oh, it's not that bad! All you have to do is handle the edge case of an empty string: s[::len(s)-1 if s else True] *ducks and runs* And the edge case of the one-character string. Also, it's been noted that calling the

Re: A curious bit of code...

2014-02-13 Thread Ethan Furman
On 02/13/2014 02:13 PM, Chris Angelico wrote: On Fri, Feb 14, 2014 at 8:33 AM, Ethan Furman et...@stoneleaf.us wrote: Oh, it's not that bad! All you have to do is handle the edge case of an empty string: s[::len(s)-1 if s else True] And the edge case of the one-character string. Oops, my

Re: A curious bit of code...

2014-02-13 Thread Terry Reedy
On 2/13/2014 1:37 PM, forman.si...@gmail.com wrote: I ran across this and I thought there must be a better way of doing it, but then after further consideration I wasn't so sure. if key[:1] + key[-1:] == '': ... if key[:1] == '' and key[-1:] == ': ... is the obvious choice to me. If the

Re: A curious bit of code...

2014-02-13 Thread Ethan Furman
On 02/13/2014 01:24 PM, Roy Smith wrote: Emile van Sebille wrote: But I didn't see this one: s[::len(s)-1] I love it. I need to add this to my list of Python trivia questions. Great interview question: What does this do? What is its weakness? How would you fix it? -- ~Ethan~ --

Re: A curious bit of code...

2014-02-13 Thread forman . simon
For the record I wasn't worried about the performance. ;-) It was for Tkinter event strings not markup tags. I'm glad this was the time winner! key and key[0] == '' and key[-1] == '' Cheers to the folks who did the timings (and saved me from the trouble!) Last but not least...

Re: A curious bit of code...

2014-02-13 Thread Ned Batchelder
On 2/13/14 9:45 PM, forman.si...@gmail.com wrote: For the record I wasn't worried about the performance. ;-) It was for Tkinter event strings not markup tags. I'm glad this was the time winner! key and key[0] == '' and key[-1] == '' Cheers to the folks who did the timings (and saved me