Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #66200 > unrolled thread
| Started by | forman.simon@gmail.com |
|---|---|
| First post | 2014-02-13 10:37 -0800 |
| Last post | 2014-02-14 09:06 -0500 |
| Articles | 20 on this page of 45 — 16 participants |
Back to article view | Back to comp.lang.python
A curious bit of code... forman.simon@gmail.com - 2014-02-13 10:37 -0800
Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 13:45 -0500
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 10:45 -0800
Re: A curious bit of code... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-13 19:09 +0000
Re: A curious bit of code... Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-02-13 20:05 +0100
Re: A curious bit of code... Neil Cerutti <neilc@norwich.edu> - 2014-02-13 19:17 +0000
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 11:20 -0800
Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 14:28 -0500
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 11:25 -0800
Re: A curious bit of code... Neil Cerutti <neilc@norwich.edu> - 2014-02-13 19:25 +0000
Re: A curious bit of code... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-13 19:32 +0000
Re: A curious bit of code... Peter Otten <__peter__@web.de> - 2014-02-13 20:43 +0100
Re: A curious bit of code... Marko Rauhamaa <marko@pacujo.net> - 2014-02-13 21:56 +0200
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 11:23 -0800
Re: A curious bit of code... Neil Cerutti <neilc@norwich.edu> - 2014-02-13 19:51 +0000
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 11:59 -0800
Re: A curious bit of code... Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-13 13:59 -0600
Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 07:29 +1100
Re: A curious bit of code... Tim Chase <python.list@tim.thechases.com> - 2014-02-13 14:39 -0600
Re: A curious bit of code... Emile van Sebille <emile@fenx.com> - 2014-02-13 12:55 -0800
Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 16:24 -0500
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 16:23 -0800
Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 08:01 +1100
Re: A curious bit of code... Neil Cerutti <neilc@norwich.edu> - 2014-02-13 21:01 +0000
Re: A curious bit of code... Peter Otten <__peter__@web.de> - 2014-02-13 22:06 +0100
Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 08:10 +1100
Re: A curious bit of code... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-13 21:14 +0000
Re: A curious bit of code... Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-13 15:20 -0600
Re: A curious bit of code... Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-13 15:19 -0600
Re: A curious bit of code... Emile van Sebille <emile@fenx.com> - 2014-02-13 13:23 -0800
Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 08:31 +1100
Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 16:38 -0500
Re: A curious bit of code... Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-13 15:47 -0600
Re: A curious bit of code... Serhiy Storchaka <storchaka@gmail.com> - 2014-02-13 23:49 +0200
Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 16:51 -0500
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 13:33 -0800
Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 09:13 +1100
Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 14:26 -0800
Re: A curious bit of code... Terry Reedy <tjreedy@udel.edu> - 2014-02-13 19:29 -0500
Re: A curious bit of code... forman.simon@gmail.com - 2014-02-13 18:45 -0800
Re: A curious bit of code... Ned Batchelder <ned@nedbatchelder.com> - 2014-02-13 22:26 -0500
Re: A curious bit of code... forman.simon@gmail.com - 2014-02-14 12:04 -0800
Re: A curious bit of code... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-14 21:01 +0000
Re: A curious bit of code... Dave Angel <davea@davea.name> - 2014-02-14 07:19 -0500
Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-14 09:06 -0500
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-02-13 16:24 -0500 |
| Message-ID | <roy-147BC8.16243513022014@news.panix.com> |
| In reply to | #66238 |
In article <mailman.6880.1392324956.18130.python-list@python.org>, Emile van Sebille <emile@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] > > Emile I love it. I need to add this to my list of Python trivia questions.
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2014-02-13 16:23 -0800 |
| Message-ID | <mailman.6904.1392338801.18130.python-list@python.org> |
| In reply to | #66249 |
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~
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-02-14 08:01 +1100 |
| Message-ID | <mailman.6881.1392325278.18130.python-list@python.org> |
| In reply to | #66200 |
On Fri, Feb 14, 2014 at 7:55 AM, Emile van Sebille <emile@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] .... AAAAAAAAAAAAAAAAAAARGGGGGGGGGGHHHH! Really, I actually did pause, double-take, and then scream under my breath, when I saw that. Yes, it works. But please, if you EVER do this, save me the trouble and just submit your code to thedailywtf.com straight away! ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2014-02-13 21:01 +0000 |
| Message-ID | <mailman.6882.1392325299.18130.python-list@python.org> |
| In reply to | #66200 |
On 2014-02-13, Zachary Ware <zachary.ware+pylist@gmail.com> wrote: > In a fit of curiosity, I did some timings: > > 'and'ed indexing: > > C:\tmp>py -m timeit -s "key = '<test>'" "key[0] == '<' and key[-1] == '>'" > 1000000 loops, best of 3: 0.35 usec per loop > > C:\tmp>py -m timeit -s "key = '<test'" "key[0] == '<' and key[-1] == '>'" > 1000000 loops, best of 3: 0.398 usec per loop > > C:\tmp>py -m timeit -s "key = 'test>'" "key[0] == '<' and key[-1] == '>'" > 1000000 loops, best of 3: 0.188 usec per loop > > C:\tmp>py -m timeit -s "key = 'test'" "key[0] == '<' and key[-1] == '>'" > 10000000 loops, best of 3: 0.211 usec per loop > > 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:\Python34\lib\timeit.py", line 178, in timeit > timing = self.inner(it, self.timer) > File "<timeit-src>", line 6, in inner > key[0] == '<' and key[-1] == '>' > IndexError: string index out of range The corrected version key and key[0] == '<' and key[-1] == '>' probably still wins the Pretty Unimportant Olympics. -- Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-02-13 22:06 +0100 |
| Message-ID | <mailman.6883.1392325597.18130.python-list@python.org> |
| In reply to | #66200 |
Chris Angelico wrote: > On Fri, Feb 14, 2014 at 7:55 AM, Emile van Sebille <emile@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] > > .... AAAAAAAAAAAAAAAAAAARGGGGGGGGGGHHHH! > > Really, I actually did pause, double-take, and then scream under my > breath, when I saw that. Yes, it works. But please, if you EVER do > this, save me the trouble and just submit your code to thedailywtf.com > straight away! 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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-02-14 08:10 +1100 |
| Message-ID | <mailman.6884.1392325848.18130.python-list@python.org> |
| In reply to | #66200 |
On Fri, Feb 14, 2014 at 8:06 AM, Peter Otten <__peter__@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 be a subclass of UnhingedProgrammerError. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-02-13 21:14 +0000 |
| Message-ID | <mailman.6886.1392326068.18130.python-list@python.org> |
| In reply to | #66200 |
On 13/02/2014 21:01, Neil Cerutti wrote: > On 2014-02-13, Zachary Ware <zachary.ware+pylist@gmail.com> wrote: >> In a fit of curiosity, I did some timings: >> >> 'and'ed indexing: >> >> C:\tmp>py -m timeit -s "key = '<test>'" "key[0] == '<' and key[-1] == '>'" >> 1000000 loops, best of 3: 0.35 usec per loop >> >> C:\tmp>py -m timeit -s "key = '<test'" "key[0] == '<' and key[-1] == '>'" >> 1000000 loops, best of 3: 0.398 usec per loop >> >> C:\tmp>py -m timeit -s "key = 'test>'" "key[0] == '<' and key[-1] == '>'" >> 1000000 loops, best of 3: 0.188 usec per loop >> >> C:\tmp>py -m timeit -s "key = 'test'" "key[0] == '<' and key[-1] == '>'" >> 10000000 loops, best of 3: 0.211 usec per loop >> >> 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:\Python34\lib\timeit.py", line 178, in timeit >> timing = self.inner(it, self.timer) >> File "<timeit-src>", line 6, in inner >> key[0] == '<' and key[-1] == '>' >> IndexError: string index out of range > > The corrected version > > key and key[0] == '<' and key[-1] == '>' > > probably still wins the Pretty Unimportant Olympics. > Exactly how I'd write it. To me it wins awards for being most boring and most obvious, obviously YMMV or we wouldn't be having this discussion. Or argument. Or contradiction :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com
[toc] | [prev] | [next] | [standalone]
| From | Zachary Ware <zachary.ware+pylist@gmail.com> |
|---|---|
| Date | 2014-02-13 15:20 -0600 |
| Message-ID | <mailman.6888.1392326438.18130.python-list@python.org> |
| In reply to | #66200 |
On Thu, Feb 13, 2014 at 3:01 PM, Neil Cerutti <neilc@norwich.edu> wrote: > On 2014-02-13, Zachary Ware <zachary.ware+pylist@gmail.com> 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:\Python34\lib\timeit.py", line 178, in timeit >> timing = self.inner(it, self.timer) >> File "<timeit-src>", line 6, in inner >> key[0] == '<' and key[-1] == '>' >> IndexError: string index out of range > > The corrected version > > key and key[0] == '<' and key[-1] == '>' > > probably still wins the Pretty Unimportant Olympics. Indeed, see the last set of timings and moral ;) -- Zach
[toc] | [prev] | [next] | [standalone]
| From | Zachary Ware <zachary.ware+pylist@gmail.com> |
|---|---|
| Date | 2014-02-13 15:19 -0600 |
| Message-ID | <mailman.6887.1392326419.18130.python-list@python.org> |
| In reply to | #66200 |
On Thu, Feb 13, 2014 at 2:55 PM, Emile van Sebille <emile@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 0.520 usec (the builtin lookup and function call is what gets it). Also, uglier than sin itself. -- Zach
[toc] | [prev] | [next] | [standalone]
| From | Emile van Sebille <emile@fenx.com> |
|---|---|
| Date | 2014-02-13 13:23 -0800 |
| Message-ID | <mailman.6889.1392326612.18130.python-list@python.org> |
| In reply to | #66200 |
On 2/13/2014 1:10 PM, Chris Angelico wrote: > On Fri, Feb 14, 2014 at 8:06 AM, Peter Otten <__peter__@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 be a subclass of UnhingedProgrammerError. > And certainly s[::len(s)-1 or 1] isn't any better. :) Emile
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-02-14 08:31 +1100 |
| Message-ID | <mailman.6891.1392327074.18130.python-list@python.org> |
| In reply to | #66200 |
On Fri, Feb 14, 2014 at 8:19 AM, Zachary Ware <zachary.ware+pylist@gmail.com> wrote: > Also, uglier than sin itself. Hey hey, no need to insult our lovely trigonometric functions! ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-02-13 16:38 -0500 |
| Message-ID | <roy-13A202.16380013022014@news.panix.com> |
| In reply to | #66254 |
In article <mailman.6891.1392327074.18130.python-list@python.org>, Chris Angelico <rosuav@gmail.com> wrote: > On Fri, Feb 14, 2014 at 8:19 AM, Zachary Ware > <zachary.ware+pylist@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Zachary Ware <zachary.ware+pylist@gmail.com> |
|---|---|
| Date | 2014-02-13 15:47 -0600 |
| Message-ID | <mailman.6892.1392328058.18130.python-list@python.org> |
| In reply to | #66200 |
On Thu, Feb 13, 2014 at 3:31 PM, Chris Angelico <rosuav@gmail.com> wrote: > On Fri, Feb 14, 2014 at 8:19 AM, Zachary Ware > <zachary.ware+pylist@gmail.com> wrote: >> Also, uglier than sin itself. > > Hey hey, no need to insult our lovely trigonometric functions! Here's your sine... -- Zach
[toc] | [prev] | [next] | [standalone]
| From | Serhiy Storchaka <storchaka@gmail.com> |
|---|---|
| Date | 2014-02-13 23:49 +0200 |
| Message-ID | <mailman.6893.1392328170.18130.python-list@python.org> |
| In reply to | #66200 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-02-13 16:51 -0500 |
| Message-ID | <roy-84AACF.16515013022014@news.panix.com> |
| In reply to | #66257 |
In article <mailman.6893.1392328170.18130.python-list@python.org>, Serhiy Storchaka <storchaka@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), > > And the time of re variant depends on the size of input. That's a good point. It's nice, for a change, to see somebody shoot down a regex solution for a valid reason :-)
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2014-02-13 13:33 -0800 |
| Message-ID | <mailman.6894.1392328506.18130.python-list@python.org> |
| In reply to | #66200 |
On 02/13/2014 01:01 PM, Chris Angelico wrote: > On Fri, Feb 14, 2014 at 7:55 AM, Emile van Sebille <emile@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] > > .... AAAAAAAAAAAAAAAAAAARGGGGGGGGGGHHHH! > > Really, I actually did pause, double-take, and then scream under my > breath, when I saw that. Yes, it works. But please, if you EVER do > this, save me the trouble and just submit your code to thedailywtf.com > straight away! 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* -- ~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-02-14 09:13 +1100 |
| Message-ID | <mailman.6896.1392330021.18130.python-list@python.org> |
| In reply to | #66200 |
On Fri, Feb 14, 2014 at 8:33 AM, Ethan Furman <ethan@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 built-in function len() is slow, so I propose
changing that.
s[::(sys.getsizeof("\N{MATHEMATICAL BOLD CAPITAL
L}"+s)-sys.getsizeof("\N{MATHEMATICAL BOLD CAPITAL E}\N{MATHEMATICAL
BOLD CAPITAL N}") or 4)//4]
Note the elegance of using the word LEN across two string literals
(because string literals are fast) as a means of clearly describing
what we are doing - an optimization of the built-in len() function.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2014-02-13 14:26 -0800 |
| Message-ID | <mailman.6899.1392330358.18130.python-list@python.org> |
| In reply to | #66200 |
On 02/13/2014 02:13 PM, Chris Angelico wrote: > On Fri, Feb 14, 2014 at 8:33 AM, Ethan Furman <ethan@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 description should have said "edge case of a one-character string". The empty string needs no extra handling. -- ~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-02-13 19:29 -0500 |
| Message-ID | <mailman.6903.1392337808.18130.python-list@python.org> |
| In reply to | #66200 |
On 2/13/2014 1:37 PM, forman.simon@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 first clause is false, it never computes the second. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | forman.simon@gmail.com |
|---|---|
| Date | 2014-02-13 18:45 -0800 |
| Message-ID | <839842ff-e0d4-4bec-93fa-b8c25b3ca471@googlegroups.com> |
| In reply to | #66200 |
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)-1] omg!!? ;-D
[toc] | [prev] | [next] | [standalone]
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
Back to top | Article view | comp.lang.python
csiph-web