Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101752

Re: Powerful perl paradigm I don't find in python

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Powerful perl paradigm I don't find in python
Date 2016-01-15 14:34 +0100
Organization None
Message-ID <mailman.12.1452864897.15297.python-list@python.org> (permalink)
References <n7adse$k6$1@dont-email.me> <n7af0o$kfr$1@ger.gmane.org> <mailman.5.1452854573.15297.python-list@python.org> <n7ajo0$k6v$1@dont-email.me>

Show all headers | View raw


Charles T. Smith wrote:

> What the original snippet does is parse *and consume* a string - actually,
> to avoid maintaining a cursor traverse the string.  The perl feature is
> that substitute allows the found pattern to be replaced, but retains the
> group after the expression is complete.

That is too technical for my taste. When is your "paradigm" more useful than 
a simple

re.finditer(), re.findall(), or re.split()

? 

>> things = []
>> while some_str != tail:
>>      m = re.match(pattern_str, some_str)
>>      things.append(some_str[:m.end()])
>>      some_str = some_str[m.end():]
 
If that were common (or even ever occured) I'd write a helper which avoids 
the brittle some_str != tail comparison and exposes the functionality in a 
for loop:

class MissingTailError(ValueError):
    pass


class UnparsedRestError(ValueError):
    pass


def shave_off(regex, text, tail=None):
    """
    >>> for s in shave_off(r"[a-z]+ \\d+\\s*",
    ...        "foo 12 bar 34 baz", tail="baz"):
    ...     s
    'foo 12 '
    'bar 34 '
    """
    if tail is not None:
        if text.endswith(tail):
            end = len(text) - len(tail)
        else:
            raise MissingTailError("%r does not end with %r" % (text, tail))
    else:
        end = len(text)

    start = 0
    r = re.compile(regex)
    while start != end:
        m = r.match(text, start, end)
        if m is None:
            raise UnparsedRestError(
                "%r does not match pattern %r"
                % (text[start:end], r.pattern))
        yield text[m.start():m.end()]
        start = m.end()

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Powerful perl paradigm I don't find in python "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-01-15 09:24 +0000
  Re: Powerful perl paradigm I don't find in python Peter Otten <__peter__@web.de> - 2016-01-15 10:43 +0100
    Re: Powerful perl paradigm I don't find in python Michael Vilain <vilain@NOspamcop.net> - 2016-01-15 02:20 -0800
  Re: Powerful perl paradigm I don't find in python Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2016-01-15 11:42 +0100
    Re: Powerful perl paradigm I don't find in python "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-01-15 11:04 +0000
      Re: Powerful perl paradigm I don't find in python "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-01-15 11:06 +0000
      Re: Powerful perl paradigm I don't find in python Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2016-01-15 14:20 +0100
        Re: Powerful perl paradigm I don't find in python "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-01-18 13:05 +0000
          Re: Powerful perl paradigm I don't find in python Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2016-01-18 14:33 +0100
      Re: Powerful perl paradigm I don't find in python Peter Otten <__peter__@web.de> - 2016-01-15 14:34 +0100
  Re: Powerful perl paradigm I don't find in python Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-01-15 13:51 +0000
    Re: Powerful perl paradigm I don't find in python me <self@example.org> - 2016-01-15 15:20 +0000
  Re: Powerful perl paradigm I don't find in python Nathan Hilterbrand <nhilterbrand@gmail.com> - 2016-01-15 11:54 -0500

csiph-web