[Python-ideas] New explicit methods to trim strings

Jonathan Fine jfine2358 at gmail.com
Mon Mar 25 09:51:31 EDT 2019

Here, concisely, is my view of the situation and my preferences. Mostly, I
won't give supporting arguments or evidence.

We can TRUNCATE either PRE or the POST, and similarly SUBTRACT.

SUBTRACT can raise a ValueError.
TRUNCATE always returns a value.

Interactive examples (not tested)
    >>> from somewhere import post_subtract
    >>> sub_ed = post_subtract('ed')
    >>> sub_ed('fred')
    >>> 'fr'
    >>> sub_ed('lead')

    >>> trunc_ed('fred')
    >>> trunc_ed('lead')

Can be 'combined into one'
   >>> pre_truncate('app')('applet)
    >>> pre_truncate('app')('paper')

1.  Allow pre_truncate('app', 'applet'), perhaps with different spelling.
2. Allow '-' as a symbol for subtract. (Likely to be controversial.)

I'm not particularly attached to the names. But I definitely think
3. None of these are string methods. (So pure Python implementation
automatically backports.)
4. Encourage a 'two-step' process. This allow separation of concerns, and
encourage good names.

Supporting argument.
When we write
    pre_subtract(suffix, s)
the suffix has a special meaning. For example, it's the header. So in one
module define and test a routine remove_header. And in another module use
remove_header. That way, the user of remove_header only needs to know the
business purpose of the command. And the implementer needs to know only the
value of the header.

If the specs change, and the implementer needs to use regular expressions,
then this does not affect the user of remove_header.

I hope this helps. Maybe others would like to express their preferences.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190325/c8a8a869/attachment.html>

More information about the Python-ideas mailing list