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


Groups > comp.lang.python > #61416 > unrolled thread

Re: Possible PEP Submission

Started byTim Chase <python.list@tim.thechases.com>
First post2013-12-09 15:01 -0600
Last post2013-12-09 15:01 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Possible PEP Submission Tim Chase <python.list@tim.thechases.com> - 2013-12-09 15:01 -0600

#61416 — Re: Possible PEP Submission

FromTim Chase <python.list@tim.thechases.com>
Date2013-12-09 15:01 -0600
SubjectRe: Possible PEP Submission
Message-ID<mailman.3798.1386622813.18130.python-list@python.org>
On 2013-12-09 14:36, Logan Collins wrote:
> Just checking whether 1) a PEP is the proper place for this and 2)
> what y'all think about it.
> 
> I would like to propose a change to the the 're' standard library to
> support iterables.
> 
> So, something like the following would work:
> 
> import re
> text = """hello user
> hello user
> hello user"""
> 
> users = ["Bob", "Alice", "Jeffery"]
> 
> newtext = re.sub("user", users, text)
> 
> # newtext = "hello Bob\nhello Alice\nhello Jeffery"
> 
> There are a few corner cases I'm not sure what would be the best
> behavior. Nor am I entirely certain this should be modified
> functionality or just... a new function. What do y'all think?

Well, it's not that hard to do in stock python:

  i = itertools.cycle(users)
  print re.sub(
    'user',
    lambda m: next(i),
    text
    )

which should handle both the "fewer matches than items in users"
case, as well as wrap around in the "more matches than items in
users" case.  If you don't want wrap-around, just use iter(users)
instead of itertools.cycle(users)

This could be wrapped up in some class to give it a clearer name, but
I'm not sure it's worth doing a PEP or even including it in the
standard library.

-tkc



[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web