Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Powerful perl paradigm I don't find in python Date: Fri, 15 Jan 2016 14:34:40 +0100 Organization: None Lines: 60 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de YgEHtw3FlKrM0WpB5Rl01A7TpsgHvdJs4c3nBY3f7zjQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'string.': 0.04; '"""': 0.05; 'none:': 0.05; '%r"': 0.09; 'cursor': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'snippet': 0.09; 'subject:don': 0.09; 'def': 0.13; 'subject:python': 0.14; '*and': 0.16; 'exposes': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'substitute': 0.16; 'traverse': 0.16; 'wrote:': 0.16; 'string': 0.17; 'helper': 0.18; 'skip:u 30': 0.18; '>>>': 0.20; 'parse': 0.22; 'text,': 0.22; 'pass': 0.22; '(or': 0.23; 'feature': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'start,': 0.27; 'yield': 0.27; 'comparison': 0.29; 'perl': 0.29; 'tail': 0.29; 'raise': 0.29; 'allows': 0.30; "i'd": 0.31; 'useful': 0.33; 'class': 0.33; 'common': 0.33; 'maintaining': 0.34; 'but': 0.36; 'too': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'things': 0.38; 'end': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'ever': 0.60; 'your': 0.60; 'skip:u 10': 0.61; 'avoid': 0.61; 'more': 0.63; 'smith': 0.76; 'actually,': 0.84; 'subject:find': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd90f8.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101752 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()