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

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 pa

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 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 winn

Re: A curious bit of code...

2014-02-14 Thread Roy Smith
In article , Dave Angel wrote: > Terry Reedy 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:] ==

Re: A curious bit of code...

2014-02-14 Thread Dave Angel
Terry Reedy 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 key[-1:]

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

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... s[::len(s

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~ -- htt

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 th

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 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 description should

Re: A curious bit of code...

2014-02-13 Thread Chris Angelico
On Fri, Feb 14, 2014 at 8:33 AM, Ethan Furman 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 built-in func

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 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[::le

Re: A curious bit of code...

2014-02-13 Thread Roy Smith
In article , Serhiy Storchaka 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), > > And the time of re variant depends on the size of input. That'

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 Zachary Ware
On Thu, Feb 13, 2014 at 3:31 PM, Chris Angelico wrote: > On Fri, Feb 14, 2014 at 8:19 AM, Zachary Ware > wrote: >> Also, uglier than sin itself. > > Hey hey, no need to insult our lovely trigonometric functions! Here's your sine... -- Zach -- https://mail.python.org/mailman/listinfo/python-li

Re: A curious bit of code...

2014-02-13 Thread Roy Smith
In article , Chris Angelico wrote: > On Fri, Feb 14, 2014 at 8:19 AM, Zachary Ware > wrote: > > Also, uglier than sin itself. > > Hey hey, no need to insult our lovely trigonometric functions! This newsgroup is taylor made for that kind of abuse. -- https://mail.python.org/mailman/listinfo/p

Re: A curious bit of code...

2014-02-13 Thread Chris Angelico
On Fri, Feb 14, 2014 at 8:19 AM, Zachary Ware 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 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 "", line 1, in ValueError: slice step cannot be zero And that, my friends, is a classic exampl

Re: A curious bit of code...

2014-02-13 Thread Roy Smith
In article , Emile van Sebille 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] > > Emile I love it. I need to add this to

Re: A curious bit of code...

2014-02-13 Thread Zachary Ware
On Thu, Feb 13, 2014 at 3:01 PM, Neil Cerutti wrote: > On 2014-02-13, Zachary Ware wrote: >> C:\tmp>py -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 = t.timeit(number) >> File "P

Re: A curious bit of code...

2014-02-13 Thread Zachary Ware
On Thu, Feb 13, 2014 at 2:55 PM, Emile van Sebille 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 0.

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 wrote: In a fit of curiosity, I did some timings: 'and'ed indexing: C:\tmp>py -m timeit -s "key = ''" "key[0] == '<' and key[-1] == '>'" 100 loops, best of 3: 0.35 usec per loop C:\tmp>py -m timeit -s "key = 'py -m time

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 "", line 1, in > ValueError: slice step cannot be zero And that, my friends, is a classic example of a Python exception that oug

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 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[::l

Re: A curious bit of code...

2014-02-13 Thread Chris Angelico
On Fri, Feb 14, 2014 at 7:55 AM, Emile van Sebille 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] AAAR

Re: A curious bit of code...

2014-02-13 Thread Neil Cerutti
On 2014-02-13, Zachary Ware wrote: > In a fit of curiosity, I did some timings: > > 'and'ed indexing: > > C:\tmp>py -m timeit -s "key = ''" "key[0] == '<' and key[-1] == '>'" > 100 loops, best of 3: 0.35 usec per loop > > C:\tmp>py -m timeit -s "key = ' 100 loops, best of 3: 0.398 usec per

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 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 fav

Re: A curious bit of code...

2014-02-13 Thread Chris Angelico
On Fri, Feb 14, 2014 at 6:32 AM, Mark Lawrence 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 indexing to avoid pr

Re: A curious bit of code...

2014-02-13 Thread Zachary Ware
On Thu, Feb 13, 2014 at 12:37 PM, 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.

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, which is equivalent to Marko -- https://m

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 key.sta

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 ;)

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 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:

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 Roy Smith
In article , 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 cha

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 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

Re: A curious bit of code...

2014-02-13 Thread Neil Cerutti
On 2014-02-13, 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 ke

Re: A curious bit of code...

2014-02-13 Thread Neil Cerutti
On 2014-02-13, 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 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 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('>'):

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 haven

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 key.en

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 key

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 possibilitie

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:]) == ('<