
Hey,
not sure how people do this, or if I missed something obvious in the stdlib, but I often have this pattern:
starts = ('a', 'b', 'c') somestring = 'acapulco'
for start in starts: if somestring.startswith(start): print "yeah"
So what about a startsin() method, that would iterate over a sequence:
if somestring.startsin('a', 'b', 'c'): print "yeah"
Implementing it in C should be faster as well
same deal with .endswith I guess
Cheers Tarek

On Fri, Sep 30, 2011 at 11:30 AM, Tarek Ziadé ziade.tarek@gmail.com wrote:
Hey,
not sure how people do this, or if I missed something obvious in the stdlib, but I often have this pattern:
starts = ('a', 'b', 'c') somestring = 'acapulco'
for start in starts: if somestring.startswith(start): print "yeah"
So what about a startsin() method, that would iterate over a sequence:
if somestring.startsin('a', 'b', 'c'): print "yeah"
Implementing it in C should be faster as well
same deal with .endswith I guess
I tend to do something like this a lot;
any(somestring.startswith(x) for x in starts)
Probably enough that having a method would be nice.

On Fri, Sep 30, 2011 at 5:46 PM, David Stanek dstanek@dstanek.com wrote: ...
I tend to do something like this a lot; any(somestring.startswith(x) for x in starts) Probably enough that having a method would be nice.
Yeah we can have all kind of compressed loops, but I feel like a method would be cool :)
Cheers Tarek

On Fri, Sep 30, 2011 at 11:46 AM, David Stanek dstanek@dstanek.com wrote:
I tend to do something like this a lot;
any(somestring.startswith(x) for x in starts)
Probably enough that having a method would be nice.
I wonder if it might be worthwhile to give any and all two-parameter API for predicate functions, so that
any(f, xs)
is the same as
any(f(x) for x in xs)
This eliminates some really common boilerplate, but it adds complication and has an ugly API.
Mike

Mike Graham writes:
I wonder if it might be worthwhile to give any and all two-parameter API for predicate functions, so that
any(f, xs)
is the same as
any(f(x) for x in xs)
This eliminates some really common boilerplate, but it adds complication and has an ugly API.
-1. The comprehension is not just boilerplate, it's documentation.

On Fri, Sep 30, 2011 at 6:57 PM, Mike Graham mikegraham@gmail.com wrote:
On Fri, Sep 30, 2011 at 11:46 AM, David Stanek dstanek@dstanek.com wrote:
I tend to do something like this a lot; any(somestring.startswith(x) for x in starts) Probably enough that having a method would be nice.
I wonder if it might be worthwhile to give any and all two-parameter API for predicate functions, so that
any(f, xs)
is the same as
any(f(x) for x in xs)
This eliminates some really common boilerplate, but it adds complication and has an ugly API.
If you don't like the comprehension you could always use any(map(f, xs)).

If accepted, think this could be:
1. Added as a tool in a std lib module. 2. Allow startswith/endswith to accept a tuple. 3. Added as another method to str.
I'm +1 for the first option.
Either way, there's an O(len_str + len_max_needle) algorithm for this: http://en.m.wikipedia.org/wiki/Aho%E2%80%93Corasick_string_matching_algorith... On Sep 30, 2011 8:30 AM, "Tarek Ziadé" ziade.tarek@gmail.com wrote:
Hey,
not sure how people do this, or if I missed something obvious in the stdlib, but I often have this pattern:
starts = ('a', 'b', 'c') somestring = 'acapulco'
for start in starts: if somestring.startswith(start): print "yeah"
So what about a startsin() method, that would iterate over a sequence:
if somestring.startsin('a', 'b', 'c'): print "yeah"
Implementing it in C should be faster as well
same deal with .endswith I guess
Cheers Tarek
-- Tarek Ziadé | http://ziade.org _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas

On 30 September 2011 16:30, Tarek Ziadé ziade.tarek@gmail.com wrote:
Hey,
not sure how people do this, or if I missed something obvious in the stdlib, but I often have this pattern:
starts = ('a', 'b', 'c') somestring = 'acapulco'
for start in starts: if somestring.startswith(start): print "yeah"
So what about a startsin() method, that would iterate over a sequence:
if somestring.startsin('a', 'b', 'c'): print "yeah"
if any(somestring.startswith(s) for s in starts) print "yeah"
Implementing it in C should be faster as well
Probably slightly, but how critical is speed here?
same deal with .endswith I guess
A benefit of the any() recipe is that it generalises, so you don't need to have endsin as well, (or whatever else people might think of...)
But it is a common pattern, and there's also the case where you want to know *which" pattern matched, which any can't do, so there may be some mileage in this. But it might be simpler just to accept that you've reached the point where a simple regex is the best answer. Yes, now you have two problems, I know :-)
Paul.

On Fri, Sep 30, 2011 at 11:30 AM, Tarek Ziadé ziade.tarek@gmail.com wrote:
Hey,
not sure how people do this, or if I missed something obvious in the stdlib, but I often have this pattern:
starts = ('a', 'b', 'c') somestring = 'acapulco'
for start in starts: if somestring.startswith(start): print "yeah"
You missed something. startswith() and endswith() already accept tuples of strings to check and have done so since 2.5 [1]:
"example".startswith(('c', 'd', 'e'))
True
[1] http://docs.python.org/library/stdtypes.html#str.startswith
Cheers, Nick.

On Fri, Sep 30, 2011 at 6:02 PM, Nick Coghlan ncoghlan@gmail.com wrote:
On Fri, Sep 30, 2011 at 11:30 AM, Tarek Ziadé ziade.tarek@gmail.com wrote:
Hey,
not sure how people do this, or if I missed something obvious in the stdlib, but I often have this pattern:
starts = ('a', 'b', 'c') somestring = 'acapulco'
for start in starts: if somestring.startswith(start): print "yeah"
You missed something. startswith() and endswith() already accept tuples of strings to check and have done so since 2.5 [1]:
"example".startswith(('c', 'd', 'e'))
True
[1] http://docs.python.org/library/stdtypes.html#str.startswith
Cheers, Nick.
Arrggggg... thanks ! :)
-- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On 30/09/2011 12:02 PM, Nick Coghlan wrote:
You missed something. startswith() and endswith() already accept tuples of strings to check and have done so since 2.5 [1]:
Ha ha, should have checked for emails that arrived while I was composing my stupid response before I sent it.
Matt

On 30 September 2011 17:02, Nick Coghlan ncoghlan@gmail.com wrote:
On Fri, Sep 30, 2011 at 11:30 AM, Tarek Ziadé ziade.tarek@gmail.com wrote:
Hey,
not sure how people do this, or if I missed something obvious in the stdlib, but I often have this pattern:
starts = ('a', 'b', 'c') somestring = 'acapulco'
for start in starts: if somestring.startswith(start): print "yeah"
You missed something. startswith() and endswith() already accept tuples of strings to check and have done so since 2.5 [1]:
"example".startswith(('c', 'd', 'e'))
True
[1] http://docs.python.org/library/stdtypes.html#str.startswith
The time machine strikes again :-) Paul.

On 30/09/2011 11:30 AM, Tarek Ziadé wrote:
not sure how people do this, or if I missed something obvious in the stdlib, but I often have this pattern:
str's interface is a bit cluttered with some questionable methods ("captialize"? "center"? "swapcase"? "title"?) that probably should have been functions in a text module instead of methods. On the other hand, I do think this is a fairly common operation.
One thing is that the equivalent of .startsin() for .endswith() would be .endsin(). In English, "ends in" is a variation of "ends with", e.g. "What words end in 'a'?"
I think it would be better for startswith/endswith to accept a tuple/list argument.
Matt

Matt Chaput wrote:
On 30/09/2011 11:30 AM, Tarek Ziadé wrote:
not sure how people do this, or if I missed something obvious in the stdlib, but I often have this pattern:
str's interface is a bit cluttered with some questionable methods ("captialize"? "center"? "swapcase"? "title"?) that probably should have been functions in a text module instead of methods.
The str methods started off as functions in the string module before becoming methods in Python 2.0.
[steve@sylar python]$ python1.5 -c "'a'.upper()" Traceback (innermost last): File "<string>", line 1, in ? AttributeError: 'string' object has no attribute 'upper'
What you describe as "questionable methods" go back to the string module, and were made methods deliberately. And so they should be.
One thing is that the equivalent of .startsin() for .endswith() would be .endsin(). In English, "ends in" is a variation of "ends with", e.g. "What words end in 'a'?"
[pedant] That may or may not be common in some dialects (although not any I'm familiar with, which isn't very many), but it isn't semantically correct. The Melbourne to Sydney marathon ends *in* Sydney because the place where it ends is *inside* Sydney; a pencil ends *with* a point because the end of the pencil *is* a point, it is NOT inside the point. Similarly, the word "race" ends *with* an 'e'.

On 30/09/2011 10:46 PM, Steven D'Aprano wrote:
What you describe as "questionable methods" go back to the string module, and were made methods deliberately.
There's a difference between something being done deliberately and something being a good idea.
And so they should be.
Riiiight, I'm sure that swapcase() has come in handy for literally zeros of people over the years.
[pedant]
I'll say. If you've never heard a phrase something like "words that end in 'ing' are often gerunds", or did and complained that it wasn't "semantically correct", well...
Matt

Greg Ewing dixit (2011-10-01, 18:17):
Matt Chaput wrote:
One thing is that the equivalent of .startsin() for .endswith() would be .endsin(). In English, "ends in" is a variation of "ends with", e.g. "What words end in 'a'?"
Also "startsin" sounds a bit like beginning to do something naughty...
Both naughty and trigonometry-related. I even fear to guess what it could be...
*j
participants (12)
-
David Stanek
-
Greg Ewing
-
Jan Kaliszewski
-
Matt Chaput
-
Mike Graham
-
Nick Coghlan
-
Paul Moore
-
Stephen J. Turnbull
-
Steven D'Aprano
-
Tarek Ziadé
-
xtian
-
Yuval Greenfield