473,763 Members | 5,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

feature request: a better str.endswith

I often feel the need to extend the string method ".endswith" to tuple
arguments, in such a way to automatically check for multiple endings.
For instance, here is a typical use case:

if filename.endswi th(('.jpg','.jp eg','.gif','.pn g')):
print "This is a valid image file"

Currently this is not valid Python and I must use the ugly

if filename.endswi th('.jpg') or filename.endswi th('.jpeg') \
or filename.endswi th('.gif') or filename.endswi th('.png'):
print "This is a valid image file"

Of course a direct implementation is quite easy:

import sys

class Str(str):
def endswith(self,s uffix,start=0,e nd=sys.maxint): #not sure about sys.maxint
endswith=super( Str,self).endsw ith
if isinstance(suff ix,tuple):
return sum([endswith(s,star t,end) for s in suffix]) # multi-or
return endswith(suffix ,start,end)

if Str(filename).e ndswith(('.jpg' ,'.jpeg','.gif' ,'.png')):
print "This is a valid image file"

nevertheless I think this kind of checking is quite common and it would be
worth to have it in standard Python.

Any reaction, comment ?
Michele
Jul 18 '05 #1
21 8648
Michele Simionato wrote:
I often feel the need to extend the string method ".endswith" to tuple
arguments, in such a way to automatically check for multiple endings.
For instance, here is a typical use case:

if filename.endswi th(('.jpg','.jp eg','.gif','.pn g')):
print "This is a valid image file"

Currently this is not valid Python and I must use the ugly

if filename.endswi th('.jpg') or filename.endswi th('.jpeg') \
or filename.endswi th('.gif') or filename.endswi th('.png'):
print "This is a valid image file"

Of course a direct implementation is quite easy:

import sys

class Str(str):
def endswith(self,s uffix,start=0,e nd=sys.maxint): #not sure about
sys.maxint
endswith=super( Str,self).endsw ith
if isinstance(suff ix,tuple):
return sum([endswith(s,star t,end) for s in suffix]) # multi-or
return endswith(suffix ,start,end)

if Str(filename).e ndswith(('.jpg' ,'.jpeg','.gif' ,'.png')):
print "This is a valid image file"

nevertheless I think this kind of checking is quite common and it would be
worth to have it in standard Python.


Hi,

I like this feature request.

if the argument to endswith is not a string,
it should try to treat the argument as a list or tuple.

thomas
Jul 18 '05 #2
On Fri, Jul 18, 2003 at 05:01:47AM -0700, Michele Simionato wrote:
I often feel the need to extend the string method ".endswith" to tuple
arguments, in such a way to automatically check for multiple endings.
For instance, here is a typical use case:

if filename.endswi th(('.jpg','.jp eg','.gif','.pn g')):
print "This is a valid image file"

Currently this is not valid Python and I must use the ugly

if filename.endswi th('.jpg') or filename.endswi th('.jpeg') \
or filename.endswi th('.gif') or filename.endswi th('.png'):
print "This is a valid image file"


extensions = ('.jpg', '.jpeg', '.gif', '.png')
if filter(filename .endswith, extensions):
print "This is a valid image file

Jp

--
"Pascal is Pascal is Pascal is dog meat."
-- M. Devine and P. Larson, Computer Science 340

Jul 18 '05 #3

Michele> I often feel the need to extend the string method ".endswith"
Michele> to tuple arguments, in such a way to automatically check for
Michele> multiple endings. For instance, here is a typical use case:

Michele> if filename.endswi th(('.jpg','.jp eg','.gif','.pn g')):
Michele> print "This is a valid image file"

This is analogous to how isinstance works, where its second arg can be a
class or type or a tuple containing classes and types.

I suggest you submit a feature request to SF. A patch to stringobject.c and
unicodeobject.c would help improve chances of acceptance, and for symmetry
you should probably also modify the startswith methods of both types.

Skip
Jul 18 '05 #4
Irmen de Jong <irmen@-NOSPAM-REMOVETHIS-xs4all.nl> wrote in message news:<3f******* *************** *@news.xs4all.n l>...
Jp Calderone wrote:
On Fri, Jul 18, 2003 at 05:01:47AM -0700, Michele Simionato wrote:
I often feel the need to extend the string method ".endswith" to tuple
arguments, in such a way to automatically check for multiple endings.
For instance, here is a typical use case:

if filename.endswi th(('.jpg','.jp eg','.gif','.pn g')):
print "This is a valid image file"

Currently this is not valid Python and I must use the ugly

if filename.endswi th('.jpg') or filename.endswi th('.jpeg') \
or filename.endswi th('.gif') or filename.endswi th('.png'):
print "This is a valid image file"

extensions = ('.jpg', '.jpeg', '.gif', '.png')
if filter(filename .endswith, extensions):
print "This is a valid image file

Jp


Using filter Michele's original statement becomes:

if filter(filename .endswith, ('.jpg','.jpeg' ,'.gif','.png') ):
print "This is a valid image file"

IMHO this is simple enough to not require a change to the
.endswith method...

--Irmen


I haven't thought of "filter". It is true, it works, but is it really
readable? I had to think to understand what it is doing.
My (implicit) rationale for

filename.endswi th(('.jpg','.jp eg','.gif','.pn g'))

was that it works exactly as "isinstance ", so it is quite
obvious what it is doing. I am asking just for a convenience,
which has already a precedent in the language and respects
the Principle of Least Surprise.

Michele
Jul 18 '05 #5
Skip Montanaro <sk**@pobox.com > wrote in message news:<ma******* *************** ***********@pyt hon.org>...
Michele> I often feel the need to extend the string method ".endswith"
Michele> to tuple arguments, in such a way to automatically check for
Michele> multiple endings. For instance, here is a typical use case:

Michele> if filename.endswi th(('.jpg','.jp eg','.gif','.pn g')):
Michele> print "This is a valid image file"

This is analogous to how isinstance works, where its second arg can be a
class or type or a tuple containing classes and types.

I suggest you submit a feature request to SF. A patch to stringobject.c and
unicodeobject.c would help improve chances of acceptance, and for symmetry
you should probably also modify the startswith methods of both types.

Skip


Too bad my skills with C are essentially unexistent :-(
Michele
Jul 18 '05 #6
>> I suggest you submit a feature request to SF. A patch to
stringobject.c and unicodeobject.c would help improve chances of
acceptance, and for symmetry you should probably also modify the
startswith methods of both types.


Michele> Too bad my skills with C are essentially unexistent :-(

Look at it as an opportunity to enhance those skills. You have plenty of
time until 2.4. ;-)

In any case, even if you can't whip up the actual C code, a complete feature
request on SF would keep it from being entirely forgotten.

Skip
Jul 18 '05 #7
[Michele Simionato]
>I often feel the need to extend the string method ".endswith" to tuple
>arguments, in such a way to automatically check for multiple endings.
>For instance, here is a typical use case:
>
>if filename.endswi th(('.jpg','.jp eg','.gif','.pn g')):
> print "This is a valid image file"
[Jp] extensions = ('.jpg', '.jpeg', '.gif', '.png')
if filter(filename .endswith, extensions):
print "This is a valid image file

Jp


[Irmen] Using filter Michele's original statement becomes:

if filter(filename .endswith, ('.jpg','.jpeg' ,'.gif','.png') ):
print "This is a valid image file"

IMHO this is simple enough to not require a change to the
.endswith method...

[Michele] I haven't thought of "filter". It is true, it works, but is it really
readable? I had to think to understand what it is doing.
My (implicit) rationale for

filename.endswi th(('.jpg','.jp eg','.gif','.pn g'))

was that it works exactly as "isinstance ", so it is quite
obvious what it is doing. I am asking just for a convenience,
which has already a precedent in the language and respects
the Principle of Least Surprise.


I prefer that this feature not be added. Convenience functions
like this one rarely pay for themselves because:

-- The use case is not that common (afterall, endswith() isn't even
used that often).

-- It complicates the heck out of the C code

-- Checking for optional arguments results in a slight slowdown
for the normal case.

-- It is easy to implement a readable version in only two or three
lines of pure python.

-- It is harder to read because it requires background knowledge
of how endswith() handles a tuple (quick, does it take any
iterable or just a tuple, how about a subclass of tuple; is it
like min() and max() in that it *args works just as well as
argtuple; which python version implemented it, etc).

-- It is a pain to keep the language consistent. Change endswith()
and you should change startswith(). Change the string object and
you should also change the unicode object and UserString and
perhaps mmap. Update the docs for each and add test cases for
each (including weird cases with zero-length tuples and such).

-- The use case above encroaches on scanning patterns that are
already efficiently implemented by the re module.

-- Worst of all, it increases the sum total of python language to be
learned without providing much in return.

-- In general, the language can be kept more compact, efficient, and
maintainable by not trying to vectorize everything (the recent addition
of the __builtin__.sum () is a rare exception that is worth it). It is
better to use a general purpose vectorizing function (like map, filter,
or reduce). This particular case is best implemented in terms of the
some() predicate documented in the examples for the new itertools module
(though any() might have been a better name for it):

some(filename.e ndswith, ('.jpg','.jpeg' ,'.gif','.png') )

The implementation of some() is better than the filter version because
it provides an "early-out" upon the first successful hit.
Raymond Hettinger




Jul 18 '05 #8
"Raymond Hettinger" <vz******@veriz on.net> wrote in message news:<Np******* ***********@nwr dny01.gnilink.n et>..
I prefer that this feature not be added. Convenience functions
like this one rarely pay for themselves because:

-- The use case is not that common (afterall, endswith() isn't even
used that often).
This is arguable.
-- It complicates the heck out of the C code
Really? Of course, you are the expert. I would do it in analogy to
"isinstance " and internally calling "ifilter" as you suggest.
-- Checking for optional arguments results in a slight slowdown
for the normal case.
Perhaps slight enough to be negligible? Of course without
implementation
we cannot say, but I would be surprised to have a sensible slowdown.
-- It is easy to implement a readable version in only two or three
lines of pure python.
Yes, but not immediately obvious. See later.
-- It is harder to read because it requires background knowledge
of how endswith() handles a tuple (quick, does it take any
iterable or just a tuple, how about a subclass of tuple; is it
like min() and max() in that it *args works just as well as
argtuple; which python version implemented it, etc).
I have used "isinstance " and never wondered about these
technicalities, so
I guess the average user should not be more concerned with .endswith.
-- It is a pain to keep the language consistent. Change endswith()
and you should change startswith(). Change the string object and
you should also change the unicode object and UserString and
perhaps mmap. Update the docs for each and add test cases for
each (including weird cases with zero-length tuples and such).
This is true for any modification of the language. One has to balance
costs and benefits. The balance is still largely subjective.
-- The use case above encroaches on scanning patterns that are
already efficiently implemented by the re module.
I think the general rule is to avoid regular expressions when
possible.
-- Worst of all, it increases the sum total of python language to be
learned without providing much in return.
That it is exactly what I am arguing *against*: there is no additional
learning
effort needed, since a similar feature is already present in
"isinstance "
and an user could be even surprised that it is not implemented in
..endswith.
-- In general, the language can be kept more compact, efficient, and
maintainable by not trying to vectorize everything (the recent addition
of the __builtin__.sum () is a rare exception that is worth it). It is
better to use a general purpose vectorizing function (like map, filter,
or reduce). This particular case is best implemented in terms of the
some() predicate documented in the examples for the new itertools module
(though any() might have been a better name for it):

some(filename.e ndswith, ('.jpg','.jpeg' ,'.gif','.png') )
Uhm... don't like "some", nor "any"; what about "the"?

import itertools
the=lambda pred,seq: list(itertools. ifilter(pred,se q))
for filename in os.listdir('.') :
if the(filename.en dswith, ('.jpg','.jpeg' ,'.gif','.png') ):
print "This is a valid image"

That's readable enough for me, still not completely obvious. The first
time,
I got it wrong by defining "the=itertools. ifilter". I had the idea
that "ifilter" was acting just as "filter", which of course is not the
case
in this example.
The implementation of some() is better than the filter version because
it provides an "early-out" upon the first successful hit.
No point against that.
Raymond Hettinger


Michele Simionato

P.S. I am not going to pursue this further, since I like quite a lot

if the(filename.en dswith, ('.jpg','.jpeg' ,'.gif','.png') ):
dosomething()

Instead, I will suggest this example to be added to the itertools
documentation ;)
I could also submit it as a cookbook recipe, since I think it is
a quite useful trick.
Also, it is good to make people aware of itertool goodies
(myself I have learned something in this thread).
Jul 18 '05 #9
Skip Montanaro schrieb:
I suggest you submit a feature request to SF.


+1 from me :-)

This is a commonly used case. Using things like stripext() is only a
solution for this specific case where filename-extensions are matched.

Michele: I suggesz menatoning this in the feature-request or simple use
a different example (not based on filename extension.)

Regards
Hartmut Goebel
--
| Hartmut Goebel | IT-Security -- effizient |
| h.******@goebel-consult.de | www.goebel-consult.de |

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2143
by: Michele Simionato | last post by:
I have read with interest the recent thread about closures. The funny thing is that the authors are arguing one against the other but I actually agree with all of them and I have a proposal that may be of some interest. To refresh your memory, I report here some quotation from that thread. Jacek Generowicz: > I sumbit to you that read-only closures are rare in Python because > they are a recent addition to the language.
0
5700
by: Daniel Kasak | last post by:
Hi all. I need to test whether the first bit of a field is numeric. For example, the field might contain: 154 boxes I'm currently testing it by doing: if(abs(left(field,locate(' ', field))>0),'Numeric bit at front', 'Not
4
2109
by: christopher diggins | last post by:
A feature that I find signficantly missing in C# is the ability to write functions in interfaces that can call other functions of the interface. Given an interface ISomeInteface the only way we can write a general purpose function to operate on all objects which implement that interface is through the following style of declaration (in the following code snippets i is an interface variable of type ISomeInterface) : SomeStaticClass {...
6
6468
by: LongBow | last post by:
Hello all, I am having a little problems getting String's StartsWith and EndsWith methods. If I have a string defined as sNormalPt which equals "0x11D0" then use the following command sNormalPt.StartsWith( "0x" ) the application throws and exception stating
30
3316
by: Raymond Hettinger | last post by:
Proposal -------- I am gathering data to evaluate a request for an alternate version of itertools.izip() with a None fill-in feature like that for the built-in map() function: >>> map(None, 'abc', '12345') # demonstrate map's None fill-in feature The motivation is to provide a means for looping over all data elements
12
2094
by: Raymond Hettinger | last post by:
I am evaluating a request for an alternate version of itertools.izip() that has a None fill-in feature like the built-in map function: >>> map(None, 'abc', '12345') # demonstrate map's None fill-in feature The movitation is to provide a means for looping over all data elements when the input lengths are unequal. The question of the day is whether that is both a common need and a good approach to real-world problems. The answer to...
4
1465
by: ThunderMusic | last post by:
Hi, Is there a way we can send a feature request for Visual Studio? When using 'Refactor-->Encapsulate Field', I'd like to have an option for the references not to be updated anywhere in the project. It would be very useful for me (and probably others too) because I use this feature to generate my properties from my member variables. So I type all my member variables and then take them one by one and encapsulate them... When the project...
4
3294
by: =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post by:
Hello, what is the use-case of parameter "start" in string's "endswith" method? Consider the following minimal example: a = "testing" suffix="ing" a.endswith(suffix, 2) Significance of "end" is obvious. But not so for "start".
10
3262
by: Conrad Lender | last post by:
In a recent thread in this group, I said that in some cases object detection and feature tests weren't sufficient in the development of cross-browser applications, and that there were situations where you could improve the application by detecting the browser vendor/version. Some of the posters here disagreed. Since then, I've had to deal with a few of these cases; some of them could be rewritten to use object detection, and some couldn't....
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.