[Patches] [ python-Patches-801847 ] Adding rsplit() to string and unicode objects.

SourceForge.net noreply at sourceforge.net
Wed Sep 10 13:35:07 EDT 2003


Patches item #801847, was opened at 2003-09-06 19:52
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=801847&group_id=5470

Category: Library (Lib)
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Sean Reifschneider (jafo)
Assigned to: Nobody/Anonymous (nobody)
Summary: Adding rsplit() to string and unicode objects.

Initial Comment:
I'm attaching patches to the library and documentation

for implementing rsplit() on string and unicode

objects.  This works like split(), but working from the

right.



   ./python -c 'print u"foo, bar, baz".rsplit(None, 1)'

   [u'foo, bar,', u'baz']



This was supposed to be against the CVS code, but I've

had a heck of a time getting it checked out -- my

checkout has been hung for half an hour now.



The code patch is against the 2.3 release, the docs

patch is against the CVS.  My checkout got to docs, but

I didn't have the code to a point where I could build

and test it.



Sean

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-09-10 14:35

Message:
Logged In: YES 
user_id=80475

I would classify this more as a technique than a fundamental 

string operation implemented by all stringlike objects 

(including UserString).  Accordingly, I recommend that the 

patch be closed and a recipe posted in the ASPN cookbook - 

something along the lines of:



>>> def rsplit(s, sep=None, maxsplit=-1):

...     return  [chunk[::-1] for chunk in s[::-1].split(sep, 

maxsplit)[::-1]]



>>> rsplit(u"foo, bar, baz", None, 1)

[u'foo, bar,', u'baz']

----------------------------------------------------------------------

Comment By: Sean Reifschneider (jafo)
Date: 2003-09-10 14:15

Message:
Logged In: YES 
user_id=81797

os.path.basename/os.path.dirname is an example of where you

could use rsplit. One of the other #python folks said he had

recently wanted rsplit for an application where he was

getting the domain name and user part from a list of e-mail

addresses, but found that some entries contained an "@" in

the user part.



Sean

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2003-09-10 12:08

Message:
Logged In: YES 
user_id=21627

I questioned the usefulness because I could not think of a

meaningful application. Now I see what a potential

application could be, but I doubt its generality, because

that approach would break if there could be two fields that

have commas in them.



I also disagree that symmetry can motivate usefulness: I

also doubt that all of the r* functions are useful, but they

cannot be removed for backwards compatibility. The fact that

rsplit would fit together with the other r* functions

indicates that adding rsplit would provide symmetry, not

that it would provide usefulness.

----------------------------------------------------------------------

Comment By: Sean Reifschneider (jafo)
Date: 2003-09-07 19:56

Message:
Logged In: YES 
user_id=81797

Can you provide more details about why the usefulness of

this function is in question?



First I would like to tell you the story of it coming to be,

then I will answer your incomplete question with a

(probably) incomplete answer.  I had a device which sent me

comma-separated fields, but one of the fields in the middle

could contain a comma.  The answer that seemed obvious to me

was to use split with a maxsplit to get the fields up to

that field, and then a rsplit with a maxsplit on the

remainder.  When I mentioned on #python that I was

implementing rsplit, 4 other fellow python users replied

right away that they had been wanting it.



To answer your question, it's useful because people using

strings are used to having r*() functions like rfind and

rstrip.  The lack of rsplit is kind of glaring in this

context.  Really, though, it's useful because otherwise

people have to implement -- often badly.



Sean

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2003-09-07 14:49

Message:
Logged In: YES 
user_id=21627

Why is this function useful?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=801847&group_id=5470



More information about the Patches mailing list