+1 on the idea of having `partition` and `rpartition` take multiple 
separators.

Keep it nice and simple: provided with multiple separators, `partition` 
will split the string on the first separator found in the source string.

In other words, `source.partition(a, b, c, d)` will split on a /or/ b 
/or/ c /or/ d, whichever comes first on the left.

Here is a proof of concept to give the basic idea:


```
def partition(source, *seps):
    if len(seps) == 0:
        raise TypeError('need at least one separator')
    indices = [(i, sep) for sep in seps if (i:=source.find(sep)) != -1]
    if indices:
        pos, sep = min(indices, key=lambda t: t[0])
        return (source[:pos], sep, source[pos + len(sep):])
    else:
        return (source, '', '')
```

That is not the most efficient implementation, but it shows the basic 
concept. Example:

    >>> partition('abc-def+ghi;klm', ';', '-', '+')
    ('abc', '-', 'def+ghi;klm')
    >>> partition('def+ghi;klm', ';', '-', '+')
    ('def', '+', 'ghi;klm')


However there are some complications that need resolving. What if the 
separators overlap? E.g. we might have '-' and '--' as two separators. 
We might want to choose the shortest separator, or the longest. That 
choice should be a keyword-only argument.


-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/GKMJOSEJYIQIUH2S3VTLVWGICKA2AFVN/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to